; Phase 3 exit program: parallel map. One buffer is split into four disjoint ; quarters; four tasks square their quarter in place through exclusive loans; ; the parent sums the result after joining. Result: 1² + … + 8² = 204. ; expect: run ; schedules: 30 ; quantum: 3 ; result: 204 ; phase: 3 ; spec: docs/spec/04-concurrency-and-atomics.md module test.program_parallel_map air 0 lang 0.1 features [arrays, effects, ownership, tasks] fn @main() -> u64 effects [task.spawn] entry: %buf = local.alloc array local.zero %buf %m = borrow.mut %buf %s = slice.make %m %i0 = const.usize 0 %v0 = const.u64 1 br @fill(%i0, %v0) fill(%i: usize, %v: u64): %n = slice.len %s %full = eq.usize %i, %n cbr %full, @split(), @store(%i, %v) store(%j: usize, %w: u64): %e = borrow.index %s, %j store %e, %w borrow.end %e %one = const.usize 1 %j2 = add.checked.usize %j, %one %uno = const.u64 1 %w2 = add.checked.u64 %w, %uno br @fill(%j2, %w2) split: %four = const.usize 4 %l, %r = slice.split %s, %four %two = const.usize 2 %a, %b = slice.split %l, %two %c, %d = slice.split %r, %two %sc = task.scope.begin %ta = task.spawn %sc, @square(%a) %tb = task.spawn %sc, @square(%b) %tc = task.spawn %sc, @square(%c) %td = task.spawn %sc, @square(%d) %xa = task.join %ta %xb = task.join %tb %xc = task.join %tc %xd = task.join %td task.scope.end %sc borrow.end %a borrow.end %b borrow.end %c borrow.end %d borrow.end %l borrow.end %r %z = const.u64 0 br @sum(%i0, %z) sum(%k: usize, %acc: u64): %len = slice.len %s %done = eq.usize %k, %len cbr %done, @finish(), @add(%k, %acc) add(%k2: usize, %acc2: u64): %e2 = borrow.index %s, %k2 %x = load %e2 borrow.end %e2 %acc3 = add.checked.u64 %acc2, %x %step = const.usize 1 %k3 = add.checked.usize %k2, %step br @sum(%k3, %acc3) finish: borrow.end %s borrow.end %m ret %acc fn @square(%q: slice_mut) -> u8 effects [] entry: %z = const.usize 0 br @loop(%z) loop(%i: usize): %n = slice.len %q %done = eq.usize %i, %n cbr %done, @fin(), @body(%i) body(%j: usize): %e = borrow.index %q, %j %v = load %e %sq = mul.checked.u64 %v, %v store %e, %sq borrow.end %e %one = const.usize 1 %k = add.checked.usize %j, %one br @loop(%k) fin: %r = const.u8 0 ret %r