simulator.worker¶
The worker of a scan: the points of a chunk on one plan and one model.
run_chunk is the same function serially and in a worker process: for every
point of a chunk it applies the values of the point to the plan of the chunk,
Plan.with_values, and runs the plan with the executor of the engine of the
model, sbmlsim.simulator.engines.execute_plan; the native solutions of the
points which ran are stacked into arrays padded with NaN, the observables of
the run are evaluated on them, see sbmlsim.simulator.observables, and the
kept timecourses are interpolated onto the grid of times of the chunk, if it
has one. A run without observables needs no native solution: the kept
selections of every point are interpolated right after its simulation, so the
memory of a chunk is bounded by its grid. A point fails exactly when its own
simulation or observables fail, not because of the other points of its chunk.
Nothing in here uses pint or xarray, and a chunk and its result are numbers,
strings, a plan and the graph of the observables, so they pickle.
In a worker process the model of a chunk is loaded once from its ModelSpec
and kept, see sbmlsim.parallel.worker_cache, in the engine of the model of
the parent, with its integrator and every one of its settings, those of the
simulator and those set on the model, so a worker integrates as the parent
does and the result does not depend on the number of workers (bitwise with
roadrunner and AMICI, within the integration with COPASI, see
sbmlsim.simulator.simulator).
In a worker of a pool a large array of the result of a chunk is written into
shared memory instead of being pickled through the pipe of the pool, see
SharedChunkResult and sbmlsim.simulator.shared, on POSIX only.
A point which fails is reported once, by its error: roadrunner logs the error
of CVODE as well, so its log level is lowered while a chunk of a model of
roadrunner runs. A feature which the engine does not simulate, the
NotImplementedError of an executor, is the same for every point of a model
and is no failed point: it is raised also with on_error="flag". The
messages of SUNDIALS follow the log level of roadrunner (libroadrunner 2.11).
ScanPointError
¶
Bases: RuntimeError
A point of a scan failed and the run raises, see Chunk.on_error.
Attributes:
| Name | Type | Description |
|---|---|---|
index |
the flat index of the point in the scan. |
|
message |
the error of the point. |
Create the error of a point, the arguments pickle.
ModelSpec
dataclass
¶
What a worker needs to load a model as the parent loaded it.
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
identifies the model, its engine, its optimization level, its integrator and the settings of the integrator, the key of the model in the cache of a worker. |
engine |
Engine
|
the engine which simulates the model. |
source |
str
|
the SBML of the model as the parent loaded it, see
|
parameters |
tuple[tuple[str, float], ...]
|
the parameters added to the model, see |
jit_optimization |
JitOptimization | None
|
the optimization level the model is compiled with,
see |
integrator |
str
|
the name of the integrator, e.g. |
settings |
tuple[tuple[str, Any], ...]
|
every setting of the integrator by its name, the absolute
tolerance as the |
cache |
str | None
|
the directory of the compiled models a model of AMICI was
loaded from, which a worker loads it from as well, see
|
of
classmethod
¶
Get the spec of a loaded model and of the state of its integrator.
The integrator and its settings are read from the model after the
simulator set its own, see SBMLModel.integrator_state, so a setting
of the model which the simulator does not set, e.g. one of
RoadrunnerSBMLModel(settings=...) or of set_integrator_settings,
holds in a worker as well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
SBMLModel
|
the loaded model, of any engine. |
required |
Chunk
dataclass
¶
Points of a scan which share a plan and a model.
Attributes:
| Name | Type | Description |
|---|---|---|
indices |
ndarray
|
the flat indices of the points in the scan, in C order. |
plan |
Plan
|
the plan of the points. |
model |
int
|
the index of the model of the points among the models of the run. |
graph |
ObservableGraph
|
the observables of the run, which give the selections. |
values |
dict[str, ndarray]
|
target -> value of every point, which replaces the target wherever the plan sets it and is a change before the initialization otherwise. |
timed |
dict[float, dict[str, ndarray]]
|
time -> target -> value of every point, a change at that time. |
time |
ndarray | None
|
the grid of times to interpolate the kept timecourses onto,
|
on_error |
OnError
|
what to do about a point which fails. |
ChunkResult
dataclass
¶
The answer of a chunk.
Attributes:
| Name | Type | Description |
|---|---|---|
indices |
ndarray
|
the flat indices of the points, those of the chunk. |
values |
ndarray
|
the kept timecourses, |
scalars |
ndarray
|
the kept values per simulation, |
status |
ndarray
|
|
errors |
tuple[tuple[int, str], ...]
|
the flat index and the error of the first |
SharedChunkResult
dataclass
¶
The answer of a chunk of a worker whose large arrays are in shared memory.
The fields of ChunkResult, of which values and scalars are a
SharedArray when the worker wrote them into a segment, see
sbmlsim.simulator.shared.
received
¶
Get the ChunkResult, its arrays read from their segments.
Such an array is a read-only view on its segment, whose name is
removed, so this is called once, in the parent, see
sbmlsim.simulator.shared.receive.
point_plan
¶
Get the plan of the k-th point of values.
The values of the dimensions without a time are applied first, then the changes of the dimensions with one in time order, so a target in both has the value of the change at its time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plan
|
Plan
|
the plan of the points. |
required |
values
|
Mapping[str, ndarray]
|
target -> value of every point. |
required |
timed
|
Mapping[float, Mapping[str, ndarray]]
|
time -> target -> value of every point. |
required |
k
|
int
|
the point. |
required |
Returns:
| Type | Description |
|---|---|
Plan
|
The plan with the values of the point. |
run_chunk
¶
Run the points of a chunk on a loaded model, see the module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
Chunk
|
the points. |
required |
model
|
SBMLModel
|
the loaded model of the chunk, of any engine. |
required |
Returns:
| Type | Description |
|---|---|
ChunkResult
|
The values of every point. |
Raises:
| Type | Description |
|---|---|
ScanPointError
|
for the first point which fails, with
|
NotImplementedError
|
for a feature the engine does not simulate, also
with |
observe
¶
Evaluate the observables on the native solutions of points.
The graph is evaluated on all solutions at once. If an observable fails
the points are evaluated one at a time, each alone on its native solution
without padding, so a point fails exactly when its own evaluation fails,
whatever the other points are; the outputs of the points which succeed are
stacked again, padded with NaN to the longest of them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
ObservableGraph
|
the observables. |
required |
selections
|
Sequence[str]
|
the columns of the solutions, the time first. |
required |
solutions
|
Sequence[ndarray]
|
the native solutions of the points. |
required |
plans
|
Sequence[Plan]
|
their plans. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The time points |
dict[str, ndarray]
|
outputs (a timecourse |
list[tuple[int, str, BaseException]]
|
|
tuple[ndarray, dict[str, ndarray], list[tuple[int, str, BaseException]]]
|
points whose observable failed (the position in |
tuple[ndarray, dict[str, ndarray], list[tuple[int, str, BaseException]]]
|
their message and error. |
run_chunk_in_worker
¶
Run a chunk in a worker process, on the model the worker keeps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ModelSpec
|
the model of the chunk and the settings of its integrator. |
required |
chunk
|
Chunk
|
the points. |
required |
segment
|
str | None
|
the stem of the names of the segments of shared memory the
arrays of the result are written into, |
None
|
Returns:
| Type | Description |
|---|---|
ChunkResult | SharedChunkResult
|
The values of every point, see |
ChunkResult | SharedChunkResult
|
shared memory when |
quiet_roadrunner
¶
Lower the log level of roadrunner to critical messages, then restore it.
roadrunner logs the error of CVODE of a point which fails, which the run reports as the error of the point.