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
CUDApp / tools / c5 / c5_matrix.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note tools/c5/c5_matrix.om -- bar C5 multi-process matrix on one GPU, orchestrated in seeded Omega (no C). |
| 3 | note Usage: c5_matrix ISO PY CASE |
| 4 | note ISO the gpu_isolation_test binary (CUDA++ tests/gpu_isolation_test.c, built unchanged) |
| 5 | note PY a Python with PyTorch; it runs the CUDA tenant (NVIDIA's stack, a verification oracle, not CUDA++ code) |
| 6 | note CASE c ISO --values-only (our two processes) beside a running CUDA process; all three must be right |
| 7 | note p the same while the CUDA process holds 16 GiB of device memory it wrote and checks at the end |
| 8 | note k ISO's chip tier is killed (SIGKILL, whole process group) 500 ms into its never-ending launch beside |
| 9 | note a CUDA process; the group must be gone within 20 s (the driver frees the hung channel on exit, which |
| 10 | note took 2.3-4.6 s on the GB10 with Xid 109), a fresh ISO --values-only must pass, the CUDA process stays right |
| 11 | note r red control: the CUDA process checks against a wrong expectation; the orchestrator must see exit 1 |
| 12 | note Children run with an empty environment and their stdout and stderr on a pipe that this program echoes. |
| 13 | note Exit 0 PASS, 1 FAIL, 2 usage or NOT_RUN. |
| 14 | text tenant "import os,sys,time,torch\ns=float(sys.argv[1]);h=int(sys.argv[2]);w=int(sys.argv[3])\nd=torch.device('cuda');k=None\nif h:k=torch.ones(h*268435456,device=d)\nx=torch.arange(1<<20,dtype=torch.float32,device=d);c=torch.arange(1<<20,dtype=torch.float32)\nn=0;b=0;print('[cuda] READY pid=%d hold_gib=%d expect_offset=%d'%(os.getpid(),h,w),flush=True);t=time.time()\nwhile time.time()-t<s:\n y=x*2+n%7;b+=int((y.cpu()!=c*2+n%7+w).sum());n+=1\nif k is not None:b+=int((k!=1).sum())\nprint('[cuda] pid=%d launches=%d wrong=%d secs=%.1f'%(os.getpid(),n,b,time.time()-t),flush=True)\nsys.exit(0 if b==0 else 1)\n" |
| 15 | text dashc "-c" |
| 16 | text vonly "--values-only" |
| 17 | text mready "[cuda] READY" |
| 18 | text mhang "a command list made before the hang" |
| 19 | text usage "usage: c5_matrix ISO PY CASE (CASE c, p, k or r)\n" |
| 20 | text sstart "[matrix] start pid " |
| 21 | text sexit " exit status " |
| 22 | text ssig " killed by signal " |
| 23 | text snl "\n" |
| 24 | text sready "[matrix] CUDA process ready after ms " |
| 25 | text shang "[matrix] hang launch seen; killing process group " |
| 26 | text sgone "[matrix] process group gone after ms " |
| 27 | text sstill "[matrix] process group still alive\n" |
| 28 | text salive "[matrix] CUDA process alive when ours finished: " |
| 29 | text snomark "[matrix] marker not seen (timeout or EOF)\n" |
| 30 | text vpass "C5 matrix: PASS case " |
| 31 | text vfail "C5 matrix: FAIL case " |
| 32 | text vnotrun "C5 matrix: NOT_RUN case " |
| 33 | global pk uchar 4096 |
| 34 | global iso uchar 1024 |
| 35 | global py uchar 1024 |
| 36 | global cs uchar 8 |
| 37 | global gl long 16 |
| 38 | global ob uchar 4096 |
| 39 | global st uchar 4 |
| 40 | global fds uchar 8 |
| 41 | global nb uchar 24 |
| 42 | global mk uchar 64 |
| 43 | |
| 44 | note gl: 0 packet offset, 1 packet count, 2 ISO length, 3 PY length, 4 pipe read fd of the last spawn, |
| 45 | note 5 marker length, 6 marker matched count, 7 last child pid seen, 8 last exit code or 128+signal |
| 46 | |
| 47 | fn putnum v long to void |
| 48 | x = v |
| 49 | if lt x 0 |
| 50 | x = neg x |
| 51 | put nb 0 45 |
| 52 | k = write 1 nb 0 1 |
| 53 | end |
| 54 | n = ulong 0 |
| 55 | for i ulong 20 |
| 56 | put nb sub 23 i uchar add 48 rem x 10 |
| 57 | x = div x 10 |
| 58 | n = add n 1 |
| 59 | if eq x 0 |
| 60 | break |
| 61 | end |
| 62 | end |
| 63 | k = write 1 nb sub 24 n n |
| 64 | end |
| 65 | |
| 66 | fn putbool v bool to void |
| 67 | if v |
| 68 | putnum 1 |
| 69 | else |
| 70 | putnum 0 |
| 71 | end |
| 72 | end |
| 73 | |
| 74 | fn pkreset to void |
| 75 | put gl 0 2 |
| 76 | put gl 1 0 |
| 77 | end |
| 78 | |
| 79 | fn pkfinish to void |
| 80 | c = get gl 1 |
| 81 | put pk 0 uchar and c 255 |
| 82 | put pk 1 uchar shr c 8 |
| 83 | end |
| 84 | |
| 85 | fn pkhead n ulong to ulong |
| 86 | o = ulong get gl 0 |
| 87 | put pk o uchar and n 255 |
| 88 | put pk add o 1 uchar shr n 8 |
| 89 | put gl 1 add get gl 1 1 |
| 90 | ret add o 2 |
| 91 | end |
| 92 | |
| 93 | fn pkiso to void |
| 94 | n = ulong get gl 2 |
| 95 | o = pkhead n |
| 96 | for i ulong n |
| 97 | put pk add o i get iso i |
| 98 | end |
| 99 | put gl 0 long add o n |
| 100 | end |
| 101 | |
| 102 | fn pkpy to void |
| 103 | n = ulong get gl 3 |
| 104 | o = pkhead n |
| 105 | for i ulong n |
| 106 | put pk add o i get py i |
| 107 | end |
| 108 | put gl 0 long add o n |
| 109 | end |
| 110 | |
| 111 | fn pkvonly to void |
| 112 | o = pkhead len vonly |
| 113 | for i ulong len vonly |
| 114 | put pk add o i get vonly i |
| 115 | end |
| 116 | put gl 0 long add o len vonly |
| 117 | end |
| 118 | |
| 119 | fn pkdashc to void |
| 120 | o = pkhead len dashc |
| 121 | for i ulong len dashc |
| 122 | put pk add o i get dashc i |
| 123 | end |
| 124 | put gl 0 long add o len dashc |
| 125 | end |
| 126 | |
| 127 | fn pktenant to void |
| 128 | o = pkhead len tenant |
| 129 | for i ulong len tenant |
| 130 | put pk add o i get tenant i |
| 131 | end |
| 132 | put gl 0 long add o len tenant |
| 133 | end |
| 134 | |
| 135 | note one decimal argument |
| 136 | fn pknum v long to void |
| 137 | x = v |
| 138 | n = ulong 0 |
| 139 | for i ulong 20 |
| 140 | put nb sub 23 i uchar add 48 rem x 10 |
| 141 | x = div x 10 |
| 142 | n = add n 1 |
| 143 | if eq x 0 |
| 144 | break |
| 145 | end |
| 146 | end |
| 147 | o = pkhead n |
| 148 | for i ulong n |
| 149 | put pk add o i get nb add sub 24 n i |
| 150 | end |
| 151 | put gl 0 long add o n |
| 152 | end |
| 153 | |
| 154 | fn fdword off ulong to int |
| 155 | x = ulong 0 |
| 156 | for i ulong 4 |
| 157 | v = ulong get fds add off i |
| 158 | x = or x shl v mul i 8 |
| 159 | end |
| 160 | ret int x |
| 161 | end |
| 162 | |
| 163 | note fork + exec the packet in pk with stdout and stderr on a fresh pipe, in its own process group. |
| 164 | note Returns the pid (gl 7) and leaves the read end in gl 4. |
| 165 | fn spawn to long |
| 166 | r = pipefds fds 0 len fds 524288 |
| 167 | if ne r 0 |
| 168 | ret neg long 1 |
| 169 | end |
| 170 | rd = fdword 0 |
| 171 | wr = fdword 4 |
| 172 | pid = fork |
| 173 | if lt pid 0 |
| 174 | ret neg long 2 |
| 175 | end |
| 176 | if eq pid 0 |
| 177 | q = setpgid 0 0 |
| 178 | q = dupfd wr 1 0 |
| 179 | q = dupfd wr 2 0 |
| 180 | q = execprog pk 0 ulong get gl 0 |
| 181 | exit 99 |
| 182 | end |
| 183 | r = fclose wr |
| 184 | put gl 4 long rd |
| 185 | put gl 7 pid |
| 186 | k = write 1 sstart 0 len sstart |
| 187 | putnum pid |
| 188 | k = write 1 snl 0 len snl |
| 189 | ret pid |
| 190 | end |
| 191 | |
| 192 | note scan ob[0..n) for the marker in mk, carrying the matched count in gl 6; true once it is complete |
| 193 | fn scan n ulong to bool |
| 194 | for i ulong n |
| 195 | b = get ob i |
| 196 | m = ulong get gl 6 |
| 197 | if eq b get mk m |
| 198 | put gl 6 long add m 1 |
| 199 | else |
| 200 | if eq b get mk 0 |
| 201 | put gl 6 1 |
| 202 | else |
| 203 | put gl 6 0 |
| 204 | end |
| 205 | end |
| 206 | if eq get gl 6 get gl 5 |
| 207 | ret true |
| 208 | end |
| 209 | end |
| 210 | ret false |
| 211 | end |
| 212 | |
| 213 | note echo the child's output until the marker in mk is seen (1), EOF (0) or the deadline passes (-1) |
| 214 | fn pump fd int deadline long usemark bool to long |
| 215 | put gl 6 0 |
| 216 | for z ulong 100000 |
| 217 | now = monons |
| 218 | if ge now deadline |
| 219 | ret neg long 1 |
| 220 | end |
| 221 | r = waitio fd 1 sub deadline now |
| 222 | if lt r 0 |
| 223 | ret neg long 1 |
| 224 | end |
| 225 | if eq r 0 |
| 226 | ret neg long 1 |
| 227 | end |
| 228 | n = read fd ob 0 len ob |
| 229 | if le n 0 |
| 230 | ret 0 |
| 231 | end |
| 232 | k = write 1 ob 0 ulong n |
| 233 | if usemark |
| 234 | if scan ulong n |
| 235 | ret 1 |
| 236 | end |
| 237 | end |
| 238 | end |
| 239 | ret neg long 1 |
| 240 | end |
| 241 | |
| 242 | note reap a pid; gl 8 = exit code, or 128 + signal |
| 243 | fn reap pid long to long |
| 244 | r = waitpid int pid st 0 len st 0 |
| 245 | if ne r pid |
| 246 | put gl 8 255 |
| 247 | ret neg long 1 |
| 248 | end |
| 249 | s = long get st 0 |
| 250 | if eq and s 127 0 |
| 251 | put gl 8 long get st 1 |
| 252 | k = write 1 sexit 0 len sexit |
| 253 | putnum get gl 8 |
| 254 | k = write 1 snl 0 len snl |
| 255 | else |
| 256 | put gl 8 add 128 and s 127 |
| 257 | k = write 1 ssig 0 len ssig |
| 258 | putnum and s 127 |
| 259 | k = write 1 snl 0 len snl |
| 260 | end |
| 261 | ret get gl 8 |
| 262 | end |
| 263 | |
| 264 | fn markready to void |
| 265 | for i ulong len mready |
| 266 | put mk i get mready i |
| 267 | end |
| 268 | put gl 5 long len mready |
| 269 | end |
| 270 | |
| 271 | fn markhang to void |
| 272 | for i ulong len mhang |
| 273 | put mk i get mhang i |
| 274 | end |
| 275 | put gl 5 long len mhang |
| 276 | end |
| 277 | |
| 278 | note start the CUDA tenant: seconds, hold GiB, wrong offset; wait for READY. Returns pid, or negative. |
| 279 | fn cuda secs long hold long wrong long to long |
| 280 | pkreset |
| 281 | pkpy |
| 282 | pkdashc |
| 283 | pktenant |
| 284 | pknum secs |
| 285 | pknum hold |
| 286 | pknum wrong |
| 287 | pkfinish |
| 288 | t0 = monons |
| 289 | pid = spawn |
| 290 | if lt pid 0 |
| 291 | ret pid |
| 292 | end |
| 293 | markready |
| 294 | r = pump int get gl 4 add t0 180000000000 true |
| 295 | if ne r 1 |
| 296 | k = write 1 snomark 0 len snomark |
| 297 | ret neg long 3 |
| 298 | end |
| 299 | k = write 1 sready 0 len sready |
| 300 | putnum div sub monons t0 1000000 |
| 301 | k = write 1 snl 0 len snl |
| 302 | ret pid |
| 303 | end |
| 304 | |
| 305 | note drain a pipe to EOF (bounded by a deadline) and close it |
| 306 | fn drain fd int secs long to void |
| 307 | r = pump fd add monons mul secs 1000000000 false |
| 308 | q = fclose fd |
| 309 | end |
| 310 | |
| 311 | note ISO --values-only to completion; returns its exit code (gl 8) |
| 312 | fn ours to long |
| 313 | pkreset |
| 314 | pkiso |
| 315 | pkvonly |
| 316 | pkfinish |
| 317 | pid = spawn |
| 318 | if lt pid 0 |
| 319 | ret 255 |
| 320 | end |
| 321 | drain int get gl 4 120 |
| 322 | ret reap pid |
| 323 | end |
| 324 | |
| 325 | fn verdict ok bool to int |
| 326 | if ok |
| 327 | k = write 1 vpass 0 len vpass |
| 328 | else |
| 329 | k = write 1 vfail 0 len vfail |
| 330 | end |
| 331 | k = write 1 cs 0 1 |
| 332 | k = write 1 snl 0 len snl |
| 333 | if ok |
| 334 | ret 0 |
| 335 | end |
| 336 | ret 1 |
| 337 | end |
| 338 | |
| 339 | fn sharecase hold long to int |
| 340 | tp = cuda 40 hold 0 |
| 341 | if lt tp 0 |
| 342 | k = write 1 vnotrun 0 len vnotrun |
| 343 | k = write 1 cs 0 1 |
| 344 | k = write 1 snl 0 len snl |
| 345 | ret 2 |
| 346 | end |
| 347 | tfd = int get gl 4 |
| 348 | ro = ours |
| 349 | alive = eq waitpid int tp st 0 len st 1 0 |
| 350 | k = write 1 salive 0 len salive |
| 351 | putbool alive |
| 352 | k = write 1 snl 0 len snl |
| 353 | drain tfd 120 |
| 354 | rt = reap tp |
| 355 | ret verdict and and eq ro 0 eq rt 0 alive |
| 356 | end |
| 357 | |
| 358 | fn killcase to int |
| 359 | tp = cuda 60 0 0 |
| 360 | if lt tp 0 |
| 361 | k = write 1 vnotrun 0 len vnotrun |
| 362 | k = write 1 cs 0 1 |
| 363 | k = write 1 snl 0 len snl |
| 364 | ret 2 |
| 365 | end |
| 366 | tfd = int get gl 4 |
| 367 | pkreset |
| 368 | pkiso |
| 369 | pkfinish |
| 370 | vp = spawn |
| 371 | if lt vp 0 |
| 372 | ret 2 |
| 373 | end |
| 374 | vfd = int get gl 4 |
| 375 | markhang |
| 376 | r = pump vfd add monons 120000000000 true |
| 377 | if ne r 1 |
| 378 | k = write 1 snomark 0 len snomark |
| 379 | q = killpid int neg vp 9 |
| 380 | rv = reap vp |
| 381 | drain tfd 120 |
| 382 | rt = reap tp |
| 383 | ret verdict false |
| 384 | end |
| 385 | q = sleepns 500000000 |
| 386 | k = write 1 shang 0 len shang |
| 387 | putnum vp |
| 388 | k = write 1 snl 0 len snl |
| 389 | t0 = monons |
| 390 | q = killpid int neg vp 9 |
| 391 | ok = eq reap vp 137 |
| 392 | gone = false |
| 393 | for i ulong 2000 |
| 394 | if eq killpid int neg vp 0 neg long 3 |
| 395 | gone = true |
| 396 | break |
| 397 | end |
| 398 | q = sleepns 10000000 |
| 399 | end |
| 400 | if gone |
| 401 | k = write 1 sgone 0 len sgone |
| 402 | putnum div sub monons t0 1000000 |
| 403 | k = write 1 snl 0 len snl |
| 404 | else |
| 405 | k = write 1 sstill 0 len sstill |
| 406 | end |
| 407 | ok = and ok gone |
| 408 | drain vfd 5 |
| 409 | ok = and ok eq ours 0 |
| 410 | alive = eq waitpid int tp st 0 len st 1 0 |
| 411 | k = write 1 salive 0 len salive |
| 412 | putbool alive |
| 413 | k = write 1 snl 0 len snl |
| 414 | ok = and ok alive |
| 415 | drain tfd 120 |
| 416 | ok = and ok eq reap tp 0 |
| 417 | ret verdict ok |
| 418 | end |
| 419 | |
| 420 | fn redcase to int |
| 421 | tp = cuda 3 0 1 |
| 422 | if lt tp 0 |
| 423 | k = write 1 vnotrun 0 len vnotrun |
| 424 | k = write 1 cs 0 1 |
| 425 | k = write 1 snl 0 len snl |
| 426 | ret 2 |
| 427 | end |
| 428 | drain int get gl 4 60 |
| 429 | rt = reap tp |
| 430 | note the control is red when the tenant's own check catches the wrong expectation: exit 1 |
| 431 | ret verdict eq rt 1 |
| 432 | end |
| 433 | |
| 434 | fn main to int |
| 435 | if ne argcnt 4 |
| 436 | k = write 1 usage 0 len usage |
| 437 | ret 2 |
| 438 | end |
| 439 | n = argread 1 iso 0 len iso |
| 440 | if le n 0 |
| 441 | ret 2 |
| 442 | end |
| 443 | put gl 2 n |
| 444 | n = argread 2 py 0 len py |
| 445 | if le n 0 |
| 446 | ret 2 |
| 447 | end |
| 448 | put gl 3 n |
| 449 | n = argread 3 cs 0 len cs |
| 450 | if ne n 1 |
| 451 | k = write 1 usage 0 len usage |
| 452 | ret 2 |
| 453 | end |
| 454 | c = get cs 0 |
| 455 | if eq c 99 |
| 456 | ret sharecase 0 |
| 457 | end |
| 458 | if eq c 112 |
| 459 | ret sharecase 16 |
| 460 | end |
| 461 | if eq c 107 |
| 462 | ret killcase |
| 463 | end |
| 464 | if eq c 114 |
| 465 | ret redcase |
| 466 | end |
| 467 | k = write 1 usage 0 len usage |
| 468 | ret 2 |
| 469 | end |