omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / device-window / race.om
| 1 | note race.om: CPU-only control for the evidence-directory step of tools/run-device-window (the |
| 2 | note Omega program tools/run-device-window.om) and of its shell predecessor tools/run-device-window.sh |
| 3 | note (DRA-105 F5, omega-bridge #78; DRA-441). Opens no GPU and makes no spark-window call. |
| 4 | note |
| 5 | note Each trial starts two device-mode runs of the program with the same new ledger id at the |
| 6 | note same moment (both children block on one pipe until the parent writes). Exactly one may get |
| 7 | note past the directory step: the other must exit 2 with a REFUSE line. Neither may reach a |
| 8 | note window call: SPARK_WINDOW=NEGCTL-none skips the open, CUBIN_DIR names a missing directory so |
| 9 | note the run that got the directory stops at the cubin copy (exit 1) before the first row, and |
| 10 | note PATH=/usr/bin:/bin holds no spark-window at all. Then, on the same ledger id, a third |
| 11 | note device-mode run must refuse (exit 2) and leave rc.txt and window.txt as they were, and a |
| 12 | note dry-run must report EXISTS (exit 1). |
| 13 | note |
| 14 | note usage: race SCRIPT SCRATCH [TRIALS] |
| 15 | note SCRIPT the program to test. A name ending in ".sh" is copied to SCRATCH/tools/ |
| 16 | note run-device-window.sh and run with /bin/sh; any other name (the compiled |
| 17 | note tools/run-device-window.om) is copied to SCRATCH/tools/run-device-window with |
| 18 | note mode 0755 and run directly, so it finds SCRATCH as its tree from /proc/self/exe. |
| 19 | note SCRATCH a directory that must not exist yet; the caller removes it afterwards |
| 20 | note TRIALS default 100 |
| 21 | note prints one summary line, then RACE PASS (exit 0) or RACE FAIL (exit 1); usage errors exit 2. |
| 22 | note compiler: the newest omega-mv main seed/out/omc; tests/device-window/README.md has the commands. |
| 23 | text tb "/usr/bin/env\nSPARK_WINDOW=NEGCTL-none\nPATH=/usr/bin:/bin\nCUBIN_DIR=\nCUDAPP=\n/bin/sh\n/tools/run-device-window.sh\n--ledger\nRACE-\n/evidence/device-window/\n/no-cubins\n/no-cudapp\n/logs/\n/rc.txt\n/window.txt\n/01-cubin-matmul.log\nREFUSE: \n/tools\n/logs\n.a\n.b\n--dry-run\nCUDA_VISIBLE_DEVICES=\nEXISTS \n.c\n.d\nRACE trials=\n one_winner=\n both_proceeded=\n both_refused=\n seq_refused=\n dryrun_exists=\n evidence_ok=\nRACE PASS\nRACE FAIL\nFAIL trial=\n exits=\nusage: race SCRIPT SCRATCH [TRIALS]; SCRATCH must not exist\n bits=\n,\nMAXPY=/nonexistent/python\n/tools/run-device-window\n" |
| 24 | global ish ulong 1 |
| 25 | global sb uchar 4096 |
| 26 | global sn ulong 1 |
| 27 | global ap uchar 1024 |
| 28 | global an ulong 1 |
| 29 | global pk uchar 4096 |
| 30 | global pn ulong 2 |
| 31 | global fb uchar 65536 |
| 32 | global st uchar 4 |
| 33 | global ends uchar 8 |
| 34 | global stb uchar 128 |
| 35 | global lg uchar 16384 |
| 36 | global lgn ulong 1 |
| 37 | global kp ulong 2 |
| 38 | global tn ulong 1 |
| 39 | global one uchar 2 |
| 40 | global cnt ulong 8 |
| 41 | global ex long 4 |
| 42 | global sz long 2 |
| 43 | note tbpos k: kp[0] = start, kp[1] = length of entry k of tb (entries end with a newline) |
| 44 | fn tbpos k ulong to void |
| 45 | i = ulong 0 |
| 46 | e = ulong 0 |
| 47 | c = true |
| 48 | while c bound 999999 |
| 49 | if ge i len tb |
| 50 | c = false |
| 51 | else |
| 52 | if eq e k |
| 53 | c = false |
| 54 | else |
| 55 | if eq get tb i 10 |
| 56 | e = add e 1 |
| 57 | end |
| 58 | i = add i 1 |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | put kp 0 i |
| 63 | j = i |
| 64 | d = true |
| 65 | while d bound 999999 |
| 66 | if ge j len tb |
| 67 | d = false |
| 68 | else |
| 69 | if eq get tb j 10 |
| 70 | d = false |
| 71 | else |
| 72 | j = add j 1 |
| 73 | end |
| 74 | end |
| 75 | end |
| 76 | put kp 1 sub j i |
| 77 | end |
| 78 | fn sbclr to void |
| 79 | put sn 0 0 |
| 80 | end |
| 81 | fn sbc b uchar to void |
| 82 | n = get sn 0 |
| 83 | put sb n b |
| 84 | put sn 0 add n 1 |
| 85 | end |
| 86 | fn sbk k ulong to void |
| 87 | tbpos k |
| 88 | s = get kp 0 |
| 89 | m = get kp 1 |
| 90 | for i ulong m |
| 91 | sbc get tb add s i |
| 92 | end |
| 93 | end |
| 94 | fn sbap to void |
| 95 | for i ulong get an 0 |
| 96 | sbc get ap i |
| 97 | end |
| 98 | end |
| 99 | fn sbnum v ulong to void |
| 100 | d = new uchar 24 0 |
| 101 | n = ulong 0 |
| 102 | x = v |
| 103 | c = true |
| 104 | while c bound 30 |
| 105 | put d n uchar add 48 rem x 10 |
| 106 | n = add n 1 |
| 107 | x = div x 10 |
| 108 | if eq x 0 |
| 109 | c = false |
| 110 | end |
| 111 | end |
| 112 | while gt n 0 bound 30 |
| 113 | n = sub n 1 |
| 114 | sbc get d n |
| 115 | end |
| 116 | end |
| 117 | fn flush to void |
| 118 | sbc 10 |
| 119 | k = write 1 sb 0 get sn 0 |
| 120 | sbclr |
| 121 | end |
| 122 | fn sayn k ulong v ulong to void |
| 123 | sbk k |
| 124 | sbnum v |
| 125 | end |
| 126 | note packet for execprog: u16 count, then records of u16 length and bytes (OMEGESE packed list) |
| 127 | fn pkbeg to void |
| 128 | put pn 0 2 |
| 129 | put pn 1 0 |
| 130 | end |
| 131 | fn pkrec to void |
| 132 | n = get sn 0 |
| 133 | p = get pn 0 |
| 134 | put pk p uchar and n 255 |
| 135 | put pk add p 1 uchar shr n 8 |
| 136 | for i ulong n |
| 137 | put pk add add p 2 i get sb i |
| 138 | end |
| 139 | put pn 0 add add p 2 n |
| 140 | put pn 1 add get pn 1 1 |
| 141 | sbclr |
| 142 | end |
| 143 | fn pkk k ulong to void |
| 144 | sbk k |
| 145 | pkrec |
| 146 | end |
| 147 | fn pkend to void |
| 148 | c = get pn 1 |
| 149 | put pk 0 uchar and c 255 |
| 150 | put pk 1 uchar shr c 8 |
| 151 | end |
| 152 | note pevid: sb = SCRATCH/evidence/device-window/RACE-<trial> |
| 153 | fn pevid to void |
| 154 | sbap |
| 155 | sbk 9 |
| 156 | sbk 8 |
| 157 | sbnum get tn 0 |
| 158 | end |
| 159 | note plog tag: sb = SCRATCH/logs/RACE-<trial><tag> |
| 160 | fn plog tag ulong to void |
| 161 | sbap |
| 162 | sbk 12 |
| 163 | sbk 8 |
| 164 | sbnum get tn 0 |
| 165 | sbk tag |
| 166 | end |
| 167 | note fsz: size of the file named by sb, -1 when stat fails; clears sb |
| 168 | fn fsz to long |
| 169 | r = stat sb 0 get sn 0 stb 0 128 |
| 170 | sbclr |
| 171 | if lt r 0 |
| 172 | ret -1 |
| 173 | end |
| 174 | v = long 0 |
| 175 | for i ulong 8 |
| 176 | v = or shl v 8 long get stb sub 55 i |
| 177 | end |
| 178 | ret v |
| 179 | end |
| 180 | note fszp k: size of SCRATCH/evidence/device-window/RACE-<trial> + entry k |
| 181 | fn fszp k ulong to long |
| 182 | pevid |
| 183 | sbk k |
| 184 | ret fsz |
| 185 | end |
| 186 | note logrd tag: lg = the bytes of the run's log, lgn[0] = their count |
| 187 | fn logrd tag ulong to void |
| 188 | plog tag |
| 189 | f = fopen sb 0 get sn 0 0 0 |
| 190 | sbclr |
| 191 | put lgn 0 0 |
| 192 | if lt f 0 |
| 193 | ret |
| 194 | end |
| 195 | n = ulong 0 |
| 196 | c = true |
| 197 | while c bound 999999 |
| 198 | if ge n 16384 |
| 199 | c = false |
| 200 | else |
| 201 | k = read int f lg n sub 16384 n |
| 202 | if le k 0 |
| 203 | c = false |
| 204 | else |
| 205 | n = add n ulong k |
| 206 | end |
| 207 | end |
| 208 | end |
| 209 | z = fclose int f |
| 210 | put lgn 0 n |
| 211 | end |
| 212 | fn hasat i ulong to bool |
| 213 | s = get kp 0 |
| 214 | m = get kp 1 |
| 215 | for j ulong m |
| 216 | if ne get lg add i j get tb add s j |
| 217 | ret false |
| 218 | end |
| 219 | end |
| 220 | ret true |
| 221 | end |
| 222 | note logfind tag k: does the run's log contain entry k of tb |
| 223 | fn logfind tag ulong k ulong to bool |
| 224 | logrd tag |
| 225 | tbpos k |
| 226 | m = get kp 1 |
| 227 | n = get lgn 0 |
| 228 | if lt n m |
| 229 | ret false |
| 230 | end |
| 231 | for i ulong add sub n m 1 |
| 232 | if hasat i |
| 233 | ret true |
| 234 | end |
| 235 | end |
| 236 | ret false |
| 237 | end |
| 238 | fn fdat i ulong to int |
| 239 | v = uint 0 |
| 240 | for j ulong 4 |
| 241 | v = or v shl uint get ends add i j uint mul j 8 |
| 242 | end |
| 243 | ret int v |
| 244 | end |
| 245 | note spawn mode tag: fork one run of the script (mode 0 device, 1 dry-run); its stdout and |
| 246 | note stderr go to SCRATCH/logs/RACE-<trial><tag>; it starts when the parent calls release |
| 247 | fn spawn mode ulong tag ulong to long |
| 248 | p = fork |
| 249 | if ne p 0 |
| 250 | ret p |
| 251 | end |
| 252 | sbclr |
| 253 | rfd = fdat 0 |
| 254 | k = read rfd one 0 1 |
| 255 | plog tag |
| 256 | f = fopen sb 0 get sn 0 577 420 |
| 257 | sbclr |
| 258 | if lt f 0 |
| 259 | exit 121 |
| 260 | end |
| 261 | a = dupfd int f 1 0 |
| 262 | b = dupfd int f 2 0 |
| 263 | pkbeg |
| 264 | pkk 0 |
| 265 | if eq mode 1 |
| 266 | pkk 22 |
| 267 | pkk 40 |
| 268 | else |
| 269 | pkk 1 |
| 270 | end |
| 271 | pkk 2 |
| 272 | sbk 3 |
| 273 | sbap |
| 274 | sbk 10 |
| 275 | pkrec |
| 276 | sbk 4 |
| 277 | sbap |
| 278 | sbk 11 |
| 279 | pkrec |
| 280 | if eq get ish 0 1 |
| 281 | pkk 5 |
| 282 | sbap |
| 283 | sbk 6 |
| 284 | pkrec |
| 285 | else |
| 286 | sbap |
| 287 | sbk 41 |
| 288 | pkrec |
| 289 | end |
| 290 | pkk 7 |
| 291 | sbk 8 |
| 292 | sbnum get tn 0 |
| 293 | pkrec |
| 294 | if eq mode 1 |
| 295 | pkk 21 |
| 296 | end |
| 297 | pkend |
| 298 | x = execprog pk 0 get pn 0 |
| 299 | exit 120 |
| 300 | end |
| 301 | fn openp to void |
| 302 | r = pipefds ends 0 8 0 |
| 303 | if ne r 0 |
| 304 | exit 91 |
| 305 | end |
| 306 | end |
| 307 | fn release to void |
| 308 | w = fdat 4 |
| 309 | k = write w one 0 2 |
| 310 | a = fclose fdat 0 |
| 311 | b = fclose w |
| 312 | end |
| 313 | note wexit pid: exit status of the run, 1000 + signal when it was killed, -1 when wait failed |
| 314 | fn wexit p long to long |
| 315 | r = waitpid int p st 0 4 0 |
| 316 | if lt r 0 |
| 317 | ret -1 |
| 318 | end |
| 319 | if ne get st 0 0 |
| 320 | ret add 1000 long get st 0 |
| 321 | end |
| 322 | ret long get st 1 |
| 323 | end |
| 324 | note racep: the two concurrent runs; bit 1 exactly one winner (exit 1, no REFUSE) and one |
| 325 | note refusal (exit 2 with REFUSE), 2 both proceeded, 4 both refused |
| 326 | fn racep to ulong |
| 327 | openp |
| 328 | pa = spawn 0 19 |
| 329 | pb = spawn 0 20 |
| 330 | release |
| 331 | ea = wexit pa |
| 332 | eb = wexit pb |
| 333 | put ex 0 ea |
| 334 | put ex 1 eb |
| 335 | fa = logfind 19 16 |
| 336 | fz = logfind 20 16 |
| 337 | res = ulong 0 |
| 338 | if and eq ea 2 eq eb 1 |
| 339 | if and fa not fz |
| 340 | res = or res 1 |
| 341 | end |
| 342 | end |
| 343 | if and eq ea 1 eq eb 2 |
| 344 | if and fz not fa |
| 345 | res = or res 1 |
| 346 | end |
| 347 | end |
| 348 | if and eq ea 1 eq eb 1 |
| 349 | res = or res 2 |
| 350 | end |
| 351 | if and eq ea 2 eq eb 2 |
| 352 | res = or res 4 |
| 353 | end |
| 354 | ret res |
| 355 | end |
| 356 | note shape: bit 32 when rc.txt is empty, window.txt was written and no row log exists |
| 357 | fn shape to ulong |
| 358 | w0 = fszp 14 |
| 359 | r0 = fszp 13 |
| 360 | put sz 0 w0 |
| 361 | put sz 1 r0 |
| 362 | m = fszp 15 |
| 363 | if gt w0 0 |
| 364 | if and eq r0 0 eq m -1 |
| 365 | ret 32 |
| 366 | end |
| 367 | end |
| 368 | ret 0 |
| 369 | end |
| 370 | note seqp: bit 8 when a later run on the same ledger id refuses and changes nothing |
| 371 | fn seqp to ulong |
| 372 | openp |
| 373 | pc = spawn 0 24 |
| 374 | release |
| 375 | ec = wexit pc |
| 376 | put ex 2 ec |
| 377 | fc = logfind 24 16 |
| 378 | w1 = fszp 14 |
| 379 | r1 = fszp 13 |
| 380 | if and eq ec 2 fc |
| 381 | if and eq w1 get sz 0 eq r1 get sz 1 |
| 382 | ret 8 |
| 383 | end |
| 384 | end |
| 385 | ret 0 |
| 386 | end |
| 387 | note dryp: bit 16 when a dry-run on the same ledger id reports EXISTS and exits 1 |
| 388 | fn dryp to ulong |
| 389 | openp |
| 390 | pd = spawn 1 25 |
| 391 | release |
| 392 | ed = wexit pd |
| 393 | put ex 3 ed |
| 394 | fx = logfind 25 23 |
| 395 | if and eq ed 1 fx |
| 396 | ret 16 |
| 397 | end |
| 398 | ret 0 |
| 399 | end |
| 400 | fn sbex i ulong to void |
| 401 | v = get ex i |
| 402 | if lt v 0 |
| 403 | sbnum 999 |
| 404 | else |
| 405 | sbnum ulong v |
| 406 | end |
| 407 | end |
| 408 | note trial: the bits above; 57 = 1 + 8 + 16 + 32 is a pass |
| 409 | fn trial to ulong |
| 410 | a = racep |
| 411 | b = shape |
| 412 | c = seqp |
| 413 | d = dryp |
| 414 | res = or or or a b c d |
| 415 | if ne res 57 |
| 416 | sayn 35 get tn 0 |
| 417 | sbk 36 |
| 418 | sbex 0 |
| 419 | sbk 39 |
| 420 | sbex 1 |
| 421 | sbk 39 |
| 422 | sbex 2 |
| 423 | sbk 39 |
| 424 | sbex 3 |
| 425 | sayn 38 res |
| 426 | flush |
| 427 | end |
| 428 | ret res |
| 429 | end |
| 430 | fn bump i ulong to void |
| 431 | put cnt i add get cnt i 1 |
| 432 | end |
| 433 | fn mkd to void |
| 434 | r = makedir sb 0 get sn 0 493 |
| 435 | sbclr |
| 436 | if ne r 0 |
| 437 | sbk 37 |
| 438 | flush |
| 439 | exit 2 |
| 440 | end |
| 441 | end |
| 442 | fn usage to void |
| 443 | sbk 37 |
| 444 | flush |
| 445 | exit 2 |
| 446 | end |
| 447 | note setup: SCRATCH from argument 2, its tools/ and logs/ directories, and the copy of SCRIPT |
| 448 | fn setup to void |
| 449 | r = argread 2 ap 0 1024 |
| 450 | if le r 0 |
| 451 | usage |
| 452 | end |
| 453 | put an 0 ulong r |
| 454 | sbap |
| 455 | mkd |
| 456 | sbap |
| 457 | sbk 17 |
| 458 | mkd |
| 459 | sbap |
| 460 | sbk 18 |
| 461 | mkd |
| 462 | q = argread 1 sb 0 4096 |
| 463 | if le q 0 |
| 464 | usage |
| 465 | end |
| 466 | put sn 0 ulong q |
| 467 | put ish 0 0 |
| 468 | if ge q 3 |
| 469 | u = ulong q |
| 470 | a = eq get sb sub u 3 46 |
| 471 | b = eq get sb sub u 2 115 |
| 472 | c = eq get sb sub u 1 104 |
| 473 | if and and a b c |
| 474 | put ish 0 1 |
| 475 | end |
| 476 | end |
| 477 | f = fopen sb 0 get sn 0 0 0 |
| 478 | sbclr |
| 479 | if lt f 0 |
| 480 | usage |
| 481 | end |
| 482 | sbap |
| 483 | if eq get ish 0 1 |
| 484 | sbk 6 |
| 485 | else |
| 486 | sbk 41 |
| 487 | end |
| 488 | g = fopen sb 0 get sn 0 577 493 |
| 489 | sbclr |
| 490 | if lt g 0 |
| 491 | usage |
| 492 | end |
| 493 | go = true |
| 494 | while go bound 9223372036854775807 |
| 495 | k = read int f fb 0 65536 |
| 496 | if lt k 0 |
| 497 | usage |
| 498 | end |
| 499 | if eq k 0 |
| 500 | go = false |
| 501 | else |
| 502 | w = write int g fb 0 ulong k |
| 503 | if ne w k |
| 504 | usage |
| 505 | end |
| 506 | end |
| 507 | end |
| 508 | y = fclose int g |
| 509 | z = fclose int f |
| 510 | end |
| 511 | note ntrials c: argument 3 as a decimal count, 100 when absent |
| 512 | fn ntrials c long to ulong |
| 513 | if lt c 4 |
| 514 | ret 100 |
| 515 | end |
| 516 | y = argread 3 sb 0 32 |
| 517 | if le y 0 |
| 518 | usage |
| 519 | end |
| 520 | t = ulong 0 |
| 521 | for i ulong ulong y |
| 522 | b = get sb i |
| 523 | if or lt b 48 gt b 57 |
| 524 | usage |
| 525 | end |
| 526 | t = add mul t 10 ulong sub b 48 |
| 527 | end |
| 528 | ret t |
| 529 | end |
| 530 | fn tally res ulong to void |
| 531 | for i ulong 6 |
| 532 | if ne and res shl ulong 1 i 0 |
| 533 | bump i |
| 534 | end |
| 535 | end |
| 536 | end |
| 537 | fn main to int |
| 538 | c = argcnt |
| 539 | if lt c 3 |
| 540 | usage |
| 541 | end |
| 542 | setup |
| 543 | trials = ntrials c |
| 544 | for t ulong trials |
| 545 | put tn 0 add t 1 |
| 546 | tally trial |
| 547 | end |
| 548 | sayn 26 trials |
| 549 | sayn 27 get cnt 0 |
| 550 | sayn 28 get cnt 1 |
| 551 | sayn 29 get cnt 2 |
| 552 | sayn 30 get cnt 3 |
| 553 | sayn 31 get cnt 4 |
| 554 | sayn 32 get cnt 5 |
| 555 | flush |
| 556 | ok = gt trials 0 |
| 557 | for i ulong 6 |
| 558 | if ne i 1 |
| 559 | if ne i 2 |
| 560 | if ne get cnt i trials |
| 561 | ok = false |
| 562 | end |
| 563 | end |
| 564 | end |
| 565 | end |
| 566 | if ok |
| 567 | sbk 33 |
| 568 | flush |
| 569 | ret 0 |
| 570 | end |
| 571 | sbk 34 |
| 572 | flush |
| 573 | ret 1 |
| 574 | end |