Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
76 lines
3.9 KiB
Plaintext
76 lines
3.9 KiB
Plaintext
// How host DRAM bandwidth is shared between PCIe H2D copies and CPU readers (the CPU expert pool).
|
|
// mb_dram_pcie [--gib 4] [--pin-gib 2] [--secs 0.8]
|
|
// A pinned 2 GiB buffer feeds 1-2 H2D streams of 2 MB copies while N pinned reader threads stream from a separate DRAM
|
|
// buffer. Both consume the same DRAM, so pcie + readers is capped by the memory controller, not by PCIe.
|
|
// RESULT mix place=... readers=N streams=S pcie_gbps=... cpu_gbps=... sum_gbps=...
|
|
#include <cuda_runtime.h>
|
|
#include <sys/mman.h>
|
|
#include "mb_common.h"
|
|
#include "mb_dram_kernels.h"
|
|
|
|
int main(int argc, char** argv) {
|
|
double gib = 4, pin_gib = 2, secs = 0.8;
|
|
for (int i = 1; i < argc; ++i) {
|
|
if (!std::strcmp(argv[i], "--gib") && i + 1 < argc) gib = std::atof(argv[++i]);
|
|
else if (!std::strcmp(argv[i], "--pin-gib") && i + 1 < argc) pin_gib = std::atof(argv[++i]);
|
|
else if (!std::strcmp(argv[i], "--secs") && i + 1 < argc) secs = std::atof(argv[++i]);
|
|
}
|
|
const size_t rbytes = (size_t)(gib * (1ull << 30)), pbytes = (size_t)(pin_gib * (1ull << 30));
|
|
if (!host_alloc_allowed(rbytes + pbytes)) return 3;
|
|
uint8_t* buf = (uint8_t*)mmap(nullptr, rbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
if (buf == MAP_FAILED) return 2;
|
|
madvise(buf, rbytes, MADV_HUGEPAGE);
|
|
for (size_t i = 0; i < rbytes; i += 4096) buf[i] = 1;
|
|
uint8_t *pin, *dev;
|
|
CK(cudaHostAlloc((void**)&pin, pbytes, cudaHostAllocPortable));
|
|
std::memset(pin, 7, pbytes);
|
|
const size_t size = 2046400;
|
|
CK(cudaMalloc(&dev, 2 * 8 * size));
|
|
cudaStream_t st[2];
|
|
for (auto& s : st) CK(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking));
|
|
Topo topo = read_topo();
|
|
cudaDeviceProp prop;
|
|
CK(cudaGetDeviceProperties(&prop, 0));
|
|
std::printf("# device %s, l3_groups=%d\n", prop.name, topo.groups());
|
|
|
|
const int counts[] = {0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 24};
|
|
// the thread issuing the copies is itself a host thread: keep it off the readers' cores where possible (it only sleeps in sync)
|
|
for (const char* place : {"spread", "ccd0", "ccd1"}) {
|
|
if (!std::strcmp(place, "ccd1") && topo.groups() < 2) continue;
|
|
for (int ns = 1; ns <= 2; ++ns)
|
|
for (int n : counts) {
|
|
std::vector<int> cpus = placement(topo, place, std::max(n, 0));
|
|
if ((int)cpus.size() != n) continue;
|
|
std::vector<double> pc, cp;
|
|
for (int rep = 0; rep < 3; ++rep) {
|
|
ReaderPool pool;
|
|
if (n > 0) pool.start(buf, rbytes, cpus, 1, size);
|
|
// PCIe: keep 2 MB copies queued for `secs`
|
|
size_t copies = 0, i = 0;
|
|
CK(cudaDeviceSynchronize());
|
|
const double t0 = now_s();
|
|
while (now_s() - t0 < secs) {
|
|
for (int b = 0; b < 32; ++b, ++i) {
|
|
const int k = (int)(i % ns);
|
|
CK(cudaMemcpyAsync(dev + ((size_t)k * 8 + (i / ns) % 8) * size, pin + (i * size) % (pbytes - size), size, cudaMemcpyHostToDevice, st[k]));
|
|
}
|
|
// bound the queue depth to ~64 copies
|
|
if (i % 64 == 0) CK(cudaDeviceSynchronize());
|
|
copies = i;
|
|
}
|
|
CK(cudaDeviceSynchronize());
|
|
const double t1 = now_s();
|
|
const double cg = n > 0 ? pool.finish() : 0.0;
|
|
pc.push_back((double)copies * size / (t1 - t0) / 1e9);
|
|
cp.push_back(cg);
|
|
}
|
|
const double a = median(pc), b = median(cp);
|
|
std::printf("RESULT mix place=%s readers=%d streams=%d pcie_gbps=%.1f cpu_gbps=%.1f sum_gbps=%.1f\n", place, n, ns, a, b, a + b);
|
|
std::fflush(stdout);
|
|
}
|
|
}
|
|
cudaFreeHost(pin);
|
|
munmap(buf, rbytes);
|
|
return 0;
|
|
}
|