Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
180 lines
8.8 KiB
Plaintext
180 lines
8.8 KiB
Plaintext
// Host<->device copy bandwidth by source memory kind, copy size, number of streams and access pattern.
|
|
// mb_h2d [--file PATH] [--gib 2] [--total-mib 1536] [--quick]
|
|
// Source kinds: pinned (cudaHostAlloc), reg4k (anonymous 4 KiB pages + cudaHostRegister), regfile (read-only file mapping +
|
|
// cudaHostRegisterReadOnly; needs --file, the page cache is read into memory first), pageable (malloc'd, plain cudaMemcpyAsync).
|
|
// RESULT h2d src=... size=... streams=N pattern=seq|rand gbps=... host_us_per_call=...
|
|
// RESULT d2h ..., RESULT bidir ..., RESULT register ... (cost of cudaHostRegister / cudaHostAlloc per GiB)
|
|
#include <cuda_runtime.h>
|
|
#include <fcntl.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include "mb_common.h"
|
|
|
|
struct Source {
|
|
const char* name;
|
|
uint8_t* p = nullptr;
|
|
size_t bytes = 0;
|
|
int kind = 0; // 0 pinned, 1 reg4k, 2 regfile, 3 pageable
|
|
int fd = -1;
|
|
};
|
|
|
|
static bool make_source(Source& s, const char* name, int kind, size_t bytes, const char* file) {
|
|
s.name = name;
|
|
s.kind = kind;
|
|
s.bytes = bytes;
|
|
if (!host_alloc_allowed(bytes)) return false;
|
|
double t0 = now_s();
|
|
if (kind == 0) {
|
|
if (!CKS(cudaHostAlloc((void**)&s.p, bytes, cudaHostAllocPortable))) return false;
|
|
std::memset(s.p, 0x5a, bytes);
|
|
} else if (kind == 1 || kind == 3) {
|
|
void* p = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
if (p == MAP_FAILED) return false;
|
|
madvise(p, bytes, MADV_NOHUGEPAGE); // plain 4 KiB pages: the worst case for the page-table walk
|
|
s.p = (uint8_t*)p;
|
|
std::memset(s.p, 0x5a, bytes);
|
|
if (kind == 1) {
|
|
double t1 = now_s();
|
|
if (!CKS(cudaHostRegister(p, bytes, cudaHostRegisterPortable))) { munmap(p, bytes); s.p = nullptr; return false; }
|
|
std::printf("RESULT register kind=reg4k gib=%.1f seconds=%.3f gib_per_s=%.1f\n", bytes / 1073741824.0, now_s() - t1,
|
|
bytes / 1073741824.0 / (now_s() - t1));
|
|
}
|
|
} else if (kind == 2) {
|
|
if (!file) return false;
|
|
s.fd = open(file, O_RDONLY);
|
|
if (s.fd < 0) { std::perror("open"); return false; }
|
|
struct stat st;
|
|
fstat(s.fd, &st);
|
|
if ((size_t)st.st_size < bytes) { std::fprintf(stderr, "file smaller than %zu\n", bytes); return false; }
|
|
void* p = mmap(nullptr, bytes, PROT_READ, MAP_SHARED, s.fd, 0);
|
|
if (p == MAP_FAILED) { std::perror("mmap file"); return false; }
|
|
s.p = (uint8_t*)p;
|
|
volatile uint64_t acc = 0;
|
|
for (size_t i = 0; i < bytes; i += 4096) acc += s.p[i]; // fault the page cache in
|
|
double t1 = now_s();
|
|
if (!CKS(cudaHostRegister(p, bytes, cudaHostRegisterPortable | cudaHostRegisterReadOnly))) { munmap(p, bytes); s.p = nullptr; return false; }
|
|
std::printf("RESULT register kind=regfile gib=%.1f seconds=%.3f gib_per_s=%.1f\n", bytes / 1073741824.0, now_s() - t1,
|
|
bytes / 1073741824.0 / (now_s() - t1));
|
|
}
|
|
if (kind == 0) std::printf("RESULT register kind=hostalloc gib=%.1f seconds=%.3f\n", bytes / 1073741824.0, now_s() - t0);
|
|
return s.p != nullptr;
|
|
}
|
|
|
|
static void free_source(Source& s) {
|
|
if (!s.p) return;
|
|
if (s.kind == 0) cudaFreeHost(s.p);
|
|
else {
|
|
if (s.kind != 3) cudaHostUnregister(s.p);
|
|
munmap(s.p, s.bytes);
|
|
if (s.fd >= 0) close(s.fd);
|
|
}
|
|
s.p = nullptr;
|
|
s.fd = -1;
|
|
}
|
|
|
|
// Time `total` bytes moved in copies of `size` bytes over `ns` streams, round-robin; offsets sequential or random.
|
|
static double run_copy(bool h2d, Source& s, uint8_t* dev, size_t slots_per_stream, size_t size, int ns, bool rnd, size_t total,
|
|
cudaStream_t* st, double* host_us_per_call) {
|
|
const size_t ncopies = std::max<size_t>(ns, total / size);
|
|
const size_t region = s.bytes - size;
|
|
uint64_t rng = 88172645463325252ull;
|
|
std::vector<size_t> offs(ncopies);
|
|
for (size_t i = 0; i < ncopies; ++i) {
|
|
if (rnd) {
|
|
rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17;
|
|
offs[i] = (size_t)((rng >> 8) % (region / 4096)) * 4096;
|
|
} else {
|
|
offs[i] = (i * size) % region;
|
|
}
|
|
}
|
|
CK(cudaDeviceSynchronize());
|
|
const double t0 = now_s();
|
|
for (size_t i = 0; i < ncopies; ++i) {
|
|
const int k = (int)(i % ns);
|
|
uint8_t* d = dev + ((size_t)k * slots_per_stream + (i / ns) % slots_per_stream) * size;
|
|
if (h2d) CK(cudaMemcpyAsync(d, s.p + offs[i], size, cudaMemcpyHostToDevice, st[k]));
|
|
else CK(cudaMemcpyAsync(s.p + offs[i], d, size, cudaMemcpyDeviceToHost, st[k]));
|
|
}
|
|
const double t_enq = now_s();
|
|
CK(cudaDeviceSynchronize());
|
|
const double t1 = now_s();
|
|
if (host_us_per_call) *host_us_per_call = (t_enq - t0) / ncopies * 1e6;
|
|
return (double)ncopies * size / (t1 - t0) / 1e9;
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
const char* file = nullptr;
|
|
double gib = 2;
|
|
size_t total_mib = 1536;
|
|
bool quick = false;
|
|
for (int i = 1; i < argc; ++i) {
|
|
if (!std::strcmp(argv[i], "--file") && i + 1 < argc) file = argv[++i];
|
|
else if (!std::strcmp(argv[i], "--gib") && i + 1 < argc) gib = std::atof(argv[++i]);
|
|
else if (!std::strcmp(argv[i], "--total-mib") && i + 1 < argc) total_mib = (size_t)std::atoll(argv[++i]);
|
|
else if (!std::strcmp(argv[i], "--quick")) quick = true;
|
|
}
|
|
cudaDeviceProp prop;
|
|
CK(cudaGetDeviceProperties(&prop, 0));
|
|
int ro = 0;
|
|
cudaDeviceGetAttribute(&ro, cudaDevAttrHostRegisterReadOnlySupported, 0);
|
|
std::printf("# device %s sm_%d%d, read-only register supported=%d, copy engines=%d\n", prop.name, prop.major, prop.minor, ro, prop.asyncEngineCount);
|
|
const size_t bytes = (size_t)(gib * (1ull << 30));
|
|
const size_t total = total_mib << 20;
|
|
const size_t sizes[] = {65536, 262144, 682000, 2046400, 8u << 20, 32u << 20};
|
|
uint8_t* dev;
|
|
CK(cudaMalloc(&dev, 3 * 8 * (32u << 20)));
|
|
cudaStream_t st[3];
|
|
for (auto& s : st) CK(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking));
|
|
|
|
static const char* names[] = {"pinned", "reg4k", "regfile", "pageable"};
|
|
for (int kind = 0; kind < 4; ++kind) {
|
|
Source s;
|
|
if (!make_source(s, names[kind], kind, bytes, file)) { std::printf("# source %s unavailable\n", names[kind]); continue; }
|
|
for (int pass = 0; pass < (kind == 0 || kind == 2 ? 2 : 1); ++pass) { // pass 1 = random offsets
|
|
const bool rnd = pass == 1;
|
|
for (size_t size : sizes) {
|
|
if (rnd && size != 2046400 && size != 682000 && size != 65536) continue;
|
|
for (int ns = 1; ns <= 3; ++ns) {
|
|
if (quick && ns == 2) continue;
|
|
std::vector<double> r, hu;
|
|
for (int rep = 0; rep < 3; ++rep) {
|
|
double h = 0;
|
|
r.push_back(run_copy(true, s, dev, 8, size, ns, rnd, kind == 3 ? total / 2 : total, st, &h));
|
|
hu.push_back(h);
|
|
}
|
|
std::printf("RESULT h2d src=%s size=%zu streams=%d pattern=%s gbps=%.1f min=%.1f max=%.1f host_us_per_call=%.2f\n", s.name, size, ns,
|
|
rnd ? "rand" : "seq", median(r), *std::min_element(r.begin(), r.end()), *std::max_element(r.begin(), r.end()), median(hu));
|
|
std::fflush(stdout);
|
|
}
|
|
}
|
|
}
|
|
if (kind == 0) { // D2H and both directions at once, from the pinned buffer
|
|
for (size_t size : {(size_t)262144, (size_t)2046400, (size_t)(32u << 20)}) {
|
|
for (int ns = 1; ns <= 2; ++ns) {
|
|
std::vector<double> r;
|
|
for (int rep = 0; rep < 3; ++rep) r.push_back(run_copy(false, s, dev, 8, size, ns, false, total, st, nullptr));
|
|
std::printf("RESULT d2h src=pinned size=%zu streams=%d gbps=%.1f\n", size, ns, median(r));
|
|
}
|
|
}
|
|
// bidirectional: H2D on stream 0 and D2H on stream 1 concurrently, 2 MB copies
|
|
const size_t size = 2046400, ncopies = total / size;
|
|
std::vector<double> r;
|
|
for (int rep = 0; rep < 3; ++rep) {
|
|
CK(cudaDeviceSynchronize());
|
|
const double t0 = now_s();
|
|
for (size_t i = 0; i < ncopies; ++i) {
|
|
const size_t off = (i * size) % (s.bytes - size);
|
|
CK(cudaMemcpyAsync(dev + (i % 8) * size, s.p + off, size, cudaMemcpyHostToDevice, st[0]));
|
|
CK(cudaMemcpyAsync(s.p + off, dev + (8 + i % 8) * size, size, cudaMemcpyDeviceToHost, st[1]));
|
|
}
|
|
CK(cudaDeviceSynchronize());
|
|
r.push_back(2.0 * ncopies * size / (now_s() - t0) / 1e9);
|
|
}
|
|
std::printf("RESULT bidir src=pinned size=%zu h2d_plus_d2h_gbps=%.1f\n", size, median(r));
|
|
}
|
|
free_source(s);
|
|
}
|
|
return 0;
|
|
}
|