1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0
//! Illustration: a compiled kernel is a pure function of its
//! coordinate inputs.
//!
//! Same coordinate in → same outputs out, every time, with no shared
//! state. That property is what lets a multi-thread benchmark generate
//! billions of distinct reproducible variates in parallel — each
//! thread gets its own kernel, the immutable program is shared via
//! `Arc`.
fn main() {
let kernel = polydat::dsl::compile_polydat_kernel(
r#"
input cycle: u64
user_id := mod(hash(cycle), 1000000)
"#,
)
.expect("compile failed");
let program = kernel.into_program();
let threads = 2;
// Each thread evaluates the same coordinate independently and
// collects the result; the determinism contract says all threads
// produce the same value for the same coord.
let results: Vec<u64> = std::thread::scope(|s| {
let handles: Vec<_> = (0..threads)
.map(|_| {
let program = program.clone();
s.spawn(move || {
let mut kernel = program.create_kernel();
kernel.set_inputs(&[42]);
kernel.pull("user_id").as_u64()
})
})
.collect();
handles.into_iter().map(|h| h.join().unwrap()).collect()
});
println!("two threads, coord=42 → results: {results:?}");
let first = results[0];
for (i, v) in results.iter().enumerate() {
assert_eq!(*v, first, "thread {i} disagreed: {v} vs {first}");
}
println!("all threads agreed: user_id = {first}");
}