Thread Pinning
Dagger runs a task on a Julia thread, and the operating system is free to move that thread between CPUs. On machines where placement matters (several NUMA domains, several jobs sharing one node, memory-bound kernels) it helps to pin Julia's threads to CPUs. Dagger does not provide its own pinning API for this. Use ThreadPinning.jl, which works on the Julia threads that Dagger's ThreadProcs run on, and needs no cooperation from Dagger.
Pinning only works in practice on Linux. On other systems ThreadPinning either does nothing or is not supported, and the test that accompanies this page is skipped there.
Basic usage
Install ThreadPinning 1.x (using Pkg; Pkg.add(name="ThreadPinning", version="1")), then pin once, before submitting work:
using Dagger, ThreadPinning
pinthreads(:affinitymask):affinitymask pins each thread of the default thread pool to one CPU, staying inside the CPU mask the process was started with. That mask can come from the job launcher, from taskset, or from numactl. For example, with
taskset -c 4-7 julia -t 4the four threads end up on four distinct CPUs of 4-7, and every Dagger task then runs on exactly one of those CPUs. Calling pinthreads(:affinitymask) a second time gives the same result. ThreadPinning.getcpuids() shows where the threads are, and ThreadPinning.getcpuid() called inside a task shows where that task runs:
Dagger.@spawn scope=Dagger.scope(worker=1, thread=2) ThreadPinning.getcpuid()Pitfalls
Use ThreadPinning 1.x. An unconstrained
Pkg.add("ThreadPinning")can resolve to 0.7.x, which has no:affinitymask. Ask forversion="1"(orThreadPinning = "1"in[compat]).Call
addprocsbefore pinning. If the main thread is in the default thread pool,pinthreadspins it to one CPU, and processes started afterwards inherit that single CPU. (On Julia 1.12 and later, with an interactive thread, the main thread stays unpinned.)Workers on the same node need separate CPUs. Each worker pins inside the mask it inherited, so by default all workers of one node would pin onto the same CPUs. Split the mask explicitly:
using Distributed addprocs(2; exeflags=`--threads=2`) # before pinning @everywhere using Dagger, ThreadPinning cpus = findall(isone, getaffinity()) .- 1 # CPUs of this process's mask for (k, w) in enumerate(workers()) nt = remotecall_fetch(() -> Threads.nthreads(:default), w) remotecall_wait(pinthreads, w, cpus[(k-1)*nt+1:k*nt]) enddistributed_pinthreads(:cores)splits over the whole machine and ignores the mask, so it is not a substitute when the processes run undertasksetor inside a job allocation.MPI ranks do not need any of this when the launcher binds them to CPUs (for example
mpiexec --bind-to core, orsrun --cpu-bind): each rank then callspinthreads(:affinitymask)and pins inside its own binding.Interactive threads. With an interactive thread (
julia -t 4,1), Dagger currently registersThreadProcs for thread ids1:Threads.nthreads(). That includes the interactive thread 1, whichpinthreads(:affinitymask)leaves unpinned, and leaves out the last default thread. This is being fixed in #841. Until then either start Julia without interactive threads (-t Non Julia 1.10/1.11,-t N,0on Julia 1.12 and later), or pin all threads:pinthreads(:affinitymask; threadpool=:all, nthreads=Threads.nthreads(:default) + Threads.nthreads(:interactive))
What was tested
test/threadpinning.jl starts Julia subprocesses under taskset -c <CPUs> with a range that does not start at CPU 0, runs Dagger tasks, calls pinthreads(:affinitymask) twice, and checks that every default thread, and the CPU reported from inside a Dagger task on each thread, lies inside the mask, that the threads are on distinct CPUs, and that the second call changes nothing. It is skipped when the system is not Linux, when taskset is missing, when fewer than four CPUs are allowed, or when ThreadPinning cannot be loaded.