For Pi: total=2000000, shard=20000, reducer=sum, fields=hits,samples.
How To Write Parallelizable Scripts
Mode auto-resolve: leave shard fields empty for single-worker; fill both shard fields for sharded cluster execution.
Good pattern (slice-aware):
const units = Number(globalThis.__BRAIN__?.units || 1_000_000);
let hits = 0;
for (let i = 0; i < units; i += 1) {
const x = Math.random();
const y = Math.random();
if (x * x + y * y <= 1) hits += 1;
}
return { hits, samples: units };
Bad pattern (hardcoded non-scalable work):
let hits = 0;
for (let i = 0; i < 100000000; i += 1) {
// hardcoded total means one huge shard and poor scaling
}
return hits;
Reducer tips: sum for counts/math, collect for per-shard outputs, min/max for optimization jobs.