Threads¶
TensorKeeper runs on threads it does not create itself. The linear algebra runs on the threads of the BLAS or of faer, and sampling runs on a rayon pool. This page covers how many of each a run gets, how to change it, and how to check what a run actually used.
Quick reference¶
| You want | Do |
|---|---|
| One count for everything, for the whole process | export MIMIQ_NUM_THREADS=8 before starting Python |
| Different counts for evolution and sampling | MIMIQ_CORE_NUM_THREADS and MIMIQ_SAMPLER_NUM_THREADS |
| A cap for the runs of one simulator | TkSimulator(max_num_threads=4) |
| To run inside a container or a batch allocation | Set MIMIQ_NUM_THREADS to the CPUs you were given (see Containers and batch jobs) |
| To see what a run resolved | tensorkeeper.thread_info() |
The two pools¶
| Pool | What runs on it | Sized by |
|---|---|---|
| core | Linear algebra on the residual MPS: dressed gates, sums, compression. | MIMIQ_CORE_NUM_THREADS |
| sampler | Drawing shots from the evolved state. | MIMIQ_SAMPLER_NUM_THREADS |
Both fall back to MIMIQ_NUM_THREADS when their own variable is unset, so the
common case is one variable:
and the uncommon case is two:
export MIMIQ_NUM_THREADS=8 # eight by default
export MIMIQ_SAMPLER_NUM_THREADS=2 # except sampling, which gets two
The pools scale differently, which is why they are separate. The core pool parallelizes inside each tensor contraction, which pays off only when the residual's bond dimension is large. On Clifford-dominated circuits it often stays small, and extra core threads then cost more than they save. The sampler pool parallelizes across shots, which pays off whenever you ask for many shots.
Set the variables before the first run: the core count is applied when the engine first runs, while the sampler count is read on every sampling call.
Capping one simulator¶
max_num_threads caps both pools for the runs of one simulator, without
touching the environment:
from tensorkeeper import TkSimulator
# At most four threads for this simulator's runs, whatever the process has.
sim = TkSimulator(bonddim=128, max_num_threads=4)
results = sim.execute(circuit, nsamples=1000)
TkSimulatorGpu takes it too. The cap is a ceiling over what the variables
give: a process sized to two threads stays at two under max_num_threads=4.
It is held for the length of each execute and evolve call and released
when the call returns.
The pools are process-wide, so two simulators running at the same time on different Python threads share them. Each then runs at the smallest cap either holds.
What resolves to what¶
For each pool, in order, with any max_num_threads cap applied on top:
- that pool's own
MIMIQ_*variable, MIMIQ_NUM_THREADS,- the native variable of the library underneath, which that library reads for itself,
- nothing, and the library sizes itself.
A value that is not a positive integer is ignored and the next name in the order is consulted.
The native variables depend on the engine (linalg):
| Engine | Core pool reads | Default with nothing set |
|---|---|---|
"openblas" |
OPENBLAS_NUM_THREADS, OMP_NUM_THREADS |
one thread per CPU in the affinity mask |
"mkl" |
MKL_NUM_THREADS, OMP_NUM_THREADS |
one thread per physical core |
"faer" |
RAYON_NUM_THREADS |
one thread per CPU the process may use, counting a CPU quota |
The sampler reads RAYON_NUM_THREADS on every engine. Prefer the MIMIQ_*
names: they mean the same thing on every engine, while OPENBLAS_NUM_THREADS
does nothing on MKL and MKL_NUM_THREADS nothing on OpenBLAS.
Containers and batch jobs¶
The default count comes from the CPU affinity mask, which taskset, numactl
and Slurm's CPU binding all set, so a run inside such an allocation sizes
itself to it.
A CPU quota is different. docker --cpus, a Kubernetes limits.cpu and a
cgroup cpu.max limit CPU time but leave the affinity mask at the whole
machine, so OpenBLAS and MKL still start one thread per machine CPU, and those
threads fight over the quota. Under a quota, always set the count:
Never set a count above the CPUs the job was given. An oversubscribed BLAS does not slow down gently: runs can take many times longer.
Checking what a run resolved¶
>>> import tensorkeeper as tk
>>> tk.thread_info()
{'core': {'source': 'MIMIQ_NUM_THREADS', 'threads': 8, 'effective': 8},
'sampler': {'source': 'MIMIQ_SAMPLER_NUM_THREADS', 'threads': 2}}
sourcenames what decided the count: a variable,"limit"while amax_num_threadscap holds it down, or"unset".threadsis the count requested.Nonemeans noMIMIQ_*variable applied and the library sized itself.effective(core only) is the count the linear algebra actually runs on. A request the library did not take shows up as a difference betweenthreadsandeffective.
tk.thread_info() reports the default engine. Each engine is a separate
library with its own counts, so for a simulator on another engine pass its
name: tk.thread_info(linalg="mkl").
A typo in a variable shows up here as a source you did not expect, not as an
error.
Results are not bitwise identical across thread counts: a different count changes the order of floating-point sums, so fidelities and amplitudes agree to about 1e-13 relative, not to the last bit. Compare runs with a tolerance.