ctLame is a multithreaded MP3 encoder: it writes exactly the same MP3
files as LAME, byte for byte, and is 14 times as fast on an 8-core CPU. LAME is sequential by design: every frame depends on
the state the one before it left behind. These eleven pictures show how, step by step, a
single encoder became a task graph on ThinkMeta ConcurrentTasks that keeps every core busy – and why
each step was needed.
Picture 1 of 11
The reference: LAME
LAME reads a block, encodes it and writes it – all on one thread,
one after another. Every stage carries state from frame to frame: the
psychoacoustic model, the filterbank, the bit reservoir. One core works,
the others wait.
These bits are the yardstick: every following picture has
to write exactly the same MP3 file, byte for byte.
Bit reservoir
An MP3 frame may use bits that earlier frames left unused. How a
frame is coded therefore depends on every frame before it –
the main reason why MP3 encoders work sequentially.
The reference
LAME 4.0, reduced to what ctLame implements: MPEG-1 Layer III
with LAME’s “new” VBR routine from WAV files.
For these settings it writes the same bytes as the unmodified
LAME.
Run time · test file, 4 minutes of music
1.9 s
Picture 2 of 11
Reading and writing run alongside
fread and fwrite become tasks of their own with
asynchronous I/O on an I/O scheduler; the whole encoder is one task on
the compute scheduler. Reading and writing now happen while the
encoder works, with up to four requests in flight.
The I/O chain manages about 600 MB/s – some 27 times what the
encoder processes on one core. It will not become the bottleneck later
either. Still only one core computes: as fast as LAME, bit-identical.
Pipes and priority
The pipes between the tasks are bounded, so memory stays small.
At this stage their fill level also set the priority of the I/O
tasks: when the input pipe ran low, the reader switched to high
priority. The last picture replaces that with fixed priorities.
The LAME tag
The LAME tag with checksum and table of contents is only known
after the last frame. Write puts it at the start of the MP3 stream
at the very end.
Still in use today
This graph is still in the code: ctLame --sequential
runs the finished encoder in it on one thread – the
reference path for comparisons.
Run time · test file, 4 minutes of music
2.0 s
Picture 3 of 11
Untangling the state
The graph stays the same, but the encoder is rebuilt from the inside: six
stages, and everything that belongs to one frame lives in a record of its
own. What is carried from frame to frame stays in the state of its stage
(the loops): psychoacoustics, filterbank, bit reservoir.
Only one dependency crosses both stages and frames (dashed): the bitstream
of frame N decides how many bits the quantization of frame N+1 may use.
You can only parallelize what you can take apart.
The map
A frame takes about 200 µs. Of that, psychoacoustics need 38 %,
the MDCT 8 %, quantization 49 % and the bitstream 5 %.
This profile guided every following step.
False alarms
Two values in LAME looked as if a frame depended on a later one.
Both turned out to be resolvable forward.
Run time · test file, 4 minutes of music
2.0 s
Picture 4 of 11
Two tasks, one ring
The encoder is cut into two tasks between analysis and quantization. Both
halves are about the same size; as a pipeline, frame N is quantized while
frame N+1 is analysed. Between them sits a ring of frames: a slot is free
again as soon as its frame is done.
Handing a frame to another thread costs about 0.4 µs, against some
200 µs of work – so every frame is handed over on its own, no
batching. The bottleneck now: Encode.
Sample stream
Each channel is a stream of samples in reserved virtual memory. A
frame points to its window instead of copying it; only what is in
the ring right now takes up memory.
Why Encode has a bold border
The bold border marks a task with high priority. Encode, the later
stage, runs before the analysis: the pipeline drains first, before
the analysis computes further ahead. Later, Resolve gets high
priority for the same reason.
Pipeline time · the same test file
2.12 → 1.27 s (1.68×)
Picture 5 of 11
The search goes frame-parallel
The profile showed: about 95 % of the quantization does not depend
on the bit reservoir at all. The search for scalefactors therefore runs
for many frames at once, one task per compute thread. Only the commit
– bit budget, bitrate choice – stays serial, and it takes a
few microseconds.
The heaviest stage is spread over all cores. The bottleneck now: the analysis, serial with its state.
The scheduler learns, too
On the way, ctLame showed that the scheduler woke all sleeping
threads for every new task. It now wakes at most one – and
the run time no longer depends on the number of threads. Every
encoder also makes ThinkMeta ConcurrentTasks itself better.
Pipeline time · the same test file
1.27 → 1.05 s
Picture 6 of 11
The graph takes shape
About 90 % of the psychoacoustics need no state: FFTs, spreading,
energies. They now run as Prepare for many frames at
once. Only Resolve stays serial: attack detection, block
type, pre-echo control. The MDCT turns out to be stateless as well.
Every frame passes five stages: serial, parallel, serial, parallel,
serial. Only three short links remain serial: Dispatch, Resolve and
Encode.
One worker pool
At this stage Prepare and Search are the same worker tasks, one
per thread. They take the search first: it belongs to older frames
and frees slots in the ring sooner. The last picture gives each
stage tasks of its own.
Pipeline time · the same test file
1.05 → 0.29 s · about 8× LAME
Picture 7 of 11
The serial links get thin
Every microsecond a serial link needs per frame limits all cores
together. So work moves out of the serial nodes into the parallel ones:
Huffman coding now runs in the search, the serial bitstream only appends
it (17 → 3.8 µs per frame). The filterbank is computed in Prepare;
Resolve only passes its carry on, in order.
From here on the result is measured over whole program runs on a corpus of music files.
Tried and dropped
Splitting Resolve further was bit-identical and shrank the serial
chain from 13.7 to 4.9 µs per frame – on 8 cores it
gained nothing, because the workers were already busy. It is not in
the code and would pay off only on CPUs with more cores. Look-ahead,
other priorities, a larger ring: within the noise.
Speed-up over LAME · whole run
7.76× → 8.33×
Picture 8 of 11
Less work per node
The graph has reached its optimum on this machine; from now on gains
come from less work per frame. SIMD kernels (SSE4.1, AVX2) in the parallel
nodes compute the same operations in the same order as the scalar code
– and so give the same bits.
The ring grows with the number of threads: 4 slots per thread, 16 to 128.
On one thread ctLame is now 1.7 times as fast as LAME.
Why the ring grew
With 16 threads the stages were only 60 to 70 % busy: the fixed
ring of 32 slots allowed too few frames in flight. Growing it with
the thread count took the corpus from 12.75× to 14.02×.
Speed-up over LAME · whole run
8.33× → 14.02×
Picture 9 of 11
The graph as it was measured
Seven kinds of nodes; with 16 threads that is 21 tasks on two schedulers.
A frame is finished about every 13 µs. On 45 hours of music, ctLame
needs 85 seconds instead of almost 20 minutes – every file
bit-identical to LAME, both without ReplayGain.
Why fibers make the difference
The serial nodes wait in the middle of their work: Dispatch for a
free slot, Resolve for the state of the frame before, Encode for the
bits it left over. On ThinkMeta ConcurrentTasks such a wait suspends only the task; its
thread meanwhile computes other frames, and every node stays a
simple loop over the frames in order.
Other CPUs
Intel Core i9-11900K, 8 cores: 13.88×
Intel Xeon E3-1275 v6, 4 cores: 8.21×
Intel Core Ultra 7 155H, notebook: 9.03×
Measured cold, every file fresh from the disk. Bit-identical to LAME on every machine.
Both programs ran without ReplayGain (--noreplaygain), so the
comparison is the encoding alone. By default both run the analysis, as
LAME 4.0 does; the next picture shows what ctLame does with it.
Speed-up over LAME · 45 h of music, measured cold
13.88×
Picture 10 of 11
ReplayGain beside the frames
ReplayGain is on by default, as in LAME 4.0. Its analysis is two
filters per channel, a serial chain over the whole file. Inside the
serial Dispatch it more than doubled the encoding time, because it held
up every frame.
So each channel gets a task of its own, Gain L and
Gain R. They read the sample stream beside the frame
stages (dashed), and Encode waits for them only once, before it writes
the LAME tag.
Still the same bits
The channels need nothing from each other until the end. LAME
analyses in pieces, but its filters run over a channel as in one
go; only the partial sums follow the pieces. The Gain tasks take
the same pieces, so the result is LAME’s, bit for bit.
What is left
A run with empty Gain tasks showed that the tasks and their
wake-ups cost nothing. The rest is computing on processors that
are busy already. The benchmarks keep measuring both programs
without ReplayGain.
Extra time with ReplayGain · quick test, 16 threads
+129 % → about +29 %
Picture 11 of 11
One task type per activity
The graph stays, but its tasks take the form of the documentation’s
chapter on task-based programming, which
derives ctLame from LAME’s loop.
The worker tasks become Prepare tasks and Search tasks, each a plain
loop over the frames of its stage; Search, Resolve and Encode run with
high priority.
With 16 threads these are 37 tasks, 39 with ReplayGain. The form costs
nothing that can be measured, cold over 86 files; the published numbers
come from the graph before it.
Simple I/O is enough
Read and Write do one request at a time. That is as fast as
keeping several requests in flight and setting the priority by
the fill level of the pipes, as they did before.
Write now reads like the file: ID3v2 tag, MP3 stream, ID3v1 tag,
then the LAME tag in its place.
A yield that cost 1 %
The worker tasks took searches first. Now the priorities say
so. At first a Prepare task also yielded to the later stages
after every frame. Measured, that cost about 1 % with
ReplayGain and gave nothing without. It was removed; the
priorities alone do the work.
Tested in every order
A test runs all tasks on one thread, started in twelve orders
and with priorities given at random. Every run must write the
file of the sequential graph. A dependency that was removed on
purpose was found only with the random priorities.
Time against picture 10 · without / with ReplayGain
+0.2 % / −0.4 %
Try it yourself
The reference build of LAME and the benchmark are on
GitHub.
Have a workload that should use every core?
Get in touch.