interplane.Get Omega
Read in your language

gpu-head-to-head

aien-dev/gpu-head-to-head

Brought by drakestapleton

branch main Branches Tags History

gpu-head-to-head / coldstart / cs_cuda.c

232 lines · 12205 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 */
2/* coldstart/cs_cuda.c: the two NVIDIA arms of the cold-start study (coldstart/PROTOCOL.md).
3 *
4 * Built twice by coldstart/build.sh:
5 * -DCS_NVRTC arm NVRTC: the CUDA C++ source below -> sm_121 cubin with NVRTC (CUDA 13.0.88)
6 * -DCS_PTXC arm PTXC: the committed coldstart/vecadd.ptx -> sm_121 cubin with the static nvPTXCompiler
7 * Both then open the GPU, load the cubin and launch through the CUDA driver API (libcuda 580.173.02).
8 *
9 * The job is the job of CUDApp's examples/hello_gpu at the pinned commit: c = a + b over 1048576 floats,
10 * eight launches of 1024 blocks x 128 threads, inputs a[i] = i * 0.25, b[i] = 1 / (1 + i % 7), c poisoned
11 * with 0xFFC0FFEE, every element checked bit for bit against the CPU's own a + b.
12 *
13 * Measurement tool (a referee for the NVIDIA side), not product code: it has to call NVIDIA's C APIs.
14 *
15 * Output: one JSON line on stdout. Exit 0 = every element right, 1 = check failed, 2 = setup failed.
16 * Options: --skip-run (negative control: no launch, the check must fail), --emit-code FILE (write the
17 * cubin), --maps FILE (copy /proc/self/maps after the run), --ptx FILE (PTXC: the PTX to compile),
18 * --emit-ptx FILE (NVRTC: write compute_121 PTX for the same source and exit), --versions, --build-only (build and
19 * time the warm builds, no GPU opened), --warm N (0..10 warm builds after the cold path; run.py passes 0 to
20 * measured cold processes). */
21#include <cuda.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <time.h>
27#if defined(CS_NVRTC)
28#include <nvrtc.h>
29#define ARM "NVRTC"
30#elif defined(CS_PTXC)
31#include <nvPTXCompiler.h>
32#define ARM "PTXC"
33#else
34#error "build with -DCS_NVRTC or -DCS_PTXC"
35#endif
36
37#define N_TOTAL (1u << 20)
38#define THREADS 128u
39#define CHUNK (THREADS * 1024u)
40#define POISON 0xFFC0FFEEu
41#define WARM 10
42
43/* The CUDA C++ source of the NVRTC arm. coldstart/vecadd.ptx is NVRTC's compute_121 PTX for this same string. */
44#if defined(CS_NVRTC)
45static const char src[] =
46 "extern \"C\" __global__ void vecadd(const float*a,const float*b,float*c,unsigned n)"
47 "{unsigned i=blockIdx.x*blockDim.x+threadIdx.x;if(i<n)c[i]=a[i]+b[i];}";
48#endif
49
50static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; }
51static uint64_t fnv(const void *p, size_t n) { const uint8_t *b = p; uint64_t h = 0xcbf29ce484222325ull; for (size_t i = 0; i < n; i++) { h ^= b[i]; h *= 0x100000001b3ull; } return h; }
52static int write_file(const char *path, const void *p, size_t n) { FILE *f = fopen(path, "wb"); if (!f) return -1; size_t w = fwrite(p, 1, n, f); return (fclose(f) == 0 && w == n) ? 0 : -1; }
53static int copy_maps(const char *path) {
54 FILE *in = fopen("/proc/self/maps", "r"), *out = fopen(path, "w"); char buf[4096]; size_t k;
55 if (!in || !out) return -1;
56 while ((k = fread(buf, 1, sizeof buf, in)) > 0) fwrite(buf, 1, k, out);
57 fclose(in); return fclose(out);
58}
59
60#if defined(CS_NVRTC)
61static int compile(const char *opt, int want_ptx, char **out, size_t *n) {
62 nvrtcProgram p;
63 if (nvrtcCreateProgram(&p, src, "vecadd.cu", 0, NULL, NULL) != NVRTC_SUCCESS) return -1;
64 nvrtcResult e = nvrtcCompileProgram(p, 1, &opt);
65 if (e != NVRTC_SUCCESS) { fprintf(stderr, "cs: nvrtcCompileProgram: %s\n", nvrtcGetErrorString(e)); nvrtcDestroyProgram(&p); return -1; }
66 if ((want_ptx ? nvrtcGetPTXSize(p, n) : nvrtcGetCUBINSize(p, n)) != NVRTC_SUCCESS || *n == 0) { nvrtcDestroyProgram(&p); return -1; }
67 *out = malloc(*n);
68 if (!*out || (want_ptx ? nvrtcGetPTX(p, *out) : nvrtcGetCUBIN(p, *out)) != NVRTC_SUCCESS) { nvrtcDestroyProgram(&p); return -1; }
69 nvrtcDestroyProgram(&p);
70 return 0;
71}
72static int build(const char *ptx, char **out, size_t *n) { (void)ptx; return compile("--gpu-architecture=sm_121", 0, out, n); }
73#else
74static int build(const char *ptx, char **out, size_t *n) {
75 nvPTXCompilerHandle h;
76 const char *opt[] = { "--gpu-name=sm_121" };
77 if (nvPTXCompilerCreate(&h, strlen(ptx), ptx) != NVPTXCOMPILE_SUCCESS) return -1;
78 if (nvPTXCompilerCompile(h, 1, opt) != NVPTXCOMPILE_SUCCESS) { nvPTXCompilerDestroy(&h); return -1; }
79 if (nvPTXCompilerGetCompiledProgramSize(h, n) != NVPTXCOMPILE_SUCCESS || *n == 0) { nvPTXCompilerDestroy(&h); return -1; }
80 *out = malloc(*n);
81 if (!*out || nvPTXCompilerGetCompiledProgram(h, *out) != NVPTXCOMPILE_SUCCESS) { nvPTXCompilerDestroy(&h); return -1; }
82 nvPTXCompilerDestroy(&h);
83 return 0;
84}
85static char *slurp(const char *path) {
86 FILE *f = fopen(path, "rb"); if (!f) return NULL;
87 fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET);
88 char *b = malloc((size_t)n + 1); if (!b || fread(b, 1, (size_t)n, f) != (size_t)n) { fclose(f); return NULL; }
89 b[n] = 0; fclose(f); return b;
90}
91#endif
92
93#define CK(x) do { CUresult r_ = (x); if (r_ != CUDA_SUCCESS) { const char *s_ = NULL; cuGetErrorName(r_, &s_); \
94 fprintf(stderr, "cs: %s: %s\n", #x, s_ ? s_ : "?"); return 2; } } while (0)
95
96static int versions(void) {
97 int drv = 0; cuDriverGetVersion(&drv);
98#if defined(CS_NVRTC)
99 int ma = 0, mi = 0; nvrtcVersion(&ma, &mi);
100 printf("{\"arm\":\"%s\",\"cuDriverGetVersion\":%d,\"nvrtcVersion\":\"%d.%d\"}\n", ARM, drv, ma, mi);
101#else
102 unsigned ma = 0, mi = 0; nvPTXCompilerGetVersion(&ma, &mi);
103 printf("{\"arm\":\"%s\",\"cuDriverGetVersion\":%d,\"nvPTXCompilerGetVersion\":\"%u.%u\"}\n", ARM, drv, ma, mi);
104#endif
105 return 0;
106}
107
108static int nwarm = WARM; /* --warm N (0..WARM): measured cold processes use 0 so process time holds the cold path only */
109static int warm_builds(const char *ptx, uint64_t *warm) {
110 for (int r = 0; r < nwarm; r++) {
111 char *w = NULL; size_t wn = 0; uint64_t s = now_ns();
112 if (build(ptx, &w, &wn)) return -1;
113 warm[r] = now_ns() - s; free(w);
114 }
115 return 0;
116}
117static void print_warm(const uint64_t *warm) { for (int r = 0; r < nwarm; r++) printf("%s%llu", r ? "," : "", (unsigned long long)warm[r]); }
118
119int main(int argc, char **argv) {
120 const uint64_t t_main = now_ns();
121 int skip_run = 0, build_only = 0;
122 const char *emit_code = NULL, *maps = NULL, *ptx_path = "coldstart/vecadd.ptx";
123 for (int i = 1; i < argc; i++) {
124 if (!strcmp(argv[i], "--skip-run")) skip_run = 1;
125 else if (!strcmp(argv[i], "--build-only")) build_only = 1;
126 else if (!strcmp(argv[i], "--warm") && i + 1 < argc) { nwarm = atoi(argv[++i]); if (nwarm < 0 || nwarm > WARM) return 2; }
127 else if (!strcmp(argv[i], "--emit-code") && i + 1 < argc) emit_code = argv[++i];
128 else if (!strcmp(argv[i], "--maps") && i + 1 < argc) maps = argv[++i];
129 else if (!strcmp(argv[i], "--ptx") && i + 1 < argc) ptx_path = argv[++i];
130 else if (!strcmp(argv[i], "--versions")) return versions();
131#if defined(CS_NVRTC)
132 else if (!strcmp(argv[i], "--emit-ptx") && i + 1 < argc) {
133 char *p; size_t n;
134 if (compile("--gpu-architecture=compute_121", 1, &p, &n)) return 2;
135 return write_file(argv[i + 1], p, strlen(p)) ? 2 : 0;
136 }
137#endif
138 else { fprintf(stderr, "usage: cs_%s [--skip-run|--build-only] [--warm N] [--emit-code FILE] [--maps FILE] [--ptx FILE] [--versions]\n", ARM); return 2; }
139 }
140 (void)ptx_path;
141
142 /* prep: host inputs (and, for PTXC, the PTX text) in memory. Not compared between arms. */
143 const size_t bytes = (size_t)N_TOTAL * sizeof(float);
144 float *fa = aligned_alloc(64, bytes), *fb = aligned_alloc(64, bytes);
145 uint32_t *wc0 = aligned_alloc(64, bytes), *wc = aligned_alloc(64, bytes);
146 if (!fa || !fb || !wc0 || !wc) return 2;
147 for (uint32_t i = 0; i < N_TOTAL; i++) { fa[i] = (float)i * 0.25f; fb[i] = 1.0f / (float)(1 + i % 7); wc0[i] = POISON; }
148#if defined(CS_PTXC)
149 char *ptx = slurp(ptx_path);
150 if (!ptx) { fprintf(stderr, "cs: cannot read %s\n", ptx_path); return 2; }
151#else
152 const char *ptx = NULL;
153#endif
154 const uint64_t t_prep = now_ns();
155
156 /* build: kernel description in memory -> loadable device code in host memory */
157 char *cubin = NULL; size_t cubin_n = 0;
158 if (build(ptx, &cubin, &cubin_n)) { fprintf(stderr, "cs: build failed\n"); return 2; }
159 const uint64_t t_build = now_ns();
160
161 /* --build-only: no GPU is opened; the build, WARM more builds, the code written if asked */
162 if (build_only) {
163 uint64_t warm[WARM];
164 if (warm_builds(ptx, warm)) return 2;
165 if (emit_code && write_file(emit_code, cubin, cubin_n)) return 2;
166 printf("{\"arm\":\"%s\",\"build_only\":1,\"t_main_ns\":%llu,\"prep_ns\":%llu,\"build_ns\":%llu,\"warm_build_ns\":[", ARM,
167 (unsigned long long)t_main, (unsigned long long)(t_prep - t_main), (unsigned long long)(t_build - t_prep));
168 print_warm(warm);
169 printf("],\"code_bytes\":%zu,\"code_fnv\":\"%016llx\"}\n", cubin_n, (unsigned long long)fnv(cubin, cubin_n));
170 return 0;
171 }
172
173 /* open: no GPU state in this process -> a current context */
174 CUdevice dev; CUcontext ctx;
175 CK(cuInit(0)); CK(cuDeviceGet(&dev, 0)); CK(cuDevicePrimaryCtxRetain(&ctx, dev)); CK(cuCtxSetCurrent(ctx));
176 const uint64_t t_open = now_ns();
177
178 /* load: device code in host memory -> a launchable function */
179 CUmodule mod; CUfunction fn;
180 CK(cuModuleLoadData(&mod, cubin)); CK(cuModuleGetFunction(&fn, mod, "vecadd"));
181 const uint64_t t_load = now_ns();
182
183 /* data: three device buffers, inputs and the poisoned output copied in */
184 CUdeviceptr a, b, c;
185 CK(cuMemAlloc(&a, bytes)); CK(cuMemAlloc(&b, bytes)); CK(cuMemAlloc(&c, bytes));
186 CK(cuMemcpyHtoD(a, fa, bytes)); CK(cuMemcpyHtoD(b, fb, bytes)); CK(cuMemcpyHtoD(c, wc0, bytes));
187 const uint64_t t_data = now_ns();
188
189 /* run: eight launches on the default stream, then wait for all of them */
190 if (!skip_run) {
191 for (uint32_t base = 0; base < N_TOTAL; base += CHUNK) {
192 uint64_t off = (uint64_t)base * sizeof(float);
193 CUdeviceptr pa = a + off, pb = b + off, pc = c + off;
194 unsigned n = N_TOTAL - base < CHUNK ? N_TOTAL - base : CHUNK;
195 void *params[] = { &pa, &pb, &pc, &n };
196 CK(cuLaunchKernel(fn, (n + THREADS - 1) / THREADS, 1, 1, THREADS, 1, 1, 0, NULL, params, NULL));
197 }
198 }
199 CK(cuCtxSynchronize());
200 const uint64_t t_run = now_ns();
201
202 /* readback: the output to host memory, every element checked bit for bit */
203 CK(cuMemcpyDtoH(wc, c, bytes));
204 uint32_t wrong = 0, unwritten = 0;
205 for (uint32_t i = 0; i < N_TOTAL; i++) {
206 float want = fa[i] + fb[i];
207 if (wc[i] == POISON) unwritten++;
208 if (memcmp(&want, &wc[i], sizeof want) != 0) wrong++;
209 }
210 const uint64_t t_check = now_ns();
211
212 /* after the cold path: WARM more builds in the same process */
213 uint64_t warm[WARM];
214 if (warm_builds(ptx, warm)) return 2;
215 if (maps && copy_maps(maps)) return 2;
216 if (emit_code && write_file(emit_code, cubin, cubin_n)) return 2;
217 const uint64_t code_fnv = fnv(cubin, cubin_n), out_fnv = fnv(wc, bytes);
218
219 const uint64_t t_close0 = now_ns();
220 CK(cuMemFree(a)); CK(cuMemFree(b)); CK(cuMemFree(c)); CK(cuModuleUnload(mod)); CK(cuDevicePrimaryCtxRelease(dev));
221 const uint64_t t_close = now_ns();
222
223 printf("{\"arm\":\"%s\",\"t_main_ns\":%llu,\"prep_ns\":%llu,\"build_ns\":%llu,\"open_ns\":%llu,\"load_ns\":%llu,"
224 "\"data_ns\":%llu,\"run_ns\":%llu,\"check_ns\":%llu,\"close_ns\":%llu,\"warm_build_ns\":[",
225 ARM, (unsigned long long)t_main, (unsigned long long)(t_prep - t_main), (unsigned long long)(t_build - t_prep),
226 (unsigned long long)(t_open - t_build), (unsigned long long)(t_load - t_open), (unsigned long long)(t_data - t_load),
227 (unsigned long long)(t_run - t_data), (unsigned long long)(t_check - t_run), (unsigned long long)(t_close - t_close0));
228 print_warm(warm);
229 printf("],\"code_bytes\":%zu,\"code_fnv\":\"%016llx\",\"out_fnv\":\"%016llx\",\"wrong\":%u,\"unwritten\":%u,\"skip_run\":%d}\n",
230 cubin_n, (unsigned long long)code_fnv, (unsigned long long)out_fnv, wrong, unwritten, skip_run);
231 return wrong ? 1 : 0;
232}