EnsembleProblem(prob) repeats a problem (usually stochastic) many times.
prob_func(prob, i, repeat) returns the problem of trajectory i (by
default prob itself), output_func(sol, i) returns list(value, rerun) with the value kept for trajectory i and whether to run it again
(by default the solution, no rerun), and reduction(u, batch, I) combines
the values of a batch, returning list(u, done). solve(ensemble, alg, trajectories = n) returns an EnsembleSolution, a list of trajectory
values with fields u and elapsed. A seed makes the whole ensemble
reproducible; trajectories draw from one random stream in turn.
Usage
EnsembleProblem(
prob,
prob_func = function(prob, i, repeat_) prob,
output_func = function(sol, i) list(sol, FALSE),
reduction = function(u, data, I) list(c(u, data), FALSE),
u_init = NULL,
safetycopy = TRUE
)
EnsembleSerial()
EnsembleThreads(cores = NULL)
EnsembleDistributed(cl = NULL, cores = NULL)
EnsembleSplitThreads(cl = NULL, cores = NULL)
EnsembleGPUKernel(backend = NULL, cpu_offload = 0, cores = NULL)
EnsembleGPUArray(backend = NULL, cpu_offload = 0, cores = NULL)
EnsembleSolution(u, elapsed = 0, converged = TRUE)
# S3 method for class 'EnsembleProblem'
solve(
a,
b = NULL,
ensemblealg = EnsembleSerial(),
...,
trajectories,
batch_size = trajectories,
seed = NULL,
on_error = c("stop", "collect"),
retries = 0L,
rng_streams = c("session", "trajectory"),
checkpoint = NULL,
run_id = NULL
)Arguments
- prob
A problem.
- prob_func, output_func, reduction
Functions as described above.
- u_init
Initial value of the reduction.
- safetycopy
Unused; kept for compatibility.
- cores
Number of worker processes (default
getOption("mc.cores", 2)).- cl
A cluster of the parallel package; by default one is created for the solve.
- backend, cpu_offload
Options of the Julia signature; R has no GPU arrays, so the trajectories run on CPU workers as with
EnsembleThreads().- u, elapsed, converged
Values of an ensemble solution.
- a
An
EnsembleProblem.- b
The solver algorithm for each trajectory.
- ensemblealg
How trajectories run:
EnsembleSerial()(the default, one random stream used in turn),EnsembleThreads()(forked workers; serial on Windows),EnsembleDistributed()andEnsembleSplitThreads()(a socket cluster of the parallel package), orEnsembleGPUKernel()andEnsembleGPUArray()(CPU workers). The parallel ones give each trajectory its own L'Ecuyer-CMRG stream.- ...
Solver options for each trajectory.
- trajectories
Number of trajectories.
- batch_size
Trajectories per reduction batch.
- seed
Seed for the random number generator.
- on_error
What a trajectory that throws an error does:
"stop"(the default) stops the ensemble with the trajectory numbers and their messages;"collect"keeps the error object in place of the output and counts it infailures.- retries
How many times a trajectory that throws an error is run again (its
prob_funcsees the repeat number) beforeon_errorapplies.- rng_streams
"session"(the default): the streams described forensemblealg."trajectory": trajectoryidraws from theith L'Ecuyer-CMRG stream ofseed, so the results do not depend on the ensemble algorithm, the number of cores orbatch_size.- checkpoint
Optional file: after each batch the reduced output, the retcodes and the random state are saved there (written to a temporary file and renamed, so an interrupted write leaves the previous checkpoint; where a rename cannot replace a file the old one is kept as
<checkpoint>.bakuntil the new one is in place), and a solve given an existing checkpoint resumes after its last batch. The checkpoint records its format version, the configuration (trajectories, batch size, streams, seed, error policy, ensemble algorithm and cores) and an identity of the run; a checkpoint of a different run is an error, not a result.- run_id
Optional stable identity of the run for
checkpoint. Without it the identity is the serialized description of the problem, the functions (by their source text), the algorithm and the options, compared exactly; give arun_idwhen that text does not identify the run (functions that read changing global data, say).
Value
An EnsembleProblem.
An EnsembleSolution: the reduced output u, elapsed,
converged (whether the reduction stopped the ensemble or every batch
ran), retcodes (the retcode of each trajectory's last solve, "Error"
for a collected error), failures (the number of trajectories whose
retcode is not successful) and backend (how the trajectories ran:
the GPU names run on CPU workers).
Examples
prob <- SDEProblem(function(u, p, t) 1.5 * u, function(u, p, t) 0.3 * u, 1, c(0, 1))
ens <- EnsembleProblem(prob)
sim <- solve(ens, EM(), trajectories = 20, dt = 0.05, seed = 1)
summ <- EnsembleSummary(sim)
tail(summ$u, 1)
#> [,1]
#> [21,] 4.446852