import aner.parallel

// Prepare private.repeat.cluster using repeat_cluster.cluster.example.
// Compatible workers share one immutable input payload per host.
// Workers on each host share native output storage for their disjoint rows.
// Coordinator-host workers need no numerical output transport; other hosts
// stream completed mapped ranges through exact typed output frames.
// Pass history directly to a later repetition: compatible coordinator-host
// workers reuse its sealed numerical storage without restaging or payload copies.
// Later calls with the same worker definitions keep the admitted processes and
// reuse their bounded immutable input cache, including on other hosts.
fn simulate(parameters: List, iteration: Int64, seed: Int64,
            output: OutputRegion<Float32>) -> Unit {
    let input = cast<FrozenArray<Float32>>(parameters.get(0))
    var state = input[iteration % input.len()]
    var step = 0
    while step < 8 {
        state = state + Float32(0.25)
        output[step, 0] = state
        output[step, 1] = Float32(iteration)
        step += 1
    }
}

let input = Array<Float32>(64, Float32(1.0)).snapshot()
let parameters = List(input)
let history = parallel.repeat(iterations: 100, task: simulate,
    parameters: parameters, output_shape: List(8, 2),
    execution: parallel.cluster(config: "private.repeat.cluster"), seed: 2026)

// A local reference verifies every retained cell and derived seed.
let reference = parallel.repeat(iterations: 100, task: simulate,
    parameters: parameters, output_shape: List(8, 2),
    execution: parallel.local(workers: 1), seed: 2026)
var identical = true
for iteration in Range(100) {
    for column in Range(16) {
        if history.value(iteration, column) != reference.value(iteration, column) {
            identical = false
        }
    }
    if history.seed(iteration) != reference.seed(iteration) { identical = false }
}
print(identical)               // true
print(type_of(history))       // OutputResults<Float32>
print(history.shape())        // List(100, 8, 2)
print(history[99, 7, 0])       // 3.0
print(history.bytes())        // 6400 packed output bytes
print(history.summary())      // Includes shared mappings, direct output and transport accounting
