interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / nvidia / native / nvml / query.om

486 lines · 9221 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note query.om: the device queries of compat/nvml, after nvml.om. Every query first checks, in order:
3note a live library (else UNINITIALIZED 1), the GPU index s 1 below the count and a non-null output
4note s 2 (else INVALID_ARGUMENT 2), as nvml.h:4432-4443 and its siblings document.
5
6fn pre s edit ulong SN to ulong
7if eq get s 17 0
8ret 1
9end
10if ge get s 1 get s 21
11ret 2
12end
13if eq get s 2 0
14ret 2
15end
16ret 0
17end
18
19fn hsub s edit ulong SN to ulong
20ret get s dv get s 1 5
21end
22
23fn scal s edit ulong SN v ulong to void
24put s 5 v
25put s 27 1
26end
27
28note nvmlDeviceGetCount_v2 (nvml.h:4221) and nvmlDeviceGetHandleByIndex_v2 (nvml.h:4289).
29fn count s edit ulong SN to void
30if eq get s 17 0
31done s 1
32ret
33end
34if eq get s 2 0
35done s 2
36ret
37end
38scal s get s 21
39done s 0
40end
41
42fn handle s edit ulong SN to void
43e = pre s
44if ne e 0
45done s e
46ret
47end
48scal s get s 1
49done s 0
50end
51
52note nvmlDeviceGetName (nvml.h:4432): NV2080_CTRL_CMD_GPU_GET_NAME_STRING (0x20800110,
53note ctrl2080gpu.h:318), 132-byte params, flags 0 (ASCII) at 0, the name at 4. The name must be
54note printable ASCII of 1..95 bytes ended by a zero within its 128 bytes. A length s 3 below the name
55note plus its terminator is INSUFFICIENT_SIZE (7).
56fn name s edit ulong SN b edit uchar BN to void
57if eq get s 22 0
58e = pre s
59if ne e 0
60done s e
61ret
62end
63bzero b PO 132
64ctl s b hsub s 545259792 132
65put s 22 1
66ret
67end
68e = chk s b 28
69if ne e 0
70done s e
71ret
72end
73if ne g32 b PO 0
74done s 999
75ret
76end
77n = ulong 0
78z = false
79for i ulong 128
80c = get b add add PO 4 i
81if not z
82if eq c 0
83z = true
84n = i
85else
86if or lt c 32 gt c 126
87done s 999
88ret
89end
90end
91end
92end
93if or not z or eq n 0 gt n 95
94done s 999
95ret
96end
97if lt get s 3 add n 1
98done s 7
99ret
100end
101for i ulong n
102put b add OO i get b add add PO 4 i
103end
104put b add OO n 0
105put s 7 add n 1
106done s 0
107end
108
109fn hexc d ulong to uchar
110if lt d 10
111ret uchar add 48 d
112end
113ret uchar add 87 d
114end
115
116note nvmlDeviceGetUUID (nvml.h:4816): NV2080_CTRL_CMD_GPU_GET_GID_INFO (0x2080014a,
117note ctrl2080gpu.h:1775-1798), 268-byte params: index 0, flags 4 = 2 (BINARY, SHA1), length 8 = 16,
118note data 12. Printed as nvidia-smi does: GPU- then 8-4-4-4-12 lower-case hex, 40 bytes plus a zero.
119fn uuid s edit ulong SN b edit uchar BN to void
120if eq get s 22 0
121e = pre s
122if ne e 0
123done s e
124ret
125end
126bzero b PO 268
127p32 b add PO 4 2
128ctl s b hsub s 545259850 268
129put s 22 1
130ret
131end
132e = chk s b 28
133if ne e 0
134done s e
135ret
136end
137if ne g32 b add PO 8 16
138done s 999
139ret
140end
141if lt get s 3 41
142done s 7
143ret
144end
145put b OO 71
146put b add OO 1 80
147put b add OO 2 85
148put b add OO 3 45
149n = add OO 4
150for i ulong 16
151if or or eq i 4 eq i 6 or eq i 8 eq i 10
152put b n 45
153n = add n 1
154end
155v = ulong get b add add PO 12 i
156put b n hexc shr v 4
157put b add n 1 hexc and v 15
158n = add n 2
159end
160put b n 0
161put s 7 41
162done s 0
163end
164
165note Thermal instructions (NV2080_CTRL_CMD_THERMAL_SYSTEM_EXECUTE_V2 0x20800513, ctrl2080thermal.h:135),
166note 1432-byte params: API version 1 at 0, instruction size 44 at 8, count at 20, 32 instructions
167note from 24, opcode at +8, operands at +12 and +16, executed flag +4 and result +0.
168fn thins b edit uchar BN k ulong op ulong a ulong to void
169o = add add PO 24 mul k 44
170p32 b add o 8 op
171p32 b add o 12 a
172end
173
174fn thgo s edit ulong SN b edit uchar BN n ulong to void
175p32 b PO 1
176p32 b add PO 8 44
177p32 b add PO 20 n
178ctl s b hsub s 545260819 1432
179end
180
181fn thok b edit uchar BN n ulong to bool
182for k ulong n
183o = add add PO 24 mul k 44
184if or ne g32 b add o 4 1 ne g32 b o 0
185ret false
186end
187end
188ret true
189end
190
191fn thread s edit ulong SN b edit uchar BN to void
192nt = get s 25
193ns = get s 26
194for k ulong ns
195q = add nt mul k 2
196tg = g32 b add add add PO 24 mul q 44 16
197if ge tg nt
198done s 999
199ret
200end
201if eq g32 b add add add PO 24 mul tg 44 16 1
202v = g32 b add add add PO 24 mul add q 1 44 16
203if ge v 2147483648
204done s 999
205ret
206end
207scal s v
208done s 0
209ret
210end
211end
212done s 3
213end
214
215note nvmlDeviceGetTemperature (nvml.h:5571), sensor s 3 must be NVML_TEMPERATURE_GPU (0, nvml.h:936).
216note Pass 1 counts targets (opcode 256) and sensors (1280); pass 2 asks each target's type (257) and
217note each sensor's target (1312) and reading (5376). The first sensor on a GPU-type (1) target
218note answers; none is NOT_SUPPORTED (3). More than 32 instructions or a negative reading is UNKNOWN.
219fn temp s edit ulong SN b edit uchar BN to void
220pc = get s 22
221if eq pc 0
222e = pre s
223if ne e 0
224done s e
225ret
226end
227if ne get s 3 0
228done s 2
229ret
230end
231bzero b PO 1432
232thins b 0 256 0
233thins b 1 1280 0
234thgo s b 2
235put s 22 1
236ret
237end
238e = chk s b 28
239if ne e 0
240done s e
241ret
242end
243if eq pc 2
244if not thok b add get s 25 mul get s 26 2
245done s 999
246ret
247end
248thread s b
249ret
250end
251if not thok b 2
252done s 999
253ret
254end
255nt = g32 b add PO 36
256ns = g32 b add PO 80
257if gt add nt mul ns 2 32
258done s 999
259ret
260end
261if eq ns 0
262done s 3
263ret
264end
265put s 25 nt
266put s 26 ns
267bzero b PO 1432
268for t ulong nt
269thins b t 257 t
270end
271for k ulong ns
272thins b add nt mul k 2 1312 k
273thins b add add nt mul k 2 1 5376 k
274end
275thgo s b add nt mul ns 2
276put s 22 2
277end
278
279note nvmlDeviceGetMemoryInfo (nvml.h:6330): NV2080_CTRL_CMD_FB_GET_INFO_V2 (0x20801303,
280note ctrl2080fb.h:459), 1028-byte params: count 0, then {index, data} pairs from 4. Asks IS_ZERO_FB
281note (0x36), TOTAL_RAM_SIZE (0x08) and HEAP_FREE (0x16), both in KiB (ctrl2080fb.h:310,328,360). A GPU
282note without framebuffer is NOT_SUPPORTED (nvml.h:6352-6354). Output nvmlMemory_t {total, free, used}.
283fn mem s edit ulong SN b edit uchar BN to void
284if eq get s 22 0
285e = pre s
286if ne e 0
287done s e
288ret
289end
290bzero b PO 1028
291p32 b PO 3
292p32 b add PO 4 54
293p32 b add PO 12 8
294p32 b add PO 20 22
295ctl s b hsub s 545264387 1028
296put s 22 1
297ret
298end
299e = chk s b 28
300if ne e 0
301done s e
302ret
303end
304if or or ne g32 b add PO 4 54 ne g32 b add PO 12 8 ne g32 b add PO 20 22
305done s 999
306ret
307end
308if ne g32 b add PO 8 0
309done s 3
310ret
311end
312t = g32 b add PO 16
313f = g32 b add PO 24
314if gt f t
315done s 999
316ret
317end
318p64 b OO mul t 1024
319p64 b add OO 8 mul f 1024
320p64 b add OO 16 mul sub t f 1024
321put s 7 24
322done s 0
323end
324
325note nvmlDeviceGetUtilizationRates (nvml.h:6671): the newest PERFMON sample as in native/smi/util.om,
326note NV2080_CTRL_CMD_PERF_GET_GPUMON_PERFMON_UTIL_SAMPLES_V2 (0x20802096, ctrl2080perf.h:447-592),
327note 55888-byte params: type 2 at 0, bufSize 55872 at 4, count 8, tracker 12, 72 samples of 776 from
328note 16; newest at (tracker+71) mod 72, fb.util +8, gr.util +136 in percent*100. An empty ring or a
329note malformed reply is UNKNOWN (999). Output nvmlUtilization_t {gpu, memory} in percent.
330fn util s edit ulong SN b edit uchar BN to void
331if eq get s 22 0
332e = pre s
333if ne e 0
334done s e
335ret
336end
337bzero b PO 55888
338put b PO 2
339p32 b add PO 4 55872
340ctl s b hsub s 545267862 55888
341put s 22 1
342ret
343end
344e = chk s b 28
345if ne e 0
346done s e
347ret
348end
349c = g32 b add PO 8
350r = g32 b add PO 12
351if or gt c 72 ge r 72
352done s 999
353ret
354end
355o = add add PO 16 mul rem add r 71 72 776
356if or eq c 0 eq g64 b o 0
357done s 999
358ret
359end
360g = g32 b add o 136
361m = g32 b add o 8
362if or gt g 10000 gt m 10000
363done s 999
364ret
365end
366p32 b OO div g 100
367p32 b add OO 4 div m 100
368put s 7 8
369done s 0
370end
371
372note One NV2080_CTRL_CMD_GPU_GET_PID_INFO (0x2080018e, ctrl2080gpu.h:3590-3666) chunk of at most 200
373note PIDs from s 23: count at 0, 72-byte entries from 8: pid 0, index 4 = 0 (VIDEO_MEMORY_USAGE),
374note result 8, memPrivate 16.
375fn pchunk s edit ulong SN b edit uchar BN to void
376k = get s 23
377m = sub get s 25 k
378if gt m 200
379m = 200
380end
381bzero b PO 14408
382p32 b PO m
383for i ulong m
384p32 b add add PO 8 mul i 72 g32 b add OO mul add k i 24
385end
386put s 26 m
387ctl s b hsub s 545259918 14408
388put s 22 2
389end
390
391fn pdone s edit ulong SN b edit uchar BN to void
392e = chk s b 28
393if ne e 0
394done s e
395ret
396end
397k = get s 23
398for i ulong get s 26
399o = add add PO 8 mul i 72
400q = add OO mul add k i 24
401if ne g32 b o g32 b q
402done s 999
403ret
404end
405if ne g32 b add o 8 0
406p64 b add q 8 18446744073709551615
407else
408p64 b add q 8 g64 b add o 16
409end
410end
411k = add k get s 26
412put s 23 k
413if lt k get s 25
414pchunk s b
415ret
416end
417scal s k
418put s 7 mul k 24
419done s 0
420end
421
422note nvmlDeviceGetComputeRunningProcesses_v3 (nvml.h:6944-6985): s 2 infoCount non-null, s 3 its
423note value, s 4 infos non-null (may be null only when s 3 is 0). NV2080_CTRL_CMD_GPU_GET_PIDS
424note (0x2080018d, ctrl2080gpu.h:3521-3540), 3812-byte params: idType 0 (CLASS), id 4 = the compute
425note class, count 8 (at most 950), pids from 12. More PIDs than s 3 is INSUFFICIENT_SIZE with the
426note count stored. Output nvmlProcessInfo_t {pid 0, usedGpuMemory 8, gpuInstanceId 16,
427note computeInstanceId 20} (nvml.h:301-312), ids 0xFFFFFFFF without MIG; a failed PID entry reports
428note usedGpuMemory NVML_VALUE_NOT_AVAILABLE (nvml.h:155).
429fn procs s edit ulong SN b edit uchar BN to void
430pc = get s 22
431if eq pc 2
432pdone s b
433ret
434end
435if eq pc 0
436e = pre s
437if ne e 0
438done s e
439ret
440end
441if and eq get s 4 0 ne get s 3 0
442done s 2
443ret
444end
445c = get s dv get s 1 6
446if eq c 0
447done s 3
448ret
449end
450bzero b PO 3812
451p32 b add PO 4 c
452ctl s b hsub s 545259917 3812
453put s 22 1
454ret
455end
456e = chk s b 28
457if ne e 0
458done s e
459ret
460end
461n = g32 b add PO 8
462if gt n 950
463done s 999
464ret
465end
466if gt n get s 3
467scal s n
468done s 7
469ret
470end
471if eq n 0
472scal s 0
473done s 0
474ret
475end
476for i ulong n
477q = add OO mul i 24
478p32 b q g32 b add add PO 12 mul i 4
479p64 b add q 8 0
480p32 b add q 16 4294967295
481p32 b add q 20 4294967295
482end
483put s 25 n
484put s 23 0
485pchunk s b
486end