Module parallel

Module parallel 

Source
Expand description

Serial or Rayon-parallel evaluation of independent items. treams-rs extension.

Four forms decide when work runs on the Rayon pool:

  • Parallel is the policy of the elementwise evaluations in broadcast: in parallel from a number of elements (Parallel::AtLeast), or in about four chunks per thread for cheap, uniform elements (Parallel::Chunked).
  • try_map, try_fill_chunks and try_fold_ordered take parallel: bool, which callers compute from a size and PARALLEL_ITEMS or PARALLEL_ENTRIES, or set for items that are expensive on their own.
  • LU factorizations and solves pass faer a worker count computed from the matrix size (linalg::lu_threads).
  • The S-matrix illumination in smatrix::illuminate compares a row count with a constant and calls Rayon directly.

Parallel, try_map and try_fill_chunks keep every output in index order, and try_fold_ordered adds in an order set by the number of items, so the thread count never changes their results. The three try_* helpers guard serial work and parallel callbacks, including callbacks on foreign or fallback workers. Ordered reductions also guard the final combine on the caller.

ModuleThresholdValueRuns in parallel
numerics::parallelPARALLEL_ITEMS1024 itemsplane-wave permutations (pw::permute), chirality densities (smatrix::chirality)
numerics::parallelPARALLEL_ENTRIES4096 entriesplane-wave phases and fields, and their pullbacks (pw::field)
special::besselPARALLELAtLeast(64)Bessel arrays and their pullbacks
special::legendrePARALLELAtLeast(1024)angular-function arrays and their pullbacks
special::wignerPARALLELAtLeast(1024)Wigner D arrays and their pullbacks
special::integralsPARALLELAtLeast(1024)incomplete gamma and Kambe arrays and their pullbacks
vectorwavesPARALLELAtLeast(1024)vector-wave arrays and their pullbacks
lattice::batchPARALLELAtLeast(8)batched lattice sums and their pullbacks, per output
sw::polarPARALLELAtLeast(1024)spherical translation arrays and their pullbacks
sw::polarPLANSAtLeast(64)coupling plans, per distinct mode pair
cw::polarliteralChunked(512) regular, Chunked(64) singularcylindrical translation arrays
cw::polarliteralChunked(64)their pullback
cw::expansionPARALLELAtLeast(64)expansion coefficients, per distinct coupling
smatrix::illuminatePARALLEL_SCAN_ROWS256 rowsthe finite check of illumination inputs
smatrix::illuminatePARALLEL_SNAPSHOT_ROWS128 rowscopies of the recorded blocks, with more than one thread
smatrix::illuminateWORKER_SNAPSHOT_ROWS512 rowscopies of the recorded blocks on the worker, with one thread
linalglu_threadsmax(min(rows / 512, 4), rows / 2048) workers, at most columns / 16; the whole pool up to 4 threadsLU factorizations and solves (faer)

The ufunc loops of the bindings use their own tiers (parallel_from in the treams-py file ufunc/kinds.rs). Other Rayon calls fill matrix columns or map independent items at any size, also in index order.

ยงReductions

Pullbacks that add gradients over many items reduce with try_fold_ordered. It splits the items into at most REDUCTION_CHUNKS consecutive chunks, with boundaries set by the number of items alone, folds each chunk in index order and adds the chunk results in chunk order, on the pool or on the calling thread alike, so the thread count sets how many chunks run at once but never the order of the additions. A gradient that belongs to one item, such as the q gradient of one column, travels with that item as a mutable slot and is written in place; a partial sum holds only the gradients that all items share.

ModulePullbackItemsWritten in place
channelsSphericalChannelsResidual::pullback, CylindricalChannelsResidual::pullbackplane-mode columnsq gradients
cluster::iterativeIterativeSphereCluster::pair_gradientsparticles i, each with its pairs (i, j)-
cw::to_swToSwResidual::pullbackcylindrical source modes-
fieldsWaveSet::pullbacksamplespoint gradients
pw::expandExpansionResidual::pullbackplane modeswavevector gradients
pw::fieldFieldResidual::pullbackat most 32 blocks of points or of modes, whichever axis is longerthe gradients of that axis
smatrix::layersLayerStackResidual::pullbacktransverse wavevectorsq gradients
sw::periodic_to_cwPeriodicToCwResidual::pullbackspherical source modes-
sw::planTranslationPlan::pullback_periodicharmonics, whose gradients are collected and then added in order-

Each item is expensive on its own (a column of entries, the pair blocks of a particle, a layer stack or a lattice sum), so these pullbacks pass parallel: true, except pw::field, which runs its blocks in parallel from PARALLEL_ENTRIES as its forward does. Its blocks are those of pw::PhasesResidual::pullback, which maps them with try_map and adds the partial gradients in block order.

Enumsยง

Parallel ๐Ÿ”’
How an elementwise evaluation uses the rayon pool.

Constantsยง

PARALLEL_ENTRIES ๐Ÿ”’
Matrix entries from which independent per-entry work runs in parallel.
PARALLEL_ITEMS ๐Ÿ”’
Items (modes, columns) from which independent per-item work runs in parallel.
REDUCTION_CHUNKS ๐Ÿ”’
Chunks of a deterministic reduction: try_fold_ordered splits its items into at most this many consecutive chunks, which bounds both the parallelism of a reduction and the partial results alive at once.

Functionsยง

try_fill_chunks ๐Ÿ”’
Fill data chunk by chunk with fill(index, chunk), stopping at the first error.
try_fold_ordered ๐Ÿ”’
Fold items into one result whose bits do not depend on the thread count.
try_map ๐Ÿ”’
map over 0..count, collected in index order and stopping at the first error.