Unit ABI¶
A task type which splits into units implements the operations of the unit ABI simrun.unit/1: describe, plan, count_units, setup, unit, merge and run. The same messages reach an implementation through every carrier: a WebAssembly module (wasm), a process which speaks them on its standard input and output (serve), argv templates of processes (command) and Rust compiled into the harness (native); and the python functions of a plugin (python), which the harness runs as the carrier serve of python -m simrun.unit_serve <type>. The manifest of a task type (simrun.task-type/1) lists its implementations, and a harness runs the first of native, wasm, serve, command, python which provides the operations of a task and whose host services it has.
The ABI is frozen: a change which a module or a host of version 1 would not understand is simrun.unit/2. A revision of version 1 may add optional fields to the messages, which a receiver ignores when it does not know them.
Messages¶
A request of the host is a JSON object with the operation in op:
{"op": "unit", "settings": {"n": 12}, "index": 7, "paths": {"out": "/out", "inputs": {"fit": "/inputs/fit"}, "scratch": "/scratch", "task": "/task"}}
| op | fields | result | when |
|---|---|---|---|
describe |
{"abi": "simrun.unit/1", "name": ..., "ops": [...], "requires": [...]} |
once, when the implementation is loaded | |
plan |
settings |
the planned settings, the ones given and the ones the plan set (the defaults stay with the type) | before a submission (the client): seeds, ids, units |
count_units |
settings |
the number of units | at the submission and in the coordinator |
setup |
settings, paths |
null |
once per instance, before its first unit |
unit |
settings, index, paths |
null |
per index, the outputs written into out |
merge |
settings, paths |
the summary of the task, or null |
once all units are done |
run |
settings, paths |
the summary of the task, or null |
a task which is not split |
tell |
answers or error |
as the operation it continues | the answers to an ask |
The response is exactly one of
{"ok": <result>},{"error": {"kind": "settings" | "failed" | "unsupported", "message": "..."}}:settingsfor settings which are not valid,failedfor an operation which failed (a unit of an analysis: exit 3 of the worker),unsupportedfor an operation the implementation does not provide (or a service the host does not provide),{"ask": {"service": "<name>/<version>", "requests": [...]}}: the operation needs answers of a host service first.
The host writes the summary of merge and run into out/summary.json (as python's json.dumps writes it), which simrun compare reads.
Paths. A module sees the directories the host preopens: /out (read and write, a port is /out/<port>), /inputs/<port> (read only, the output the port reads, or its staged copy in the scratch directory), /scratch (read and write, per instance) and /task (read and write: the settings, checkpoints). A native implementation sees the same names through its TaskFs; a process gets the real directories in paths.
Rules of a unit. A unit may run more than once (an evicted worker, a restarted incarnation of a worker), so its outputs depend on the settings and the index only, and every file is written atomically: a temporary file in the same directory (a name which starts with .), then renamed. A merge reads the units by index. That is what makes the result of a task independent of the number of pods, processes, threads and web workers.
Host services¶
An operation which runs on a host (setup, unit, merge, run) may answer an ask instead of a result: requests to a host service named <name>/<version>. The host answers every request of the ask in order with {"op": "tell", "answers": [...]}, or tells {"op": "tell", "error": {...}} when it has no answers (a service it does not provide, a service which failed), and the operation answers that tell as it would have answered the request: a result, an error, or the next ask. An instance keeps the state of an operation between an ask and the tell of its answers; the host sends nothing else to it meanwhile.
This is how a module drives work which it cannot do inside its own instance. sbmlsim-core's fits and scans are drivers which ask for simulations: compiler.wasm compiles a model to a WebAssembly module which the host instantiates and runs (wasmtime natively, WebAssembly in a page), which a WASI reactor cannot do itself. Its unit asks sbmlsim.simulation/1 and is told the tables.
An implementation declares the services it asks in requires of its manifest (and of its describe). A harness runs an implementation only when it provides every service it requires, else it picks the next implementation (native, command, python). The services are pluggable and not part of simrun beyond their routing:
- native: a Rust
Serviceregistered with the host (simrun_exec::services::Services), - a process: a service manifest
{"format": "simrun.service/1", "name": "sbmlsim.simulation/1", "argv": [...]}in a directory ofSIMRUN_SERVICES(a path list) or inshare/simrun/services/of the environment; the process gets one JSON line{"service": ..., "requests": [...]}per ask and answers{"answers": [...]}or{"error": {...}}, - a browser: a JavaScript registration of the browser backend of the web app,
services.register("<name>/<version>", (requests) => answers)ofsimrun-core/browser/services, the npm package simrun-core (an answer may be a promise); a page providessimrun.echo/1and the services registered, see Running in the browser.
Every host provides simrun.echo/1, which answers every request with itself: the example of the kit asks it.
Progress. A module may call simrun.progress(ptr, len) with a JSON object, a process may write lines {"progress": {...}} before a response; the host appends a line {"time", "worker", "event": "progress", "unit", ...} with the fields of the object to progress.jsonl of the task, whose numbers the web app shows.
Determinism. Units are deterministic per settings and index across instances, threads and carriers, so one instance per worker thread (or web worker) is the parallelism and nothing is shared between instances.
Carriers¶
WebAssembly. A core module for wasm32-wasip1 built as a reactor, which exports
memory
simrun_abi() -> i32 ; 1
simrun_alloc(len: i32) -> i32 ; a buffer of len bytes for a request
simrun_free(ptr: i32, len: i32) ; frees a buffer of a response
simrun_call(ptr: i32, len: i32) -> i64 ; the response, (ptr << 32) | len
The host writes a request into a buffer of simrun_alloc and calls simrun_call, which owns the buffer of the request from then on (the module frees it); the host reads the response and frees it with simrun_free. The module imports WASI preview 1 only, and optionally simrun.progress(ptr: i32, len: i32), which every host provides. No environment, no arguments, no sockets; its standard output and error go to the log of the task. One instance is one "process": setup once, then units one after the other. The memory of an instance is capped at the memory per worker of the plan: a module beyond it ends its worker with exit 137, which is retried with more memory, as a process killed for its memory is. A module of a manifest is checked against its sha256 before it is instantiated.
Process (serve). The same messages, one per line on standard input and output, standard error for the log; lines {"progress": {...}} before a response. The host starts one process per instance, sends setup, then units; the process ends at the end of its input. It runs in a process group of its own; the harness kills it after the grace of a stop.
Command. Argv templates per operation with {settings} (the file of the settings), {out}, {index}, {scratch} and a part with {input} repeated per input port, sorted by port, as <port>=<path>; count_units may be {"setting": "units"}. A process per operation, exit 0 done, killed (SIGKILL or exit 137) a kill for memory, any other code a failure.
Native. A Rust Units of the crate simrun-unit: describe and instances which answer requests (Instance::call(request, cx)), with the files of the task in cx.fs and the progress in cx.progress. The same code compiles to a module with simrun_unit::export_units!, so a Rust implementation is native in the harness and a module in a browser. demo.squares (crates/simrun-unit-example) is both.
Writing a unit module¶
crates/simrun-unit-example is the template: a crate of crate-type = ["cdylib", "rlib"] which implements simrun_unit::Units and exports it for wasm32-wasip1:
Its Cargo.toml depends on simrun-unit with the feature guest (a git dependency on this repository) and denies, not forbids, unsafe_code, since the exports are #[unsafe(no_mangle)]. Build it with a profile of panic = "abort" and lto, as the profile unit of the workspace:
Any language with a wasm32-wasip1 target can implement the four exports instead; the messages are JSON.
The conformance kit¶
simrun-abi check runs a module through every operation and checks what a host relies on; exit 0 when it conforms, 1 when a check fails:
pip install simrun # or: cargo install --git https://github.com/matthiaskoenig/simrun simrun --bin simrun-abi
simrun-abi check model.wasm --settings @settings.json [--manifest sbmlsim.scan.json] [--input data=path] [--service sbmlsim.simulation/1="sbmlsim-core serve-simulations"]
| check | what holds |
|---|---|
load, imports |
the module compiles and imports only WASI preview 1 and simrun.progress |
exports |
memory, simrun_abi() = 1, simrun_alloc, simrun_free, simrun_call |
describe |
the ABI, a name, known operations, the same answer twice |
services |
the host has every service the module requires (--service adds one) |
manifest |
with --manifest: the wasm implementation declares the operations and services the module describes, the settings are valid for its schema (and are used with its defaults) |
plan, count_units |
deterministic |
units, summary |
every unit in one instance, then the merge, whose summary is an object or null |
split |
the units in reverse order over several instances (--workers, 3), unit 0 twice: the same files byte for byte and the same summary |
rerun |
a merge and a unit again change nothing |
atomic |
a unit interrupted midway (by fuel, at a quarter, a half and three quarters of its work) leaves no part of an output under its final name |
errors |
a malformed request and an operation which is not provided are answered with an error, not a trap |
progress |
every progress report is a JSON object |
--max-units computes only the first units of a large task (the merge is not checked then), --keep <dir> keeps the outputs of the runs, --json prints the checks as JSON. simrun-abi serve <module> hosts a module as a process of the protocol and simrun-abi call <module> <op> ... runs one operation, so a module also runs through the carriers serve and command.
The harness¶
The harness of the pods is the binaries simrun-worker, simrun-merge and simrun-exec of the wheel simrun (the crate crates/simrun on the library simrun-exec), which every image of an environment installs with the wheel, and which the local backend runs on this machine. It reads the environment SIMRUN_* of a pod (simrun_core::podspec), keeps the ledger (units/ with pending/, claimed/<i>@<worker>, done/, size and heartbeats/, one rename per transition, NFS >= 4.1), writes the reports (workers/<worker>-<attempt>.json) and the progress lines, and exits with the codes 0 done, 3 a unit or the merge or the command failed, 4 a configuration error, 137 an instance died (killed for its memory, retried with more memory), 143 stopped on SIGTERM. The fixtures of its tests (ledger.json of the goldens, the outputs of demo.squares) hold the ledger, the reports and the progress.
simrun submit examples/squares.yaml --local # the harness of the installed wheel
cargo build --release -p simrun # the binaries of a checkout
SIMRUN_HARNESS_BIN=$PWD/target/release simrun submit examples/squares.yaml --local
It finds the task types in the built-in manifests, in the directories of SIMRUN_TASK_TYPES, in share/simrun/task-types/ of the environment (VIRTUAL_ENV, the prefix of SIMRUN_PYTHON) and, for a type found nowhere else, among the python plugins (python -m simrun.manifest); a type which none of these has may be one of the sources of the run, whose snapshots (SIMRUN_EDITABLE) it installs then and looks again. A type whose implementation is python runs as the carrier serve of python -m simrun.unit_serve <type>: a process per instance, which imports the plugin once and answers the messages with its functions (setup, compute_unit, merge, the count of TaskType.units), what the functions print going to the log, since the standard output of the process carries the protocol alone. Its own variables:
| variable | meaning |
|---|---|
SIMRUN_IMPLEMENTATION |
the kind of implementation to run instead of the first of the order (native, wasm, serve, command, python) |
SIMRUN_PYTHON |
the interpreter of the python plugins, python3 by default |
SIMRUN_SERVICES |
directories of service manifests |
SIMRUN_GRACE_S |
seconds the running units get after SIGTERM, 60 by default |
SIMRUN_CGROUP_ROOT |
the cgroup v2 directory, /sys/fs/cgroup by default (the tests) |
The images of the cluster install the wheel simrun (simrun_wheel of the settings of the service, see the service image), so the pods run its harness; the tests on the k3d cluster check it.
The browser host¶
The browser backend of the web app, the npm package simrun-core, hosts unit modules in web workers (frontend/packages/simrun-core/src/browser/): wasi.ts is its host of WASI preview 1, over the files of an instance in memory (memfs.ts, the preopens /out, /inputs/<port> read only, /scratch and /task; a rename is atomic), with the clocks, random_get, poll_oneoff on a clock (a sleep, Atomics.wait in an isolated page) and the standard output and error into the log of the task; no arguments, no environment, no sockets, and NOSYS for a function it does not provide. unit.ts instantiates a module (only WASI preview 1 and simrun.progress imported), checks simrun_abi, calls _initialize, caps its memory by lowering the maximum its memory section declares to the memory per worker of the plan, and calls the ABI with JSON text. A worker loads the outputs its input ports read and the files of the task from the store first, and writes what the module changed below /out and /task into the store after every call, before it answers. The vitest of frontend/packages/simrun-core/src/browser runs the example module through the checks of the conformance kit with this host in node.