Overhead

Per-loop time for a loop with 64 trivial iterations (x[i] += 1.0), benchmark/overhead/overhead.jl, pinned to 8 cores (taskset -c 8-15) of a Ryzen 9 5950X, Julia 1.12.7 and 1.13.1, OPENBLAS_NUM_THREADS=1. Polyester.jl 0.7.18 was loaded from a separate environment for comparison (* on 1.13 from an earlier run, with 0.7.19); OhMyThreads.jl 0.8.7 and KernelAbstractions.jl 0.10.0 (foreach_index on the CPU backend, then synchronize) come from the benchmark environment. Values are min / median in µs.

These numbers come from one shared machine, with other jobs running on the same cores. Minima are more reliable than medians.

mechanism1.12 -t 8 (8+1 interactive)1.12 -t 8,01.13 -t 81.13 -t 8,0allocations per loop
tforeach, scheduler = TeamScheduler()0.26 / 0.310.32 / 0.380.30 / 0.340.27 / 0.340
tmapreduce, scheduler = TeamScheduler()0.30 / 0.350.33 / 0.380.31 / 0.370.30 / 0.370
@tasks with @set scheduler = TeamScheduler()0.29 / 0.330.30 / 0.370.29 / 0.350.28 / 0.340
tforeach, scheduler = GreedyTeamScheduler() (32 chunks)0.90 / 1.330.89 / 1.450.76 / 0.910.73 / 0.910
Polyester @batch (v0.7.18)0.23 / 0.370.26 / 0.380.23 / 0.35*0.24 / 0.37*0
Threads.@threads :static11.57 / 1131.86 / 2.7710.92 / 1141.65 / 2.6742 (2.9 KB)
Threads.@threads :dynamic2.46 / 3.942.52 / 3.882.36 / 3.802.27 / 3.6442 (2.9 KB)
OhMyThreads tforeach (v0.8.7, DynamicScheduler)4.22 / 5.354.31 / 5.273.58 / 4.623.53 / 4.4566–68 (3.6 KB)
OhMyThreads tforeach, scheduler = :static17.87 / 1184.63 / 5.3011.09 / 1154.23 / 4.6467–69 (3.6 KB)
OhMyThreads tmapreduce(+)3.98 / 5.044.10 / 5.033.52 / 4.393.34 / 4.2664–66 (3.6 KB)
KernelAbstractions foreach_index (v0.10.0)3.24 / 4.613.17 / 4.483.13 / 4.203.08 / 4.385 (96 B)
serial loop0.02 / 0.020.02 / 0.020.02 / 0.020.02 / 0.020
  • Pass the scheduler as an object. OhMyThreads resolves a symbol (scheduler = :team, @set scheduler = :team) with a dynamic dispatch, about 0.3 µs and 64 bytes per loop.
  • GreedyTeamScheduler() splits the 64 iterations into 32 chunks (4 per thread), handed out one by one, hence the higher cost; with chunksize or nchunks matched to the loop, it costs about as much as the static schedule.
  • -t 8 is the default since Julia 1.12: 8 default threads plus one interactive thread, so 9 threads share 8 cores. Threads.@threads :static then pays about the scheduler's sleep threshold (100 µs) per loop: its sticky tasks wait until the OS deschedules a thread spinning in the scheduler. :dynamic, the team and Polyester are not affected: the worker tasks already run, and the caller (on the interactive thread) runs chunk 1 while one default thread stays unused.
  • OhMyThreads' :static scheduler also uses sticky tasks and has the same problem with -t 8.
  • KernelAbstractions 0.10 runs CPU kernels through POCL (CPU() is a POCLBackend()), so foreach_index compiles the body as a kernel. A launch costs about as much as a @threads loop but allocates much less.
  • With -t 8,0 (no interactive thread) @threads :static costs about 2–3 µs.
  • Waking sleeping workers costs about 10–15 µs, for the team and @threads alike. Raise the spin time when longer serial phases separate the loops.

Where the time goes

Hardware counters per loop on the calling thread (benchmark/overhead/counters.jl, LinuxPerf.jl, same loop and machine, Julia 1.12, -t 8):

cyclesinstructionsL1d misseslast-level cache misses
tforeach, TeamScheduler()1460–15901690–171020–2411
tmapreduce, TeamScheduler()1520–15701750–176023–2914–15
Polyester @batch1620–167050035–3616
serial loop167000

(Ranges over three runs; the spread comes from other jobs on the machine.)

The cache misses are the hand-off: each worker's command field is a cache line that moves between the calling thread's core and the worker's core twice per loop (the worker's claim and its completion). Timestamps at the stages of a loop (8 threads, 64 iterations, median nanoseconds; the timers add about 20 ns per stage) show where the calling thread's time goes:

stage of the calling threadns
OhMyThreads' entry, run_fold, fold of the partials, release150
publishing the region and storing the seven commands90
chunk 1 (mapreduce over 8 elements)30
checking which chunks the workers have not taken (seven loads)140
waiting for the last worker20–100
clearing the slot, the scope and the exceptions20

A worker claims its chunk within about 20 ns of the stores and finishes about 100–150 ns later, of which the chunk itself takes about 60 ns. The rest is the wake-up check for its two children in the binary tree and the call of the chunk closure (see job_closure in src/pool.jl).

Five details keep the hand-off at that level:

  • The command stores are release stores followed by one fence, not sequentially consistent stores: an xchg per store would wait for each cache line in turn, where plain stores overlap in the store buffer.
  • The check for untaken chunks loads before it compare-and-swaps: a failed compare-and-swap would still take the line from the worker, which then has to take it back.
  • Each worker's sleeping flag sits on its own cache line, apart from its command. The flag is written only when the worker sleeps or wakes, so the two flag loads of every wake-up check hit a line shared between all cores, not the line the worker claims its chunk on. Before this, the wake-up checks cost the calling thread 70 ns and every worker 50–120 ns per loop.
  • The dispatcher stores the chunk function into a slot of each worker. Reading it from the dispatcher's slot, rewritten every loop, cost every worker a second cache miss after the one that brings the command (30–40 ns per loop with 4 threads). Chunk functions above 256 bytes still run from the dispatcher's slot.
  • The slot that hands the chunk function to the workers and the buffer for the partial results are constants embedded by @generated functions, one per type, not IdDict entries: the two lookups cost about 50 ns per loop.

The remaining difference to Polyester is instruction count on the calling thread (OhMyThreads' entry with its keyword arguments and scheduler object, the job struct, mapreduce over the first chunk): about three times Polyester's at a similar cycle count. Exception handlers were also cut to one per loop on the calling thread and one per worker lifetime; entering the caller's dynamic scope installs a handler only when the caller has a scope at all (a ScopedValue or logger in effect).