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