CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note tests/d8_lib.om -- the shared part of the D8 programs (tests/d8_process_tax.om, tests/d8_mutants.om). Make |
| 4 | note concatenates it in front of each program and compiles the result with the pinned seeded omc (omega-mv e5b32567, |
| 5 | note sha256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f); see mk/dx-d8.mk. |
| 6 | note Byte regions: B holds 32 regions of 4096 bytes (Bn their lengths). Globals are never passed to functions, so |
| 7 | note helpers take region numbers. By convention region 4 holds the output directory; regions 2, 3, 22, 23, 29 and 30 |
| 8 | note are scratch of these helpers (2 output, 3 argument and capture, 22 environment entry, 23 PATH, 29 capture |
| 9 | note path, 30 path), region 19 receives the program found by which. |
| 10 | note L is one loaded file (Ln its length). AL is a packed argument list for execprog: u16 count, then a u16 length |
| 11 | note and the bytes of each argument (AP: write position and count). ST is a waitpid status. |
| 12 | note execprog starts every child with an empty environment; callers that need variables pass them through |
| 13 | note /usr/bin/env K=V (envkv) until omega-mv has an execprog form that takes an environment. |
| 14 | global B uchar 131072 |
| 15 | global Bn ulong 32 |
| 16 | global L uchar 524288 |
| 17 | global Ln ulong 1 |
| 18 | global AL uchar 4096 |
| 19 | global AP ulong 2 |
| 20 | global ST uchar 8 |
| 21 | |
| 22 | text TBig "d8: refused (a buffer bound or a failed host call)\n" |
| 23 | text TSl "/" |
| 24 | text TDevNul "/dev/null" |
| 25 | text TPathV "PATH=" |
| 26 | |
| 27 | note ---- regions |
| 28 | fn bfail to void |
| 29 | r = write 2 TBig 0 len TBig |
| 30 | exit 90 |
| 31 | end |
| 32 | |
| 33 | fn bc r ulong c uchar to void |
| 34 | n = get Bn r |
| 35 | if ge n 4095 |
| 36 | bfail |
| 37 | end |
| 38 | put B add mul r 4096 n c |
| 39 | put Bn r add n 1 |
| 40 | end |
| 41 | |
| 42 | fn bclr r ulong to void |
| 43 | put Bn r 0 |
| 44 | end |
| 45 | |
| 46 | fn bt r ulong t view uchar * to void |
| 47 | for i ulong len t |
| 48 | bc r get t i |
| 49 | end |
| 50 | end |
| 51 | |
| 52 | fn bs r ulong t view uchar * to void |
| 53 | bclr r |
| 54 | bt r t |
| 55 | end |
| 56 | |
| 57 | fn bb d ulong s ulong to void |
| 58 | for i ulong get Bn s |
| 59 | bc d get B add mul s 4096 i |
| 60 | end |
| 61 | end |
| 62 | |
| 63 | fn bu r ulong v ulong to void |
| 64 | d = ulong 1 |
| 65 | while ge div v d 10 bound 20 |
| 66 | d = mul d 10 |
| 67 | end |
| 68 | while gt d 0 bound 21 |
| 69 | bc r uchar add 48 rem div v d 10 |
| 70 | d = div d 10 |
| 71 | end |
| 72 | end |
| 73 | |
| 74 | fn bz r ulong v ulong w ulong to void |
| 75 | d = ulong 1 |
| 76 | for i ulong sub w 1 |
| 77 | d = mul d 10 |
| 78 | end |
| 79 | while gt d 0 bound 21 |
| 80 | bc r uchar add 48 rem div v d 10 |
| 81 | d = div d 10 |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | fn bl r ulong v long to void |
| 86 | if lt v 0 |
| 87 | bc r 45 |
| 88 | bu r ulong neg v |
| 89 | else |
| 90 | bu r ulong v |
| 91 | end |
| 92 | end |
| 93 | |
| 94 | fn bist r ulong t view uchar * to bool |
| 95 | n = get Bn r |
| 96 | if ne n len t |
| 97 | ret false |
| 98 | end |
| 99 | for i ulong n |
| 100 | if ne get B add mul r 4096 i get t i |
| 101 | ret false |
| 102 | end |
| 103 | end |
| 104 | ret true |
| 105 | end |
| 106 | |
| 107 | fn beq a ulong b ulong to bool |
| 108 | n = get Bn a |
| 109 | if ne n get Bn b |
| 110 | ret false |
| 111 | end |
| 112 | oa = mul a 4096 |
| 113 | ob = mul b 4096 |
| 114 | for i ulong n |
| 115 | if ne get B add oa i get B add ob i |
| 116 | ret false |
| 117 | end |
| 118 | end |
| 119 | ret true |
| 120 | end |
| 121 | |
| 122 | note ^[0-9]+$ of at most 18 digits as a value; -1 otherwise |
| 123 | fn bnum r ulong to long |
| 124 | n = get Bn r |
| 125 | if or eq n 0 gt n 18 |
| 126 | ret neg long 1 |
| 127 | end |
| 128 | v = long 0 |
| 129 | for i ulong n |
| 130 | c = get B add mul r 4096 i |
| 131 | if or lt c 48 gt c 57 |
| 132 | ret neg long 1 |
| 133 | end |
| 134 | v = add mul v 10 long sub c 48 |
| 135 | end |
| 136 | ret v |
| 137 | end |
| 138 | |
| 139 | fn bout fd int r ulong to void |
| 140 | o = mul r 4096 |
| 141 | n = get Bn r |
| 142 | k = ulong 0 |
| 143 | while lt k n bound 4096 |
| 144 | w = write fd B add o k sub n k |
| 145 | if le w 0 |
| 146 | bfail |
| 147 | end |
| 148 | k = add k ulong w |
| 149 | end |
| 150 | end |
| 151 | |
| 152 | fn say t view uchar * to void |
| 153 | bs 2 t |
| 154 | bout 1 2 |
| 155 | end |
| 156 | |
| 157 | fn barg i ulong r ulong to void |
| 158 | n = argread i B mul r 4096 4095 |
| 159 | if lt n 0 |
| 160 | bfail |
| 161 | end |
| 162 | put Bn r ulong n |
| 163 | end |
| 164 | |
| 165 | note path region d = OUTDIR "/" nm sx |
| 166 | fn bpath d ulong nm view uchar * sx view uchar * to void |
| 167 | bclr d |
| 168 | bb d 4 |
| 169 | bt d TSl |
| 170 | bt d nm |
| 171 | bt d sx |
| 172 | end |
| 173 | |
| 174 | note path region d = OUTDIR "/" nm |
| 175 | fn bpath1 d ulong nm view uchar * to void |
| 176 | bclr d |
| 177 | bb d 4 |
| 178 | bt d TSl |
| 179 | bt d nm |
| 180 | end |
| 181 | |
| 182 | fn bopen r ulong flags int to long |
| 183 | n = get Bn r |
| 184 | if eq n 0 |
| 185 | bfail |
| 186 | end |
| 187 | ret fopen B mul r 4096 n flags 420 |
| 188 | end |
| 189 | |
| 190 | note writes region s to the file named by region p: flags 578 create/truncate, 1089 create/append |
| 191 | fn bfile p ulong s ulong flags int to void |
| 192 | fd = bopen p flags |
| 193 | if lt fd 0 |
| 194 | bfail |
| 195 | end |
| 196 | bout int fd s |
| 197 | c = fclose int fd |
| 198 | end |
| 199 | |
| 200 | fn bexists r ulong to bool |
| 201 | n = get Bn r |
| 202 | if eq n 0 |
| 203 | ret false |
| 204 | end |
| 205 | ret eq faccess B mul r 4096 n 0 0 |
| 206 | end |
| 207 | |
| 208 | note UTC from realns. style 0 %Y-%m-%dT%H:%M:%S.%NZ, 1 %FT%TZ, 2 %Y%m%dT%H%M%SZ |
| 209 | fn bsep r ulong style ulong c uchar to void |
| 210 | if ne style 2 |
| 211 | bc r c |
| 212 | end |
| 213 | end |
| 214 | |
| 215 | note the civil date of a day count since 1970-01-01 (Hinnant's civil_from_days) |
| 216 | fn bdate r ulong days ulong style ulong to void |
| 217 | z = add days 719468 |
| 218 | era = div z 146097 |
| 219 | doe = sub z mul era 146097 |
| 220 | z = sub doe div doe 1460 |
| 221 | z = add z div doe 36524 |
| 222 | yoe = div sub z div doe 146096 365 |
| 223 | y = add yoe mul era 400 |
| 224 | z = add mul 365 yoe div yoe 4 |
| 225 | doe = sub doe sub z div yoe 100 |
| 226 | z = div add mul 5 doe 2 153 |
| 227 | era = add sub doe div add mul 153 z 2 5 1 |
| 228 | if ge z 10 |
| 229 | z = sub z 9 |
| 230 | else |
| 231 | z = add z 3 |
| 232 | end |
| 233 | if le z 2 |
| 234 | y = add y 1 |
| 235 | end |
| 236 | bz r y 4 |
| 237 | bsep r style 45 |
| 238 | bz r z 2 |
| 239 | bsep r style 45 |
| 240 | bz r era 2 |
| 241 | end |
| 242 | |
| 243 | fn bstamp r ulong style ulong to void |
| 244 | ns = ulong realns |
| 245 | s = div ns 1000000000 |
| 246 | bdate r div s 86400 style |
| 247 | s = rem s 86400 |
| 248 | bc r 84 |
| 249 | bz r div s 3600 2 |
| 250 | bsep r style 58 |
| 251 | bz r rem div s 60 60 2 |
| 252 | bsep r style 58 |
| 253 | bz r rem s 60 2 |
| 254 | if eq style 0 |
| 255 | bc r 46 |
| 256 | bz r rem ns 1000000000 9 |
| 257 | end |
| 258 | bc r 90 |
| 259 | end |
| 260 | |
| 261 | note ---- the loaded file L |
| 262 | fn fload r ulong to bool |
| 263 | put Ln 0 0 |
| 264 | fd = bopen r 0 |
| 265 | if lt fd 0 |
| 266 | ret false |
| 267 | end |
| 268 | n = ulong 0 |
| 269 | while lt n len L bound 999999 |
| 270 | k = read int fd L n sub len L n |
| 271 | if le k 0 |
| 272 | break |
| 273 | end |
| 274 | n = add n ulong k |
| 275 | end |
| 276 | put Ln 0 n |
| 277 | c = fclose int fd |
| 278 | ret true |
| 279 | end |
| 280 | |
| 281 | fn lend p ulong to ulong |
| 282 | n = get Ln 0 |
| 283 | q = p |
| 284 | while lt q n bound 999999 |
| 285 | if eq get L q 10 |
| 286 | ret q |
| 287 | end |
| 288 | q = add q 1 |
| 289 | end |
| 290 | ret n |
| 291 | end |
| 292 | |
| 293 | fn lat p ulong t view uchar * to bool |
| 294 | for i ulong len t |
| 295 | if ne get L add p i get t i |
| 296 | ret false |
| 297 | end |
| 298 | end |
| 299 | ret true |
| 300 | end |
| 301 | |
| 302 | fn leq p ulong e ulong t view uchar * to bool |
| 303 | if ne sub e p len t |
| 304 | ret false |
| 305 | end |
| 306 | ret lat p t |
| 307 | end |
| 308 | |
| 309 | fn lpre p ulong e ulong t view uchar * to bool |
| 310 | if lt sub e p len t |
| 311 | ret false |
| 312 | end |
| 313 | ret lat p t |
| 314 | end |
| 315 | |
| 316 | fn lhas p ulong e ulong t view uchar * to bool |
| 317 | if lt sub e p len t |
| 318 | ret false |
| 319 | end |
| 320 | for s ulong add sub sub e p len t 1 |
| 321 | if lat add p s t |
| 322 | ret true |
| 323 | end |
| 324 | end |
| 325 | ret false |
| 326 | end |
| 327 | |
| 328 | fn lcopy p ulong e ulong r ulong to void |
| 329 | q = p |
| 330 | while lt q e bound 999999 |
| 331 | bc r get L q |
| 332 | q = add q 1 |
| 333 | end |
| 334 | end |
| 335 | |
| 336 | note lines equal to t (mode 0) or starting with t (mode 1) |
| 337 | fn lcnt t view uchar * mode ulong to ulong |
| 338 | c = ulong 0 |
| 339 | p = ulong 0 |
| 340 | while lt p get Ln 0 bound 999999 |
| 341 | e = lend p |
| 342 | if eq mode 0 |
| 343 | if leq p e t |
| 344 | c = add c 1 |
| 345 | end |
| 346 | else |
| 347 | if lpre p e t |
| 348 | c = add c 1 |
| 349 | end |
| 350 | end |
| 351 | p = add e 1 |
| 352 | end |
| 353 | ret c |
| 354 | end |
| 355 | |
| 356 | note the first line starting with t, appended to region r; false when none |
| 357 | fn lfirst t view uchar * r ulong to bool |
| 358 | p = ulong 0 |
| 359 | while lt p get Ln 0 bound 999999 |
| 360 | e = lend p |
| 361 | if lpre p e t |
| 362 | lcopy p e r |
| 363 | ret true |
| 364 | end |
| 365 | p = add e 1 |
| 366 | end |
| 367 | ret false |
| 368 | end |
| 369 | |
| 370 | note the first line containing a or b, appended to region r |
| 371 | fn lfirst2 a view uchar * b view uchar * r ulong to void |
| 372 | p = ulong 0 |
| 373 | while lt p get Ln 0 bound 999999 |
| 374 | e = lend p |
| 375 | if or lhas p e a lhas p e b |
| 376 | lcopy p e r |
| 377 | ret |
| 378 | end |
| 379 | p = add e 1 |
| 380 | end |
| 381 | end |
| 382 | |
| 383 | note every line starting with key: the rest, newline separated, into region r (sed -n 's/^key//p') |
| 384 | fn lvals key view uchar * r ulong to void |
| 385 | bclr r |
| 386 | k = ulong 0 |
| 387 | p = ulong 0 |
| 388 | while lt p get Ln 0 bound 999999 |
| 389 | e = lend p |
| 390 | if lpre p e key |
| 391 | if gt k 0 |
| 392 | bc r 10 |
| 393 | end |
| 394 | lcopy add p len key e r |
| 395 | k = add k 1 |
| 396 | end |
| 397 | p = add e 1 |
| 398 | end |
| 399 | end |
| 400 | |
| 401 | note the digits of the last line "# exit: [0-9]*" into region r (empty when none) |
| 402 | note ---- processes. AL is a packed argument list: u16 count, then a u16 length and the bytes per argument. |
| 403 | fn alclr to void |
| 404 | put AP 0 2 |
| 405 | put AP 1 0 |
| 406 | end |
| 407 | |
| 408 | fn alb r ulong to void |
| 409 | n = get Bn r |
| 410 | p = get AP 0 |
| 411 | if or ge get AP 1 64 gt add add p n 2 4096 |
| 412 | bfail |
| 413 | end |
| 414 | put AL p uchar and n 255 |
| 415 | put AL add p 1 uchar shr n 8 |
| 416 | o = mul r 4096 |
| 417 | for i ulong n |
| 418 | put AL add add p 2 i get B add o i |
| 419 | end |
| 420 | put AP 0 add add p 2 n |
| 421 | put AP 1 add get AP 1 1 |
| 422 | end |
| 423 | |
| 424 | fn alt t view uchar * to void |
| 425 | bs 3 t |
| 426 | alb 3 |
| 427 | end |
| 428 | |
| 429 | note the command, arguments joined by spaces (the "# command:" line) |
| 430 | fn bcmd r ulong to void |
| 431 | p = ulong 2 |
| 432 | for a ulong get AP 1 |
| 433 | n = add ulong get AL p shl ulong get AL add p 1 8 |
| 434 | if gt a 0 |
| 435 | bc r 32 |
| 436 | end |
| 437 | for i ulong n |
| 438 | bc r get AL add add p 2 i |
| 439 | end |
| 440 | p = add add p 2 n |
| 441 | end |
| 442 | end |
| 443 | |
| 444 | note fork and execprog AL. out 1: stdout and stderr appended to the file named by region r; out 2: stdout to |
| 445 | note descriptor fd, stderr to /dev/null; out 0: inherited. |
| 446 | fn spawn how ulong r ulong fd int to long |
| 447 | put AL 0 uchar get AP 1 |
| 448 | put AL 1 0 |
| 449 | pid = fork |
| 450 | if lt pid 0 |
| 451 | bfail |
| 452 | end |
| 453 | if eq pid 0 |
| 454 | if eq how 1 |
| 455 | f = bopen r 1089 |
| 456 | if lt f 0 |
| 457 | exit 127 |
| 458 | end |
| 459 | x = dupfd int f 1 0 |
| 460 | x = dupfd int f 2 0 |
| 461 | end |
| 462 | if eq how 2 |
| 463 | x = dupfd fd 1 0 |
| 464 | g = fopen TDevNul 0 len TDevNul 1 0 |
| 465 | if ge g 0 |
| 466 | x = dupfd int g 2 0 |
| 467 | end |
| 468 | end |
| 469 | x = execprog AL 0 get AP 0 |
| 470 | exit 127 |
| 471 | end |
| 472 | ret pid |
| 473 | end |
| 474 | |
| 475 | note exit code, or 128 + signal; -1 while running (opt 1, WNOHANG) |
| 476 | fn reap pid long opt int to long |
| 477 | w = waitpid int pid ST 0 4 opt |
| 478 | if eq w 0 |
| 479 | ret neg long 1 |
| 480 | end |
| 481 | if lt w 0 |
| 482 | bfail |
| 483 | end |
| 484 | s = get ST 0 |
| 485 | if ne s 0 |
| 486 | ret add 128 long and s 127 |
| 487 | end |
| 488 | ret long get ST 1 |
| 489 | end |
| 490 | |
| 491 | fn runwait how ulong r ulong to long |
| 492 | pid = spawn how r 0 |
| 493 | ret reap pid 0 |
| 494 | end |
| 495 | |
| 496 | note ---- environment and PATH |
| 497 | fn bpfx r ulong t view uchar * to bool |
| 498 | if lt get Bn r len t |
| 499 | ret false |
| 500 | end |
| 501 | o = mul r 4096 |
| 502 | for i ulong len t |
| 503 | if ne get B add o i get t i |
| 504 | ret false |
| 505 | end |
| 506 | end |
| 507 | ret true |
| 508 | end |
| 509 | |
| 510 | note region d gets bytes [k, e) of region s |
| 511 | fn bmid d ulong s ulong k ulong e ulong to void |
| 512 | o = mul s 4096 |
| 513 | while lt k e bound 4096 |
| 514 | bc d get B add o k |
| 515 | k = add k 1 |
| 516 | end |
| 517 | end |
| 518 | |
| 519 | note region d gets region s from byte k on |
| 520 | fn bfrom d ulong s ulong k ulong to void |
| 521 | o = mul s 4096 |
| 522 | n = get Bn s |
| 523 | while lt k n bound 4096 |
| 524 | bc d get B add o k |
| 525 | k = add k 1 |
| 526 | end |
| 527 | end |
| 528 | |
| 529 | fn envget key view uchar * r ulong to bool |
| 530 | bclr r |
| 531 | for i ulong ulong envcnt |
| 532 | n = envread i B 90112 4095 |
| 533 | if lt n 0 |
| 534 | bfail |
| 535 | end |
| 536 | put Bn 22 ulong n |
| 537 | if bpfx 22 key |
| 538 | bfrom r 22 len key |
| 539 | ret true |
| 540 | end |
| 541 | end |
| 542 | ret false |
| 543 | end |
| 544 | |
| 545 | note a program name searched along PATH into region 19 (command -v) |
| 546 | fn which nm view uchar * to bool |
| 547 | if not envget TPathV 23 |
| 548 | ret false |
| 549 | end |
| 550 | n = get Bn 23 |
| 551 | s = ulong 0 |
| 552 | p = ulong 0 |
| 553 | while le p n bound 4096 |
| 554 | if or eq p n eq get B add 94208 p 58 |
| 555 | bclr 19 |
| 556 | bmid 19 23 s p |
| 557 | if eq get Bn 19 0 |
| 558 | bc 19 46 |
| 559 | end |
| 560 | bt 19 TSl |
| 561 | bt 19 nm |
| 562 | if eq faccess B mul ulong 19 4096 get Bn 19 1 0 |
| 563 | ret true |
| 564 | end |
| 565 | s = add p 1 |
| 566 | end |
| 567 | p = add p 1 |
| 568 | end |
| 569 | ret false |
| 570 | end |
| 571 | |
| 572 | fn mkdirp r ulong to void |
| 573 | n = get Bn r |
| 574 | for i ulong n |
| 575 | last = eq add i 1 n |
| 576 | if not last |
| 577 | last = eq get B add mul r 4096 add i 1 47 |
| 578 | end |
| 579 | if and last gt i 0 |
| 580 | x = makedir B mul r 4096 add i 1 493 |
| 581 | end |
| 582 | end |
| 583 | end |
| 584 | |
| 585 | note the D8_CORES pin: false when unpinned; the cores in region 16 |
| 586 | fn envkv key view uchar * to void |
| 587 | if envget key 3 |
| 588 | bs 30 key |
| 589 | bb 30 3 |
| 590 | alb 30 |
| 591 | end |
| 592 | end |
| 593 | |
| 594 | note /usr/bin/env with the variables spark-window needs, then the spark-window found on PATH (region 18) |
| 595 | fn capture to long |
| 596 | bclr 3 |
| 597 | bfile 29 3 578 |
| 598 | rc = runwait 1 29 |
| 599 | bclr 3 |
| 600 | x = fload 29 |
| 601 | n = get Ln 0 |
| 602 | while gt n 0 bound 999999 |
| 603 | if ne get L sub n 1 10 |
| 604 | break |
| 605 | end |
| 606 | n = sub n 1 |
| 607 | end |
| 608 | if gt n 4000 |
| 609 | n = ulong 4000 |
| 610 | end |
| 611 | lcopy 0 n 3 |
| 612 | ret rc |
| 613 | end |
| 614 | |
| 615 | fn catfile r ulong to void |
| 616 | if fload r |
| 617 | bclr 2 |
| 618 | n = get Ln 0 |
| 619 | k = ulong 0 |
| 620 | while lt k n bound 999999 |
| 621 | bc 2 get L k |
| 622 | k = add k 1 |
| 623 | if ge get Bn 2 4000 |
| 624 | bout 1 2 |
| 625 | bclr 2 |
| 626 | end |
| 627 | end |
| 628 | bout 1 2 |
| 629 | end |
| 630 | end |