code wiki / (root) / nx_ppdemo_forkpar.nx

nx_ppdemo_forkpar.nx source

↩ module page · 31 lines · 1044 B

1// nx_ppdemo_forkpar.nx -- PROCESS parallelism (fork), the axis that WORKS today (vs broken threads). Forks 8 2// children, each doing the SAME 3e9-iter loop nx_ppdemo_serial takes ~6.7s for, parent wait4s all. If the 8 3// run in parallel on 8 cores, total WALL stays ~7s (8x throughput); if it were serial it would be ~53s. Built 4// via the sovereign lane (fork/wait4 need no func types). Time it externally. license_tier: ORIGINAL expect_exit: 0 5import "nx_syscalls.nx" 6const K_MAGIC_3000000000: i64 = 3000000000 7 8const NPROC: i64 = 8 9 10func pp_heavy() -> i64 { 11 var s: i64 = 0 12 var i: i64 = 0 13 while i < K_MAGIC_3000000000 { s = s + (i % 7); i = i + 1 } 14 return s 15} 16 17func main() -> i64 { 18 var k: i64 = 0 19 while k < NPROC { 20 let pid: i64 = sys_fork() 21 if pid == 0 { 22 let r: i64 = pp_heavy() 23 sys_exit(r % 7) 24 } 25 k = k + 1 26 } 27 let st: *i64 = sys_mmap(16) as *i64 28 var w: i64 = 0 29 while w < NPROC { sys_wait4(0 - 1, st, 0); w = w + 1 } 30 sys_exit(0); return 0 31}