code wiki / (root) / nx_parallelism_census.nx

nx_parallelism_census.nx source

↩ module page · 111 lines · 6431 B

1// nx_parallelism_census.nx -- the PARALLELIZATION WORKLIST: walk the tree and flag COMPUTE-HEAVY organs (deep 2// nested loops = the hot-path signature: matmul / convolution / codec macroblock loops / hashing rounds) that 3// would benefit from multi-core once NishiLang threading is unblocked. Every organ is single-threaded today 4// (thread-parallelism is toolchain-blocked, 2026-07-06), so this names WHERE the wins are when threads land. 5// Structural signal = max concurrent `while`-nesting depth (comment/string-aware). Depth>=3 = a candidate. 6// HONEST: a candidate-generator, not proof of compute-boundedness (a deep parse/IO loop can also nest); 7// confirm with nx_parallelism_probe on the actual run. Sovereign (getdents64 walk, no shell). expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 10import "nx_dir.nx" 11 12const PC_CAP: i64 = 16384 13const PC_BUF: i64 = 1048576 14 15func pc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 16// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 17// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 18// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 19// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 20func pc_putn(v: i64) -> i64 { nxi_out(v); return 0 } 21func pc_alnum(c: u8) -> i64 { 22 if c >= (48 as u8) { if c <= (57 as u8) { return 1 } } 23 if c >= (65 as u8) { if c <= (90 as u8) { return 1 } } 24 if c >= (97 as u8) { if c <= (122 as u8) { return 1 } } 25 if c == (95 as u8) { return 1 } 26 return 0 27} 28func pc_match(buf: *u8, i: i64, total: i64, lit: *u8) -> i64 { 29 var j: i64 = 0 30 while lit[j] != (0 as u8) { if i + j >= total { return 0 } if buf[i + j] != lit[j] { return 0 } j = j + 1 } 31 return 1 32} 33func pc_read(path: *u8, buf: *u8) -> i64 { 34 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } 35 var t: i64 = 0; var go: i64 = 1 36 while go == 1 { let r: i64 = sys_read(fd, ((buf as i64) + t) as *u8, PC_BUF - t); if r <= 0 { go = 0 } else { t = t + r; if t >= PC_BUF { go = 0 } } } 37 sys_close(fd); return t 38} 39 40// max concurrent `while`-block nesting depth (comment/string-aware; brace stack tracks which open blocks are whiles). 41func pc_max_loop_depth(buf: *u8, total: i64) -> i64 { 42 var i: i64 = 0; var in_comment: i64 = 0; var in_str: i64 = 0 43 var depth: i64 = 0; var wmask: i64 = 0; var wopen: i64 = 0; var pending: i64 = 0; var maxw: i64 = 0 44 while i < total { 45 let c: i64 = buf[i] & 0xff 46 if in_comment == 1 { if c == 10 { in_comment = 0 } i = i + 1; continue } 47 if in_str == 1 { if c == 92 { i = i + 2; continue } if c == 34 { in_str = 0 } i = i + 1; continue } 48 if c == 34 { in_str = 1; i = i + 1; continue } 49 if c == 47 { if i + 1 < total { if (buf[i+1] & 0xff) == 47 { in_comment = 1; i = i + 2; continue } } } 50 if c == 123 { 51 depth = depth + 1 52 if depth <= 60 { var b: i64 = 0; if pending == 1 { b = 1; wopen = wopen + 1; if wopen > maxw { maxw = wopen } } wmask = wmask * 2 + b } 53 pending = 0; i = i + 1; continue 54 } 55 if c == 125 { 56 if depth > 0 { if depth <= 60 { if (wmask % 2) == 1 { wopen = wopen - 1 } wmask = wmask / 2 } depth = depth - 1 } 57 i = i + 1; continue 58 } 59 if c == 119 { 60 if pc_match(buf, i, total, "while" as *u8) == 1 { 61 var okb: i64 = 1 62 if i > 0 { if pc_alnum(buf[i-1]) == 1 { okb = 0 } } 63 if i + 5 < total { if pc_alnum(buf[i+5]) == 1 { okb = 0 } } 64 if okb == 1 { pending = 1; i = i + 5; continue } 65 } 66 } 67 i = i + 1 68 } 69 return maxw 70} 71 72func main() -> i64 { 73 pc_puts("=== nx_parallelism_census -- compute-heavy hot paths (deep loop nesting) = the parallelization worklist ===\n" as *u8) 74 let dir: *u8 = "runtime\x00" as *u8 75 let rows: *NxDirRow = (sys_mmap(NX_DIR_ROW_BYTES * PC_CAP)) as *NxDirRow 76 let arena: *u8 = sys_mmap(PC_CAP * 128) 77 let res: *NxDirResult = (sys_mmap(64)) as *NxDirResult 78 nx_dir_list(dir, rows, PC_CAP, arena, PC_CAP * 128, 0, res) 79 if res.n_filled <= 0 { pc_puts(" walk failed\n" as *u8); sys_exit(1); return 1 } 80 let fbuf: *u8 = sys_mmap(PC_BUF) 81 let path: *u8 = sys_mmap(512) 82 var scanned: i64 = 0; var d3: i64 = 0; var d4: i64 = 0; var topd: i64 = 0 83 let toppath: *u8 = sys_mmap(512); toppath[0] = 0 as u8 84 var i: i64 = 0 85 while i < res.n_filled { 86 let row: *NxDirRow = ((rows as i64) + i * NX_DIR_ROW_BYTES) as *NxDirRow 87 i = i + 1 88 if row.dtype != NX_DT_REG { continue } 89 let nm: *u8 = row.name_ptr; let nl: i64 = row.name_len 90 if nl < 4 { continue } 91 if nm[nl-3] != (46 as u8) { continue } 92 if nm[nl-2] != (110 as u8) { continue } 93 if nm[nl-1] != (120 as u8) { continue } 94 var o: i64 = 0; var k: i64 = 0 95 while dir[k] != (0 as u8) { path[o] = dir[k]; o = o + 1; k = k + 1 } 96 path[o] = 47 as u8; o = o + 1; k = 0 97 while nm[k] != (0 as u8) { path[o] = nm[k]; o = o + 1; k = k + 1 } path[o] = 0 as u8 98 scanned = scanned + 1 99 let n: i64 = pc_read(path, fbuf); if n == 0 { continue } 100 let dep: i64 = pc_max_loop_depth(fbuf, n) 101 if dep >= 4 { d4 = d4 + 1 } 102 if dep >= 3 { d3 = d3 + 1; if dep > topd { topd = dep; var t: i64 = 0; while path[t] != (0 as u8) { toppath[t] = path[t]; t = t + 1 } toppath[t] = 0 as u8 } } 103 } 104 pc_puts(" scanned " as *u8); pc_putn(scanned); pc_puts(" .nx files\n" as *u8) 105 pc_puts(" COMPUTE-HEAVY candidates (loop-nesting depth >= 3): " as *u8); pc_putn(d3); pc_puts(" (>= 4 deep: " as *u8); pc_putn(d4); pc_puts(")\n" as *u8) 106 pc_puts(" deepest: " as *u8); pc_puts(toppath); pc_puts(" (depth " as *u8); pc_putn(topd); pc_puts(")\n" as *u8) 107 pc_puts(" => these are the PARALLELIZATION TARGETS once NishiLang threading is unblocked (func-types + nxasm trampoline + __thread_clone). ALL single-threaded today.\n" as *u8) 108 pc_puts(" CONFIRM a candidate is really compute-bound (not deep-parse/IO) with: nx_parallelism_probe <its-elf>.\n" as *u8) 109 pc_puts("NX-PARALLELISM-CENSUS GREEN: worklist emitted (candidate-generator; semantic/probe tier confirms)\n" as *u8) 110 sys_exit(0); return 0 111}