View Source Handoff.Function (Handoff v0.1.0)
Represents a function in a computation graph.
structure
Structure
Each function in a Handoff DAG has these key components:
:id
- A unique identifier for the function (any term, typically an atom):args
- A list of other function IDs whose results this function depends on:code
- The actual function to execute, receives results of dependencies as input:results
- Storage for function output after execution:node
- Optional node assignment for distributed execution:cost
- Optional resource requirements map (e.g., %{cpu: 2, memory: 1000}):extra_args
- Additional arguments provided at execution time:type
- Type of the function, :regular or :inline:argument_inclusion
- One of :variadic or :as_list (defaults to :variadic):variadic
- Pass the list of N arguments as the first N arguments to the function:as_list
- Pass arguments as a list in the first argument to the function
examples
Examples
# A simple computation function with no dependencies
%Handoff.Function{
id: :generate_data,
args: [],
code: &Enum.random/1,
extra_args: [1..100]
}
# A function that depends on another function's result.
# The result of :generate_data (e.g., an integer) will be passed as the first argument
# to &*/2. The second argument for the multiplication (2) comes from extra_args.
%Handoff.Function{
id: :process_data,
args: [:generate_data],
code: &*/2,
extra_args: [2]
# If :generate_data produces X, this executes X * 2.
}
# A function with resource requirements for distributed execution.
# The result of :process_data will be passed as the first argument to IO.inspect/2.
%Handoff.Function{
id: :inspect_result,
args: [:process_data],
code: &IO.inspect/2,
extra_args: [label: "Inspect result"],
cost: %{cpu: 1, memory: 500} # Adjusted cost for a simple inspect
}
resource-costs
Resource Costs
The :cost
field allows you to specify resource requirements for distributed execution:
:cpu
- Number of CPU cores required:memory
- Memory in MB required:gpu
- GPU units required (if applicable)- Custom resource types can also be defined
These costs are used by allocators to determine which nodes can run which functions.