.nib guide std nib 0.3.1-pre

Parallel kernels

How the walker runs an ordinary Vec.map or Vec.fold on several cores — with no parallel syntax, and an answer bit-identical to the serial one.

nib has no par, no spawn and no annotation. Parallelism is something the run finds: a Vec.map or Vec.fold whose loop is simple enough to prove safe becomes a kernel, and --parallel N lets kernels split their vector across N cores. A loop that is not a kernel runs serially and gives the same answer — declining is never an error.

module Sums where
  func fill :: i: Int, n: Int, v: Vec<Int> -> Vec<Int> =
    if i >= n then v else fill(i + 1, n, Vec.push(v, i))

  func main :: -> Int =
    let xs = fill(0, 200000, #[]);
    let sq = Vec.map(xs, { x -> x * x % 1000 });
    let total = Vec.fold(sq, 0, { acc, y -> acc + y });
    let top = Vec.fold(sq, 0, { acc, y -> if y > acc then y else acc });
    let label = Vec.map(xs, { x -> "{x}" });
    total + top + Vec.size(label)

wire Main runs Sums.main
92500996

Which loops are kernels

All six must hold:

  1. the vector is flat and numeric: Vec<Int> or Vec<Float>;
  2. the function is a lambda written at the call site;
  3. it captures only immutable scalars — not an effector's var, a container or a closure;
  4. its effect row is empty;
  5. its body is certified total: literals, the parameter, its captures, + - *, / and % by a nonzero literal, if over a comparison, and let — no calls, so it cannot raise;
  6. for a fold, the step is in a fixed associative set: Int + and *, a min or max written as an if, &&/|| over a predicate, or Vec.append(acc, #[g(x)]).

A Float sum is declined on purpose: IEEE addition is not associative, so splitting it would change the answer.

Asking which ones are

--kernels prints a line per site, to stderr, before the run:

$ nibc --run --kernels sums.nib
kernel  sums.nib:7:14   Vec.map   parallel  may run in chunks on --parallel lanes
kernel  sums.nib:8:17   Vec.fold  parallel  may run in chunks on --parallel lanes
kernel  sums.nib:9:14   Vec.fold  parallel  may run in chunks on --parallel lanes
kernel  sums.nib:10:17  Vec.map   serial    the kernel body is not in the certified-total subset — that subset is Int/Float literals, the parameter, certified captures, `+ - *`, `/` and `%` by a nonzero literal, and `if` over a comparison
92500996

The last map builds Strings, so it stays serial. --parallel N (default 1) sets the lanes; it is refused above the machine's core count rather than clamped.

How the answer stays the same

A kernel runs over unboxed arrays it allocates itself, on a resident pool of OCaml 5 domains that stays parked between calls; it never touches the evaluator's values, cells or frames. The vector is cut into chunks of 8,192 elements, each chunk is reduced in order, and the partial results are combined by a fixed tree, so the answer is bit-identical at every lane count tested (1–4). Effects stay on the main domain, in program order, and because a kernel cannot raise, the first fault a program hits is the same one.

What it buys

Measured on a shared 4-vCPU Linux container: examples/parallel-site.nib takes 904 ms at --parallel=1 and 480 ms at 4, same answer. Most of a kernel's win is its representation, not its cores: at 2^21 elements a fold that Iter runs in 14.2 s takes 379 ms as a kernel on one lane and 338 ms on four. A vector of 8,193 elements is the smallest that can split, and two lanes lose there (0.70×); at 16,384 they win 2.8×. The language chapter docs/language/parallelism.md in the compiler tree has the full derivation.

Natively

A native build widens this. There, any Vec.map whose callback performs no effect and raises nothing, over and into Ints, Floats or Bools, runs on every core, with no flag: the callback may call functions, recurse, allocate and capture any value, because each thread runs it on its own heap with its own copy of the closure. Folds still run serially there. See parallel maps.