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