A task wraps a top-level def with its argument and result types:
def square(x: Int) -> Int:
return x * x
comptime Square = Task[Int, Int, square]
The types are spelled out because Mojo can't infer a function's result type
from the function value. The function may raise or not; both bind to
def(A) thin raises -> R.
Every node runs the same binary, so a task message names the task by ID: a
64-bit FNV-1a hash of the function's linkage name, which includes its module
and argument types. The hash is fixed, unlike hash(), so a driver and a
worker started separately agree on it.
An actor is a state type and the function that builds it, and each of its
methods is a function taking the state mut:
def new_counter(start: Int) -> Counter:
return Counter(start)
def add(mut c: Counter, n: Int) -> Int:
c.total += n
return c.total
comptime CounterActor = Actor[Counter, Int, new_counter]
comptime Add = Method[CounterActor, Int, Int, add]
Actors and methods go in the runtime's registry with the tasks. The worker
hosting an actor keeps its state behind a StatePtr, which only the actor's
own create, destroy, save and restore and its methods' call cast
back to its type. A checkpoint is the state through the codec, so a state
type that implements Codec chooses what it saves.
Structs
Task
struct Task[A_: Deinitable & Movable, R_: Deinitable & Movable, f: def(A_) raises thin -> R_]
A remote function taking one A_ and returning an R_.
Implements Movable, TaskLike
name
def name() -> StaticString
run
def run(args: Span[UInt8], mut out: List[UInt8])
Task2
struct Task2[A_: Deinitable & Movable, B_: Deinitable & Movable, R_: Deinitable & Movable, f: def(A_, B_) raises thin -> R_]
A remote function taking two arguments: rt.remote[T](a, b).
Implements Movable, Task2Like, TaskLike
name
def name() -> StaticString
run
def run(args: Span[UInt8], mut out: List[UInt8])
Task3
struct Task3[A_: Deinitable & Movable, B_: Deinitable & Movable, C_: Deinitable & Movable, R_: Deinitable & Movable, f: def(A_, B_, C_) raises thin -> R_]
A remote function taking three arguments: rt.remote[T](a, b, c). See Task2.
Implements Movable, Task2Like, Task3Like, TaskLike
name
def name() -> StaticString
run
def run(args: Span[UInt8], mut out: List[UInt8])
TaskWith
struct TaskWith[A_: Deinitable & Movable, X: Copyable & Deinitable, R_: Deinitable & Movable, f: def(A_, Seq[X]) raises thin -> R_]
A task taking a value and other tasks' results, gathered in order: f(a, xs), submitted as rt.remote[T](a, refs). A shuffle's reducer is one: its partition number, and the map outputs holding that partition.
Implements Movable, TaskLike
name
def name() -> StaticString
run
def run(args: Span[UInt8], mut out: List[UInt8])
TaskMany
struct TaskMany[A_: Deinitable & Movable, X: Copyable & Deinitable, f: def(A_) raises thin -> Seq[X]]
A task with several results, each its own object: rt.remote_many[T](a, n) returns n ObjectRef[X]s, and f must return a Seq of exactly n. Each is stored, fetched and freed on its own, so a shuffle's map task can make one partition per reducer.
On the wire its result is a count, then each part as a length and its
encoding, so the worker splits it without knowing X.
Implements ManyLike, Movable, TaskLike
name
def name() -> StaticString
run
def run(args: Span[UInt8], mut out: List[UInt8])
Actor
struct Actor[S_: Deinitable & Movable, A_: Deinitable & Movable, new: def(A_) raises thin -> S_]
An actor whose state is an S_, built by new from an A_.
Implements ActorLike, Movable, TaskLike
name
def name() -> StaticString
create
def create(args: Span[UInt8]) -> StatePtr
destroy
def destroy(state: Pointer[NoneType, MutUntrackedOrigin])
save
def save(state: Pointer[NoneType, MutUntrackedOrigin], mut out: List[UInt8])
restore
def restore(bytes: Span[UInt8]) -> StatePtr
Method
struct Method[Act_: ActorLike, A_: Deinitable & Movable, R_: Deinitable & Movable, f: def(mut Act_.S, A_) raises thin -> R_]
A method of actor Act_: f on its state and an A_, returning R_.
Implements MethodLike, Movable, TaskLike
name
def name() -> StaticString
call
def call(state: Pointer[NoneType, MutUntrackedOrigin], args: Span[UInt8], mut out: List[UInt8])