The C backend for cell models#
beat advances the cell model with a function fun(states, t, parameters, dt) (and
missing_variables, for a split model) that acts on a
(num_states, num_points) array. beat.c_backend turns the C code that gotranx generates for a
single cell into such a function: it adds a loop over the points, compiles the result, and loads it
with ctypes. The result can be passed as fun to beat.odesolver.DolfinODESolver, and, for a
model without missing variables, to beat.odesolver.DolfinMultiODESolver, which takes none. See the
demo for a comparison with NumPy and Numba.
Usage#
From a gotranx ODE,
import gotranx
import beat
ode = gotranx.load_ode("tentusscher_panfilov_2006_epi_cell.ode")
fun = beat.c_backend.from_ode(ode, scheme="rush_larsen_2")
ode_solver = beat.odesolver.DolfinODESolver(
v_ode=v_ode, # e.g. a dolfinx.fem.Function in a P1 space
v_pde=pde.state, # pde is a beat.MonodomainModel
fun=fun,
init_states=fun.init_state_values(),
parameters=fun.init_parameter_values(),
num_states=ode.num_states,
v_index=fun.state_index("V"),
)
or from C code that you already have, for example from gotranx ode2c,
fun = beat.c_backend.compile_scheme(
c_code,
scheme="rush_larsen_2",
num_states=...,
num_parameters=...,
)
Always pass num_states and num_parameters here. The generated C code does not know the sizes of
the arrays, so without them nothing is checked and too-small arrays make the C code read out of
bounds (compile_scheme logs a warning). from_ode fills them in.
Split models#
A gotranx ODE with missing variables, the half of a split model that reads values from the other
half (ep = ode - ode.get_component("mechanics")), compiles the same way. from_ode(ep) gives a
scheme that takes missing_variables=, and DolfinODESolver(missing_variables=..., num_missing_variables=...) passes it on every step:
ode = gotranx.load_ode("split_model.ode")
ep = ode - ode.get_component("mechanics")
fun = beat.c_backend.from_ode(ep, scheme="rush_larsen_2")
ode_solver = beat.odesolver.DolfinODESolver(
v_ode=v_ode,
v_pde=pde.state,
fun=fun,
init_states=fun.init_state_values(),
parameters=fun.init_parameter_values(),
num_states=ep.num_states,
v_index=fun.state_index("v"),
missing_variables=missing_variables, # (num_missing_variables, num_points)
num_missing_variables=len(ep.missing_variables),
)
The rows are in the order of ep.missing_variables, the same as the missing dict of the numpy
module that gotranx generates, and fun.missing_index(name) gives a row. With compiled C code
that you already have, pass num_missing_variables to compile_scheme. As for num_states, the
generated C code does not know how many it reads: a num_missing_variables smaller than what the
C code reads is not detected, and the C code then reads past the end of the array.
A library knows whether it was built with missing variables, but not how many, and
CScheme(library_path, scheme, ...) refuses it when num_missing_variables says otherwise
(ValueError): a library built with missing variables needs a positive num_missing_variables,
and one built without needs 0. The count itself is not checked, so a library loaded by path must
be given the count it was built with; a smaller one makes the C code read past the end of the
array. A library built by an older beat, which does not say, must be rebuilt.
The function also has state_index(name), parameter_index(name), init_state_values() and
init_parameter_values(), which call the functions of the same name in the generated C.
init_state_values() and init_parameter_values() need num_states and num_parameters.
Options#
Option |
Default |
Meaning |
|---|---|---|
|
|
The name of the scheme (and of the C function) |
|
|
Passed on to |
|
|
Checked against the arrays; without it there is no bounds check |
|
|
Checked against the arrays; without it there is no bounds check |
|
|
The number of missing variables. A scheme with missing variables must be called with them, and one without must not ( |
|
|
Where the libraries are stored |
|
|
The C compiler |
|
|
The compiler flags |
|
|
Compile with |
|
|
The number of threads (with |
|
|
Rank 0 compiles, all ranks load |
The states, parameters and missing_variables must be C-contiguous float64 arrays.
parameters is either one vector for all points or a (num_parameters, num_points) array, for
parameters that vary in space, and missing_variables is either one vector or a
(num_missing_variables, num_points) array.
Pass out= to write the result into an existing array (it may be states itself).
Parallel runs and clusters#
Rank 0 compiles the library and the other ranks load it from the cache directory, so the cache directory must be on a file system that all nodes share. If it is not, all ranks raise an error that says so. The home directory usually is shared; otherwise set
BEAT_C_CACHE.The library is compiled inside the job, so
-march=nativetargets the compute nodes. With a native flag the cache key includes the CPU features the compiler detects (its predefined macros), so a library built on a node with a different CPU (e.g. a login node with AVX-512) is not reused on the compute nodes. Passcflags=("-O3",)for a library that runs anywhere.With
-march=native, all nodes of a multi-node job must have the same CPU type, since rank 0’s library is loaded on every node. Otherwise pass portablecflags=("-O3",).Several jobs can compile the same library into the same cache at the same time.
OpenMP#
With openmp=True the loop over the points runs on several threads. The points are handed out
dynamically, in chunks of 256, since schemes that adapt their sub-steps make some points much more
expensive than others. Threads and MPI ranks can be combined: with R ranks on a node of C cores,
use num_threads (or OMP_NUM_THREADS) of at most C / R, and make sure that the scheduler gives
each rank that many cores (for SLURM, --cpus-per-task). beat.c_backend.openmp_available() tells
whether the compiler supports OpenMP. Apple’s clang does not, without extra setup.
Requirements#
A C compiler. Any working dolfinx installation has one, since FFCx uses it to compile the finite element kernels. Windows is not supported.