pub fn initialize_multithreaded(threads: Option<NonZeroUsize>)Expand description
initialize, and start the threads that the multithreaded calls
use: every program that makes such calls calls this once,
before them, in place of initialize. It starts threads - 1 workers,
and each multithreaded call hashes on its calling thread and the
workers (calls made at once share the workers), and on CPUs with SME2
(Apple M4) the SME2 thread too. None uses the available logical CPUs,
limited on Linux to the allowed CPUs of one memory node (below). With
1, calls hash on their calling
thread alone: no workers start, nor the SME2 thread. Otherwise it
starts the first worker and returns; the first worker starts the others
(each 20-30 us on Linux x86-64), and calls run on the workers started
so far. The
threads wait asleep whenever no call has work for them (their memory:
see Memory). Later calls return at once, and must ask
for the same number: another Some panics. A panic on a worker thread
(a bug in this crate, or a waker of the program’s that panics when a
worker wakes it) ends the process, once the process’s panic hook has
returned (the standard one prints the panic’s message, and waits
where standard error does not take it), rather than leaving the call
that waits for that worker’s piece waiting forever. A worker thread
that the system refuses to start ends the process too, for the same
reason, with one line on standard error: a limit on the process’s
threads (RLIMIT_NPROC, a container’s pids.max) or a lack of
memory can refuse one. The first worker is started by this call and
the others by the first worker, so the end can come shortly after
this call has returned, and the program cannot catch it: one that
runs under such a limit passes a count of threads that fits.
None counts logical CPUs, so on a CPU with SMT (Hyper-Threading) both
threads of each core, and that is the faster default. On an EPYC 9255,
the 16-input kernels leave much of a core waiting on its loads, and a
core’s second thread fills that time: two threads hash 1.4-1.7 times
what one does, as with official BLAKE3. The 32-input AVX-512 kernel
(Linux) keeps a core busier on its own, so a second thread adds less
(1.1-1.3 times); even so, on 1 GiB 24 threads, one a core, took 4-14%
longer than 48. A thread slowed by the other thread on its core, by an
efficiency core or by another program takes fewer of a call’s pieces,
and the pieces shrink toward the end of the input, so the slowest thread
finishes holding a small one. To leave CPUs to other work, pass fewer
threads. The count is also the one setting for energy against speed:
every thread started may be woken by a call, and while a Pipeline is
open one that finds no work looks for 5 us before it sleeps.
On Linux where the node topology is available and the system allows
it, workers stay on one memory node: the node of the initializing
thread’s CPU or, where another node holds at least twice as many of
the CPUs that thread is allowed, the node that holds the most of
them. (The CPUs a
process is allowed need not lie evenly over the nodes, and its first
thread may start on a node that has one of them.)
On a machine with more than one last-level cache, each worker is kept
to a cache within that node.
A call wakes workers under its own cache first where it has any there.
Calling threads keep their own affinity and their buffer placement,
the initializing thread too: it may run on another node than the
workers.
To do this, on a Linux machine with more than one memory node, the
call reads the nodes’ CPUs from /sys/devices/system, and the first
worker binds itself to the chosen node’s with sched_setaffinity
before it starts the others, which inherit them (the SME2 thread
binds itself too).
All of this is for speed, and no call’s result rests on it: what the
system does not tell or allow is left undone, each part by itself,
and the calls work as anywhere. Where the system shows no list of
memory nodes, or keeps from the process that list, the node of the
initializing CPU, that node’s CPUs or the thread’s own (a sandbox
may), or shows parts that do not agree (a node that is not online,
or does not hold its CPU), no node is chosen, and the workers start
with the initializing thread’s CPUs. Another node is taken in place
of the initializing CPU’s only where what the system shows of that
node agrees in the same way. Where it refuses a thread’s
binding, to the node or later to a cache, that thread keeps the CPUs
it has. (A pool of one thread asked for by number,
NonZeroUsize::new(1), has no workers: it reads no topology and
binds nothing.)
For each kind and size of call under 256 MiB, a caller with local
workers learns whether they or all the workers finish sooner.
The count None takes is the standard library’s estimate
(std::thread::available_parallelism, or 1 where it gives none)
capped by the thread’s allowed CPUs (reading /proc/thread-self/status
on Linux when sched_getaffinity is refused) and, where a node is
chosen, by the node’s allowed CPUs. It is taken before any thread is
bound, and a binding refused afterwards does not change it. Where the
system hides both sched_getaffinity and /proc/thread-self/status,
the estimate can still be as many as the machine has online: a program
kept to fewer CPUs there does better to pass its number. A request for
n threads starts n - 1 workers whether or not the node, or the
process, has n CPUs.
Several programs each with a full pool share the machine without trouble, and unevenly. On an i7-12700K (20 CPUs), N programs each streaming 64 KiB messages hashed 26 GB/s alone, 37-41 together at 2 to 10, and 21-28 at 20 to 80 (400 to 1600 threads): the machine’s total holds, and oversubscription costs it 31% at one program a core and 48% at four. What each program gets is the OS’s choice among equal threads: at 20 programs the luckiest got 2.6 times the unluckiest, at 80, 8 times (bench-hashes #116, November 2026). A program that needs a predictable share beside others sizes its pool to its share of the machine.
The threads are this process’s: fork before starting threads, or exec in
the child and initialize in the new process (POSIX restricts a pre-exec
child of a multithreaded process to async-signal-safe calls; this library’s
fork guard does not make Rust allocation, TLS, or unwinding async-signal-safe).
As a diagnostic in a child forked after the pool started, every multithreaded
call and every new Pipeline panics, saying so (the
child has no pool workers). A Pipeline made before the fork is not to be used
in the child: its calls panic the same way once one needs a worker, including
while waiting for a free group or piece node when work was in flight at the
fork. Nor is it to be dropped there with work in flight at the fork, which
the child never sees finished: that drop, or the drop of a
Lent guard for such work, stops the child (a child
that leaves by _exit or exec drops neither).
Each thread that makes multithreaded calls keeps a little pool storage for
them (its piece cut, its job record, and — for pooled tree calls — its
chaining-value scratch; the two 1,024-piece vectors take 56 KiB together,
plus the job record), grown on its first pooled call of that kind and on
the first call that outgrows it, and otherwise untouched: a pooled call
whose pieces fit in its thread’s existing piece cut (and, for pooled tree
calls including large items in hash_each_multithreaded,
its existing chaining-value scratch) does not grow the pool’s scratch.
Batch jobs and the small-item pass of hash_each_multithreaded do not grow
the pool’s chaining-value vector (note that small-item / single-thread
hash_each_multithreaded also uses blake3-servil-core’s own per-thread
collection scratch, pre-sized to 144 middle messages / 288 chunk CVs). This
call prepares the calling thread’s pool storage for up to 1,024 pieces (and
on a 512-bit SME2 CPU, allocates that thread’s 64 KiB SME2 flat-walk
scratch — even when called with Some(1) or on later calls from fresh
threads); a call from an unprepared thread prepares its own on first use.
blake3_servil::initialize_multithreaded(None);
let message = vec![blake3_servil::throughput::Block::ZERO; (1 << 20) / 64];
let hash = blake3_servil::throughput::hash_multithreaded(blake3_servil::Mode::Plain, &message, 1 << 20);
assert_eq!(hash, blake3_servil::convenience::hash(&[0u8; 1 << 20]));