okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1// src/core/dflash2_launch_bench.cpp - what a dependent kernel costs inside a captured graph on this GPU: N tiny kernels in a chain, and N kernels of 256 blocks. 2// 3// dflash2_launch_bench [n=100] 4#include <cuda_runtime.h> 5 6#include <chrono> 7#include <cstdio> 8#include <cstdlib> 9 10__global__ void tiny(float* x, int n) { 11 const int i = blockIdx.x * blockDim.x + threadIdx.x; 12 if (i < n) x[i] = x[i] * 1.0001f + 1.0f; 13} 14 15int main(int argc, char** argv) { 16 const int n = argc > 1 ? std::atoi(argv[1]) : 100; 17 float* x; 18 cudaMalloc((void**) &x, 1 << 24); 19 cudaMemset(x, 0, 1 << 24); 20 cudaStream_t cs; 21 cudaStreamCreateWithFlags(&cs, cudaStreamNonBlocking); 22 cudaEvent_t a, b; 23 cudaEventCreate(&a); 24 cudaEventCreate(&b); 25 for (int blocks : {1, 64, 256, 1024}) { 26 cudaGraph_t g; 27 cudaGraphExec_t ge; 28 cudaStreamBeginCapture(cs, cudaStreamCaptureModeThreadLocal); 29 for (int i = 0; i < n; ++i) tiny<<<blocks, 256, 0, cs>>>(x, blocks * 256); 30 cudaStreamEndCapture(cs, &g); 31 cudaGraphInstantiate(&ge, g, 0); 32 cudaGraphLaunch(ge, cs); 33 cudaStreamSynchronize(cs); 34 cudaEventRecord(a, cs); 35 for (int it = 0; it < 50; ++it) cudaGraphLaunch(ge, cs); 36 cudaEventRecord(b, cs); 37 cudaEventSynchronize(b); 38 float ms; 39 cudaEventElapsedTime(&ms, a, b); 40 std::printf("graph of %d chained kernels, %4d blocks: %.2f us per kernel\n", n, blocks, 1000.0 * ms / 50 / n); 41 // one launch from an idle stream: event, launch, event, wait (what the drafter does each round) 42 { 43 double tot = 0, wall = 0; 44 for (int it = 0; it < 20; ++it) { 45 cudaStreamSynchronize(cs); 46 auto t0 = std::chrono::steady_clock::now(); 47 cudaEventRecord(a, cs); 48 cudaGraphLaunch(ge, cs); 49 cudaEventRecord(b, cs); 50 cudaStreamSynchronize(cs); 51 wall += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count(); 52 float m; 53 cudaEventElapsedTime(&m, a, b); 54 tot += m; 55 } 56 std::printf("single launch from idle, %d kernels, %4d blocks: events %.3f ms, wall %.3f ms\n", n, blocks, tot / 20, wall / 20); 57 } 58 // the same without a graph 59 cudaEventRecord(a, cs); 60 for (int it = 0; it < 5; ++it) 61 for (int i = 0; i < n; ++i) tiny<<<blocks, 256, 0, cs>>>(x, blocks * 256); 62 cudaEventRecord(b, cs); 63 cudaEventSynchronize(b); 64 cudaEventElapsedTime(&ms, a, b); 65 std::printf("stream of %d chained kernels, %4d blocks: %.2f us per kernel\n", n, blocks, 1000.0 * ms / 5 / n); 66 } 67 return 0; 68}