Skip to main content

Module threads

Module threads 

Source
Expand description

How many threads this process may actually run on.

std::thread::available_parallelism ignores the process affinity mask on Windows: under start /affinity 4 (one core) it still reports every logical CPU. rayon sizes its global pool the same way, and candle hands gemm Parallelism::Rayon(physical cores) on every matmul, so a process pinned to one core ran two dozen threads on it.

PdfCraft measured what sizing the pool to the mask is worth (ABBA, output identical): on 8 cores, 0.927x the time (10/10 rounds) and peak memory 1 959 -> 1 240 MB; on 1 core, time neutral and memory 1 285 -> 747 MB. It could not do it itself, because reading the mask is an FFI call and it forbids unsafe. This module is that one call, behind a safe function.

Use affinity_pool and run the engine inside it:

let pool = ffai_core::threads::affinity_pool().expect("thread pool");
pool.install(|| {
    // engine.recognize(&image, &options) — every rayon task the engine and
    // candle spawn now runs on this pool's threads.
});

Functions§

affinity_pool
A rayon pool with available_threads threads.
available_threads
The number of CPUs this process may run on: the affinity mask on Windows, available_parallelism elsewhere (which already honours affinity and cgroup quotas on Linux). At least 1.