Kernel memory and threading
The compiler plans memory and execution order. The backend allocates that memory and passes pointers to a kernel. The kernel uses those buffers for the current invocation.
Requirements
The schema’s requirements callback has this signature:
RedwoodStatus requirements(
const void *parameters,
size_t *input_bytes,
size_t *output_bytes,
size_t *scratch_bytes
);
Fill one byte count per declared input/output and one scratch count. Report the physical span accessed, including strides or packing. Guard size arithmetic against overflow and return REDWOOD_ERROR for unsupported parameters.
The compiler allocates from bounds. Required scratch must be safe for every supported workload in those bounds, including a smaller shape taking a different implementation path. The allocated storage stays fixed during execution.
Do not allocate heap buffers inside a kernel. Use supplied scratch and bounded stack temporaries. Calculate scratch requirements from the declared parameters.
Output and scratch lifetimes
Intermediate outputs and scratch share planned temporary memory. An output remains live until its last consumer finishes, after which its storage can be reused.
Do not retain pointers after returning. Returned metadata strings and schemas have a different lifetime: they must remain valid while the library is loaded, so static storage is appropriate.
Input/output overlap
can_alias(parameters, output_index, input_index) declares whether an output may overwrite an input. The compiler still checks layout and whether the old input is needed later.
A kernel can refuse overlap. When granting it, account for the whole algorithm, including parallel tasks and reads that happen after earlier writes.
The compiler may ask for shape-independent permission by passing NULL parameters. Handle that query safely or return false. An absent alias callback also grants no overwrite permission.
Views and caches
Inputs can be strided views or broadcasts. Accept the layouts your kernel supports and reject the others in bind.
Cache storage can be an input or output pointer. A projection can write straight into an appropriate cache destination when the graph, matching and memory plan permit it. Parameters must carry needed position, capacity and stride information.
Follow the cache update policy declared in RDL and stay within its fixed capacity.
Thread pool
The context supplies a backend-owned RedwoodThreadPool. Submit work through its run function:
context->thread_pool->run(
context->thread_pool->context,
task_count,
task,
task_data
);
A task callback receives its data, task index and worker index. Tasks are dynamically assigned; faster workers can receive more tasks.
The call waits until its tasks stop. Stack-owned task data can therefore remain local until run returns. Partition output writes and scratch between tasks to avoid races. Use the supplied thread pool.
For a BLAS path, coordinate library threading with Redwood’s pool to avoid oversubscribing the CPU.
Cancellation and failure
Check context->is_cancelled(context->cancellation_context) at entry and between bounded work chunks. Keep checks outside tight arithmetic loops when practical. External calls need bounded work too; cancellation is observed at the next check.
Return REDWOOD_CANCELLED when stopped, REDWOOD_ERROR on failure and REDWOOD_OK on success. Propagate failures and cancellation from worker tasks.
All workers must stop using invocation buffers before the kernel returns any status. The runtime treats a failed or cancelled session as unusable, since its outputs or caches may already be partly written.