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 / om_qmd_sweep_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Host differential for om/qmd/bw_qmd.om. No device, no /dev/nvidia*. |
| 3 | * Cited constants are WolframLanguageEvaluator results, not hand arithmetic: |
| 4 | * 16^^00800002 -> 8388610 |
| 5 | * 16^^013f0000 -> 20905984 |
| 6 | * 16^^2f5003a4 -> 793772964 |
| 7 | * BitOr[BitAnd[16^^04b44808, BitNot[16^^7ff]], 8] -> 78923784 |
| 8 | * BitOr[BitAnd[16^^04b44808, BitNot[16^^7ff]], 256] -> 78924032 |
| 9 | * 16^^26420000 -> 641859584 |
| 10 | * 16^^00fffdc0 -> 16776640 |
| 11 | * With CUDAPP_MIGRATION_PENDING_QMD_DIFF the shell aborts on any C mismatch. |
| 12 | * QMD_DIFF_FLIP (red) xors a written word; the first packing call must abort. |
| 13 | */ |
| 14 | #include "omega_blackwell_qmd.h" |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | static int g_checks, g_failed; |
| 20 | |
| 21 | #define CHECK(cond) do { \ |
| 22 | g_checks++; \ |
| 23 | if (!(cond)) { \ |
| 24 | g_failed++; \ |
| 25 | printf("FAIL %s:%d\n", __FILE__, __LINE__); \ |
| 26 | return 1; \ |
| 27 | } \ |
| 28 | } while (0) |
| 29 | |
| 30 | static OmegaBlackwellQmdConfig base(void) { |
| 31 | OmegaBlackwellQmdConfig c; |
| 32 | memset(&c, 0, sizeof c); |
| 33 | c.code_va = 0x1000; |
| 34 | c.cbank_va = 0x2000; |
| 35 | c.scratch_va = 0x3000; |
| 36 | c.sem_va = 0x4000; |
| 37 | c.qmd0_va = 0x5000; |
| 38 | c.qmd1_va = 0x6000; |
| 39 | c.threads_x = 128; |
| 40 | c.threads_y = 1; |
| 41 | c.grid_x = 1; |
| 42 | c.grid_y = 1; |
| 43 | c.num_elements = 128; |
| 44 | c.gpr_count = 32; |
| 45 | c.shared_bytes = 1024; |
| 46 | return c; |
| 47 | } |
| 48 | |
| 49 | static int pack1(const OmegaBlackwellQmdConfig *c, uint32_t q[OMEGA_BW_QMD_WORDS]) { |
| 50 | memset(q, 0xa5, sizeof(uint32_t) * OMEGA_BW_QMD_WORDS); |
| 51 | return omega_blackwell_build_qmd1(q, c); |
| 52 | } |
| 53 | |
| 54 | static int cited(void) { |
| 55 | uint32_t q0[OMEGA_BW_QMD_WORDS], q1[OMEGA_BW_QMD_WORDS], cb[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 56 | OmegaBlackwellQmdConfig c = base(); |
| 57 | memset(q0, 0, sizeof q0); |
| 58 | CHECK(omega_blackwell_build_qmd0(q0, 0x5000, 0x6000) == 0); |
| 59 | CHECK(q0[4] == 8388610u); |
| 60 | CHECK(q0[12] == (uint32_t)(0x6000ull >> 8)); |
| 61 | CHECK(pack1(&c, q1) == 0); |
| 62 | CHECK(q1[4] == 20905984u); |
| 63 | CHECK(q1[14] == 793772964u); |
| 64 | CHECK((q1[36] & 0x7ffu) == 8u); |
| 65 | CHECK(q1[36] == 78923784u); |
| 66 | CHECK(omega_blackwell_verify_qmd_invariants(q1) == 0); |
| 67 | /* Wrong field offset: move the version word one slot earlier. */ |
| 68 | q1[13] = q1[14]; q1[14] = 0; |
| 69 | CHECK(omega_blackwell_verify_qmd_invariants(q1) == -1); |
| 70 | q1[14] = q1[13]; q1[13] = 0; |
| 71 | q1[37] ^= 1u; |
| 72 | CHECK(omega_blackwell_verify_qmd_invariants(q1) == -1); |
| 73 | CHECK(omega_blackwell_build_cbank_driver(cb, 0x2000) == 0); |
| 74 | CHECK(cb[0] == 641859584u); |
| 75 | CHECK(cb[223] == 16776640u); |
| 76 | CHECK(cb[68] == 0u); |
| 77 | CHECK(cb[69] == 0x2000u); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int boundaries(void) { |
| 82 | static const uint32_t txs[] = {0, 1, 63, 64, 65, 255, 256, 1024, 65535, 65536}; |
| 83 | static const uint32_t tys[] = {0, 1, 65535}; |
| 84 | static const uint32_t tpbs[] = {0, 1, 32, 128}; |
| 85 | static const uint32_t gxs[] = {0, 1, 0xffffffffu}; |
| 86 | static const uint32_t gys[] = {0, 1, 7}; |
| 87 | static const uint32_t gws[] = {0, 1, 9}; |
| 88 | static const uint32_t nes[] = {0, 1, 63, 64, 65, 100000u, 0xffffffffu}; |
| 89 | static const uint32_t gprs[] = {0, 1, 15, 16, 17, 511, 512}; |
| 90 | static const uint32_t shs[] = {0, 1, 127, 128, 129, 1024, 32767, 32768, 32769, 0xffffffffu}; |
| 91 | static const uint64_t vas[] = {0, 1, 0xff, 0x100, 0xffffffffull, 0x100000000ull, ~0ull}; |
| 92 | uint32_t q[OMEGA_BW_QMD_WORDS]; |
| 93 | OmegaBlackwellQmdConfig c; |
| 94 | size_t i; |
| 95 | |
| 96 | for (i = 0; i < sizeof txs / sizeof txs[0]; i++) { |
| 97 | c = base(); |
| 98 | c.threads_x = txs[i]; |
| 99 | CHECK(pack1(&c, q) == 0); |
| 100 | CHECK(omega_blackwell_verify_qmd_invariants(q) == 0); |
| 101 | } |
| 102 | for (i = 0; i < sizeof tys / sizeof tys[0]; i++) { |
| 103 | c = base(); |
| 104 | c.threads_y = tys[i]; |
| 105 | CHECK(pack1(&c, q) == 0); |
| 106 | } |
| 107 | for (i = 0; i < sizeof tpbs / sizeof tpbs[0]; i++) { |
| 108 | c = base(); |
| 109 | c.threads_x = 0; |
| 110 | c.threads_per_block = tpbs[i]; |
| 111 | CHECK(pack1(&c, q) == 0); |
| 112 | } |
| 113 | for (i = 0; i < sizeof gxs / sizeof gxs[0]; i++) { |
| 114 | c = base(); |
| 115 | c.grid_x = gxs[i]; |
| 116 | CHECK(pack1(&c, q) == 0); |
| 117 | } |
| 118 | for (i = 0; i < sizeof gys / sizeof gys[0]; i++) { |
| 119 | c = base(); |
| 120 | c.grid_y = gys[i]; |
| 121 | CHECK(pack1(&c, q) == 0); |
| 122 | } |
| 123 | for (i = 0; i < sizeof gws / sizeof gws[0]; i++) { |
| 124 | c = base(); |
| 125 | c.grid_x = 0; |
| 126 | c.grid_width = gws[i]; |
| 127 | CHECK(pack1(&c, q) == 0); |
| 128 | } |
| 129 | for (i = 0; i < sizeof nes / sizeof nes[0]; i++) { |
| 130 | c = base(); |
| 131 | c.grid_x = 0; |
| 132 | c.grid_width = 0; |
| 133 | c.num_elements = nes[i]; |
| 134 | CHECK(pack1(&c, q) == 0); |
| 135 | } |
| 136 | for (i = 0; i < sizeof gprs / sizeof gprs[0]; i++) { |
| 137 | c = base(); |
| 138 | c.gpr_count = gprs[i]; |
| 139 | CHECK(pack1(&c, q) == 0); |
| 140 | } |
| 141 | for (i = 0; i < sizeof shs / sizeof shs[0]; i++) { |
| 142 | c = base(); |
| 143 | c.shared_bytes = shs[i]; |
| 144 | int want = shs[i] > 32768u ? -1 : 0; |
| 145 | CHECK(pack1(&c, q) == want); |
| 146 | if (want == 0) { |
| 147 | uint32_t field = shs[i] == 0 ? 8u : (shs[i] + 127u) / 128u; |
| 148 | CHECK((q[36] & 0x7ffu) == field); |
| 149 | } |
| 150 | } |
| 151 | for (i = 0; i < sizeof vas / sizeof vas[0]; i++) { |
| 152 | c = base(); |
| 153 | c.code_va = vas[i]; |
| 154 | c.cbank_va = vas[i] + 0x100; |
| 155 | c.scratch_va = vas[i] + 0x200; |
| 156 | c.sem_va = vas[i] + 0x300; |
| 157 | c.qmd0_va = vas[i] + 0x400; |
| 158 | c.qmd1_va = vas[i] + 0x500; |
| 159 | CHECK(pack1(&c, q) == 0); |
| 160 | uint32_t q0[OMEGA_BW_QMD_WORDS]; |
| 161 | memset(q0, 0, sizeof q0); |
| 162 | CHECK(omega_blackwell_build_qmd0(q0, c.qmd0_va, c.qmd1_va) == 0); |
| 163 | CHECK(q0[12] == (uint32_t)(c.qmd1_va >> 8)); |
| 164 | uint32_t cb[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 165 | memset(cb, 0x5a, sizeof cb); |
| 166 | CHECK(omega_blackwell_build_cbank_driver(cb, c.cbank_va) == 0); |
| 167 | CHECK(cb[69] == (uint32_t)c.cbank_va); |
| 168 | CHECK(cb[68] == (uint32_t)(c.cbank_va >> 32)); |
| 169 | } |
| 170 | CHECK(omega_blackwell_build_qmd0(NULL, 1, 2) == -1); |
| 171 | CHECK(omega_blackwell_build_qmd1(NULL, &c) == -1); |
| 172 | CHECK(omega_blackwell_build_qmd1(q, NULL) == -1); |
| 173 | CHECK(omega_blackwell_verify_qmd_invariants(NULL) == -1); |
| 174 | CHECK(omega_blackwell_build_cbank_driver(NULL, 1) == -1); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int cross(void) { |
| 179 | static const uint32_t txs[] = {0, 1, 64, 65535}; |
| 180 | static const uint32_t tpbs[] = {0, 32, 128}; |
| 181 | static const uint32_t gxs[] = {0, 1}; |
| 182 | static const uint32_t gws[] = {0, 3}; |
| 183 | static const uint32_t nes[] = {0, 63, 64, 65, 0xffffffffu}; |
| 184 | uint32_t q[OMEGA_BW_QMD_WORDS]; |
| 185 | for (size_t a = 0; a < 4; a++) |
| 186 | for (size_t b = 0; b < 3; b++) |
| 187 | for (size_t d = 0; d < 2; d++) |
| 188 | for (size_t e = 0; e < 2; e++) |
| 189 | for (size_t f = 0; f < 5; f++) { |
| 190 | OmegaBlackwellQmdConfig c = base(); |
| 191 | c.threads_x = txs[a]; |
| 192 | c.threads_per_block = tpbs[b]; |
| 193 | c.grid_x = gxs[d]; |
| 194 | c.grid_width = gws[e]; |
| 195 | c.num_elements = nes[f]; |
| 196 | CHECK(pack1(&c, q) == 0); |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static uint32_t rnd(uint32_t *s) { |
| 202 | *s = *s * 1664525u + 1013904223u; |
| 203 | return *s; |
| 204 | } |
| 205 | |
| 206 | static int random_cases(void) { |
| 207 | uint32_t s = 0xC0FFEEU; |
| 208 | uint32_t q[OMEGA_BW_QMD_WORDS]; |
| 209 | uint32_t args[16]; |
| 210 | uint32_t cb[OMEGA_BW_CBANK_DRIVER_WORDS]; |
| 211 | for (int n = 0; n < 10000; n++) { |
| 212 | OmegaBlackwellQmdConfig c = base(); |
| 213 | c.threads_x = rnd(&s); |
| 214 | c.threads_y = rnd(&s) & 0xffffu; |
| 215 | c.threads_per_block = rnd(&s) & 0x1ffu; |
| 216 | c.grid_x = rnd(&s); |
| 217 | c.grid_y = rnd(&s) & 7u; |
| 218 | c.grid_width = rnd(&s) & 0x3ffu; |
| 219 | c.num_elements = rnd(&s); |
| 220 | c.shared_bytes = rnd(&s) & 0x1ffffu; |
| 221 | c.gpr_count = rnd(&s) & 0x3ffu; |
| 222 | c.code_va = ((uint64_t)rnd(&s) << 32) | rnd(&s); |
| 223 | c.cbank_va = ((uint64_t)rnd(&s) << 32) | rnd(&s); |
| 224 | int want = c.shared_bytes > 32768u ? -1 : 0; |
| 225 | CHECK(pack1(&c, q) == want); |
| 226 | if (want == 0) CHECK(omega_blackwell_verify_qmd_invariants(q) == 0); |
| 227 | memset(args, 0x11, sizeof args); |
| 228 | CHECK(omega_blackwell_build_cbank_args(args, c.code_va, c.cbank_va, c.scratch_va, c.num_elements) == 0); |
| 229 | CHECK(args[7] == 0x11111111u); |
| 230 | memset(cb, 0x22, sizeof cb); |
| 231 | CHECK(omega_blackwell_build_cbank_driver_2d(cb, c.cbank_va, c.threads_x, c.threads_y, c.grid_x, c.grid_y) == 0); |
| 232 | if (c.threads_x > 0) CHECK(cb[216] == c.threads_x); |
| 233 | CHECK(omega_blackwell_build_cbank_args_matmul(args, c.code_va, c.cbank_va, c.scratch_va, c.threads_x, c.threads_y, c.grid_x) == 0); |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* Source-derived partition. Every threshold the C oracle (src/omega_blackwell_qmd.c c_bw_*) compares a field |
| 239 | * against or cuts it at, each as v-1, v, v+1, plus 0 and UINT32_MAX, set one field at a time on base(), and every |
| 240 | * value pushed through every packer. u32 thresholds: 1 and 16 (gpr floor), 64 (tx default), 128 (smem7 rounding), |
| 241 | * 0x1ff/0x200 (gpr mask), 0x8000 (MAX_SHARED_BYTES), 0xffff/0x10000 (tx/ty masks), 0xffffffff-127 (ceil-div wrap of |
| 242 | * num_elements + tx - 1). u64 thresholds: 2^s for the shifts 4, 6, 8, 32, 36, 38 and the 57-bit field tops |
| 243 | * (36+21, 38+19), and 2^64-0x300 (the cbank_va + 0x300 wrap). Under CUDAPP_MIGRATION_PENDING_QMD_DIFF the shell |
| 244 | * aborts on the first word where .om and C differ. */ |
| 245 | static int pack_all(const OmegaBlackwellQmdConfig *c) { |
| 246 | uint32_t q[OMEGA_BW_QMD_WORDS], q0[OMEGA_BW_QMD_WORDS], cb[OMEGA_BW_CBANK_DRIVER_WORDS], args[16]; |
| 247 | int want = c->shared_bytes > 32768u ? -1 : 0; |
| 248 | CHECK(pack1(c, q) == want); |
| 249 | if (want == 0) CHECK(omega_blackwell_verify_qmd_invariants(q) == 0); |
| 250 | memset(q0, 0, sizeof q0); |
| 251 | CHECK(omega_blackwell_build_qmd0(q0, c->qmd0_va, c->qmd1_va) == 0); |
| 252 | memset(cb, 0x5a, sizeof cb); |
| 253 | CHECK(omega_blackwell_build_cbank_driver(cb, c->cbank_va) == 0); |
| 254 | memset(cb, 0x22, sizeof cb); |
| 255 | CHECK(omega_blackwell_build_cbank_driver_2d(cb, c->cbank_va, c->threads_x, c->threads_y, c->grid_x, c->grid_y) == 0); |
| 256 | memset(args, 0x11, sizeof args); |
| 257 | CHECK(omega_blackwell_build_cbank_args(args, c->code_va, c->cbank_va, c->scratch_va, c->num_elements) == 0); |
| 258 | CHECK(omega_blackwell_build_cbank_args_matmul(args, c->code_va, c->cbank_va, c->scratch_va, c->threads_x, |
| 259 | c->threads_y, c->grid_x) == 0); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int g_partition_cfgs; |
| 264 | static int partition(void) { |
| 265 | static const uint32_t t32[] = {1u, 16u, 64u, 128u, 0x1ffu, 0x200u, 0x8000u, 0xffffu, 0x10000u, 0xffffffffu - 127u}; |
| 266 | static const unsigned sh[] = {4, 6, 8, 32, 36, 38, 57}; |
| 267 | uint32_t v32[3 * sizeof t32 / sizeof t32[0] + 2]; |
| 268 | uint64_t v64[3 * (sizeof sh / sizeof sh[0] + 1) + 2]; |
| 269 | size_t n32 = 0, n64 = 0, i, f; |
| 270 | for (i = 0; i < sizeof t32 / sizeof t32[0]; i++) { |
| 271 | v32[n32++] = t32[i] - 1u; |
| 272 | v32[n32++] = t32[i]; |
| 273 | v32[n32++] = t32[i] + 1u; |
| 274 | } |
| 275 | v32[n32++] = 0u; |
| 276 | v32[n32++] = 0xffffffffu; |
| 277 | for (i = 0; i <= sizeof sh / sizeof sh[0]; i++) { |
| 278 | uint64_t v = i < sizeof sh / sizeof sh[0] ? 1ull << sh[i] : 0ull - 0x300ull; |
| 279 | v64[n64++] = v - 1u; |
| 280 | v64[n64++] = v; |
| 281 | v64[n64++] = v + 1u; |
| 282 | } |
| 283 | v64[n64++] = 0u; |
| 284 | v64[n64++] = ~0ull; |
| 285 | for (f = 0; f < 9; f++) |
| 286 | for (i = 0; i < n32; i++) { |
| 287 | OmegaBlackwellQmdConfig c = base(); |
| 288 | uint32_t *fld[9] = {&c.num_elements, &c.threads_per_block, &c.grid_width, &c.threads_x, &c.threads_y, |
| 289 | &c.grid_x, &c.grid_y, &c.gpr_count, &c.shared_bytes}; |
| 290 | *fld[f] = v32[i]; |
| 291 | /* the derived-default chains (tx from tpb, gx from grid_width or num_elements) only read a field when |
| 292 | * the ones before it are 0: cover both settings */ |
| 293 | if (pack_all(&c)) return 1; |
| 294 | c.threads_x = 0; |
| 295 | c.grid_x = 0; |
| 296 | if (f != 2) c.grid_width = 0; |
| 297 | if (pack_all(&c)) return 1; |
| 298 | g_partition_cfgs += 2; |
| 299 | } |
| 300 | for (f = 0; f < 6; f++) |
| 301 | for (i = 0; i < n64; i++) { |
| 302 | OmegaBlackwellQmdConfig c = base(); |
| 303 | uint64_t *fld[6] = {&c.code_va, &c.cbank_va, &c.scratch_va, &c.sem_va, &c.qmd0_va, &c.qmd1_va}; |
| 304 | *fld[f] = v64[i]; |
| 305 | if (pack_all(&c)) return 1; |
| 306 | g_partition_cfgs++; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | int main(void) { |
| 312 | if (cited()) return 1; |
| 313 | if (boundaries()) return 1; |
| 314 | if (cross()) return 1; |
| 315 | if (random_cases()) return 1; |
| 316 | if (partition()) return 1; |
| 317 | printf("om qmd sweep: %d checks PASS (partition %d configs)\n", g_checks, g_partition_cfgs); |
| 318 | return g_failed ? 1 : 0; |
| 319 | } |