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 / compat / cuda / fixture / vadd_drv.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* CUDApp#184 C0 reference application: an independently authored CUDA Driver API program. |
| 3 | * It stands in for an unchanged third-party CUDA executable; it is a qualification input, not |
| 4 | * CUDA++ or Omega implementation code. Written from the public CUDA Driver API reference |
| 5 | * (PROVENANCE class A). Built against the toolkit's cuda.h so the imported symbol variants are |
| 6 | * exactly what that header selects (compat/cuda/profile-c0-*.tsv records them). |
| 7 | * |
| 8 | * usage: vadd_drv <kernel.ptx> |
| 9 | * exit 0 VADD_U32 PASS, every element and every tail sentinel correct |
| 10 | * exit 1 wrong output (element or tail sentinel) |
| 11 | * exit 2 a Driver API call returned an error (call name and CUresult printed) |
| 12 | * exit 3 usage or PTX file error |
| 13 | * Contract fixed before implementation: N=4099 elements, 256 threads per block, 17 blocks, |
| 14 | * a[i] = i*2654435761 mod 2^32, b[i] = (2^32-1) - (7*i mod 2^32), c[i] = a[i]+b[i] mod 2^32, |
| 15 | * 64 tail sentinels 0xA5A5A5A5 after c[N-1] must be unchanged, checksum = sum c[i] as u64. */ |
| 16 | #include <cuda.h> |
| 17 | #include <stdio.h> |
| 18 | #include "../../../om/standalone-oracles/oracle.h" |
| 19 | |
| 20 | #define VADD_N 4099u |
| 21 | #define VADD_BLOCK 256u |
| 22 | #define VADD_TAIL 64u |
| 23 | #define VADD_SENTINEL 0xA5A5A5A5u |
| 24 | |
| 25 | static unsigned int ha[VADD_N]; |
| 26 | static unsigned int hb[VADD_N]; |
| 27 | static unsigned int hc[VADD_N + VADD_TAIL]; |
| 28 | static char ptx[16384]; |
| 29 | |
| 30 | static int check(CUresult rc, const char *call) |
| 31 | { |
| 32 | if (rc != CUDA_SUCCESS) { |
| 33 | printf("VADD_U32 CUDA_ERROR call=%s rc=%d\n", call, (int)rc); |
| 34 | return 1; |
| 35 | } |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | #define CALL(expr) do { if (check((expr), #expr)) return 2; } while (0) |
| 40 | |
| 41 | int main(int argc, char **argv) |
| 42 | { |
| 43 | CUdevice dev; |
| 44 | CUcontext ctx; |
| 45 | CUmodule mod; |
| 46 | CUfunction fn; |
| 47 | CUdeviceptr da, db, dc; |
| 48 | unsigned int n = VADD_N; |
| 49 | unsigned int grid = (unsigned)so_call(7,VADD_N,0,0,0); |
| 50 | unsigned long long sum = 0; |
| 51 | unsigned int bad = 0; |
| 52 | size_t len; |
| 53 | FILE *f; |
| 54 | void *params[4]; |
| 55 | |
| 56 | if (argc != 2) { |
| 57 | printf("VADD_U32 USAGE vadd_drv <kernel.ptx>\n"); |
| 58 | return 3; |
| 59 | } |
| 60 | f = fopen(argv[1], "rb"); |
| 61 | if (f == NULL) { |
| 62 | printf("VADD_U32 PTX_OPEN_FAILED\n"); |
| 63 | return 3; |
| 64 | } |
| 65 | len = fread(ptx, 1, sizeof(ptx) - 1u, f); |
| 66 | if (fclose(f) != 0 || len == 0 || len == sizeof(ptx) - 1u) { |
| 67 | printf("VADD_U32 PTX_READ_FAILED bytes=%zu\n", len); |
| 68 | return 3; |
| 69 | } |
| 70 | ptx[len] = '\0'; |
| 71 | for (unsigned int i = 0; i < VADD_N; i++) { |
| 72 | ha[i] = (unsigned)so_call(0,i,0,0,0); |
| 73 | hb[i] = (unsigned)so_call(1,i,0,0,0); |
| 74 | } |
| 75 | for (unsigned int i = 0; i < VADD_N + VADD_TAIL; i++) |
| 76 | hc[i] = VADD_SENTINEL; |
| 77 | |
| 78 | CALL(cuInit(0)); |
| 79 | CALL(cuDeviceGet(&dev, 0)); |
| 80 | CALL(cuCtxCreate(&ctx, NULL, 0, dev)); |
| 81 | CALL(cuModuleLoadData(&mod, ptx)); |
| 82 | CALL(cuModuleGetFunction(&fn, mod, "vadd_u32")); |
| 83 | CALL(cuMemAlloc(&da, sizeof(ha))); |
| 84 | CALL(cuMemAlloc(&db, sizeof(hb))); |
| 85 | CALL(cuMemAlloc(&dc, sizeof(hc))); |
| 86 | CALL(cuMemcpyHtoD(da, ha, sizeof(ha))); |
| 87 | CALL(cuMemcpyHtoD(db, hb, sizeof(hb))); |
| 88 | CALL(cuMemcpyHtoD(dc, hc, sizeof(hc))); |
| 89 | params[0] = &da; |
| 90 | params[1] = &db; |
| 91 | params[2] = &dc; |
| 92 | params[3] = &n; |
| 93 | CALL(cuLaunchKernel(fn, grid, 1, 1, VADD_BLOCK, 1, 1, 0, NULL, params, NULL)); |
| 94 | CALL(cuCtxSynchronize()); |
| 95 | CALL(cuMemcpyDtoH(hc, dc, sizeof(hc))); |
| 96 | for (unsigned int i = 0; i < VADD_N; i++) { |
| 97 | bad = so_bad(bad,so_eq(hc[i],so_call(2,ha[i],hb[i],0,0))); |
| 98 | sum = so_call(5,sum,hc[i],0,0); |
| 99 | } |
| 100 | for (unsigned int i = VADD_N; i < VADD_N + VADD_TAIL; i++) |
| 101 | bad = so_bad(bad,so_eq(hc[i],VADD_SENTINEL)); |
| 102 | CALL(cuMemFree(da)); |
| 103 | CALL(cuMemFree(db)); |
| 104 | CALL(cuMemFree(dc)); |
| 105 | CALL(cuModuleUnload(mod)); |
| 106 | CALL(cuCtxDestroy(ctx)); |
| 107 | if (bad != 0) { |
| 108 | printf("VADD_U32 FAIL n=%u grid=%u block=%u bad=%u checksum=%llu\n", n, grid, VADD_BLOCK, bad, sum); |
| 109 | return 1; |
| 110 | } |
| 111 | printf("VADD_U32 PASS n=%u grid=%u block=%u checksum=%llu\n", n, grid, VADD_BLOCK, sum); |
| 112 | return 0; |
| 113 | } |