okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
1.7 KiB41 linesraw
1// read-bandwidth probe: sum of a 256 MB buffer with float4 loads, a few launch shapes
2#include <cstdio>
3#include <cuda_runtime.h>
4__global__ void rd(const float4* __restrict__ p, size_t n, float* out) {
5    float s = 0;
6    for (size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t) gridDim.x * blockDim.x) {
7        float4 v = p[i];
8        s += v.x + v.y + v.z + v.w;
9    }
10    if (s == 1234.5f) out[0] = s;
11}
12__global__ void rd_unroll(const int4* __restrict__ p, size_t n, int* out) {
13    int s = 0;
14    size_t stride = (size_t) gridDim.x * blockDim.x;
15    size_t i = (size_t) blockIdx.x * blockDim.x + threadIdx.x;
16    for (; i + 3 * stride < n; i += 4 * stride) {
17        int4 a = p[i], b = p[i + stride], c = p[i + 2 * stride], d = p[i + 3 * stride];
18        s += a.x ^ a.y ^ a.z ^ a.w ^ b.x ^ b.y ^ b.z ^ b.w ^ c.x ^ c.y ^ c.z ^ c.w ^ d.x ^ d.y ^ d.z ^ d.w;
19    }
20    if (s == 1234) out[0] = s;
21}
22int main() {
23    const size_t bytes = 256u << 20, n = bytes / 16;
24    void* p; float* o;
25    cudaMalloc(&p, bytes); cudaMalloc(&o, 4);
26    cudaMemset(p, 1, bytes);
27    cudaEvent_t a, b; cudaEventCreate(&a); cudaEventCreate(&b);
28    for (int blocks : {112, 224, 448, 896, 3584, 14336}) {
29        for (int k = 0; k < 2; ++k) {
30            cudaEventRecord(a);
31            for (int it = 0; it < 20; ++it) {
32                if (k == 0) rd<<<blocks, 256>>>((const float4*) p, n, o);
33                else rd_unroll<<<blocks, 256>>>((const int4*) p, n, (int*) o);
34            }
35            cudaEventRecord(b); cudaEventSynchronize(b);
36            float ms; cudaEventElapsedTime(&ms, a, b);
37            printf("%s blocks %5d: %.1f GB/s\n", k == 0 ? "loop  " : "unroll", blocks, 20.0 * bytes / (ms / 1000) / 1e9);
38        }
39    }
40    return 0;
41}