Files
leleka/scripts/profile/h2d_max_probe.cu
T
Pavlo RatushnyiandClaude Sonnet 5.5 d84d465ff9
Linux / keyless-cuda-gate (push) Skipped
Rust port / port-fast (push) Failing after 9s
Rust port / port-typecheck (push) Failing after 9s
Rust port / port-oracles (push) Failing after 14s
Rust port / port-linux-tests (push) Failing after 18s
Linux / verify (push) Failing after 21s
Linux / keyless-cuda (push) Skipped
Linux / static-cuda-package (push) Skipped
Linux / hosted-cuda-package (push) Skipped
Linux / linux-static-cli (push) Skipped
Linux / linux-cli-container (push) Skipped
Linux / release-cli (push) Skipped
scripts/profile: h2d_max_probe (PCIe 5.0 x16 host-to-device ceiling: streams, copy size, source kind, SM copy kernel)
Measured on the 5090: 51.0 GB/s cudaHostAlloc 8-64 MiB, 51.8 GB/s THP + register 8 MiB, extra streams add nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 22:35:56 +01:00

84 lines
4.7 KiB
Plaintext

// Model-free probe: how close to the PCIe 5.0 x16 line rate (63 GB/s) can a host-to-device stream get?
// The expert stream measured ~50 GB/s with one stream of cudaMemcpyAsync (h2d_src_probe.cu). This tries what could add the rest:
// * N concurrent copy streams (the 5090 has more than one copy engine),
// * copy size (1, 2, 8, 64 MiB),
// * source kind (cudaHostAlloc vs anonymous THP + cudaHostRegister),
// * a copy kernel that reads mapped pinned host memory with 16-byte loads (SM-issued PCIe reads, no copy engine).
// nvcc -O2 -arch=sm_120 h2d_max_probe.cu -o h2d_max_probe ; ./h2d_max_probe
#include <cuda_runtime.h>
#include <sys/mman.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define CK(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { printf("CUDA error %s at %d: %s\n", #x, __LINE__, cudaGetErrorString(e_)); exit(1);} } while (0)
static const size_t HOST_BYTES = 8ull << 30; // pinned source
static const size_t DEV_BYTES = 4ull << 30; // destination
__global__ void copy_kernel(const uint4* __restrict__ src, uint4* __restrict__ dst, size_t n16) {
size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x;
size_t stride = (size_t)gridDim.x * blockDim.x;
for (; i < n16; i += stride) dst[i] = __ldcs(src + i);
}
static double dma(uint8_t* host, uint8_t* dev, size_t chunk, int nstreams, size_t total) {
std::vector<cudaStream_t> st(nstreams);
for (auto& s : st) CK(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking));
cudaEvent_t a, b; CK(cudaEventCreate(&a)); CK(cudaEventCreate(&b));
size_t n = total / chunk;
// warm-up (brings the link to its top speed state)
for (size_t k = 0; k < 64; ++k) CK(cudaMemcpyAsync(dev + (k * chunk) % (DEV_BYTES - chunk), host + (k * chunk) % (HOST_BYTES - chunk), chunk, cudaMemcpyHostToDevice, st[k % nstreams]));
CK(cudaDeviceSynchronize());
auto t0 = std::chrono::steady_clock::now();
for (size_t k = 0; k < n; ++k)
CK(cudaMemcpyAsync(dev + (k * chunk) % (DEV_BYTES - chunk), host + (k * chunk) % (HOST_BYTES - chunk), chunk, cudaMemcpyHostToDevice, st[k % nstreams]));
CK(cudaDeviceSynchronize());
double s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
for (auto& x : st) cudaStreamDestroy(x);
cudaEventDestroy(a); cudaEventDestroy(b);
return (double)n * chunk / s / 1e9;
}
static double kernel(const uint8_t* host_dev_ptr, uint8_t* dev, int blocks, int threads, size_t total) {
size_t n16 = total / 16;
for (int w = 0; w < 3; ++w) copy_kernel<<<blocks, threads>>>((const uint4*)host_dev_ptr, (uint4*)dev, n16);
CK(cudaDeviceSynchronize());
auto t0 = std::chrono::steady_clock::now();
for (int w = 0; w < 4; ++w) copy_kernel<<<blocks, threads>>>((const uint4*)host_dev_ptr, (uint4*)dev, n16);
CK(cudaDeviceSynchronize());
double s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
return 4.0 * total / s / 1e9;
}
int main() {
CK(cudaSetDevice(0));
cudaDeviceProp p; CK(cudaGetDeviceProperties(&p, 0));
printf("%s, %d SMs, PCIe gen %d x%d reported by NVML separately\n", p.name, p.multiProcessorCount, 0, 0);
uint8_t *dev; CK(cudaMalloc(&dev, DEV_BYTES));
const size_t total = 16ull << 30;
uint8_t* hostalloc; CK(cudaHostAlloc(&hostalloc, HOST_BYTES, cudaHostAllocPortable | cudaHostAllocMapped)); memset(hostalloc, 0x5a, HOST_BYTES);
printf("--- cudaHostAlloc (portable, mapped)\n");
for (size_t chunk : {1ull << 20, 2ull << 20, 8ull << 20, 64ull << 20})
for (int ns : {1, 2, 3, 4, 8})
printf("dma chunk %3zu MiB streams %d: %.1f GB/s\n", chunk >> 20, ns, dma(hostalloc, dev, chunk, ns, total));
void* raw = mmap(nullptr, HOST_BYTES + (2 << 20), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
uint8_t* thp = (uint8_t*)(((size_t)raw + (2 << 20) - 1) & ~((size_t)(2 << 20) - 1));
madvise(thp, HOST_BYTES, MADV_HUGEPAGE); memset(thp, 0x5a, HOST_BYTES);
CK(cudaHostRegister(thp, HOST_BYTES, cudaHostRegisterPortable));
printf("--- anonymous THP + cudaHostRegister(portable)\n");
for (size_t chunk : {2ull << 20, 8ull << 20})
for (int ns : {1, 2, 4})
printf("dma chunk %3zu MiB streams %d: %.1f GB/s\n", chunk >> 20, ns, dma(thp, dev, chunk, ns, total));
uint8_t* mapped; CK(cudaHostGetDevicePointer((void**)&mapped, hostalloc, 0));
printf("--- copy kernel reading mapped pinned memory (cudaHostAlloc)\n");
for (int blocks : {p.multiProcessorCount, p.multiProcessorCount * 2, p.multiProcessorCount * 4})
for (int threads : {256, 1024})
printf("kernel blocks %4d threads %4d: %.1f GB/s\n", blocks, threads, kernel(mapped, dev, blocks, threads, 2ull << 30));
return 0;
}