aien-yardsticks
aien-dev/aien-yardsticks
Brought by drakestapleton
aien-yardsticks / yardsticks / prime-drag-race-cuda / sieve_cuda.cu
| 1 | // aien-cuda-mirror: outside CUDA yardstick that mirrors the AIEN native GPU kernel algorithm exactly. |
| 2 | // One thread per 32-bit output word, loop over odd base primes, no atomics. See README.md. |
| 3 | #include <cuda_runtime.h> |
| 4 | |
| 5 | #include <stdint.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | extern "C" { |
| 11 | #include "prime_race_impl.h" |
| 12 | } |
| 13 | |
| 14 | #include "build_info.h" |
| 15 | |
| 16 | #define BLOCK 256 |
| 17 | |
| 18 | struct PrimeEnt { uint32_t p, magic, k0; }; |
| 19 | |
| 20 | static __global__ void sieve_words(uint32_t *out, uint32_t nwords, uint32_t odd_count, |
| 21 | const PrimeEnt *primes, uint32_t nprimes) { |
| 22 | uint32_t w = blockIdx.x * blockDim.x + threadIdx.x; |
| 23 | if (w >= nwords) return; |
| 24 | uint32_t base = 32u * w; |
| 25 | uint32_t acc = 0; |
| 26 | for (uint32_t i = 0; i < nprimes; i++) { |
| 27 | PrimeEnt e = primes[i]; |
| 28 | if (e.k0 >= base + 32u) break; |
| 29 | uint32_t j; |
| 30 | if (e.k0 >= base) { |
| 31 | j = e.k0 - base; |
| 32 | } else { |
| 33 | uint32_t d = base - e.k0; |
| 34 | uint32_t q = __umulhi(d, e.magic); |
| 35 | uint32_t r = d - q * e.p; |
| 36 | if ((int32_t)r < 0) r += e.p; // magic = ceil(2^32/p) can overshoot q by one |
| 37 | if (r >= e.p) r -= e.p; |
| 38 | j = (r == 0) ? 0u : e.p - r; |
| 39 | } |
| 40 | while (j < 32u) { acc |= 1u << j; j += e.p; } |
| 41 | } |
| 42 | if (w == 0) acc |= 1u; // number 1 is not prime |
| 43 | uint32_t tail = 0xffffffffu; |
| 44 | if (w == nwords - 1 && (odd_count & 31u)) tail = (1u << (odd_count & 31u)) - 1u; |
| 45 | out[w] = ~acc & tail; |
| 46 | } |
| 47 | |
| 48 | struct State { |
| 49 | uint64_t limit, odd_count; |
| 50 | uint32_t nwords32, nprimes, grid; |
| 51 | uint32_t *d_out; |
| 52 | PrimeEnt *d_primes; |
| 53 | uint32_t *h_out; |
| 54 | PrimeEnt *h_primes; |
| 55 | uint8_t *small; // host small sieve scratch |
| 56 | uint32_t cap_primes; |
| 57 | uint64_t sqrt_limit; |
| 58 | cudaEvent_t ev0, ev1; |
| 59 | double diag_kernel_ns; |
| 60 | }; |
| 61 | |
| 62 | static int cu(cudaError_t e, const char *what) { |
| 63 | if (e != cudaSuccess) { fprintf(stderr, "cuda error in %s: %s\n", what, cudaGetErrorString(e)); return 1; } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static uint64_t isqrt64(uint64_t n) { |
| 68 | uint64_t lo = 0, hi = 1ull << 21, r = 0; |
| 69 | while (lo <= hi) { uint64_t m = lo + (hi - lo) / 2; if (m * m <= n) { r = m; lo = m + 1; } else hi = m - 1; } |
| 70 | return r; |
| 71 | } |
| 72 | |
| 73 | static void cuda_teardown(void *vs) { |
| 74 | State *s = (State *)vs; |
| 75 | if (!s) return; |
| 76 | if (s->d_out) cudaFree(s->d_out); |
| 77 | if (s->d_primes) cudaFree(s->d_primes); |
| 78 | if (s->ev0) cudaEventDestroy(s->ev0); |
| 79 | if (s->ev1) cudaEventDestroy(s->ev1); |
| 80 | free(s->h_out); free(s->h_primes); free(s->small); |
| 81 | free(s); |
| 82 | } |
| 83 | |
| 84 | static int cuda_setup(void **state, uint64_t limit) { |
| 85 | if (limit > (1ull << 33) - 2) { fprintf(stderr, "limit too large for 32-bit indices\n"); return 1; } |
| 86 | State *s = (State *)calloc(1, sizeof *s); |
| 87 | if (!s) return 1; |
| 88 | s->limit = limit; |
| 89 | s->odd_count = pr_odd_count(limit); |
| 90 | s->nwords32 = (uint32_t)((s->odd_count + 31) / 32); |
| 91 | s->grid = (s->nwords32 + BLOCK - 1) / BLOCK; |
| 92 | s->sqrt_limit = isqrt64(limit); |
| 93 | s->cap_primes = (uint32_t)(s->sqrt_limit / 2 + 2); |
| 94 | size_t nw = s->nwords32 ? s->nwords32 : 1; |
| 95 | s->h_out = (uint32_t *)calloc(nw, 4); |
| 96 | s->h_primes = (PrimeEnt *)calloc(s->cap_primes, sizeof(PrimeEnt)); |
| 97 | s->small = (uint8_t *)malloc(s->sqrt_limit + 2); |
| 98 | *state = s; |
| 99 | if (!s->h_out || !s->h_primes || !s->small) return 1; |
| 100 | if (cu(cudaSetDevice(0), "cudaSetDevice")) return 1; |
| 101 | if (cu(cudaMalloc(&s->d_out, nw * 4), "cudaMalloc out")) return 1; |
| 102 | if (cu(cudaMalloc(&s->d_primes, s->cap_primes * sizeof(PrimeEnt)), "cudaMalloc primes")) return 1; |
| 103 | if (cu(cudaEventCreate(&s->ev0), "event0") || cu(cudaEventCreate(&s->ev1), "event1")) return 1; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int cuda_pass(void *vs, uint64_t limit) { |
| 108 | State *s = (State *)vs; |
| 109 | (void)limit; |
| 110 | // timed work: host odd primes <= isqrt(limit) plus division magics, then H2D, launch, D2H |
| 111 | uint64_t sq = s->sqrt_limit; |
| 112 | memset(s->small, 1, sq + 1); |
| 113 | uint32_t np = 0; |
| 114 | for (uint64_t p = 3; p <= sq; p += 2) { |
| 115 | if (!s->small[p]) continue; |
| 116 | for (uint64_t m = p * p; m <= sq; m += 2 * p) s->small[m] = 0; |
| 117 | s->h_primes[np].p = (uint32_t)p; |
| 118 | s->h_primes[np].magic = (uint32_t)(((1ull << 32) + p - 1) / p); |
| 119 | s->h_primes[np].k0 = (uint32_t)((p * p - 1) / 2); |
| 120 | np++; |
| 121 | } |
| 122 | s->nprimes = np; |
| 123 | if (s->nwords32 == 0) return 0; |
| 124 | if (np && cu(cudaMemcpy(s->d_primes, s->h_primes, np * sizeof(PrimeEnt), cudaMemcpyHostToDevice), "H2D primes")) return 1; |
| 125 | if (cu(cudaEventRecord(s->ev0), "ev0")) return 1; |
| 126 | sieve_words<<<s->grid, BLOCK>>>(s->d_out, s->nwords32, (uint32_t)s->odd_count, s->d_primes, np); |
| 127 | if (cu(cudaGetLastError(), "kernel launch")) return 1; |
| 128 | if (cu(cudaEventRecord(s->ev1), "ev1")) return 1; |
| 129 | // result must be host-resident: the D2H copy is counted in the pass |
| 130 | if (cu(cudaMemcpy(s->h_out, s->d_out, (size_t)s->nwords32 * 4, cudaMemcpyDeviceToHost), "D2H out")) return 1; |
| 131 | if (cu(cudaDeviceSynchronize(), "sync")) return 1; |
| 132 | if (cu(cudaGetLastError(), "post-run")) return 1; |
| 133 | float ms = 0; |
| 134 | if (cu(cudaEventElapsedTime(&ms, s->ev0, s->ev1), "elapsed")) return 1; |
| 135 | s->diag_kernel_ns += (double)ms * 1e6; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int cuda_export(void *vs, uint64_t limit, uint64_t *out) { |
| 140 | State *s = (State *)vs; |
| 141 | size_t n64 = pr_words64(limit); |
| 142 | for (size_t i = 0; i < n64; i++) { |
| 143 | uint64_t lo = s->h_out[2 * i]; |
| 144 | uint64_t hi = (2 * i + 1 < s->nwords32) ? s->h_out[2 * i + 1] : 0; |
| 145 | out[i] = lo | (hi << 32); |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static uint64_t cuda_threads(void *vs) { State *s = (State *)vs; return (uint64_t)s->grid * BLOCK; } |
| 151 | |
| 152 | static int cuda_detail(void *vs, FILE *f) { |
| 153 | State *s = (State *)vs; |
| 154 | int rt = 0, drv = 0; |
| 155 | cudaRuntimeGetVersion(&rt); |
| 156 | cudaDriverGetVersion(&drv); |
| 157 | fprintf(f, "{\"grid\":%u,\"block\":%d,\"words32\":%u,\"nprimes\":%u," |
| 158 | "\"cuda_runtime\":%d,\"cuda_driver\":%d," |
| 159 | "\"diag_kernel_ns_total\":%.0f,\"diag_kernel_ns_note\":\"sum of cudaEvent kernel-only times, diagnostic, not the score\"," |
| 160 | "\"result_copy\":\"D2H of full output counted in every pass\"," |
| 161 | "\"nvcc_version\":\"%s\",\"nvcc_flags\":\"%s\",\"omega_commit\":\"%s\",\"yardsticks_commit\":\"%s\"}", |
| 162 | s->grid, BLOCK, s->nwords32, s->nprimes, rt, drv, s->diag_kernel_ns, |
| 163 | YS_NVCC_VERSION, YS_NVCC_FLAGS, YS_OMEGA_COMMIT, YS_YARD_COMMIT); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int main(int argc, char **argv) { |
| 168 | static const pr_impl impl = { |
| 169 | "cuda-mirror", "other", "no", 1, "linux-hosted-gb10-cuda", |
| 170 | cuda_setup, cuda_pass, cuda_export, cuda_threads, cuda_detail, cuda_teardown, |
| 171 | }; |
| 172 | return pr_main(argc, argv, &impl); |
| 173 | } |