okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3641d2f8d
main
1// tools/sm60_dp4a_check.cu - proves the sm_60 STRATA_DP4A fallback in include/strata/kernels/dp4a.hpp is 2// bit-exact with a plain signed byte-wise dot product, and times it against that byte loop. 3// 4// It includes the committed header, so the check covers the code the kernels compile, not a copy. On sm_61+ 5// STRATA_DP4A is the hardware __dp4a and the check still applies. No CMake target; build by hand: 6// 7// nvcc -O3 -arch=sm_60 -Iinclude tools/sm60_dp4a_check.cu -o sm60_dp4a_check && ./sm60_dp4a_check 8// 9// Exit status 0 = no mismatches and no CUDA error. 10#include <cstdint> 11#include <cstdio> 12#include <cuda_runtime.h> 13 14#include "strata/kernels/dp4a.hpp" 15 16// Reference: the signed-byte semantics __dp4a(int, int, int) has for these operands. 17__device__ __forceinline__ int byte_dp4a(const int a, const int b, const int c) { 18 const int8_t* a8 = (const int8_t*) &a; 19 const int8_t* b8 = (const int8_t*) &b; 20 return c + a8[0] * b8[0] + a8[1] * b8[1] + a8[2] * b8[2] + a8[3] * b8[3]; 21} 22 23// splitmix32: every output byte depends on the whole state, so all byte lanes see all byte pairs. 24__device__ uint32_t rnd(uint32_t& s) { 25 uint32_t z = (s += 0x9e3779b9u); 26 z = (z ^ (z >> 16)) * 0x85ebca6bu; 27 z = (z ^ (z >> 13)) * 0xc2b2ae35u; 28 return z ^ (z >> 16); 29} 30 31constexpr int BLOCKS = 1024, THREADS = 256, ITERS = 4096; // 1024 * 256 * 4096 = ~1.07e9 cases 32 33// Per thread: iterations 0-15 pair every edge byte (0x7f, 0x80, 0xff, 0x00) in a with every edge byte in b. 34// Iteration 16 is exhaustive across the grid: the 262,144 threads are exactly 4 lanes x 256 x 256, so every 35// (lane, a byte, b byte) combination is tested once, with random bytes in the other lanes. The rest is random. 36__global__ void check(unsigned long long* mismatches, int iters) { 37 const int edge[4] = {0x7f7f7f7f, (int) 0x80808080u, (int) 0xffffffffu, 0}; 38 const uint32_t t = blockIdx.x * blockDim.x + threadIdx.x; 39 uint32_t s = t * 0x2545f491u; 40 unsigned long long bad = 0; 41 for (int i = 0; i < iters; ++i) { 42 int a = (int) rnd(s), b = (int) rnd(s), c = (int) rnd(s); 43 if (i < 16) { a = edge[i & 3]; b = edge[i >> 2]; } 44 if (i == 16) { 45 const int sh = 8 * (t >> 16); 46 a = (int) (((uint32_t) a & ~(0xffu << sh)) | (((t >> 8) & 0xffu) << sh)); 47 b = (int) (((uint32_t) b & ~(0xffu << sh)) | ((t & 0xffu) << sh)); 48 } 49 bad += byte_dp4a(a, b, c) != STRATA_DP4A(a, b, c); 50 } 51 atomicAdd(mismatches, bad); 52} 53 54template <bool STRATA> 55__global__ void bench(int* out, int iters) { 56 int a = threadIdx.x * 0x01020304, b = blockIdx.x * 0x05060708 + 1, acc = 0; 57 for (int i = 0; i < iters; ++i) { 58 acc = STRATA ? STRATA_DP4A(a, b, acc) : byte_dp4a(a, b, acc); 59 a ^= acc; b += 0x01010101; 60 } 61 out[blockIdx.x * blockDim.x + threadIdx.x] = acc; 62} 63 64template <bool STRATA> 65float time_it(int* out) { 66 cudaEvent_t t0, t1; cudaEventCreate(&t0); cudaEventCreate(&t1); 67 bench<STRATA><<<BLOCKS, THREADS>>>(out, 1 << 12); // warm-up 68 cudaEventRecord(t0); 69 bench<STRATA><<<BLOCKS, THREADS>>>(out, 1 << 16); 70 cudaEventRecord(t1); cudaEventSynchronize(t1); 71 float ms; cudaEventElapsedTime(&ms, t0, t1); 72 cudaEventDestroy(t0); cudaEventDestroy(t1); 73 return ms; 74} 75 76int main() { 77 static_assert(BLOCKS * THREADS == 4 * 256 * 256, "iteration 16 needs one thread per (lane, a byte, b byte)"); 78 cudaDeviceProp prop; 79 cudaGetDeviceProperties(&prop, 0); 80 printf("device: %s (sm_%d%d)\n", prop.name, prop.major, prop.minor); 81 82 unsigned long long* d_bad; int* d_out; 83 cudaMalloc(&d_bad, sizeof(*d_bad)); cudaMemset(d_bad, 0, sizeof(*d_bad)); 84 cudaMalloc(&d_out, BLOCKS * THREADS * sizeof(int)); 85 check<<<BLOCKS, THREADS>>>(d_bad, ITERS); 86 unsigned long long bad = 0; 87 cudaError_t e = cudaMemcpy(&bad, d_bad, sizeof(bad), cudaMemcpyDeviceToHost); 88 float byte_ms = time_it<false>(d_out), strata_ms = time_it<true>(d_out); 89 printf("cases=%llu mismatches=%llu\n", (unsigned long long) BLOCKS * THREADS * ITERS, bad); 90 printf("byte loop %.1f ms STRATA_DP4A %.1f ms speedup %.2fx\n", byte_ms, strata_ms, byte_ms / strata_ms); 91 if (e == cudaSuccess) e = cudaGetLastError(); 92 printf("cuda: %s\n", cudaGetErrorString(e)); 93 cudaFree(d_bad); cudaFree(d_out); 94 return (bad == 0 && e == cudaSuccess) ? 0 : 1; 95}