interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / apu_fabric_linux.om

1124 lines · 25618 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note native/apu_fabric_linux.om: True 20-Core Grace ARMv9 + 48-SM Blackwell Simultaneous Co-Execution Fabric.
3note
4note Architecture:
5note - Allocates a lock-free MAP_SHARED|MAP_ANONYMOUS (mapnew -1 0 bytes 3 33) coherent unified ring BEFORE fork()
6note so all 20 Grace child workers share zero-copy LPDDR5X pages with the parent GPU producer.
7note - Forks 20 persistent Grace workers pinned 1-to-1 across physical cores via cpuset with bonClosePrevPipes
8note pipe EOF hygiene (mirroring omega-mv/om/gpu/apu_fullchip.om & bonsai.om):
9note * 2 Prefetcher/Arboris workers (A725 cores 0, 1): disk read, token validation, window pack, 2-gram novelty
10note * 16 Cholesky spcryst workers (all 10 fast X925 cores 5..9, 15..19 + 6 A725 cores): Gram/RHS/Cholesky/cal/W4
11note * 2 Live spweval + 1-bit tamper Verifier workers (A725 cores 13, 14): Merkle + held-out W4 eval + rc=61 tamper
12note - Opens GB10 GPU AFTER fork() (parent only, avoiding NVRM fork/UVM VMA bugs) and streams batched GPU
13note forwards (packing multiple shards per B-window batch when n_win <= B!) directly into the shared ring,
14note immediately waking idle Grace workers via non-blocking pollfds + pipes with ZERO waiting for all 64 shards!
15
16const APU_MAX_W ulong 20
17const APU_PREF_SLOTS ulong 16
18const APU_WORK_SLOTS ulong 32
19const APU_SLOT_HDR ulong 64
20
21note Worker pool pipes, pids, roles (0=pref, 1=cryst+steal_verif, 2=verif), active cmd, and core pin table
22global apu_pids long 20
23global apu_gofd int 20
24global apu_rsfd int 20
25global apu_role ulong 20
26global apu_core ulong 20
27global apu_busy ulong 20
28global apu_wcmd ulong 20
29global apu_wslot ulong 20
30global apu_wshrd ulong 20
31
32note Shared-memory coherent unified ring handle & geometry:
33note apu_shm: 0=pref_off, 1=pref_stride, 2=work_off, 3=work_stride, 4=z_off, 5=r_off,
34note 6=s_off, 7=y_off, 8=zh_off, 9=yh_off, 10=train_cap, 11=held_cap, 12=n_work_slots
35global apu_shmh long 1
36global apu_shm ulong 16
37global apu_slot_use ulong 32
38global apu_pslot_use ulong 16
39global apu_pslot_sh ulong 16
40global apu_pslot_nw ulong 16
41global apu_pslot_nh ulong 16
42global apu_pslot_nov ulong 16
43
44note Ready queues (prefetch->gpu, cryst->verif)
45global apu_pq_sh ulong 64
46global apu_pq_ps ulong 64
47global apu_pq_st ulong 4
48global apu_vq_sh ulong 2048
49global apu_vq_ws ulong 2048
50global apu_vq_st ulong 4
51
52note Per-shard GPU/cryst telemetry (indexed by shard 0..2047)
53global apu_s_toks ulong 2048
54global apu_s_bcal ulong 2048
55global apu_s_ccal ulong 2048
56global apu_s_pb ulong 2048
57global apu_s_gms ulong 2048
58global apu_s_cms ulong 2048
59global apu_s_nov ulong 2048
60
61note Pipe/poll/dirent scratch
62global apu_msg uchar 64
63global apu_pfd uchar 160
64global apu_pidx ulong 20
65global apu_dirb uchar 4096
66global apu_arb_ht ulong 4096
67global apu_rowb uchar 8192
68global apu_xbuf uchar 65536
69global apu_bsh ulong 16
70global apu_bps ulong 16
71global apu_bws ulong 16
72global apu_bnw ulong 16
73global apu_bnh ulong 16
74
75note ---- Pipe & Little-Endian Helpers ----
76
77fn apu_get_i32_m off ulong to int
78b0 = uint get apu_msg off
79b1 = shl uint get apu_msg add off 1 8
80b2 = shl uint get apu_msg add off 2 16
81b3 = shl uint get apu_msg add off 3 24
82ret int or or b0 b1 or b2 b3
83end
84
85fn apu_put_u64_m off ulong v ulong to void
86for i ulong 8
87 put apu_msg add off i uchar and shr v mul i 8 255
88end
89end
90
91fn apu_get_u64_m off ulong to ulong
92v = ulong 0
93for i ulong 8
94 v = or v shl ulong get apu_msg add off i mul i 8
95end
96ret v
97end
98
99fn apu_pipe_wr fd int n ulong to void
100p = ulong 0
101while lt p n bound 1024
102 w = write fd apu_msg p sub n p
103 if le w 0
104 exit 70
105 end
106 p = add p ulong w
107end
108end
109
110fn apu_pipe_rd fd int n ulong to bool
111p = ulong 0
112while lt p n bound 1024
113 r = read fd apu_msg p sub n p
114 if le r 0
115 ret false
116 end
117 p = add p ulong r
118end
119ret true
120end
121
122fn apu_send_job fd int cmd ulong a ulong b ulong c ulong to void
123apu_put_u64_m 0 cmd
124apu_put_u64_m 8 a
125apu_put_u64_m 16 b
126apu_put_u64_m 24 c
127apu_pipe_wr fd 32
128end
129
130note Pin current Grace worker process to physical CPU core `cpu` (0..19) via sched_setaffinity.
131fn apu_pin_core cpu ulong to void
132m = new uchar 8 0
133put m div cpu 8 uchar shl ulong 1 rem cpu 8
134r = cpuset 0 m 0 8
135end
136
137note ---- Directory Scanner (getdnts) & Output Dir Creation (makedir) ----
138
139fn apu_ends_tok n ulong to bool
140if lt n 4
141 ret false
142end
143if ne get apu_pbuf sub n 4 46
144 ret false
145end
146if ne get apu_pbuf sub n 3 116
147 ret false
148end
149if ne get apu_pbuf sub n 2 111
150 ret false
151end
152ret eq get apu_pbuf sub n 1 107
153end
154
155note Probe apu_pbuf[0..tot) when dirent d_type is DT_UNKNOWN (0) or DT_LNK (10):
156note stat follows symlinks without opening the target and writes the normalized 128-byte ARM64 layout
157note on both aarch64 and x86linux (st_mode u32 LE at byte offset 16; S_IFMT=61440, S_IFREG=32768).
158fn apu_is_reg_file tot ulong to bool
159if ne stat apu_pbuf 0 tot apu_io 0 128 0
160 ret false
161end
162mode = apu_get4_io 16
163ret eq and mode 61440 32768
164end
165
166fn apu_build_dir_path q ulong nl ulong dn ulong to ulong
167for i ulong dn bound 256
168 put apu_pbuf i get oarg i
169end
170pos = dn
171if and gt pos 0 ne get apu_pbuf sub pos 1 47
172 put apu_pbuf pos 47
173 pos = add pos 1
174end
175for j ulong nl bound 256
176 put apu_pbuf add pos j get apu_dirb add q j
177end
178ret add pos nl
179end
180
181fn apu_scan_dir_entry p ulong dn ulong to void
182if ge add p 19 4096
183 ret
184end
185dt = ulong get apu_dirb add p 18
186if and ne dt 8 and ne dt 10 ne dt 0
187 ret
188end
189q = add p 19
190if eq get apu_dirb q 46
191 ret
192end
193nl = ulong 0
194while lt add q nl 4096 bound 256
195 if eq get apu_dirb add q nl 0
196 break
197 end
198 nl = add nl 1
199end
200if or eq nl 0 gt add dn add nl 2 sub APU_PATH_STRIDE 1
201 ret
202end
203tot = apu_build_dir_path q nl dn
204if not apu_ends_tok tot
205 ret
206end
207if ne dt 8
208 if not apu_is_reg_file tot
209 ret
210 end
211end
212cnt = get apu_cfg 0
213if lt cnt APU_MAX_SHARDS
214 apu_spath_set cnt 0 tot add cnt 1
215 put apu_cfg 0 add cnt 1
216end
217end
218
219fn apu_walk_dir_buf nr ulong pl ulong to void
220p = ulong 0
221while lt add p 19 nr bound 512
222 lo = ulong get apu_dirb add p 16
223 hi = ulong get apu_dirb add p 17
224 recl = or lo shl hi 8
225 if eq recl 0
226 break
227 end
228 apu_scan_dir_entry p pl
229 p = add p recl
230end
231end
232
233fn apu_discover_shards to void
234pl = oargp 3
235fd = int fopen oarg 0 pl 0 0
236if lt fd 0
237 exit 70
238end
239r0 = getdnts fd apu_dirb 0 4096
240if ge r0 0
241 r = r0
242 while gt r 0 bound 4096
243 apu_walk_dir_buf ulong r pl
244 r = getdnts fd apu_dirb 0 4096
245 end
246 c1 = fclose fd
247 apu_sort_shards
248else
249 c2 = fclose fd
250 fd2 = int fopen oarg 0 pl 0 0
251 if lt fd2 0
252 exit 70
253 end
254 apu_load_list_fd fd2
255 c3 = fclose fd2
256end
257if eq get apu_cfg 0 0
258 exit 20
259end
260mr = makedir apu_odir 0 get apu_odirn 0 493
261end
262
263note ---- Coherent Shared-Memory Ring Setup (MAP_SHARED | MAP_ANONYMOUS before fork) ----
264
265fn apu_shm_init to void
266tt = get og 1
267d = get og 2
268tcap = mul get apu_cfg 10 tt
269hcap = mul get apu_cfg 11 tt
270totw = add get apu_cfg 10 get apu_cfg 11
271pstride = mul div add add APU_SLOT_HDR mul mul totw tt 8 4095 4096 4096
272put apu_shm 0 4096
273put apu_shm 1 pstride
274put apu_shm 2 add 4096 mul APU_PREF_SLOTS pstride
275put apu_shm 4 APU_SLOT_HDR
276put apu_shm 5 add get apu_shm 4 mul mul tcap d 4
277put apu_shm 6 add get apu_shm 5 mul mul tcap d 4
278put apu_shm 7 add get apu_shm 6 mul tcap 4
279put apu_shm 8 add get apu_shm 7 mul tcap 4
280put apu_shm 9 add get apu_shm 8 mul mul hcap d 4
281put apu_shm 10 tcap
282put apu_shm 11 hcap
283wend = add get apu_shm 9 mul hcap 4
284put apu_shm 3 mul div add wend 4095 4096 4096
285nw = add get apu_cfg 5 12
286if gt nw APU_WORK_SLOTS
287 nw = APU_WORK_SLOTS
288end
289if lt nw 4
290 nw = ulong 4
291end
292put apu_shm 12 nw
293h = mapnew int -1 0 add get apu_shm 2 mul nw get apu_shm 3 3 33
294if lt h 0
295 exit 70
296end
297put apu_shmh 0 h
298for i ulong APU_WORK_SLOTS
299 put apu_slot_use i 0
300end
301for j ulong APU_PREF_SLOTS
302 put apu_pslot_use j 0
303end
304end
305
306fn apu_pref_base ps ulong to ulong
307ret add get apu_shm 0 mul ps get apu_shm 1
308end
309
310fn apu_work_base ws ulong to ulong
311ret add get apu_shm 2 mul ws get apu_shm 3
312end
313
314note ---- Grace Role 0: Prefetcher / Token Validator / Arboris 2-Gram Novelty Worker ----
315
316fn apu_pref_tok1 w ulong idx ulong to ulong
317if eq w 1
318 ret ulong get apu_xbuf idx
319end
320lo = ulong get apu_xbuf mul idx 2
321hi = ulong get apu_xbuf add mul idx 2 1
322ret or lo shl hi 8
323end
324
325fn apu_pref_calc_win sz ulong to ulong
326tw = ulong 1
327if gt get og 0 256
328 tw = ulong 2
329end
330if ne rem sz tw 0
331 exit 21
332end
333ntok = div sz tw
334tt = get og 1
335if le ntok tt
336 exit 20
337end
338avail_w = div sub ntok 1 tt
339ntw = get apu_cfg 10
340if gt ntw avail_w
341 ntw = avail_w
342end
343nhw = ulong 0
344if gt avail_w ntw
345 nhw = get apu_cfg 11
346end
347totw = add ntw nhw
348need_b = mul add mul totw tt 1 tw
349if gt need_b 65536
350 totw = div sub div 65536 tw 1 tt
351 if lt totw ntw
352 ntw = totw
353 nhw = ulong 0
354 else
355 nhw = sub totw ntw
356 end
357 need_b = mul add mul totw tt 1 tw
358end
359put apu_hdr 0 tw
360put apu_hdr 1 ntw
361put apu_hdr 2 nhw
362ret need_b
363end
364
365fn apu_pref_read_file shard ulong to ulong
366plen = apu_copy_spath shard
367fd = int fopen apu_pbuf 0 plen 0 0
368if lt fd 0
369 exit 70
370end
371sz = fseek fd 0 2
372if lt sz 0
373 c0 = fclose fd
374 exit 70
375end
376need_b = apu_pref_calc_win ulong sz
377p = ulong 0
378while lt p need_b bound 65536
379 r = pread fd apu_xbuf p sub need_b p long p
380 if le r 0
381 c3 = fclose fd
382 exit 70
383 end
384 p = add p ulong r
385end
386c4 = fclose fd
387ret mul add get apu_hdr 1 get apu_hdr 2 get og 1
388end
389
390fn apu_pref_pack_one pbase ulong tw ulong i ulong to ulong
391xt = apu_pref_tok1 tw i
392yt = apu_pref_tok1 tw add i 1
393v = get og 0
394if or ge xt v ge yt v
395 exit 25
396end
397sh = get apu_shmh 0
398off = add pbase mul i 8
399r1 = mapset sh off 4 xt
400r2 = mapset sh add off 4 4 yt
401h2 = and smx xor shl xt 16 yt 4095
402if eq get apu_arb_ht h2 0
403 put apu_arb_ht h2 1
404 ret 1
405end
406ret 0
407end
408
409fn apu_pref_pack_slot ps ulong tw ulong tot_t ulong to ulong
410for hi ulong 4096
411 put apu_arb_ht hi 0
412end
413nov = ulong 0
414pbase = add apu_pref_base ps APU_SLOT_HDR
415for i ulong tot_t bound 32768
416 nov = add nov apu_pref_pack_one pbase tw i
417end
418cpufence
419ret div mul nov 1000 tot_t
420end
421
422fn apu_pref_exec shard ulong ps ulong to void
423t0 = monons
424tot_t = apu_pref_read_file shard
425nov_m = apu_pref_pack_slot ps get apu_hdr 0 tot_t
426apu_put_u64_m 0 shard
427apu_put_u64_m 8 ps
428apu_put_u64_m 16 get apu_hdr 1
429apu_put_u64_m 24 get apu_hdr 2
430apu_put_u64_m 32 nov_m
431apu_put_u64_m 40 sub ulong monons ulong t0
432apu_put_u64_m 48 0
433apu_put_u64_m 56 0
434end
435
436note ---- Grace Role 1: Cholesky spcryst + Linear Calibration + W4 Pack Worker ----
437
438fn apu_rowb_f32 o ulong to f64
439u = uint get apu_rowb o
440u = or u shl uint get apu_rowb add o 1 8
441u = or u shl uint get apu_rowb add o 2 16
442u = or u shl uint get apu_rowb add o 3 24
443ret f64 bitcast f32 u
444end
445
446fn apu_unpack_row_z sh long off ulong d ulong to void
447r = mapread sh off apu_rowb 0 mul d 4
448for j ulong d bound 2048
449 put apu_z j apu_rowb_f32 mul j 4
450end
451end
452
453fn apu_unpack_row_r sh long off ulong d ulong scale f64 to void
454r = mapread sh off apu_rowb 0 mul d 4
455for j ulong d bound 2048
456 put apu_rv j mul scale apu_rowb_f32 mul j 4
457end
458end
459
460fn apu_cryst_save_cal wb ulong t ulong ncal ulong to void
461d = get og 2
462cz = mul ncal d
463for j ulong d bound 2048
464 put apu_calz add cz j get apu_z j
465end
466put apu_caly ncal mapget get apu_shmh 0 add add wb get apu_shm 7 mul t 4 4
467end
468
469fn apu_cryst_tok wb ulong sid ulong t ulong rscale f64 to void
470sh = get apu_shmh 0
471d = get og 2
472apu_unpack_row_z sh add add wb get apu_shm 4 mul mul t d 4 d
473apu_unpack_row_r sh add add wb get apu_shm 5 mul mul t d 4 d rscale
474surp = f64 bitcast f32 uint mapget sh add add wb get apu_shm 6 mul t 4 4
475apu_acc_tok add 0.25f64 fmin surp 8.0f64 get apu_cfg 1 d sid
476end
477
478fn apu_cryst_stream wb ulong sid ulong ntrain ulong rscale f64 to ulong
479ncal = ulong 0
480for t ulong ntrain bound 32768
481 apu_cryst_tok wb sid t rscale
482 if lt ncal APU_CALN
483 apu_cryst_save_cal wb t ncal
484 ncal = add ncal 1
485 end
486end
487ret ncal
488end
489
490fn apu_cryst_reply shard ulong ws ulong ntrain ulong fbytes ulong dt ulong to void
491apu_put_u64_m 0 shard
492apu_put_u64_m 8 ws
493apu_put_u64_m 16 ntrain
494apu_put_u64_m 24 get apu_hdr 0
495apu_put_u64_m 32 get apu_hdr 1
496apu_put_u64_m 40 fbytes
497apu_put_u64_m 48 dt
498apu_put_u64_m 56 0
499end
500
501fn apu_cryst_solve_pack shard ulong sid ulong ncal ulong to ulong
502apu_norm_cent get og 2
503apu_sym_reg get apu_cfg 1 fmax div f64 get apu_cfg 2 1000.0f64 0.0001f64
504if ne apu_chol get apu_cfg 1 0
505 exit 43
506end
507apu_chol_solve get apu_cfg 1 get og 2
508apu_calibrate ncal get apu_cfg 1 sid
509ret apu_write_sp2 apu_make_outpath shard get apu_cfg 6 get apu_cfg 1 sid get apu_cfg 4
510end
511
512fn apu_cryst_exec shard ulong ws ulong to void
513t0 = monons
514cpufence
515wb = apu_work_base ws
516sid = mapget get apu_shmh 0 add wb 8 8
517ntrain = mapget get apu_shmh 0 add wb 16 8
518if gt ntrain get apu_cfg 3
519 ntrain = get apu_cfg 3
520end
521apu_zero_cryst get apu_cfg 1 get og 2
522ncal = apu_cryst_stream wb sid ntrain bitcast f64 mapget get apu_shmh 0 add wb 32 8
523apu_cryst_reply shard ws ntrain apu_cryst_solve_pack shard sid ncal sub ulong monons ulong t0
524end
525
526note ---- Grace Role 2: Live Merkle + 1-Bit Tamper + Held-Out W4 spweval Verifier Worker ----
527
528fn apu_verif_copy_row hi ulong d ulong to void
529cz = mul hi d
530for j ulong d bound 2048
531 put apu_calz add cz j get apu_z j
532end
533end
534
535fn apu_verif_load_eval ev_n ulong zb ulong yb ulong to void
536sh = get apu_shmh 0
537d = get og 2
538for hi ulong ev_n bound 128
539 apu_unpack_row_z sh add zb mul mul hi d 4 d
540 apu_verif_copy_row hi d
541 put apu_caly hi mapget sh add yb mul hi 4 4
542end
543end
544
545fn apu_verif_run_eval wb ulong to void
546sh = get apu_shmh 0
547ev_n = mapget sh add wb 24 8
548ev_zb = add wb get apu_shm 8
549ev_yb = add wb get apu_shm 9
550if eq ev_n 0
551 ev_n = mapget sh add wb 16 8
552 ev_zb = add wb get apu_shm 4
553 ev_yb = add wb get apu_shm 7
554end
555if gt ev_n APU_CALN
556 ev_n = APU_CALN
557end
558apu_verif_load_eval ev_n ev_zb ev_yb
559apu_eval_loaded ev_n get apu_cfg 1 mapget sh add wb 8 8
560end
561
562fn apu_verif_reply shard ulong ws ulong vrc ulong dt ulong to void
563apu_put_u64_m 0 shard
564apu_put_u64_m 8 ws
565if ne vrc 0
566 apu_put_u64_m 16 0
567 apu_put_u64_m 24 0
568else
569 apu_put_u64_m 16 get apu_hdr 0
570 apu_put_u64_m 24 get apu_hdr 1
571end
572apu_put_u64_m 32 vrc
573apu_put_u64_m 40 dt
574apu_put_u64_m 48 get apu_hdr 11
575apu_put_u64_m 56 0
576end
577
578fn apu_verif_exec shard ulong ws ulong to void
579t0 = monons
580cpufence
581vrc = apu_verify_pack apu_make_outpath shard get apu_cfg 6
582if ne vrc 0
583 apu_verif_reply shard ws vrc sub ulong monons ulong t0
584 ret
585end
586apu_verif_run_eval apu_work_base ws
587apu_verif_reply shard ws 0 sub ulong monons ulong t0
588end
589
590note ---- Worker Main Loop + bonClosePrevPipes EOF Hygiene ----
591
592fn apu_worker_loop w ulong go_fd int res_fd int to void
593apu_pin_core get apu_core w
594apu_put_u64_m 0 add w 1
595apu_pipe_wr res_fd 8
596for round ulong 100000 bound 100000
597 if not apu_pipe_rd go_fd 32
598 exit 0
599 end
600 cmd = apu_get_u64_m 0
601 if eq cmd 0
602 exit 0
603 end
604 shrd = apu_get_u64_m 8
605 slot = apu_get_u64_m 16
606 if eq cmd 1
607 apu_pref_exec shrd slot
608 else
609 if eq cmd 2
610 apu_cryst_exec shrd slot
611 else
612 apu_verif_exec shrd slot
613 end
614 end
615 apu_pipe_wr res_fd 64
616end
617exit 0
618end
619
620note Close all previously opened parent pipe fds in child so EOF never hangs (bonsai.om bonClosePrevPipes!)
621fn apu_close_prev_pipes w ulong to void
622for pw ulong w bound 20
623 c1 = fclose get apu_gofd pw
624 c2 = fclose get apu_rsfd pw
625end
626end
627
628fn apu_assign_topology to ulong
629nc = get apu_cfg 5
630np = ulong 2
631nv = ulong 2
632if lt nc 4
633 np = ulong 1
634 nv = ulong 1
635end
636if gt add add nc np nv 20
637 np = ulong 2
638 nv = ulong 2
639 nc = ulong 16
640 put apu_cfg 5 16
641end
642put apu_cfg 8 np
643put apu_cfg 9 nv
644note Pin Cholesky workers to ALL 10 fast Cortex-X925 cores (5..9, 15..19) first, then A725 cores (2..4, 10..12)!
645xtab = new ulong 16 0
646put xtab 0 5
647put xtab 1 6
648put xtab 2 7
649put xtab 3 8
650put xtab 4 9
651put xtab 5 15
652put xtab 6 16
653put xtab 7 17
654put xtab 8 18
655put xtab 9 19
656put xtab 10 2
657put xtab 11 3
658put xtab 12 4
659put xtab 13 10
660put xtab 14 11
661put xtab 15 12
662w = ulong 0
663for i ulong np bound 4
664 put apu_role w 0
665 put apu_core w i
666 w = add w 1
667end
668for j ulong nc bound 16
669 put apu_role w 1
670 put apu_core w get xtab j
671 w = add w 1
672end
673for k ulong nv bound 4
674 put apu_role w 2
675 put apu_core w add 13 k
676 w = add w 1
677end
678ret w
679end
680
681fn apu_mkpipe_pair off ulong to void
682p = new uchar 8 0
683if ne pipefds p 0 8 0 0
684 exit 70
685end
686for b ulong 8
687 put apu_msg add off b get p b
688end
689end
690
691fn apu_spawn_one w ulong to void
692apu_mkpipe_pair 0
693apu_mkpipe_pair 8
694pid = fork
695if lt pid 0
696 exit 70
697end
698if eq pid 0
699 rc = fclose apu_get_i32_m 4
700 rc = fclose apu_get_i32_m 8
701 apu_close_prev_pipes w
702 apu_worker_loop w apu_get_i32_m 0 apu_get_i32_m 12
703end
704rc = fclose apu_get_i32_m 0
705rc = fclose apu_get_i32_m 12
706put apu_pids w pid
707put apu_gofd w apu_get_i32_m 4
708put apu_rsfd w apu_get_i32_m 8
709put apu_busy w 0
710end
711
712fn apu_pool_start ntot ulong to void
713oflush
714for w ulong ntot bound 20
715 apu_spawn_one w
716end
717for w2 ulong ntot bound 20
718 if not apu_pipe_rd get apu_rsfd w2 8
719 exit 70
720 end
721end
722end
723
724fn apu_pool_stop ntot ulong to void
725st = new uchar 4 0
726for w ulong ntot bound 20
727 apu_send_job get apu_gofd w 0 0 0 0
728 c1 = fclose get apu_gofd w
729 c2 = fclose get apu_rsfd w
730 wp = waitpid int get apu_pids w st 0 4 0
731end
732end
733
734note ---- Non-Blocking pollfds Event Loop & Co-Execution Scheduler ----
735
736fn apu_find_free_pslot to long
737for i ulong APU_PREF_SLOTS
738 if eq get apu_pslot_use i 0
739 ret long i
740 end
741end
742ret long -1
743end
744
745fn apu_find_free_wslot to long
746nw = get apu_shm 12
747for i ulong nw bound 32
748 if eq get apu_slot_use i 0
749 ret long i
750 end
751end
752ret long -1
753end
754
755fn apu_find_idle_worker ntot ulong want_role ulong to long
756for w ulong ntot bound 20
757 if and eq get apu_role w want_role eq get apu_busy w 0
758 ret long w
759 end
760end
761ret long -1
762end
763
764fn apu_count_busy ntot ulong want_role ulong to ulong
765c = ulong 0
766for w ulong ntot bound 20
767 if and eq get apu_role w want_role ne get apu_busy w 0
768 c = add c 1
769 end
770end
771ret c
772end
773
774fn apu_count_cmd ntot ulong want_cmd ulong to ulong
775c = ulong 0
776for w ulong ntot bound 20
777 if and ne get apu_busy w 0 eq get apu_wcmd w want_cmd
778 c = add c 1
779 end
780end
781ret c
782end
783
784fn apu_pq_push shrd ulong ps ulong to void
785tail = get apu_pq_st 1
786put apu_pq_sh and tail 63 shrd
787put apu_pq_ps and tail 63 ps
788put apu_pq_st 1 add tail 1
789end
790
791fn apu_pq_len to ulong
792ret sub get apu_pq_st 1 get apu_pq_st 0
793end
794
795fn apu_vq_push shrd ulong ws ulong to void
796tail = get apu_vq_st 1
797put apu_vq_sh and tail 2047 shrd
798put apu_vq_ws and tail 2047 ws
799put apu_vq_st 1 add tail 1
800end
801
802fn apu_vq_len to ulong
803ret sub get apu_vq_st 1 get apu_vq_st 0
804end
805
806fn apu_touch_cpu_span to void
807now = onsnow
808if eq get apu_cfg 21 0
809 put apu_cfg 21 now
810end
811put apu_cfg 22 now
812end
813
814fn apu_done_pref shrd ulong slot ulong to void
815put apu_cfg 14 add get apu_cfg 14 apu_get_u64_m 40
816put apu_pslot_sh slot shrd
817put apu_pslot_nw slot apu_get_u64_m 16
818put apu_pslot_nh slot apu_get_u64_m 24
819nov = apu_get_u64_m 32
820put apu_pslot_nov slot nov
821put apu_s_nov shrd nov
822apu_pq_push shrd slot
823end
824
825fn apu_done_cryst shrd ulong slot ulong to void
826dt = apu_get_u64_m 48
827fbytes = apu_get_u64_m 40
828put apu_cfg 14 add get apu_cfg 14 dt
829put apu_cfg 15 add get apu_cfg 15 fbytes
830put apu_s_toks shrd apu_get_u64_m 16
831put apu_s_bcal shrd apu_get_u64_m 24
832put apu_s_ccal shrd apu_get_u64_m 32
833put apu_s_pb shrd fbytes
834put apu_s_cms shrd div dt 1000000
835apu_vq_push shrd slot
836end
837
838fn apu_emit_v_s1 shrd ulong bev ulong wev ulong to void
839sid = get apu_sids shrd
840toks = get apu_s_toks shrd
841bc = get apu_s_bcal shrd
842cc = get apu_s_ccal shrd
843apu_print_strand1 sid toks bc cc bev wev
844end
845
846fn apu_emit_v_s2 shrd ulong vms ulong trc ulong to void
847pb = get apu_s_pb shrd
848gms = get apu_s_gms shrd
849cms = get apu_s_cms shrd
850nov = get apu_s_nov shrd
851apu_print_strand2 pb gms cms vms trc nov
852end
853
854fn apu_done_verif shrd ulong slot ulong to void
855dt = apu_get_u64_m 40
856vrc = apu_get_u64_m 32
857trc = apu_get_u64_m 48
858bev = apu_get_u64_m 16
859wev = apu_get_u64_m 24
860put apu_cfg 14 add get apu_cfg 14 dt
861put apu_slot_use slot 0
862if ne vrc 0
863 exit int vrc
864end
865if eq trc 61
866 put apu_cfg 16 add get apu_cfg 16 1
867end
868put apu_cfg 17 add get apu_cfg 17 bev
869put apu_cfg 18 add get apu_cfg 18 wev
870apu_emit_v_s1 shrd bev wev
871apu_emit_v_s2 shrd div dt 1000000 trc
872end
873
874fn apu_handle_worker_done w ulong to void
875if not apu_pipe_rd get apu_rsfd w 64
876 exit 70
877end
878put apu_busy w 0
879apu_touch_cpu_span
880wcmd = get apu_wcmd w
881put apu_wcmd w 0
882shrd = apu_get_u64_m 0
883slot = apu_get_u64_m 8
884if eq wcmd 1
885 apu_done_pref shrd slot
886 ret
887end
888if eq wcmd 2
889 apu_done_cryst shrd slot
890 ret
891end
892apu_done_verif shrd slot
893end
894
895fn apu_set_pfd j ulong fd int to void
896u = ulong uint fd
897b = mul j 8
898put apu_pfd b uchar and u 255
899put apu_pfd add b 1 uchar and shr u 8 255
900put apu_pfd add b 2 uchar and shr u 16 255
901put apu_pfd add b 3 uchar and shr u 24 255
902put apu_pfd add b 4 1
903put apu_pfd add b 5 0
904put apu_pfd add b 6 0
905put apu_pfd add b 7 0
906end
907
908note Poll all busy workers; harvest every ready worker (timeout_ns = 0 for non-blocking, > 0 to wait for any 1).
909fn apu_poll_workers ntot ulong timeout_ns long to ulong
910m = ulong 0
911for w ulong ntot bound 20
912 if ne get apu_busy w 0
913 apu_set_pfd m get apu_rsfd w
914 put apu_pidx m w
915 m = add m 1
916 end
917end
918if eq m 0
919 ret 0
920end
921r = pollfds apu_pfd 0 mul m 8 timeout_ns
922if le r 0
923 ret 0
924end
925done = ulong 0
926for j ulong m bound 20
927 if ne get apu_pfd add mul j 8 6 0
928 apu_handle_worker_done get apu_pidx j
929 done = add done 1
930 end
931end
932ret done
933end
934
935fn apu_kick_verifs ntot ulong force_steal bool to void
936while gt apu_vq_len 0 bound 20
937 vw = apu_find_idle_worker ntot 2
938 if lt vw 0
939 if or force_steal or ge apu_vq_len 4 lt apu_find_free_wslot 0
940 vw = apu_find_idle_worker ntot 1
941 end
942 end
943 if lt vw 0
944 break
945 end
946 hd = get apu_vq_st 0
947 shrd = get apu_vq_sh and hd 2047
948 ws = get apu_vq_ws and hd 2047
949 put apu_vq_st 0 add hd 1
950 put apu_busy ulong vw 1
951 put apu_wcmd ulong vw 3
952 apu_touch_cpu_span
953 apu_send_job get apu_gofd ulong vw 3 shrd ws 0
954end
955end
956
957fn apu_kick_prefs ntot ulong next_pref ulong n_shards ulong to ulong
958np = next_pref
959while lt np n_shards bound 20
960 pw = apu_find_idle_worker ntot 0
961 if lt pw 0
962 break
963 end
964 ps = apu_find_free_pslot
965 if lt ps 0
966 break
967 end
968 put apu_pslot_use ulong ps 1
969 put apu_busy ulong pw 1
970 put apu_wcmd ulong pw 1
971 apu_touch_cpu_span
972 apu_send_job get apu_gofd ulong pw 1 np ulong ps 0
973 np = add np 1
974end
975ret np
976end
977
978note Pack up to `max_k` prefetched shards whose windows fit in one GPU B-batch, run producer, and immediately
979note dispatch EVERY shard in the batch to idle Grace Cholesky workers (ZERO waiting for all 64 shards!).
980fn apu_pack_one k ulong hd ulong ps ulong cw ulong ws ulong to void
981put apu_pq_st 0 add hd 1
982put apu_slot_use ws 1
983put apu_busy cw 1
984put apu_wcmd cw 2
985put apu_bsh k get apu_pq_sh and hd 63
986put apu_bps k ps
987put apu_bws k ws
988put apu_bnw k get apu_pslot_nw ps
989put apu_bnh k get apu_pslot_nh ps
990put apu_wslot k cw
991end
992
993fn apu_batch_cap to ulong
994if eq get apu_cfg 7 0
995 ret 1
996end
997ret get og 6
998end
999
1000fn apu_try_pack_head ntot ulong k ulong used_w ulong to ulong
1001hd = get apu_pq_st 0
1002ps = get apu_pq_ps and hd 63
1003tw = add get apu_pslot_nw ps get apu_pslot_nh ps
1004if and gt k 0 gt add used_w tw apu_batch_cap
1005 ret 0
1006end
1007cw = apu_find_idle_worker ntot 1
1008ws = apu_find_free_wslot
1009if or lt cw 0 lt ws 0
1010 ret 0
1011end
1012apu_pack_one k hd ps ulong cw ulong ws
1013ret tw
1014end
1015
1016fn apu_pack_batch ntot ulong to ulong
1017k = ulong 0
1018used_w = ulong 0
1019while gt apu_pq_len 0 bound 16
1020 tw = apu_try_pack_head ntot k used_w
1021 if eq tw 0
1022 break
1023 end
1024 used_w = add used_w tw
1025 k = add k 1
1026 if ge k 16
1027 break
1028 end
1029end
1030ret k
1031end
1032
1033fn apu_dispatch_one_batch ntot ulong to ulong
1034k = apu_pack_batch ntot
1035if eq k 0
1036 ret 0
1037end
1038t0 = monons
1039apu_be_run_batch k
1040gns = sub ulong monons ulong t0
1041put apu_cfg 13 add get apu_cfg 13 gns
1042gms = div gns 1000000
1043for i ulong k bound 16
1044 shrd = get apu_bsh i
1045 put apu_pslot_use get apu_bps i 0
1046 put apu_s_gms shrd div gms k
1047 apu_touch_cpu_span
1048 apu_send_job get apu_gofd get apu_wslot i 2 shrd get apu_bws i 0
1049end
1050put apu_cfg 20 add get apu_cfg 20 1
1051apu_print_wave get apu_cfg 20 k gms apu_pq_len apu_count_cmd ntot 2 apu_count_cmd ntot 3
1052ret k
1053end
1054
1055fn apu_target_k to ulong
1056if eq get apu_cfg 7 0
1057 ret 1
1058end
1059tk = div get og 6 add get apu_cfg 10 get apu_cfg 11
1060if eq tk 0
1061 tk = ulong 1
1062end
1063if gt tk APU_PREF_SLOTS
1064 tk = APU_PREF_SLOTS
1065end
1066if gt tk get apu_cfg 5
1067 tk = get apu_cfg 5
1068end
1069ret tk
1070end
1071
1072fn apu_wait_pref ntot ulong tk ulong rem_s ulong to void
1073want_k = tk
1074if lt rem_s want_k
1075 want_k = rem_s
1076end
1077for tries ulong 6 bound 6
1078 if or ge apu_pq_len want_k eq apu_count_cmd ntot 1 0
1079 break
1080 end
1081 h0 = apu_poll_workers ntot 2000000
1082end
1083end
1084
1085fn apu_fabric_run to void
1086apu_discover_shards
1087n_shards = get apu_cfg 0
1088ntot = apu_assign_topology
1089apu_shm_init
1090apu_pool_start ntot
1091put apu_cfg 7 apu_be_open
1092put apu_pq_st 0 0
1093put apu_pq_st 1 0
1094put apu_vq_st 0 0
1095put apu_vq_st 1 0
1096tk = apu_target_k
1097next_pref = ulong 0
1098dispatched = ulong 0
1099h = ulong 0
1100while lt dispatched n_shards bound 10000000
1101 next_pref = apu_kick_prefs ntot next_pref n_shards
1102 h = apu_poll_workers ntot 0
1103 apu_kick_verifs ntot false
1104 apu_wait_pref ntot tk sub n_shards dispatched
1105 next_pref = apu_kick_prefs ntot next_pref n_shards
1106 did = apu_dispatch_one_batch ntot
1107 if gt did 0
1108 dispatched = add dispatched did
1109 next_pref = apu_kick_prefs ntot next_pref n_shards
1110 h = apu_poll_workers ntot 0
1111 else
1112 h = apu_poll_workers ntot 5000000
1113 end
1114 apu_kick_verifs ntot false
1115end
1116while or gt apu_count_cmd ntot 2 0 or gt apu_count_cmd ntot 3 0 gt apu_vq_len 0 bound 10000000
1117 apu_kick_verifs ntot true
1118 h = apu_poll_workers ntot 10000000
1119 apu_kick_verifs ntot true
1120end
1121apu_be_close
1122apu_pool_stop ntot
1123h = ulong mapdrop get apu_shmh 0
1124end