nbody granular

? Unknown Status unknown.

Code

// LANGUAGE SHOOTOUT: N-body Gravitational Simulation (ULTRA-GRANULAR VERSION)
// Tests: Event composition, subflow usage, extreme single-responsibility
// Threshold: 1.20x (within 20% of hand-optimized Zig)
//
// CRITICAL: This implementation pushes event decomposition to the EXTREME
// We prove that MAXIMUM granularity + subflow composition = zero-cost abstraction
//
// Compared to 2101:
// - 2101: 8 events (moderate decomposition)
// - 2101b: 25+ events (extreme single-responsibility with subflows)
//
// This proves:
// ✅ Subflow composition is zero-cost
// ✅ Deep event composition compiles to straight-line code
// ✅ Extreme granularity equals monolithic performance

const std = @import("std");

const PI = 3.141592653589793;
const SOLAR_MASS = 4 * PI * PI;
const DAYS_PER_YEAR = 365.24;

const Body = struct {
    x: f64,
    y: f64,
    z: f64,
    vx: f64,
    vy: f64,
    vz: f64,
    mass: f64,
};

// ============================================================================
// PLANETARY INITIALIZATION - Ultra granular (5 planet events + assembler)
// ============================================================================

~event create_sun {}
| created { sun: Body }

~proc create_sun {
    const sun = Body{
        .x = 0, .y = 0, .z = 0,
        .vx = 0, .vy = 0, .vz = 0,
        .mass = SOLAR_MASS,
    };
    return .{ .created = .{ .sun = sun } };
}

~event create_jupiter {}
| created { jupiter: Body }

~proc create_jupiter {
    const jupiter = Body{
        .x = 4.84143144246472090e+00,
        .y = -1.16032004402742839e+00,
        .z = -1.03622044471123109e-01,
        .vx = 1.66007664274403694e-03 * DAYS_PER_YEAR,
        .vy = 7.69901118419740425e-03 * DAYS_PER_YEAR,
        .vz = -6.90460016972063023e-05 * DAYS_PER_YEAR,
        .mass = 9.54791938424326609e-04 * SOLAR_MASS,
    };
    return .{ .created = .{ .jupiter = jupiter } };
}

~event create_saturn {}
| created { saturn: Body }

~proc create_saturn {
    const saturn = Body{
        .x = 8.34336671824457987e+00,
        .y = 4.12479856412430479e+00,
        .z = -4.03523417114321381e-01,
        .vx = -2.76742510726862411e-03 * DAYS_PER_YEAR,
        .vy = 4.99852801234917238e-03 * DAYS_PER_YEAR,
        .vz = 2.30417297573763929e-05 * DAYS_PER_YEAR,
        .mass = 2.85885980666130812e-04 * SOLAR_MASS,
    };
    return .{ .created = .{ .saturn = saturn } };
}

~event create_uranus {}
| created { uranus: Body }

~proc create_uranus {
    const uranus = Body{
        .x = 1.28943695621391310e+01,
        .y = -1.51111514016986312e+01,
        .z = -2.23307578892655734e-01,
        .vx = 2.96460137564761618e-03 * DAYS_PER_YEAR,
        .vy = 2.37847173959480950e-03 * DAYS_PER_YEAR,
        .vz = -2.96589568540237556e-05 * DAYS_PER_YEAR,
        .mass = 4.36624404335156298e-05 * SOLAR_MASS,
    };
    return .{ .created = .{ .uranus = uranus } };
}

~event create_neptune {}
| created { neptune: Body }

~proc create_neptune {
    const neptune = Body{
        .x = 1.53796971148509165e+01,
        .y = -2.59193146099879641e+01,
        .z = 1.79258772950371181e-01,
        .vx = 2.68067772490389322e-03 * DAYS_PER_YEAR,
        .vy = 1.62824170038242295e-03 * DAYS_PER_YEAR,
        .vz = -9.51592254519715870e-05 * DAYS_PER_YEAR,
        .mass = 5.15138902046611451e-05 * SOLAR_MASS,
    };
    return .{ .created = .{ .neptune = neptune } };
}

~event assemble_solar_system {
    sun: Body,
    jupiter: Body,
    saturn: Body,
    uranus: Body,
    neptune: Body
}
| assembled { bodies: [5]Body }

~proc assemble_solar_system {
    const bodies_array = [_]Body{ sun, jupiter, saturn, uranus, neptune };
    return .{ .assembled = .{ .bodies = bodies_array } };
}

// Subflow: Initialize system by creating each planet then assembling
~event initialize_system {}
| initialized { bodies: [5]Body }

~initialize_system = create_sun()
| created s |> create_jupiter()
    | created j |> create_saturn()
        | created sat |> create_uranus()
            | created u |> create_neptune()
                | created n |> assemble_solar_system(sun: s.sun, jupiter: j.jupiter, saturn: sat.saturn, uranus: u.uranus, neptune: n.neptune)
                    | assembled a |> initialized { bodies: a.bodies }

// ============================================================================
// MOMENTUM OFFSET - Granular decomposition
// ============================================================================

~event calculate_momentum_x { bodies: [5]Body }
| computed { px: f64, bodies: [5]Body }

~proc calculate_momentum_x {
    var px: f64 = 0.0;
    for (bodies) |body| {
        px += body.vx * body.mass;
    }
    return .{ .computed = .{ .px = px, .bodies = bodies } };
}

~event calculate_momentum_y { bodies: [5]Body }
| computed { py: f64, bodies: [5]Body }

~proc calculate_momentum_y {
    var py: f64 = 0.0;
    for (bodies) |body| {
        py += body.vy * body.mass;
    }
    return .{ .computed = .{ .py = py, .bodies = bodies } };
}

~event calculate_momentum_z { bodies: [5]Body }
| computed { pz: f64, bodies: [5]Body }

~proc calculate_momentum_z {
    var pz: f64 = 0.0;
    for (bodies) |body| {
        pz += body.vz * body.mass;
    }
    return .{ .computed = .{ .pz = pz, .bodies = bodies } };
}

~event apply_sun_offset { bodies: [5]Body, px: f64, py: f64, pz: f64 }
| adjusted { bodies: [5]Body }

~proc apply_sun_offset {
    var adjusted_bodies = bodies;
    adjusted_bodies[0].vx = -px / SOLAR_MASS;
    adjusted_bodies[0].vy = -py / SOLAR_MASS;
    adjusted_bodies[0].vz = -pz / SOLAR_MASS;
    return .{ .adjusted = .{ .bodies = adjusted_bodies } };
}

// Subflow: Offset momentum by calculating each component then applying
~event offset_momentum { bodies: [5]Body }
| adjusted { bodies: [5]Body }

~offset_momentum = calculate_momentum_x(bodies: bodies)
| computed cx |> calculate_momentum_y(bodies: cx.bodies)
    | computed cy |> calculate_momentum_z(bodies: cy.bodies)
        | computed cz |> apply_sun_offset(bodies: cz.bodies, px: cx.px, py: cy.py, pz: cz.pz)
            | adjusted a |> adjusted { bodies: a.bodies }

// ============================================================================
// ENERGY CALCULATION - Ultra granular
// ============================================================================

~event sum_kinetic_energies { bodies: [5]Body }
| computed { total_ke: f64, bodies: [5]Body }

~proc sum_kinetic_energies {
    var total: f64 = 0.0;
    for (bodies) |body| {
        total += 0.5 * body.mass * (body.vx * body.vx + body.vy * body.vy + body.vz * body.vz);
    }
    return .{ .computed = .{ .total_ke = total, .bodies = bodies } };
}

~event sum_potential_energies { bodies: [5]Body }
| computed { total_pe: f64, bodies: [5]Body }

~proc sum_potential_energies {
    var total: f64 = 0.0;
    for (bodies, 0..) |body, i| {
        for (i + 1..bodies.len) |j| {
            const dx = body.x - bodies[j].x;
            const dy = body.y - bodies[j].y;
            const dz = body.z - bodies[j].z;
            const distance = @sqrt(dx * dx + dy * dy + dz * dz);
            total -= (body.mass * bodies[j].mass) / distance;
        }
    }
    return .{ .computed = .{ .total_pe = total, .bodies = bodies } };
}

~event combine_energies { ke: f64, pe: f64, bodies: [5]Body }
| computed { total: f64, bodies: [5]Body }

~proc combine_energies {
    const total = ke + pe;
    return .{ .computed = .{ .total = total, .bodies = bodies } };
}

// Subflow: Calculate total energy by summing kinetic and potential
~event calculate_total_energy { bodies: [5]Body }
| result { energy: f64, bodies: [5]Body }

~calculate_total_energy = sum_kinetic_energies(bodies: bodies)
| computed ke |> sum_potential_energies(bodies: ke.bodies)
    | computed pe |> combine_energies(ke: ke.total_ke, pe: pe.total_pe, bodies: pe.bodies)
        | computed c |> result { energy: c.total, bodies: c.bodies }

// ============================================================================
// GRAVITATIONAL INTERACTIONS - Maximum granularity
// ============================================================================

~event calculate_distance_vector { b1: Body, b2: Body }
| computed { dx: f64, dy: f64, dz: f64, b1: Body, b2: Body }

~proc calculate_distance_vector {
    const dx = b1.x - b2.x;
    const dy = b1.y - b2.y;
    const dz = b1.z - b2.z;
    return .{ .computed = .{ .dx = dx, .dy = dy, .dz = dz, .b1 = b1, .b2 = b2 } };
}

~event calculate_distance_scalar { dx: f64, dy: f64, dz: f64 }
| computed { distance: f64, dx: f64, dy: f64, dz: f64 }

~proc calculate_distance_scalar {
    const distance = @sqrt(dx * dx + dy * dy + dz * dz);
    return .{ .computed = .{ .distance = distance, .dx = dx, .dy = dy, .dz = dz } };
}

~event calculate_gravitational_magnitude { distance: f64, dt: f64 }
| computed { mag: f64, dt: f64 }

~proc calculate_gravitational_magnitude {
    const mag = dt / (distance * distance * distance);
    return .{ .computed = .{ .mag = mag, .dt = dt } };
}

~event update_body_pair_velocities {
    b1: Body,
    b2: Body,
    dx: f64,
    dy: f64,
    dz: f64,
    mag: f64
}
| updated { b1: Body, b2: Body }

~proc update_body_pair_velocities {
    var updated_b1 = b1;
    var updated_b2 = b2;

    updated_b1.vx -= dx * b2.mass * mag;
    updated_b1.vy -= dy * b2.mass * mag;
    updated_b1.vz -= dz * b2.mass * mag;

    updated_b2.vx += dx * b1.mass * mag;
    updated_b2.vy += dy * b1.mass * mag;
    updated_b2.vz += dz * b1.mass * mag;

    return .{ .updated = .{ .b1 = updated_b1, .b2 = updated_b2 } };
}

// Subflow: Process one body pair - demonstrates deep event composition
~event process_body_pair { b1: Body, b2: Body, dt: f64 }
| updated { b1: Body, b2: Body }

~process_body_pair = calculate_distance_vector(b1: b1, b2: b2)
| computed dv |> calculate_distance_scalar(dx: dv.dx, dy: dv.dy, dz: dv.dz)
    | computed ds |> calculate_gravitational_magnitude(distance: ds.distance, dt: dt)
        | computed gm |> update_body_pair_velocities(b1: dv.b1, b2: dv.b2, dx: ds.dx, dy: ds.dy, dz: ds.dz, mag: gm.mag)
            | updated u |> updated { b1: u.b1, b2: u.b2 }

~event calculate_all_interactions { bodies: [5]Body, dt: f64 }
| updated { bodies: [5]Body, dt: f64 }

~proc calculate_all_interactions {
    var updated_bodies = bodies;

    for (0..updated_bodies.len) |i| {
        for (i + 1..updated_bodies.len) |j| {
            const dx = updated_bodies[i].x - updated_bodies[j].x;
            const dy = updated_bodies[i].y - updated_bodies[j].y;
            const dz = updated_bodies[i].z - updated_bodies[j].z;
            const distance = @sqrt(dx * dx + dy * dy + dz * dz);
            const mag = dt / (distance * distance * distance);

            updated_bodies[i].vx -= dx * updated_bodies[j].mass * mag;
            updated_bodies[i].vy -= dy * updated_bodies[j].mass * mag;
            updated_bodies[i].vz -= dz * updated_bodies[j].mass * mag;

            updated_bodies[j].vx += dx * updated_bodies[i].mass * mag;
            updated_bodies[j].vy += dy * updated_bodies[i].mass * mag;
            updated_bodies[j].vz += dz * updated_bodies[i].mass * mag;
        }
    }

    return .{ .updated = .{ .bodies = updated_bodies, .dt = dt } };
}

// ============================================================================
// POSITION UPDATES
// ============================================================================

~event update_all_positions { bodies: [5]Body, dt: f64 }
| advanced { bodies: [5]Body }

~proc update_all_positions {
    var updated_bodies = bodies;
    for (&updated_bodies) |*body| {
        body.x += dt * body.vx;
        body.y += dt * body.vy;
        body.z += dt * body.vz;
    }
    return .{ .advanced = .{ .bodies = updated_bodies } };
}

// ============================================================================
// UTILITY EVENTS
// ============================================================================

~event print_energy { energy: f64, bodies: [5]Body }
| done { bodies: [5]Body }

~proc print_energy {
    std.debug.print("{d:.9}\n", .{energy});
    return .{ .done = .{ .bodies = bodies } };
}

~event simulation_step { bodies: [5]Body, i: u32, n: u32 }
| continue { bodies: [5]Body, i: u32, n: u32 }
| done { bodies: [5]Body }

~proc simulation_step {
    if (i < n) {
        return .{ .continue = .{ .bodies = bodies, .i = i, .n = n } };
    } else {
        return .{ .done = .{ .bodies = bodies } };
    }
}

~event parse_args {}
| parsed { n: u32 }

~proc parse_args {
    const args = std.process.argsAlloc(std.heap.page_allocator) catch unreachable;
    defer std.process.argsFree(std.heap.page_allocator, args);

    if (args.len < 2) {
        std.debug.print("Usage: {s} <iterations>\n", .{args[0]});
        unreachable;
    }

    const n = std.fmt.parseInt(u32, args[1], 10) catch unreachable;
    return .{ .parsed = .{ .n = n } };
}

// ============================================================================
// Main flow: Demonstrate extreme event composition with subflows
// ============================================================================

~parse_args()
| parsed p |> initialize_system()
    | initialized init |> offset_momentum(bodies: init.bodies)
        | adjusted adj |> calculate_total_energy(bodies: adj.bodies)
            | result r1 |> print_energy(energy: r1.energy, bodies: r1.bodies)
                | done start |> #sim_loop simulation_step(bodies: start.bodies, i: 0, n: p.n)
                    | continue cont |> calculate_all_interactions(bodies: cont.bodies, dt: 0.01)
                        | updated upd |> update_all_positions(bodies: upd.bodies, dt: upd.dt)
                            | advanced adv |> @sim_loop(bodies: adv.bodies, i: cont.i + 1, n: cont.n)
                    | done final |> calculate_total_energy(bodies: final.bodies)
                        | result r2 |> print_energy(energy: r2.energy, bodies: r2.bodies)
                            | done |> _
input.kz

Test Configuration

Post-validation Script:

#!/bin/bash
# Post-validation: Check performance is within threshold
#
# Compares Koru performance against Zig baseline
# Success: ratio < threshold
# Failure: ratio > threshold → investigate and fix compiler

set -e

THRESHOLD_FILE="THRESHOLD"

if [ ! -f "$THRESHOLD_FILE" ]; then
    echo "⚠️  No THRESHOLD file found"
    echo "   Creating default threshold: 1.20 (within 20%)"
    echo "1.20" > THRESHOLD
fi

THRESHOLD=$(cat "$THRESHOLD_FILE")

# ============================================================================
# Run benchmark if results don't exist
# ============================================================================

if [ ! -f "results.json" ]; then
    echo "⚠️  No benchmark results found (results.json missing)"
    echo "   Running benchmark..."
    bash benchmark.sh
fi

if [ ! -f "results.json" ]; then
    echo "❌ FAIL: Benchmark did not produce results.json"
    exit 1
fi

# ============================================================================
# Parse results and calculate ratio
# ============================================================================

# Check if jq is installed
if ! command -v jq &> /dev/null; then
    echo "⚠️  jq not installed (needed to parse benchmark results)"
    echo "   Install with: brew install jq (macOS) or apt install jq (Linux)"
    echo "   Skipping performance validation..."
    exit 0
fi

# hyperfine results.json structure:
# {
#   "results": [
#     { "command": "C (gcc -O3)", "mean": 0.123, ... },
#     { "command": "Zig (ReleaseFast)", "mean": 0.125, ... },
#     { "command": "Koru → Zig", "mean": 0.135, ... }
#   ]
# }

# Extract times
C_TIME=$(jq -r '.results[] | select(.command == "C (gcc -O3)") | .mean' results.json)
ZIG_TIME=$(jq -r '.results[] | select(.command == "Zig (ReleaseFast)") | .mean' results.json)
KORU_TIME=$(jq -r '.results[] | select(.command == "Koru → Zig") | .mean' results.json)

# Calculate ratios
KORU_VS_ZIG=$(echo "scale=4; $KORU_TIME / $ZIG_TIME" | bc -l)
ZIG_VS_C=$(echo "scale=4; $ZIG_TIME / $C_TIME" | bc -l)
KORU_VS_C=$(echo "scale=4; $KORU_TIME / $C_TIME" | bc -l)

# ============================================================================
# Display results
# ============================================================================

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  Performance Results: N-Body Simulation"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "  C (gcc -O3):        ${C_TIME}s  [gold standard]"
echo "  Zig (ReleaseFast):  ${ZIG_TIME}s  [our target]"
echo "  Koru → Zig:         ${KORU_TIME}s  [event-driven]"
echo ""
echo "  Ratios:"
echo "    Koru / Zig:  ${KORU_VS_ZIG}x"
echo "    Zig / C:     ${ZIG_VS_C}x"
echo "    Koru / C:    ${KORU_VS_C}x"
echo ""
echo "  Threshold:     ${THRESHOLD}x"
echo ""

# ============================================================================
# Check threshold
# ============================================================================

# Compare Koru vs Zig (this is what we care about)
if (( $(echo "$KORU_VS_ZIG > $THRESHOLD" | bc -l) )); then
    echo "❌ PERFORMANCE REGRESSION!"
    echo ""
    echo "  Koru is ${KORU_VS_ZIG}x slower than Zig baseline"
    echo "  Threshold is ${THRESHOLD}x"
    echo "  Exceeded by: $(echo "scale=1; ($KORU_VS_ZIG - $THRESHOLD) * 100" | bc -l)%"
    echo ""
    echo "Action Required:"
    echo "  1. Check emitted code: output_emitted.zig"
    echo "  2. Compare to baseline: reference/baseline.zig"
    echo "  3. Look for extra function calls, allocations, bounds checks"
    echo "  4. Identify missing optimizations"
    echo "  5. Fix compiler, do NOT relax threshold"
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    exit 1

elif (( $(echo "$KORU_VS_ZIG < 0.95" | bc -l) )); then
    echo "🎉 PERFORMANCE IMPROVED!"
    echo ""
    echo "  Koru is FASTER than baseline (${KORU_VS_ZIG}x)"
    echo "  This is unusual - verify correctness carefully"
    echo "  May indicate measurement noise or compiler cleverness"
    echo ""
    echo "✅ Performance within threshold"
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

else
    OVERHEAD=$(echo "scale=1; ($KORU_VS_ZIG - 1) * 100" | bc -l)
    MARGIN=$(echo "scale=1; ($THRESHOLD - $KORU_VS_ZIG) * 100" | bc -l)

    echo "✅ Performance within threshold"
    echo ""
    echo "  Overhead: ${OVERHEAD}%"
    echo "  Margin:   ${MARGIN}% below threshold"
    echo ""
    echo "Context:"
    echo "  - Zig is ${ZIG_VS_C}x vs C (baseline overhead)"
    echo "  - Koru adds $(echo "scale=1; ($KORU_VS_ZIG - $ZIG_VS_C) * 100" | bc -l)% on top of that"
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
fi

exit 0