API documentation
Python bindings for yMMSL.
This package contains all the classes needed to represent a yMMSL file, as well as to read and write yMMSL files.
- class BaseEnv(*values)
Describes the base shell environment for execution.
Several options in
Implementation
describe additions to the shell environment to make subsequently, but we need to start somewhere. Different starting points make sense in different contexts, so there’s a choice:LOGIN
starts from an environment that resembles the default environment of the user running the simulation. The exact environment you get when you log in depends on whether you’re on a text terminal, graphical terminal, or using SSH, and also on the operating system and any local changes made by your system administrator. This cannot be reproduced exactly in all circumstances, but the below should get close on most systems.This will copy
TERM
,HOME
,SHELL
,USER
, andLOGNAME
from the manager environment, then run/bin/bash
and have it load/etc/environment
,/etc/profile
, and then the first of~/.bash_profile
,~/.bash_login
, and~/.profile
that it finds. Note that on most machines, these files will load other files in turn, often including~/.bashrc
.CLEAN
starts from the environment thatmuscle_manager
was started in, and then unloads any loaded modules and deactivates any active Python virtual environments. Any modules specified inmodules
and any virtual environment specified invirtual_env
will be activate after that, of course.Note that on some HPC machines, SLURM is made available though the environment modules. If you use the SRUNMPI execution model, then you’ll have to load it again explicitly to make the
srun
command available, or MUSCLE3 won’t be able to start your program.MANAGER
starts from the exact environment thatmuscle_manager
was started in, including any loaded modules and active virtual environments. Any modules specified inmodules
are then loaded on top of this, which may cause some of the existing modules to be unloaded if they are incompatible. If a virtual environment is specified invirtual_env
, then it will replace the active environment.- CLEAN = 2
Like MANAGER, but with any modules unloaded and venvs deactivated.
- LOGIN = 1
The environment you get after logging in when using bash.
- MANAGER = 3
The environment the manager was started in.
- class CheckpointAtRule(at: List[float | int] | None)
Defines an “at” checkpoint rule.
An “at” checkpoint rule creates a snapshot at the specified moments.
- at
List of checkpoints.
- class CheckpointRangeRule(start: float | int | None = None, stop: float | int | None = None, every: float | int = 0)
Defines a range of checkpoint moments.
If
start
is supplied, this rule specifies a checkpoint at:start
,start + every
,start + 2*every
, etc., for as long asstart + n*every <= stop
, withn
a whole number. If stop is not given, the range continues indefinitely.Start may be omitted, in which case a checkpoint is defined for
0
,every
,2*every
, etc. Note that in this case the range also extends to negative numbers (-every
,-2*every
, etc.), as simulated time may be negative (e.g. in rocket launch,t=0
is generally taken as lift-off time, but events already take place before that moment).- start
Start of the range.
- stop
Stopping criterium of the range.
- every
Step size of the range, must be positive.
- class CheckpointRule
Defines a checkpoint rule.
There are two flavors of rules:
CheckpointRangeRule
andCheckpointAtRule
. Do not use this class directly.
- class Checkpoints(at_end: bool = False, wallclock_time: List[CheckpointRule] | None = None, simulation_time: List[CheckpointRule] | None = None)
Defines checkpoints in a configuration.
There are three checkpoint triggers: at_end, wallclock_time and simulation_time. The at_end trigger specifies that a checkpoint should be created just before the workflow finishes. The wallclock_time trigger is based on the elapsed real time since starting the muscle_manager in seconds. The simulation_time trigger is based on the time in the simulation as reported by the instances.
Note that the simulation_time trigger assumes a shared concept of time among all components of the model.
- at_end
Whether a checkpoint should be created just before ending the workflow.
- wallclock_time
Checkpoint rules for the wallclock_time trigger.
- simulation_time
Checkpoint rules for the simulation_time trigger.
- update(overlay: Checkpoints) None
Update this checkpoints with the given overlay.
Sets at_end to True if it is set in the overlay, otherwise at_end remains as is. Updates the checkpoint rules for wallclock time and simulation time. See
CheckpointRules.update()
.
- class Component(name: str, implementation: str | None = None, multiplicity: None | int | List[int] = None, ports: Ports | None = None)
An object declaring a simulation component.
Simulation components are things like submodels, scale bridges, proxies, and any other program that makes up a model. This class represents a declaration of a set of instances of a simulation component, and it’s used to describe which instances are needed to perform a certain simulation.
- name
The name of this component.
- Type:
- implementation
A reference to the implementation to use.
- Type:
- multiplicity
The shape of the array of instances that execute simultaneously.
- Type:
List[int]
- class Conduit(sender: str, receiver: str)
A conduit transports data between simulation components.
A conduit has two endpoints, which are references to a port on a simulation component. These references must be of one of the following forms:
component.port
namespace.component.port (or several namespace prefixes)
- sender
The sending port that this conduit is connected to.
- receiver
The receiving port that this conduit is connected to.
- receiving_port() Identifier
Returns the identity of the receiving port.
- receiving_slot() List[int]
Returns the slot on the receiving port.
If no slot was given, an empty list is returned.
- Returns:
A list of slot indexes.
- sending_port() Identifier
Returns the identity of the sending port.
- sending_slot() List[int]
Returns the slot on the sending port.
If no slot was given, an empty list is returned.
- Returns:
A list of slot indexes.
- class Configuration(model: Model, settings: Settings | None = None, implementations: List[Implementation] | Dict[Reference, Implementation] = [], resources: Sequence[ResourceRequirements] | MutableMapping[Reference, ResourceRequirements] = [], description: str | None = None, checkpoints: Checkpoints | None = None, resume: Dict[Reference, Path] | None = None)
Configuration that includes all information for a simulation.
PartialConfiguration has some optional attributes, because we want to allow configuration files which only contain some of the information needed to run a simulation. At some point however, you need all the bits, and this class requires them.
When loading a yMMSL file, you will automatically get an object of this class if all the required components are there; if the file is incomplete, you’ll get a PartialConfiguration instead.
- model
A model to run.
- settings
Settings to run the model with.
- implementations
Implementations to use to run the model. Dictionary mapping implementation names (as References) to Implementation objects.
- resources
Resources to allocate for the model components. Dictionary mapping component names to Resources objects.
- description
A human-readable description of the configuration.
- checkpoints
Defines when each model component should create a snapshot
- resume
Defines what snapshot each model component should resume from
- check_consistent() None
Checks that the configuration is internally consistent.
This checks whether all conduits are connected to existing components, that there’s an implementation for every component, and that resources have been requested for each component.
If any of these requirements is false, this function will raise a RuntimeError with an explanation of the problem.
- class ExecutionModel(*values)
Describes how to start a model component.
- DIRECT = 1
Start directly on the allocated core(s), without MPI.
- INTELMPI = 3
Start using Intel MPI’s mpirun.
- OPENMPI = 2
Start using OpenMPI’s mpirun.
- SRUNMPI = 4
Start MPI implementation using srun.
- class Identifier(seq: Any)
A custom string type that represents an identifier.
An identifier may consist of upper- and lowercase characters, digits, and underscores.
- class Implementation(name: Reference, base_env: BaseEnv | None = None, modules: str | List[str] | None = None, virtual_env: Path | None = None, env: Dict[str, str] | None = None, execution_model: ExecutionModel = ExecutionModel.DIRECT, executable: Path | None = None, args: str | List[str] | None = None, script: str | List[str] | None = None, can_share_resources: bool = True, keeps_state_for_next_use: KeepsStateForNextUse = KeepsStateForNextUse.NECESSARY)
Describes an installed implementation.
An Implementation normally has an
executable
and any other needed attributes, withscript
set to None. You should specify a script only as a last resort, probably after getting some help from the authors of this library. If a script is specified, all other attributes except for the name, the execution model, can_share_resources and keeps_state_for_next_use must beNone
.If base_env is not specified then it defaults to MANAGER.
For
execution_model
, the following values are supported:- direct
The program will be executed directly. Use this for non-MPI programs.
- openmpi
For MPI programs that should be started using OpenMPI’s mpirun.
- intelmpi
For MPI programs that should be started using Intel MPI’s mpirun.
The
can_share_resources
attribute describes whether this implementation can share resources (cores) with other components in a macro-micro coupling. Set this toFalse
if the implementation does significant computing inside of its time update loop after having sent messages on its O_I port(s) but before receiving messages on its S port(s). In the unlikely case that it’s doing significant computing before receiving for F_INIT or after sending its O_F messages, likewise set this toFalse
.Setting this to
False
unnecessarily will waste core hours, setting it toTrue
incorrectly will slow down your simulation.- name
Name of the implementation
- base_env
Base environment to start from
- modules
HPC software modules to load
- virtual_env
Path to a virtual env to activate
- env
Environment variables to set
- execution_model
How to start the executable
- executable
Full path to executable to run
- args
Arguments to pass to the executable
- script
A script that starts the implementation
Whether this implementation can share resources (cores) with other components or not
- keeps_state_for_next_use
Does this implementation keep state for the next iteration of the reuse loop. See
KeepsStateForNextUse
.
- class KeepsStateForNextUse(*values)
Describes whether an implementation keeps internal state between iterations of the reuse loop.
See also Keeps state for next use.
- HELPFUL = 3
The implementation has an internal state, though this could be regenerated. Doing so may be expensive.
- NECESSARY = 1
The implementation has an internal state that is necessary for continuing the implementation.
- NO = 2
The implementation has no internal state.
- class MPICoresResReq(name: Reference, mpi_processes: int, threads_per_mpi_process: int = 1)
Describes resources for simple MPI implementations.
This allocates individual cores or sets of cores on the same node for a given number of MPI processes per instance.
- name
Name of the component to configure.
- mpi_processes
Number of MPI processes to start.
- threads_per_mpi_process
Number of threads/cores per process.
- class MPINodesResReq(name: Reference, nodes: int, mpi_processes_per_node: int, threads_per_mpi_process: int = 1)
Describes resources for node based MPI implementations.
This allocates resources for an MPI process in terms of nodes and cores, processes and threads on them.
- name
Name of the component to configure.
- nodes
Number of nodes to reserve.
- mpi_processes_per_node
Number of MPI processes to start on each node.
- threads_per_mpi_process
Number of threads/cores per process.
- class Model(name: str, components: List[Component], conduits: List[Conduit | MulticastConduit] | None = None)
Describes a simulation model.
A model consists of a number of components connected by conduits.
Note that there may be no conduits, if there is only a single component. In that case, the conduits argument may be omitted when constructing the object, and also from the YAML file; the conduits attribute will then be set to an empty list.
- name
The name by which this simulation model is known to the system.
- components
A list of components making up the model.
- conduits
A list of conduits connecting the components.
- check_consistent() None
Checks that the model is internally consistent.
This checks whether all conduits are connected to existing components, and will raise a RuntimeError with an explanation if one is not.
- update(overlay: Model) None
Overlay another model definition on top of this one.
This updates the object with the name, components and conduits given in the argument. The name is overwritten, and components are overwritten if they have the same name as an existing argument or else added.
Conduits are added. If a receiving port was already connected, the old conduit is removed. If a sending port was already connected, the new conduit is added and the sending port acts as a multicast port.
- Parameters:
overlay – A Model definition to overlay on top of this one.
- class ModelReference(name: str)
Describes a reference (by name) to a model.
- name
The name of the simulation model this refers to.
- class Operator(*values)
An operator of a component.
This is a combination of the Submodel Execution Loop operators, and operators for other components such as mappers.
- F_INIT = 1
Initialisation phase, before start of the SEL
- NONE = 0
No operator
- O_F = 5
Observation of final state, after the SEL
- O_I = 2
State observation within the model’s main loop
- S = 3
State update in the model’s main loop
- allows_receiving() bool
Whether ports on this operator can receive.
- allows_sending() bool
Whether ports on this operator can send.
- class PartialConfiguration(model: ModelReference | None = None, settings: Settings | None = None, implementations: List[Implementation] | Dict[Reference, Implementation] | None = None, resources: Sequence[ResourceRequirements] | MutableMapping[Reference, ResourceRequirements] | None = None, description: str | None = None, checkpoints: Checkpoints | None = None, resume: Dict[Reference, Path] | None = None)
Top-level class for all information in a yMMSL file.
- model
A model to run.
- settings
Settings to run the model with.
- implementations
Implementations to use to run the model. Dictionary mapping implementation names (as References) to Implementation objects.
- resources
Resources to allocate for the model components. Dictionary mapping component names to ResourceRequirements objects.
- description
A human-readable description of the configuration.
- checkpoints
Defines when each model component should create a snapshot
- resume
Defines what snapshot each model component should resume from
- as_configuration() Configuration
Converts to a full Configuration object.
This checks that this PartialConfiguration has all the pieces needed to run a simulation, and if so converts it to a Configuration object.
Note that this doesn’t check references, just that there is a model, implementations and resources. For the more extensive check, see
Configuration.check_consistent()
.- Returns:
A corresponding Configuration.
- Raises:
ValueError – If this configuration isn’t complete.
- update(overlay: PartialConfiguration) None
Update this configuration with the given overlay.
This will update the model according to
Model.update()
, copy settings from overlay on top of the current settings, overwrite implementations with the same name and add implementations with a new name, and likewise for resources and resume. The description of the overlay is appended to the current description. Checkpoints are updated according toCheckpoints.update()
.- Parameters:
overlay – A configuration to overlay onto this one.
- class Port(name: Identifier, operator: Operator)
A port on a component.
Ports are used by component to send or receive messages on. They are connected by conduits to enable communication between components.
- name
The name of the port.
- operator
The MMSL operator in which this port is used.
- class Ports(f_init: None | str | List[str] = None, o_i: None | str | List[str] = None, s: None | str | List[str] = None, o_f: None | str | List[str] = None)
Ports declaration for a component.
Ports objects compare for equality by value. The names may be specified as a list of strings, or separated by spaces in a single string. If a particular operator has no associated ports, it may be omitted. For example:
ports: f_init: # list of names - a - b o_i: c d # on one line, space-separated s: e # single port # o_f omitted as it has no ports
- f_init
The ports associated with the F_INIT operator.
- o_i
The ports associated with the O_I operator
- s
The ports associated with the S operator.
- o_f
The ports associated with the O_F operator
- operator(port_name: Identifier) Operator
Looks up the operator for a given port.
- Parameters:
port_name – Name of the port to look up.
- Returns:
The operator for that port.
- Raises:
KeyError – If no port with this name was found.
- port_names() Iterable[Identifier]
Returns an iterable containing the names of all ports.
- class Reference(parts: str | List[Identifier | int])
A reference to an object in the MMSL execution model.
References in string form are written as either:
an Identifier,
a Reference followed by a period and an Identifier, or
a Reference followed by an integer enclosed in square brackets.
In object form, they consist of a list of Identifiers and ints. The first list item is always an Identifier. For the rest of the list, an Identifier represents a period operator with that argument, while an int represents the indexing operator with that argument.
Reference objects act like a list of Identifiers and ints, you can get their length using len(), iterate through the parts using a loop, and get sublists or individual items using []. Note that the sublist has to be a valid Reference, so it cannot start with an int.
References can be compared for equality to each other or to a plain string, and they can be used as dictionary keys. Reference objects are immutable (or they’re supposed to be anyway), so do not try to change any of the elements. Instead, make a new Reference. Especially References that are used as dictionary keys must not be modified, this will get your dictionary in a very confused state.
- class ResourceRequirements(name: Reference)
Describes resources to allocate for components.
- name
Name of the component to configure.
- class Settings(settings: Dict[str, str | int | float | bool | List[int] | List[float] | List[List[float]] | bool_union_fix] | None = None)
Settings for doing an experiment.
An experiment is done by running a model with particular settings, for the submodel scales, model parameters and any other configuration.
- as_ordered_dict() OrderedDict
Represent as a dictionary of plain built-in types.
- Returns: A dictionary that uses only built-in types, containing
the configuration.
- class ThreadedResReq(name: Reference, threads: int)
Describes resources for threaded implementations.
This includes singlethreaded and multithreaded implementations that do not support MPI. As many cores as specified will be allocated on a single node, for each instance.
- name
Name of the component to configure.
- threads
Number of threads/cores per instance.
- dump(config: PartialConfiguration) str
Converts a yMMSL configuration to a string containing YAML.
- Parameters:
config – The configuration to be saved to yMMSL.
- Returns:
A yMMSL YAML description of the given document.
- load(source: str | Path | IO[Any]) PartialConfiguration
Loads a yMMSL document from a string or a file.
- Parameters:
source – A string containing yMMSL data, a pathlib Path to a file containing yMMSL data, or an open file-like object containing from which yMMSL data can be read.
- Returns:
A PartialConfiguration object corresponding to the input data.
- save(config: PartialConfiguration, target: str | Path | IO[Any]) None
Saves a yMMSL configuration to a file.
- Parameters:
config – The configuration to save to yMMSL.
target – The file to save to, either as a string containing a path, as a pathlib Path object, or as an open file-like object.