okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1// 0.1.43 5.2: do two streams move host->device data faster than one (both copy engines)? And what does one layer's 2// worth of expert-sized copies cost end to end (issue, copy, host function, sync)? 3// nvcc -O2 -o h2d_bench research/tools/ab-harnesses-2026-10/h2d_bench.cu && ./h2d_bench [device] 4#include <cuda_runtime.h> 5#include <algorithm> 6#include <chrono> 7#include <cstdio> 8#include <cstdlib> 9#include <vector> 10 11#define CK(x) do { cudaError_t e = (x); if (e != cudaSuccess) { std::printf("%s:%d %s\n", __FILE__, __LINE__, cudaGetErrorString(e)); std::exit(1); } } while (0) 12using Clock = std::chrono::steady_clock; 13static double ms_since(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); } 14 15int main(int argc, char** argv) { 16 int dev = argc > 1 ? std::atoi(argv[1]) : 0; 17 CK(cudaSetDevice(dev)); 18 cudaDeviceProp p; 19 CK(cudaGetDeviceProperties(&p, dev)); 20 std::printf("%s asyncEngineCount=%d\n", p.name, p.asyncEngineCount); 21 const size_t blob = 1382400; // a Q2_0 expert 22 const size_t nb = 256; // blobs in the arena 23 const size_t arena_bytes = nb * blob; 24 uint8_t* host = nullptr; 25 // like the engine's arena: plain pages registered with cudaHostRegister (PORTABLE) 26 host = (uint8_t*) aligned_alloc(2 << 20, (arena_bytes + (2 << 20) - 1) / (2 << 20) * (2 << 20)); 27 for (size_t i = 0; i < arena_bytes; i += 4096) host[i] = (uint8_t) i; 28 CK(cudaHostRegister(host, arena_bytes, cudaHostRegisterPortable)); 29 uint8_t* dbuf; 30 CK(cudaMalloc(&dbuf, 2 * 64 * blob)); 31 cudaStream_t s[2]; 32 for (int i = 0; i < 2; ++i) CK(cudaStreamCreateWithFlags(&s[i], cudaStreamNonBlocking)); 33 34 // 1. throughput: N blobs, one stream vs two streams (alternate blobs) 35 for (int nstreams = 1; nstreams <= 2; ++nstreams) { 36 double best = 1e9; 37 for (int rep = 0; rep < 7; ++rep) { 38 auto t0 = Clock::now(); 39 for (size_t i = 0; i < nb; ++i) CK(cudaMemcpyAsync(dbuf + (i % 64) * blob, host + i * blob, blob, cudaMemcpyHostToDevice, s[i % nstreams])); 40 for (int i = 0; i < nstreams; ++i) CK(cudaStreamSynchronize(s[i])); 41 best = std::min(best, ms_since(t0)); 42 } 43 std::printf("throughput %d stream(s): %.3f ms for %zu blobs = %.2f GB/s\n", nstreams, best, nb, arena_bytes / best / 1e6); 44 } 45 // 1b. the same with each stream copying one big contiguous range 46 for (int nstreams = 1; nstreams <= 2; ++nstreams) { 47 double best = 1e9; 48 for (int rep = 0; rep < 7; ++rep) { 49 auto t0 = Clock::now(); 50 size_t half = 64 * blob / nstreams; 51 for (int i = 0; i < nstreams; ++i) CK(cudaMemcpyAsync(dbuf + i * half, host + i * half, half, cudaMemcpyHostToDevice, s[i])); 52 for (int i = 0; i < nstreams; ++i) CK(cudaStreamSynchronize(s[i])); 53 best = std::min(best, ms_since(t0)); 54 } 55 std::printf("big range %d stream(s): %.3f ms for %zu bytes = %.2f GB/s\n", nstreams, best, 64 * blob, 64 * blob / best / 1e6); 56 } 57 // 2. per-layer latency: k blobs, then a host function, then wait for it (the engine's flag B) 58 for (int k : {1, 2, 4, 8, 16}) { 59 for (int nstreams = 1; nstreams <= 2; ++nstreams) { 60 std::vector<double> v; 61 for (int rep = 0; rep < 200; ++rep) { 62 static volatile int flag; 63 flag = 0; 64 auto t0 = Clock::now(); 65 for (int i = 0; i < k; ++i) CK(cudaMemcpyAsync(dbuf + i * blob, host + ((rep * 13 + i * 7) % nb) * blob, blob, cudaMemcpyHostToDevice, s[i % nstreams])); 66 if (nstreams == 2) { 67 cudaEvent_t ev; 68 CK(cudaEventCreateWithFlags(&ev, cudaEventDisableTiming)); 69 CK(cudaEventRecord(ev, s[1])); 70 CK(cudaStreamWaitEvent(s[0], ev, 0)); 71 CK(cudaEventDestroy(ev)); 72 } 73 CK(cudaLaunchHostFunc(s[0], [](void*) { flag = 1; }, nullptr)); 74 while (!flag) {} 75 v.push_back(ms_since(t0)); 76 } 77 std::sort(v.begin(), v.end()); 78 std::printf("layer of %2d blobs, %d stream(s): median %.3f ms min %.3f p90 %.3f (ideal at 11 GB/s %.3f)\n", k, nstreams, v[v.size() / 2], v[0], v[v.size() * 9 / 10], k * blob / 11e6); 79 } 80 } 81 return 0; 82}