Skip to main content

should_parallelise

Function should_parallelise 

Source
pub fn should_parallelise(backend: ComputeBackend, n: usize) -> bool
Expand description

The one place that decides Serial vs multi-threaded execution for an element-wise field operation over n elements.

Every operator in this module routes through this function; none of them contains its own size test. It returns true only when all of:

n is counted in field elements — cells for a volume field, internal faces for a surface field. Dimensionless.

§Relationship to crate::compute::select_backend

select_backend answers “which backend should I ask for?” from a work-item count; this function answers “given the backend I was asked for, is this particular field big enough to be worth threading?” using the measured field-kernel crossover rather than the workspace placeholder. A caller may use either; passing select_backend(n) straight in works and is safe, because this function re-checks the size against the measured threshold.

use outram_foam_basic_lib::compute::ComputeBackend;
use outram_foam_basic_lib::fields::parallel::{field_parallel_crossover, should_parallelise};

// Serial never parallelises, whatever the size.
assert!(!should_parallelise(ComputeBackend::Serial, 100_000_000));
// Nor does any backend on a tiny field.
assert!(!should_parallelise(ComputeBackend::CpuMulti, 8));
// Above the crossover it depends only on whether `parallel` is enabled.
assert_eq!(
    should_parallelise(ComputeBackend::CpuMulti, field_parallel_crossover()),
    cfg!(feature = "parallel"),
);