code wiki / (root) / nx_ppdemo_parallel.nx

nx_ppdemo_parallel.nx source

↩ module page · 29 lines · 1142 B

1// nx_ppdemo_parallel.nx -- a controlled MULTI-THREADED compute workload: the SAME arithmetic loop as 2// nx_ppdemo_serial, but split across a thread pool (8 real OS threads). nx_parallelism_probe should measure 3// cpu/wall ~= 8 (eight cores busy at once) and verdict PARALLEL -- the opposite of the single-threaded serial 4// demo, proving the probe distinguishes real parallelism from the codec trap. license_tier: ORIGINAL expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_thread_pool.nx" 7const K_MAGIC_500000000: i64 = 500000000 8 9const NW: i64 = 8 // worker threads (of the 16 cores; 8 keeps room for the pool machinery) 10 11// one heavy compute task -- pure arithmetic, no I/O (matches nx_ppdemo_serial's inner loop). 12func pp_heavy(ctx: i64) -> i64 { 13 var s: i64 = 0 14 var i: i64 = 0 15 while i < K_MAGIC_500000000 { 16 s = s + ((i * 7) % 13) 17 i = i + 1 18 } 19 return s + ctx 20} 21 22func main() -> i64 { 23 let pool: *NxThreadPool = nx_pool_new(NW, 64) 24 var i: i64 = 0 25 while i < NW { nx_pool_submit(pool, pp_heavy, i); i = i + 1 } 26 nx_pool_wait(pool, NW) 27 nx_pool_shutdown(pool) 28 sys_exit(0); return 0 29}