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 / tests / share / chip-share.om

222 lines · 4630 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Chip gate for native/share/share.om: two processes, two RM clients, one buffer.
3note Compose after native/nvrm/context.om and native/share/share.om, then fn main returning cmain.
4note make test only compiles this unit. Running it opens /dev/nvidia*: authorized GPU window only.
5note The parent forks before either process opens a device, so each has its own RM client.
6note Parent (owner): nvfull, nvalloc 65536, fill, nvexport, send the 24-byte record.
7note Child (importer): nvfull, import read-write and read-only, verify the owner's bytes through
8note both, write a marker through the read-write import, then acknowledge.
9note Parent sees the child's marker in its own mapping (zero copy), frees its buffer, says so.
10note Child re-verifies both imports after the owner's free, drops them, stops, exits 0.
11note Exit codes: 0 pass; 20..32 owner step; 40..53 importer step; 60..64 pipe, fork, reap.
12note stdout names the failing step.
13note Sizes (Wolfram): 2^16 65536 (16 pages); wait 10*10^9 ns 10000000000.
14text cpass "ZERO_COPY_SHARE_CHIP_PASS\n"
15text cstep "ZERO_COPY_SHARE_CHIP_FAIL step "
16text cnl "\n"
17global cfds uchar 16
18global cbuf uchar 4096
19global cbyte uchar 1
20global cstat uchar 4
21fn cdigit fd int v ulong to void
22if ge v 10
23cdigit fd div v 10
24end
25put cbyte 0 uchar add 48 rem v 10
26w = write fd cbyte 0 1
27end
28fn cfail step int to int
29w = write 1 cstep 0 len cstep
30cdigit 1 ulong step
31w = write 1 cnl 0 len cnl
32ret step
33end
34fn cfd off ulong to int
35x = ulong 0
36for i ulong 4
37x = or x shl ulong get cfds add off i mul i 8
38end
39ret int x
40end
41note Pattern byte for offset o of a buffer: 13*o + 90 mod 256.
42fn cfill v long size ulong to bool
43off = ulong 0
44while lt off size bound 4096
45for j ulong 4096
46put cbuf j uchar and add mul add off j 13 90 255
47end
48if ne mapwrite v off cbuf 0 4096 4096
49ret false
50end
51off = add off 4096
52end
53ret true
54end
55fn cverify v long size ulong marker bool to bool
56off = ulong 0
57while lt off size bound 4096
58if ne mapread v off cbuf 0 4096 4096
59ret false
60end
61for j ulong 4096
62want = uchar and add mul add off j 13 90 255
63if and marker eq add off j 0
64want = 165
65end
66if ne get cbuf j want
67ret false
68end
69end
70off = add off 4096
71end
72ret true
73end
74note Read exactly n bytes from fd within the wait budget: into nvshrec when rec, else one byte to cbyte.
75fn cget fd int rec bool n ulong to bool
76got = ulong 0
77while lt got n bound 64
78r = waitio fd 1 10000000000
79if le r 0
80ret false
81end
82if rec
83r = read fd nvshrec got sub n got
84else
85r = read fd cbyte 0 1
86end
87if le r 0
88ret false
89end
90got = add got ulong r
91end
92ret true
93end
94fn cput fd int b uchar to bool
95put cbyte 0 b
96ret eq write fd cbyte 0 1 1
97end
98fn cowner tox int rx int to int
99if not nvfull
100ret cfail 20
101end
102m = nvalloc 65536 false
103if le m 0
104ret cfail 21
105end
106if not cfill m 65536
107ret cfail 22
108end
109if not nvexport m
110ret cfail 23
111end
112if ne write tox nvshrec 0 24 24
113ret cfail 24
114end
115if not cget rx false 1
116ret cfail 25
117end
118if ne get cbyte 0 1
119ret cfail 26
120end
121note Zero copy: the child's write through its import is in the owner's pages.
122if not cverify m 65536 true
123ret cfail 27
124end
125if not nvdrop m
126ret cfail 28
127end
128if not cput tox 2
129ret cfail 29
130end
131if not cget rx false 1
132ret cfail 30
133end
134if ne get cbyte 0 3
135ret cfail 31
136end
137if not nvstop
138ret cfail 32
139end
140ret 0
141end
142fn cimport rx int tox int to int
143if not cget rx true 24
144ret cfail 40
145end
146if not nvfull
147ret cfail 41
148end
149rw = nvimport 65536 false
150if le rw 0
151ret cfail 42
152end
153ro = nvimport 65536 true
154if le ro 0
155ret cfail 43
156end
157if or not cverify rw 65536 false not cverify ro 65536 false
158ret cfail 44
159end
160put cbuf 0 165
161if ne mapwrite rw 0 cbuf 0 1 1
162ret cfail 45
163end
164note The read-only import sees the write made through the read-write one.
165if not cverify ro 65536 true
166ret cfail 46
167end
168if not cput tox 1
169ret cfail 47
170end
171if not cget rx false 1
172ret cfail 48
173end
174if ne get cbyte 0 2
175ret cfail 49
176end
177note The owner has freed its buffer; the duplicates keep the pages alive.
178if or not cverify rw 65536 true not cverify ro 65536 true
179ret cfail 50
180end
181if or not nvdrop ro not nvdrop rw
182ret cfail 51
183end
184if not nvstop
185ret cfail 52
186end
187if not cput tox 3
188ret cfail 53
189end
190ret 0
191end
192fn cmain to int
193r = pipefds cfds 0 8 524288
194if ne r 0
195ret cfail 60
196end
197r = pipefds cfds 8 8 524288
198if ne r 0
199ret cfail 61
200end
201pid = fork
202if lt pid 0
203ret cfail 62
204end
205if eq pid 0
206note Child: reads pipe 1, writes pipe 2.
207ret cimport cfd 0 cfd 12
208end
209st = cowner cfd 4 cfd 8
210if ne st 0
211ret st
212end
213r = waitpid int pid cstat 0 len cstat 0
214if ne r pid
215ret cfail 63
216end
217if or ne get cstat 0 0 ne get cstat 1 0
218ret cfail 64
219end
220w = write 1 cpass 0 len cpass
221ret 0
222end