interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tools / c5 / c5_matrix.om

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