parallel_map_rng
- sherpa.utils.parallel.parallel_map_rng(function: CallbackWithRNG[I_contra, O_co], sequence: Sequence[I_contra], numcores: int | None = None, rng: Generator | RandomState | None = None) list[O_co] [source] [edit on github]
Run a function on a sequence of inputs in parallel with a RNG.
Similar to parallel_map, but the function takes two arguments, with the second one being
rng
, the random generator to use. This is for those functions which need random numbers, and this routine takes care to create a separate generator for each process run in parallel.Added in version 4.16.0.
- Parameters:
function (function) – This function accepts two arguments - the first being an element of
sequence
and second calledrng
- and returns a value.sequence (array_like) – The data to be passed to
function
as the first argument.numcores (int or None, optional) – The number of calls to
function
to run in parallel. When set toNone
, all the available CPUs on the machine - as set either by the ‘numcores’ setting of the ‘parallel’ section of Sherpa’s preferences or by multiprocessing.cpu_count - are used.rng (numpy.random.Generator, numpy.random.RandomState, or None, optional) – Controls how random numbers are generated. When code is run in parallel, each worker is sent a separate generator, to ensure that the sequences are different, and the rng parameter is used to create the seed number passed to
numpy.random.SeedSequence
for this case.
- Returns:
ans – The return values from the calls, in the same order as the
sequence
array.- Return type:
array
See also
Notes
The input
rng
argument is used to create a seed number, which is passed to thenumpy.random.SeedSequence
object to create a separate generator for each worker. The generator used for these is always created by a call tonumpy.random.default_rng
, even when therng
argument is None (indicating that the legacy random API is being used), or a different generator that that used bydefault_rng
.