code wiki / _hdl_build / nx_sdfrender_mt_gate.nx
nx_sdfrender_mt_gate.nx source
↩ module page · 122 lines · 5721 B
1// nx_sdfrender_mt_gate.nx -- R1c gate: the THREADED SDF render must be BYTE-IDENTICAL to the single-thread
2// renderer AND measurably faster (the whole point of the rung). A negative control proves the comparator bites.
3// T1 BODY: sdfmt_render (auto workers) == sdf_render, all 196608 px byte-identical
4// T2 FACE: the same identity law on the carve/material program (different code paths: no floor, eye/hair/lash mats)
5// T3 MEASURED speedup on the body frame (monotonic clock): threaded must be >= 2.0x faster (honest floor on a
6// multi-core box; the ACTUAL ratio is printed -- no-wave, measured exceed)
7// T4 NEG-CONTROL: corrupt one threaded pixel -> the comparator must flag EXACTLY one diff (then restore)
8// T5 mt-vs-mt determinism: two threaded renders byte-identical (scheduling-independence) + PNG knowledge/nx_sdfmt.png
9// license_tier: ORIGINAL expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_png.nx"
12import "nx_hw.nx"
13import "nx_sdfrender_mt.nx"
14
15func hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
16func pn(v: i64) -> i64 {
17 let b: *u8 = sys_mmap(32) as *u8
18 var x: i64 = v
19 var neg: i64 = 0
20 if x < 0 { neg = 1; x = 0 - x }
21 var i: i64 = 31
22 if x == 0 { b[i] = 48 as u8; i = i - 1 }
23 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 }
24 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
25 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i)
26 return 0
27}
28func now_ms() -> i64 {
29 let ts: *i64 = sys_mmap(16) as *i64
30 sys_clock_gettime_mono(ts)
31 return ts[0] * 1000 + ts[1] / 1000000
32}
33func fbdiff(a: *i64, b: *i64, n: i64) -> i64 {
34 var d: i64 = 0
35 var i: i64 = 0
36 while i < n { if a[i] != b[i] { d = d + 1 } i = i + 1 }
37 return d
38}
39
40func main() -> i64 {
41 var fails: i64 = 0
42 let npx: i64 = ww() * hh()
43 let nw: i64 = nx_hw_worker_count()
44 hw("hardware workers: " as *u8); pn(nw); hw("\n" as *u8)
45
46 // --- BODY reference: single-thread (timed) ---
47 let b1: i64 = sys_mmap(sdf_bytes()) as i64
48 sdf_body(b1)
49 let t0: i64 = now_ms()
50 sdf_render(b1, 0, 5, 240, 184, 160)
51 let t1: i64 = now_ms()
52 let fb1: *i64 = (b1 + fb_off()) as *i64
53 let single_ms: i64 = t1 - t0
54
55 // --- BODY threaded (timed, auto workers) ---
56 let b2: i64 = sys_mmap(sdf_bytes()) as i64
57 sdf_body(b2)
58 let t2: i64 = now_ms()
59 let rc: i64 = sdfmt_render(b2, 0, 5, 240, 184, 160, 0)
60 let t3: i64 = now_ms()
61 let fb2: *i64 = (b2 + fb_off()) as *i64
62 let mt_ms: i64 = t3 - t2
63
64 var d1: i64 = fbdiff(fb1, fb2, npx)
65 if rc == 0 { if d1 == 0 {
66 hw("T1 PASS BODY threaded == single: " as *u8); pn(npx); hw(" px byte-identical\n" as *u8)
67 } }
68 if rc != 0 { hw("T1 FAIL threaded render rc=" as *u8); pn(rc); hw("\n" as *u8); fails = fails + 1 }
69 else { if d1 != 0 { hw("T1 FAIL diff px=" as *u8); pn(d1); hw("\n" as *u8); fails = fails + 1 } }
70
71 var spx100: i64 = 0
72 if mt_ms > 0 { spx100 = single_ms * 100 / mt_ms }
73 if spx100 >= 200 {
74 hw("T3 PASS MEASURED speedup: single=" as *u8); pn(single_ms); hw("ms threaded=" as *u8); pn(mt_ms)
75 hw("ms = x" as *u8); pn(spx100 / 100); hw("." as *u8); pn((spx100 % 100) / 10); pn(spx100 % 10)
76 hw(" on " as *u8); pn(nw); hw(" workers\n" as *u8)
77 } else {
78 hw("T3 FAIL speedup x100=" as *u8); pn(spx100); hw(" (single=" as *u8); pn(single_ms)
79 hw("ms threaded=" as *u8); pn(mt_ms); hw("ms)\n" as *u8)
80 fails = fails + 1
81 }
82
83 // T4 NEG-CONTROL: the comparator must bite on a single corrupted pixel
84 let kk: i64 = 100000
85 fb2[kk] = fb2[kk] + 1
86 let d2: i64 = fbdiff(fb1, fb2, npx)
87 fb2[kk] = fb2[kk] - 1
88 if d2 == 1 { hw("T4 PASS neg-control: 1 corrupted px -> comparator flags exactly 1\n" as *u8) }
89 else { hw("T4 FAIL neg-control diff=" as *u8); pn(d2); hw("\n" as *u8); fails = fails + 1 }
90
91 // T5 scheduling-independence: a SECOND threaded render is byte-identical to the first + PNG artifact
92 let b3: i64 = sys_mmap(sdf_bytes()) as i64
93 sdf_body(b3)
94 let rc3: i64 = sdfmt_render(b3, 0, 5, 240, 184, 160, 0)
95 let fb3: *i64 = (b3 + fb_off()) as *i64
96 let d3: i64 = fbdiff(fb2, fb3, npx)
97 write_png(fb3, ww(), hh(), "knowledge/nx_sdfmt.png" as *u8)
98 if rc3 == 0 { if d3 == 0 { hw("T5 PASS mt-vs-mt determinism + PNG knowledge/nx_sdfmt.png\n" as *u8) } }
99 if rc3 != 0 { hw("T5 FAIL rc=" as *u8); pn(rc3); hw("\n" as *u8); fails = fails + 1 }
100 else { if d3 != 0 { hw("T5 FAIL mt-vs-mt diff=" as *u8); pn(d3); hw("\n" as *u8); fails = fails + 1 } }
101
102 // T2 FACE: identity on the carve/material program
103 let f1: i64 = sys_mmap(sdf_bytes()) as i64
104 sdf_face(f1)
105 sdf_render(f1, 200, 4, 236, 180, 156)
106 let f2: i64 = sys_mmap(sdf_bytes()) as i64
107 sdf_face(f2)
108 let rcf: i64 = sdfmt_render(f2, 200, 4, 236, 180, 156, 0)
109 let ff1: *i64 = (f1 + fb_off()) as *i64
110 let ff2: *i64 = (f2 + fb_off()) as *i64
111 let dF: i64 = fbdiff(ff1, ff2, npx)
112 if rcf == 0 { if dF == 0 { hw("T2 PASS FACE threaded == single: byte-identical (carves + eye/hair/lash mats)\n" as *u8) } }
113 if rcf != 0 { hw("T2 FAIL rc=" as *u8); pn(rcf); hw("\n" as *u8); fails = fails + 1 }
114 else { if dF != 0 { hw("T2 FAIL face diff px=" as *u8); pn(dF); hw("\n" as *u8); fails = fails + 1 } }
115
116 if fails == 0 {
117 hw("VERDICT GREEN: nx_sdfrender_mt 5/5 -- R1c threaded tiles: byte-identical to single-thread on body AND face, x" as *u8)
118 pn(spx100 / 100); hw("." as *u8); pn((spx100 % 100) / 10); pn(spx100 % 10)
119 hw(" measured speedup, scheduling-independent, comparator proven\n" as *u8)
120 } else { hw("VERDICT RED fails=" as *u8); pn(fails); hw("\n" as *u8) }
121 return fails
122}