Compilers
Contents
Compilers#
By default,
the Simulator uses the NumpyCompiler,
which compiles the model to solve as an ODE system.
But the compiler can be changed, for instance, for other types of integration, such a stochastic simulations,
For large models,
we have implemented a NumbaCompiler,
which compiles the RHS (right hand side) using numba,
providing a significant speed boost.
NumbaCompiler#
When integrating a large model,
such as Corbat2018,
which has 82 mass-action reactions between 69 species,
it is significantly faster to use the NumbaCompiler.
Using the default NumpyCompiler:
import numpy as np
from simbio.models.corbat2018 import Corbat2018_extrinsic
from simbio.simulator import Simulator
t = np.linspace(0, 30_000, 100)
sim = Simulator(Corbat2018_extrinsic)
%time df = sim.run(t)
CPU times: user 5.72 s, sys: 73.6 ms, total: 5.8 s
Wall time: 5.66 s
To switch to the NumbaCompiler,
we must import it from simbio.compilers,
and pass it to Simulator:
from simbio.compilers.numba import NumbaCompiler
sim = Simulator(Corbat2018_extrinsic, compiler=NumbaCompiler)
%time df = sim.run(t)
/home/docs/checkouts/readthedocs.org/user_builds/simbio/envs/latest/lib/python3.10/site-packages/numba/core/utils.py:612: NumbaExperimentalFeatureWarning: First-class function type feature is experimental
warnings.warn("First-class function type feature is experimental",
CPU times: user 10.2 s, sys: 42.2 ms, total: 10.3 s
Wall time: 10.3 s
The first run is slower,
as it includes the numba compilation time.
But subsequent runs are lightning fast,
as the compilation is cached:
%time df = sim.run(t)
CPU times: user 365 ms, sys: 3.91 ms, total: 369 ms
Wall time: 368 ms