This library is in flux. APIs may change without notice. Generated from source with koruc 0.1.7 on 8/19/2026.
Kernel
~import std/kernelKoru Standard Library: Computation Kernels
kernel.kz · 6 tors
Koru Standard Library: Computation Kernels · 38 more lines
Koru Standard Library: Computation Kernels
High-performance computation primitives inspired by shader languages.
Describe RELATIONSHIPS between data elements, not iteration patterns.
The compiler decides optimal data layout and iteration strategy.
USAGE:
import std/kernel
// 1. Declare the shape (structure with explicit types)
std/kernel:shape(Body) { x: f64, y: f64, z: f64, vx: f64, vy: f64, vz: f64, mass: f64 }
// 2. Initialize kernel data (creates instance of shape)
// Uses Koru syntax (x: value) which transforms to Zig (.x = value)
std/kernel:init(Body) { x: 0, y: 0, z: 0, vx: 1.0, vy: 0, vz: 0, mass: 1.0 }
| kernel bodies |> ...
// 3. Perform kernel operations
std/kernel:pairwise {
bodies.vel -= d * mag * bodies.other.mass
bodies.other.vel += d * mag * bodies.mass
}
DESIGN:
- kernel.shape is [norun] - just a declaration, sits in AST as metadata
- kernel.init is [transform] - looks up shape, emits struct + instance
- kernel.pairwise is [transform] - emits iteration code
KERNEL OPERATIONS:
- kernel.pairwise: All unique pairs (i,j where i < j)
- kernel.self: Per-element operations (future)
- kernel.reduce: Reduction to single value (future)
- kernel.cross: Cross-product of two kernels (future)
MAGIC BINDINGS:
Inside kernel.pairwise:
- `bodies` refers to the current element (self, bodies[i])
- `bodies.other` refers to the paired element (bodies[j])
// KERNEL.SHAPE - Declare kernel data structure (just metadata, no execution)
//
// Declares the STRUCTURE of kernel data with explicit types.
// This is NOT a transform - it just sits in the AST for kernel.init to look up.
//
// Example:
// std/kernel:shape(Body) { x: f64, y: f64, z: f64, mass: f64 }
~[norun] pub tor shape { expr: Expression, source: Source }// Internal no-op event for refactored flows
// (This is here for reference; the transform injects a local one into the user program)
~[norun] pub tor nop {}~[comptime|transform|claims_descendants] pub tor init {
expr: Expression,
source: Source,
invocation: *const Invocation,
item: *const Item,
program: *const Program,
reporter: std/compiler:*ErrorReporter,
allocator: std.mem.Allocator
} -> SiteResult~[comptime|transform] pub tor pairwise {
expr: ?Expression,
source: Source,
invocation: *const Invocation,
item: *const Item,
program: *const Program,
allocator: std.mem.Allocator
} -> SiteResult~[comptime|transform] pub tor self {
expr: Expression,
source: Source,
invocation: *const Invocation,
item: *const Item,
program: *const Program,
allocator: std.mem.Allocator
} -> SiteResult// Kernel:step - wraps kernel ops in an outer iteration loop
// Kernel:step - wraps kernel ops in an outer iteration loop.
// The proc removes step from the AST after init's fusion consumes it.
~[comptime|transform] pub tor step {
expr: ?Expression,
invocation: *const Invocation,
item: *const Item,
program: *const Program,
allocator: std.mem.Allocator
} -> SiteResult