okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1fb3e9a32
main
1// E00 capability probe: peer access, GPU<->GPU / H2D / D2H transfer latency+bandwidth at plan sizes, RAM bandwidth. 2// build: nvcc -O2 -std=c++17 e00_probe.cu -o e00_probe -lpthread ; run: ./e00_probe > e00.json 3#include <cuda_runtime.h> 4#include <cstdio> 5#include <cstdlib> 6#include <cstring> 7#include <chrono> 8#include <thread> 9#include <vector> 10#include <algorithm> 11using Clk = std::chrono::steady_clock; 12static double now_us() { return std::chrono::duration<double, std::micro>(Clk::now().time_since_epoch()).count(); } 13#define CK(x) do { cudaError_t e = (x); if (e != cudaSuccess) { fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, cudaGetErrorString(e)); exit(2);} } while (0) 14static const size_t SIZES[] = {4096, 16384, 65536, 262144, 1 << 20, 8 << 20, 64 << 20}; 15 16template <class F> double med(F f, int reps) { 17 f(); f(); 18 std::vector<double> v; 19 for (int i = 0; i < reps; ++i) { double t = now_us(); f(); v.push_back(now_us() - t); } 20 std::sort(v.begin(), v.end()); 21 return v[v.size() / 2]; 22} 23int main() { 24 int n = 0; CK(cudaGetDeviceCount(&n)); 25 printf("{\n \"gpus\": ["); 26 for (int d = 0; d < n; ++d) { 27 cudaDeviceProp p; CK(cudaGetDeviceProperties(&p, d)); 28 size_t fr, tot; CK(cudaSetDevice(d)); CK(cudaMemGetInfo(&fr, &tot)); 29 printf("%s{\"index\":%d,\"name\":\"%s\",\"sm\":%d,\"sms\":%d,\"free_mib\":%zu,\"total_mib\":%zu,\"pci_bus\":%d}", d ? "," : "", d, p.name, p.major * 10 + p.minor, p.multiProcessorCount, fr >> 20, tot >> 20, p.pciBusID); 30 } 31 printf("],\n \"peer\": ["); 32 bool first = true; 33 for (int a = 0; a < n; ++a) for (int b = 0; b < n; ++b) if (a != b) { 34 int can = 0; cudaDeviceGetP2PAttribute(&can, cudaDevP2PAttrAccessSupported, a, b); 35 int nat = 0; cudaDeviceGetP2PAttribute(&nat, cudaDevP2PAttrNativeAtomicSupported, a, b); 36 printf("%s{\"from\":%d,\"to\":%d,\"access\":%d,\"native_atomics\":%d}", first ? "" : ",", a, b, can, nat); first = false; 37 } 38 printf("],\n \"transfers\": ["); 39 first = true; 40 auto emit = [&](const char* kind, int a, int b, size_t sz, double us) { 41 double gbs = (double) sz / us / 1e3; 42 printf("%s\n {\"kind\":\"%s\",\"a\":%d,\"b\":%d,\"bytes\":%zu,\"median_us\":%.2f,\"gb_s\":%.3f}", first ? "" : ",", kind, a, b, sz, us, gbs); first = false; 43 }; 44 size_t maxs = 64 << 20; 45 void* h = nullptr; CK(cudaMallocHost(&h, maxs)); memset(h, 1, maxs); 46 std::vector<void*> dbuf(n), dbuf2(n); 47 std::vector<cudaStream_t> st(n); 48 for (int d = 0; d < n; ++d) { CK(cudaSetDevice(d)); CK(cudaMalloc(&dbuf[d], maxs)); CK(cudaMalloc(&dbuf2[d], maxs)); CK(cudaStreamCreateWithFlags(&st[d], cudaStreamNonBlocking)); } 49 for (int d = 0; d < n; ++d) { 50 CK(cudaSetDevice(d)); 51 for (size_t sz : SIZES) { 52 emit("h2d_pinned", d, -1, sz, med([&] { CK(cudaMemcpyAsync(dbuf[d], h, sz, cudaMemcpyHostToDevice, st[d])); CK(cudaStreamSynchronize(st[d])); }, 30)); 53 emit("d2h_pinned", d, -1, sz, med([&] { CK(cudaMemcpyAsync(h, dbuf[d], sz, cudaMemcpyDeviceToHost, st[d])); CK(cudaStreamSynchronize(st[d])); }, 30)); 54 } 55 cudaStream_t s2; CK(cudaStreamCreateWithFlags(&s2, cudaStreamNonBlocking)); 56 double us = med([&] { CK(cudaMemcpyAsync(dbuf[d], h, maxs / 2, cudaMemcpyHostToDevice, st[d])); CK(cudaMemcpyAsync((char*) h + maxs / 2, dbuf2[d], maxs / 2, cudaMemcpyDeviceToHost, s2)); CK(cudaStreamSynchronize(st[d])); CK(cudaStreamSynchronize(s2)); }, 10); 57 emit("h2d_d2h_bidir_total", d, -1, maxs, us); 58 } 59 for (int a = 0; a < n; ++a) for (int b = 0; b < n; ++b) if (a != b) { 60 int can = 0; cudaDeviceCanAccessPeer(&can, a, b); 61 if (can) { CK(cudaSetDevice(a)); cudaDeviceEnablePeerAccess(b, 0); cudaGetLastError(); } 62 CK(cudaSetDevice(a)); 63 for (size_t sz : SIZES) 64 emit(can ? "p2p_peer_memcpy" : "gpu_to_gpu_memcpy_staged", a, b, sz, med([&] { CK(cudaMemcpyPeerAsync(dbuf2[b], b, dbuf[a], a, sz, st[a])); CK(cudaStreamSynchronize(st[a])); }, 30)); 65 } 66 if (n >= 2) { 67 CK(cudaSetDevice(0)); cudaDeviceEnablePeerAccess(1, 0); cudaGetLastError(); CK(cudaSetDevice(1)); cudaDeviceEnablePeerAccess(0, 0); cudaGetLastError(); 68 double us = med([&] { CK(cudaMemcpyPeerAsync(dbuf2[1], 1, dbuf[0], 0, maxs, st[0])); CK(cudaMemcpyPeerAsync(dbuf2[0], 0, dbuf[1], 1, maxs, st[1])); CK(cudaStreamSynchronize(st[0])); CK(cudaStreamSynchronize(st[1])); }, 10); 69 emit("p2p_bidir_total_bytes", 0, 1, 2 * maxs, us); 70 void* h2 = nullptr; CK(cudaMallocHost(&h2, maxs)); 71 us = med([&] { CK(cudaMemcpyAsync(dbuf[0], h, maxs, cudaMemcpyHostToDevice, st[0])); CK(cudaMemcpyAsync(dbuf[1], h2, maxs, cudaMemcpyHostToDevice, st[1])); CK(cudaStreamSynchronize(st[0])); CK(cudaStreamSynchronize(st[1])); }, 10); 72 emit("h2d_both_gpus_total_bytes", 0, 1, 2 * maxs, us); 73 } 74 printf("\n ],\n"); 75 size_t N = (512u << 20) / sizeof(double); 76 std::vector<double> A(N, 1.0), Bv(N, 2.0), C(N, 0.0); 77 printf(" \"ram_triad_gb_s\": {"); 78 unsigned hw = std::thread::hardware_concurrency(); 79 bool f2 = true; 80 for (unsigned t : {1u, 2u, 4u, 8u, 16u, hw}) { 81 if (t > hw) continue; 82 double best = 1e30; 83 for (int r = 0; r < 3; ++r) { 84 double t0 = now_us(); 85 std::vector<std::thread> th; 86 for (unsigned i = 0; i < t; ++i) th.emplace_back([&, i] { size_t lo = N * i / t, hi = N * (i + 1) / t; for (size_t k = lo; k < hi; ++k) C[k] = A[k] + 3.0 * Bv[k]; }); 87 for (auto& x : th) x.join(); 88 best = std::min(best, now_us() - t0); 89 } 90 printf("%s\"%u\": %.1f", f2 ? "" : ", ", t, 3.0 * N * sizeof(double) / best / 1e3); f2 = false; 91 } 92 printf("},\n \"hw_threads\": %u\n}\n", hw); 93 return 0; 94}