Files
leleka/tools/microbench/mb_lat.cu
T

298 lines
13 KiB
Plaintext

// Latency microbenchmarks for the synchronization primitives the serve schedule is built from.
// mb_lat
// RESULT lat what=... median_us=... p10_us=... p90_us=... p99_us=...
#include <atomic>
#include <cuda_runtime.h>
#include <linux/futex.h>
#include <pthread.h>
#include <sched.h>
#include <sys/syscall.h>
#include <thread>
#include <unistd.h>
#include "mb_common.h"
static void report(const char* what, std::vector<double> us, const char* extra = "") {
std::printf("RESULT lat what=%s median_us=%.2f p10_us=%.2f p90_us=%.2f p99_us=%.2f n=%zu %s\n", what, pct(us, 0.5), pct(us, 0.1), pct(us, 0.9), pct(us, 0.99),
us.size(), extra);
std::fflush(stdout);
}
__device__ __forceinline__ unsigned long long gtimer() {
unsigned long long t;
asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t));
return t;
}
__global__ void k_empty() {}
// one thread: stamp start/end (device-wide timer) so a gap between dependent kernels can be read off
__global__ void k_stamp(unsigned long long* st, unsigned long long* en, int i, int spin_ns) {
unsigned long long t0 = gtimer();
if (threadIdx.x == 0) st[i] = t0;
while (spin_ns > 0 && gtimer() - t0 < (unsigned long long)spin_ns) {}
if (threadIdx.x == 0) en[i] = gtimer();
}
// doorbell: wait for the host to raise `a` to the next value, then write it to `b`; N rounds
__global__ void k_pong(volatile unsigned* a, volatile unsigned* b, int rounds) {
for (int i = 1; i <= rounds; ++i) {
while (*a != (unsigned)i) {}
__threadfence_system();
*b = (unsigned)i;
__threadfence_system();
}
}
// GPU raises flag `up` after a delay, host observes; used to time device->host visibility via the host clock
__global__ void k_raise(volatile unsigned* up, unsigned v, int spin_ns) {
unsigned long long t0 = gtimer();
while (gtimer() - t0 < (unsigned long long)spin_ns) {}
__threadfence_system();
*up = v;
__threadfence_system();
}
static void gap_stats(const char* what, const std::vector<unsigned long long>& st, const std::vector<unsigned long long>& en) {
std::vector<double> gaps;
for (size_t i = 1; i < st.size(); ++i) gaps.push_back(((double)st[i] - (double)en[i - 1]) / 1000.0);
report(what, gaps);
}
static void pin(int cpu) {
cpu_set_t s;
CPU_ZERO(&s);
CPU_SET(cpu, &s);
pthread_setaffinity_np(pthread_self(), sizeof s, &s);
}
int main() {
cudaDeviceProp p;
CK(cudaGetDeviceProperties(&p, 0));
std::printf("# device %s sm_%d%d\n", p.name, p.major, p.minor);
CK(cudaSetDeviceFlags(cudaDeviceMapHost));
cudaStream_t s0, s1;
CK(cudaStreamCreateWithFlags(&s0, cudaStreamNonBlocking));
CK(cudaStreamCreateWithFlags(&s1, cudaStreamNonBlocking));
unsigned long long *dst, *den;
CK(cudaMalloc(&dst, 8 * 4096));
CK(cudaMalloc(&den, 8 * 4096));
const int N = 2000;
// 1. launch cost and back-to-back rate
{
for (int i = 0; i < 100; ++i) k_empty<<<1, 32, 0, s0>>>();
CK(cudaStreamSynchronize(s0));
std::vector<double> host, rt;
for (int i = 0; i < N; ++i) {
double t0 = now_s();
k_empty<<<1, 32, 0, s0>>>();
double t1 = now_s();
CK(cudaStreamSynchronize(s0));
double t2 = now_s();
host.push_back((t1 - t0) * 1e6);
rt.push_back((t2 - t0) * 1e6);
}
report("kernel_launch_host_call", host);
report("kernel_launch_plus_sync_roundtrip", rt);
const int M = 20000;
double t0 = now_s();
for (int i = 0; i < M; ++i) k_empty<<<1, 32, 0, s0>>>();
double t1 = now_s();
CK(cudaStreamSynchronize(s0));
double t2 = now_s();
std::printf("RESULT lat what=kernel_stream_throughput host_issue_us_per_kernel=%.2f end_to_end_us_per_kernel=%.2f\n", (t1 - t0) / M * 1e6, (t2 - t0) / M * 1e6);
}
// 2. GPU-side gap between dependent kernels, stream order and graph order
{
const int K = 1000;
std::vector<unsigned long long> st(K), en(K);
for (int i = 0; i < K; ++i) k_stamp<<<1, 32, 0, s0>>>(dst, den, i, 0);
CK(cudaStreamSynchronize(s0));
CK(cudaMemcpy(st.data(), dst, K * 8, cudaMemcpyDeviceToHost));
CK(cudaMemcpy(en.data(), den, K * 8, cudaMemcpyDeviceToHost));
gap_stats("kernel_to_kernel_gap_stream", st, en);
cudaGraph_t g;
cudaGraphExec_t ge;
CK(cudaStreamBeginCapture(s0, cudaStreamCaptureModeThreadLocal));
for (int i = 0; i < K; ++i) k_stamp<<<1, 32, 0, s0>>>(dst, den, i, 0);
CK(cudaStreamEndCapture(s0, &g));
CK(cudaGraphInstantiate(&ge, g, 0));
CK(cudaGraphLaunch(ge, s0));
CK(cudaStreamSynchronize(s0));
CK(cudaMemcpy(st.data(), dst, K * 8, cudaMemcpyDeviceToHost));
CK(cudaMemcpy(en.data(), den, K * 8, cudaMemcpyDeviceToHost));
gap_stats("kernel_to_kernel_gap_graph_chain", st, en);
// 3. graph launch host cost vs node count
for (int nodes : {1, 10, 100, 1000, 2400}) {
cudaGraph_t g2;
cudaGraphExec_t e2;
CK(cudaStreamBeginCapture(s0, cudaStreamCaptureModeThreadLocal));
for (int i = 0; i < nodes; ++i) k_empty<<<1, 32, 0, s0>>>();
CK(cudaStreamEndCapture(s0, &g2));
CK(cudaGraphInstantiate(&e2, g2, 0));
CK(cudaGraphUpload(e2, s0));
CK(cudaStreamSynchronize(s0));
std::vector<double> call, total;
for (int i = 0; i < 200; ++i) {
double t0 = now_s();
CK(cudaGraphLaunch(e2, s0));
double t1 = now_s();
CK(cudaStreamSynchronize(s0));
double t2 = now_s();
call.push_back((t1 - t0) * 1e6);
total.push_back((t2 - t0) * 1e6);
}
char b[64];
std::snprintf(b, sizeof b, "graph_launch_call_nodes_%d", nodes);
report(b, call);
std::snprintf(b, sizeof b, "graph_launch_to_done_nodes_%d", nodes);
report(b, total, "(empty kernels)");
CK(cudaGraphExecDestroy(e2));
CK(cudaGraphDestroy(g2));
}
}
// 4. cross-stream dependency through an event: GPU gap between the end of a kernel in s0 and the start of one in s1
{
cudaEvent_t ev;
CK(cudaEventCreateWithFlags(&ev, cudaEventDisableTiming));
const int K = 1000;
std::vector<double> rec, wait;
std::vector<unsigned long long> st(K), en(K);
for (int i = 0; i < K; ++i) {
k_stamp<<<1, 32, 0, s0>>>(dst, den, i, 300000); // ~300 us body so the wait + kernel are already queued in s1 (pure GPU-side dependency)
double t0 = now_s();
CK(cudaEventRecord(ev, s0));
double t1 = now_s();
CK(cudaStreamWaitEvent(s1, ev, 0));
double t2 = now_s();
rec.push_back((t1 - t0) * 1e6);
wait.push_back((t2 - t1) * 1e6);
k_stamp<<<1, 32, 0, s1>>>(dst + 2048, den + 2048, i, 0);
CK(cudaStreamSynchronize(s1));
}
std::vector<unsigned long long> s_b(K), e_a(K);
CK(cudaMemcpy(s_b.data(), dst + 2048, K * 8, cudaMemcpyDeviceToHost));
CK(cudaMemcpy(e_a.data(), den, K * 8, cudaMemcpyDeviceToHost));
std::vector<double> gap;
for (int i = 0; i < K; ++i) gap.push_back(((double)s_b[i] - (double)e_a[i]) / 1000.0);
report("event_record_host_call", rec);
report("stream_wait_event_host_call", wait);
report("event_dependency_gpu_gap_s0_to_s1", gap);
}
// 5. host-side completion notice: blocking-sync event vs spin vs query polling
{
cudaEvent_t eb, es;
CK(cudaEventCreateWithFlags(&eb, cudaEventBlockingSync | cudaEventDisableTiming));
CK(cudaEventCreateWithFlags(&es, cudaEventDisableTiming));
std::vector<double> blk, spin, poll;
for (int i = 0; i < 1000; ++i) {
double t0 = now_s();
k_empty<<<1, 32, 0, s0>>>();
CK(cudaEventRecord(eb, s0));
CK(cudaEventSynchronize(eb));
blk.push_back((now_s() - t0) * 1e6);
t0 = now_s();
k_empty<<<1, 32, 0, s0>>>();
CK(cudaEventRecord(es, s0));
CK(cudaEventSynchronize(es));
spin.push_back((now_s() - t0) * 1e6);
t0 = now_s();
k_empty<<<1, 32, 0, s0>>>();
CK(cudaEventRecord(es, s0));
while (cudaEventQuery(es) == cudaErrorNotReady) {}
poll.push_back((now_s() - t0) * 1e6);
}
report("launch_event_wait_blockingsync", blk, "(includes kernel launch)");
report("launch_event_wait_spin_default", spin);
report("launch_event_query_poll", poll);
}
// 6. host <-> GPU doorbell through mapped pinned memory (the flag A / flag B mechanism of the decode window)
{
unsigned *hf, *df;
CK(cudaHostAlloc((void**)&hf, 4096, cudaHostAllocMapped));
CK(cudaHostGetDevicePointer((void**)&df, hf, 0));
volatile unsigned* a = hf;
volatile unsigned* b = hf + 256; // different cache line / page offset
*a = 0; *b = 0;
const int R = 5000;
pin(0);
k_pong<<<1, 1, 0, s0>>>((volatile unsigned*)df, (volatile unsigned*)(df + 256), R);
std::vector<double> rtt;
for (int i = 1; i <= R; ++i) {
double t0 = now_s();
*a = (unsigned)i;
while (*b != (unsigned)i) {}
rtt.push_back((now_s() - t0) * 1e6);
}
CK(cudaStreamSynchronize(s0));
report("doorbell_roundtrip_host_flag_to_gpu_spin_and_back", rtt, "(one way ~ half)");
// GPU -> host visibility: GPU raises the flag after a known spin, host sees it; subtract the spin
std::vector<double> vis;
for (int i = 1; i <= 500; ++i) {
*a = 0;
const int spin = 20000; // 20 us
double t0 = now_s();
k_raise<<<1, 1, 0, s0>>>((volatile unsigned*)df, (unsigned)i, spin);
while (*a != (unsigned)i) {}
double t1 = now_s();
vis.push_back((t1 - t0) * 1e6 - 20.0);
CK(cudaStreamSynchronize(s0));
}
report("launch_plus_gpu_raise_flag_seen_by_host_minus_20us_spin", vis, "(includes kernel launch latency)");
}
// 7. host threads: spin ping-pong, futex ping-pong, sched_yield ping-pong
for (int variant = 0; variant < 3; ++variant) {
std::atomic<unsigned> x{0}, y{0};
const int R = 20000;
const int cpu_a = 0, cpu_b = std::thread::hardware_concurrency() >= 16 ? 8 : 1; // different CCD on the 9950X3D
std::thread th([&] {
pin(cpu_b);
for (unsigned i = 1; i <= (unsigned)R; ++i) {
if (variant == 0) { while (x.load(std::memory_order_acquire) != i) {} }
else if (variant == 1) {
while (x.load(std::memory_order_acquire) != i) syscall(SYS_futex, (unsigned*)&x, FUTEX_WAIT_PRIVATE, i - 1, nullptr, nullptr, 0);
} else { while (x.load(std::memory_order_acquire) != i) sched_yield(); }
y.store(i, std::memory_order_release);
if (variant == 1) syscall(SYS_futex, (unsigned*)&y, FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
}
});
pin(cpu_a);
std::vector<double> rtt;
for (unsigned i = 1; i <= (unsigned)R; ++i) {
double t0 = now_s();
x.store(i, std::memory_order_release);
if (variant == 1) syscall(SYS_futex, (unsigned*)&x, FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
while (y.load(std::memory_order_acquire) != i) {
if (variant == 2) sched_yield();
}
rtt.push_back((now_s() - t0) * 1e6);
if (variant == 1) { for (volatile int k = 0; k < 2000; ++k) {} } // let the peer go back to sleep: the cold wake path
}
th.join();
static const char* nm[] = {"threads_spin_pingpong_roundtrip_cross_ccd", "threads_futex_pingpong_roundtrip_cross_ccd", "threads_yield_pingpong_roundtrip_cross_ccd"};
report(nm[variant], rtt, "(one way ~ half)");
}
// 8. small copy enqueue cost and tiny-copy latency
{
void *hp, *dp;
CK(cudaHostAlloc(&hp, 1 << 20, cudaHostAllocDefault));
CK(cudaMalloc(&dp, 1 << 20));
std::vector<double> call, rt;
for (int i = 0; i < 2000; ++i) {
double t0 = now_s();
CK(cudaMemcpyAsync(dp, hp, 4096, cudaMemcpyHostToDevice, s0));
double t1 = now_s();
CK(cudaStreamSynchronize(s0));
call.push_back((t1 - t0) * 1e6);
rt.push_back((now_s() - t0) * 1e6);
}
report("memcpyasync_4KiB_h2d_host_call", call);
report("memcpyasync_4KiB_h2d_to_done", rt);
call.clear(); rt.clear();
for (int i = 0; i < 2000; ++i) {
double t0 = now_s();
CK(cudaMemcpyAsync(hp, dp, 4096, cudaMemcpyDeviceToHost, s0));
CK(cudaStreamSynchronize(s0));
rt.push_back((now_s() - t0) * 1e6);
}
report("memcpyasync_4KiB_d2h_to_done", rt);
}
return 0;
}