Skip to content

phyelds.vm

phyelds.vm

Engine interface: how to implement an aggregate computing engine.

NodeContext(node_id, sensors=None, outputs=None)

Bases: ABC

Abstract base class for the node context. This class should be implemented by the user.

Create the context of a device.

Parameters:

Name Type Description Default
node_id int

The ID of the device.

required
sensors dict[str, any]

The sensors of the device, mapping each name to its value.

None
outputs dict[str, any]

The outputs of the device, mapping each name to its value; empty by default.

None

sense(sensor_name)

Read a sensor of the device.

Parameters:

Name Type Description Default
sensor_name str

The name of the sensor.

required

Returns:

Type Description
any

The value of the sensor, or None if the device has no such sensor.

store(output_name, value)

Write an output of the device, replacing its previous value.

Parameters:

Name Type Description Default
output_name str

The name of the output.

required
value any

The value to store.

required

position()

Return the position of the device.

Returns:

Type Description
any

The value of the "position" sensor, or None if there is none.

time()

Return the time of the current round, as perceived by the device.

Returns:

Type Description
float

The value of the "time" sensor, or None if there is none.

EngineState(stack=None, state_trace=None, count_stack=None, to_send=None, messages=None, node_id=0, reads=None) dataclass

Inner state of the engine used to process aggregate computation.

Engine()

Bases: ABC

Abstract base class for the engine. This class should be implemented by the user.

Attributes:

Name Type Description
vectorized bool

Whether the engine runs many devices at once, each value holding one per device: @aggregate then rewrites the if statements, the conditional expressions, and, or, not, min and max into branch and choose, which run each side for the devices choosing it. False by default: one device, plain Python.

setup(node_context, messages, state=None)

Prepare the engine for a round of the device.

Parameters:

Name Type Description Default
node_context NodeContext

The context of the device running the round.

required
messages dict[int, dict[str, any]]

The messages received from the neighbors, mapping each neighbor ID to the values it sent, keyed by alignment path.

required
state dict[str, Any] | None

The state left by the previous round (see state_trace), or None on the first round.

None

Returns:

Type Description
Engine

The engine itself.

enter(name)

Enter a new alignment point, nested in the current one.

Parameters:

Name Type Description Default
name str

The name of the alignment point.

required

exit()

Leave the current alignment point, going back to the enclosing one.

branch(condition, if_true, if_false)

Run the branch of a conditional that condition chooses, in its own alignment point.

if_true runs in "left" and if_false in "right", as align_left and align_right do, so devices on different branches do not exchange values. A batch engine, running many devices at once, runs each branch for the devices choosing it.

Parameters:

Name Type Description Default
condition Any

What chooses the branch: if_true if it is truthy.

required
if_true Callable[[], Any]

The branch for a truthy condition.

required
if_false Callable[[], Any]

The branch for a falsy condition.

required

Returns:

Type Description
Any

What the branch returns.

choose(condition, if_true, if_false)

Run the side of a choice that condition picks, without an alignment point.

A batch engine, running many devices at once, runs each side for the devices picking it.

Parameters:

Name Type Description Default
condition Any

What picks the side: if_true if it is truthy.

required
if_true Callable[[], Any]

The side for a truthy condition.

required
if_false Callable[[], Any]

The side for a falsy condition.

required

Returns:

Type Description
Any

What the side returns.

current_path()

Return the current alignment path.

Returns:

Type Description
list

A copy of the segments of the alignment points entered so far, outermost

list

first, as the engine encodes them: pass it back to the other methods.

write_state(value, stack=None)

Store a value of the device's state, to be read in the next rounds.

Parameters:

Name Type Description Default
value any

The value to store.

required
stack list | None

The alignment path the value belongs to; the current one by default.

None

read_state(stack=None)

Read a value of the device's state, marking it as still in use.

Parameters:

Name Type Description Default
stack list | None

The alignment path the value belongs to; the current one by default.

None

Returns:

Type Description
any

The stored value, or None if there is none.

read_state_or(init, stack=None)

Read a value of the device's state, storing init first if there is none.

A batch engine, running many devices at once, reads or stores each one's value.

Parameters:

Name Type Description Default
init Any

The value to store, and return, if the state has none at stack.

required
stack list | None

The alignment path the value belongs to; the current one by default.

None

Returns:

Type Description
Any

The stored value, or init.

state_trace()

Return the state of the device, to pass to setup in the next round.

Returns:

Type Description
dict[str, any]

A copy of the stored values, keyed by alignment path.

forget(_self_path)

Remove a value from the device's state.

Parameters:

Name Type Description Default
_self_path list

The alignment path of the value to remove.

required

send(data, stack=None)

Queue a value to send to the neighbors at the end of the round.

Parameters:

Name Type Description Default
data any

The value to send.

required
stack list | None

The alignment path to send it at; the current one by default.

None

aligned()

Return the neighbors aligned with the current alignment point.

Returns:

Type Description
list[int]

The IDs of the neighbors that sent a value at the current alignment path.

aligned_values(path=None)

Return the values the aligned neighbors sent at path.

Parameters:

Name Type Description Default
path list | None

The alignment path of the values; the current one by default.

None

Returns:

Type Description
dict[int, any]

A dictionary mapping each neighbor ID to the value it sent at path.

own_value(value)

Return what value holds for the device itself.

A field, as exchange sends, addresses each device its own value: the device takes its entry, or the default of the field if it has none. A batch engine, running many devices at once, takes the entry of each one.

Parameters:

Name Type Description Default
value Any

A field, or a value the same for every device.

required

Returns:

Type Description
Any

The entry of the device in a field, or its default; any other value as it is.

default_value(value)

Return what value addresses to the devices it has no entry for.

Parameters:

Name Type Description Default
value Any

A field, or a value the same for every device.

required

Returns:

Type Description
Any

The default of a field; any other value as it is.

neighbor_field(local, path=None, default=_LOCAL, *, addressed=False)

Return the field of the values the aligned neighbors sent at path, and local.

A batch engine, running many devices at once, returns one field for all of them.

Parameters:

Name Type Description Default
local Any

The value of the device itself.

required
path list | None

The alignment path of the values; the current one by default.

None
default Any

The value of the field for the devices it does not hold; local by default.

_LOCAL
addressed bool

Whether a neighbor may have sent a field, addressing each device its own value, as exchange does: the device then takes the value addressed to it.

False

Returns:

Type Description
NeighborhoodField

A NeighborhoodField mapping the device to local and each aligned neighbor to what it sent.

begin_process(name)

Enter an isolated alignment point for a process key, saving current round effects.

Parameters:

Name Type Description Default
name str

The process alignment segment name.

required

Returns:

Type Description
Any

An opaque checkpoint token for end_process.

end_process(token, commit)

Leave a process alignment point, committing or rolling back its round effects.

Parameters:

Name Type Description Default
token Any

The checkpoint token returned by begin_process.

required
commit bool

True to keep state and outgoing messages, False to restore token.

required

cooldown()

End the round: drop the state not read in it and collect the outgoing messages.

Returns:

Type Description
dict[str, Any]

The values to send to the neighbors, keyed by alignment path.

phyelds.vm.rounds

One round of one device: the step every host repeats, whatever its devices and their messages.

Round(result, outbox, state, outputs) dataclass

What a round of a device leaves behind.

result instance-attribute

What the program returned.

outbox instance-attribute

The messages to send to the neighbors, keyed by alignment path.

state instance-attribute

The state to give to the next round.

outputs instance-attribute

What the program stored with store.

run_round(program, device, sensors, inbox, state=None, *, paths='debug', engine_instance=None)

Run program once on device, setting the round engine as the current one.

Parameters:

Name Type Description Default
program Callable[[], Any]

The @aggregate function to run.

required
device Hashable

The id of the device.

required
sensors Mapping[str, Any]

The sensors of the device in this round, by name.

required
inbox Mapping[Hashable, Mapping[str, Any]]

The messages of the neighbors, by their id.

required
state Mapping[str, Any] | None

The state left by the previous round; none on the first.

None
paths Any

How the engine encodes alignment paths when creating a MutableEngine.

'debug'
engine_instance Engine | None

Optional pre-built Engine to reuse across rounds.

None

Returns:

Type Description
Round

The result, messages, state and outputs of the round.

phyelds.vm.synchronous

Synchronous rounds of many devices in one process: the building block of hosts that step all their agents together, such as the model of a multi-agent simulator.

Rounds(results, outboxes, states, outputs) dataclass

What the rounds of many devices leave behind, by device id.

results instance-attribute

What the program returned on each device.

outboxes instance-attribute

The messages each device sends to its neighbors, keyed by alignment path.

states instance-attribute

The state each device gives to its next round.

outputs instance-attribute

What the program stored with store on each device.

RoundRunner(program, *, workers=1, paths='compact', engine_factory=None)

Runs the rounds of many devices that do not depend on each other: what a device reads comes from its inbox and its state, never from the rounds run with it.

The rounds can run in a pool of workers threads: the results are the same, and they take less time on a free-threaded Python (3.14t), where threads run in parallel. Elsewhere one thread at a time runs Python code, so more workers only add their cost. With one worker, the calling thread runs the rounds on one engine, kept from a run to the next.

Parameters:

Name Type Description Default
program Callable[[], Any]

The @aggregate function to run on each device.

required
workers int

How many threads run the rounds; with 1, the calling thread runs them.

1
paths Any

Alignment path encoding for the default MutableEngine ("compact" by default).

'compact'
engine_factory Callable[[], Engine] | None

Optional factory building one Engine per thread.

None

Raises:

Type Description
ValueError

If workers is below 1.

run(sensors, inbox_of, states)

Run the program once on each device of sensors.

Parameters:

Name Type Description Default
sensors Mapping[Hashable, Mapping[str, Any]]

The sensors of each device to run, by its id.

required
inbox_of Callable[[Hashable], Mapping[Hashable, Mapping[str, Any]]]

The messages a device reads, by the id of their sender, given its id.

required
states Mapping[Hashable, Mapping[str, Any]]

The state each device left in its previous round, by its id; a device missing from it runs its first round.

required

Returns:

Type Description
Rounds

What the rounds leave behind, by device id, in the order of sensors.

SynchronousRuntime(program, *, workers=1, paths='compact', engine_factory=None)

Runs an aggregate program on many devices, one synchronous round at a time.

In a round every device receives what its neighbors sent in the previous round, so the result does not depend on the order of the devices. The devices are known by their ids, and the host gives their sensors and neighbors at each round.

The rounds of the devices are independent, so they can run in a pool of workers threads: the results are the same, and they take less time on a free-threaded Python (3.14t), where threads run in parallel. Elsewhere one thread at a time runs Python code, so more workers only add their cost.

Parameters:

Name Type Description Default
program Callable[[], Any]

The @aggregate function to run on each device.

required
workers int

How many threads run the rounds of the devices; with 1, the calling thread runs them.

1
paths Any

Alignment path encoding for the default MutableEngine ("compact" by default).

'compact'
engine_factory Callable[[], Engine] | None

Optional factory building one Engine per worker thread.

None

Raises:

Type Description
ValueError

If workers is below 1.

outputs = {} instance-attribute

What the program stored with store on each device, in the last round.

run_round(sensors, neighbors)

Run the program once on each device of sensors.

A device missing from a round is forgotten: its state and messages are dropped.

Parameters:

Name Type Description Default
sensors Mapping[Hashable, Mapping[str, Any]]

The sensors of each device of this round, by its id.

required
neighbors Mapping[Hashable, Iterable[Hashable]]

The ids of the neighbors of each device, by its id.

required

Returns:

Type Description
dict[Hashable, Any]

The result of the program on each device, by its id, in the order of sensors.

reset()

Forget the state and the messages of every device.

phyelds.vm.asynchronous

Asynchronous rounds of many devices in one process: each device sends right after its own round (a spike), so the next devices to run already read its new message.

AsynchronousRuntime(program, *, retention=None, paths='compact', engine_factory=None)

Runs an aggregate program on many devices, one spike or sequential round at a time.

When a device runs, it reads the last messages its current neighbors sent and publishes its new message right away, so the next devices to run read it in the same step.

Parameters:

Name Type Description Default
program Callable[[], Any]

The @aggregate function to run on each device.

required
retention float | None

For how long, in the units of the "time" sensor or time, a silent device is kept before its state, messages and outputs are forgotten; None by default, which keeps silent devices until forget or reset.

None
paths Any

Alignment path encoding for the default MutableEngine ("compact" by default).

'compact'
engine_factory Callable[[], Engine] | None

Optional factory building the Engine used to run rounds.

None

Raises:

Type Description
ValueError

If retention is not positive.

outputs = {} instance-attribute

What the program stored with store on each device, in its last spike.

spike(device, sensors, neighbors, *, time=None)

Run the program once on device, keeping the state and messages of the others.

Parameters:

Name Type Description Default
device Hashable

The id of the device running the spike.

required
sensors Mapping[str, Any]

The sensors of device in this spike, by name.

required
neighbors Iterable[Hashable]

The ids of the current neighbors of device.

required
time float | None

The time of this spike, when it is not in sensors["time"].

None

Returns:

Type Description
Any

The result of the program on device.

Raises:

Type Description
ValueError

If retention is set and neither time nor sensors["time"] is given.

run_round(sensors, neighbors, *, time=None)

Run the program once on each device of sensors, in the order of sensors.

Parameters:

Name Type Description Default
sensors Mapping[Hashable, Mapping[str, Any]]

The sensors of each device of this round, by its id.

required
neighbors Mapping[Hashable, Iterable[Hashable]]

The ids of the neighbors of each device, by its id.

required
time float | None

The time of this round, when it is not in sensors[device]["time"].

None

Returns:

Type Description
dict[Hashable, Any]

The result of the program on each device, by its id, in the order of sensors.

forget(device)

Forget the state, the messages and the outputs of device.

Parameters:

Name Type Description Default
device Hashable

The id of the device to forget.

required

reset()

Forget the state and the messages of every device.