Files
nvdriver/tools/cuda_multigpu_parity.c
morebrackets 60b7ef23b3 tools: guard unchecked host allocations, NUL-terminate NVRTC PTX, and replace sprintf in C parity suites
- tools/cuda_parity_suite.c: add must_malloc/must_calloc helpers guarding all 11 previously unchecked host allocations (memset, cuBLAS SGEMM/TensorCore/batched, cuFFT 1D/2D, cuRAND, cuBLASLt, NVRTC PTX), NUL-terminate NVRTC PTX buffer, and replace sprintf with bounded snprintf for UUID hex formatting.
- tools/cuda_ddp_training_parity.c & tools/cuda_ml_training_parity.c: add must_malloc/must_calloc guarding NVRTC compile log, PTX buffer (allocated as ptx_sz + 1 and explicitly NUL-terminated), and host weight buffers; destroy nvrtcProgram on compile failure before returning.
- tools/cuda_multigpu_parity.c: add must_malloc/must_calloc guarding h_tmp, h_expected_sum, h_in[g], and h_out[g].
2026-12-02 03:10:55 -08:00

814 lines
33 KiB
C

/* Multi-GPU & Direct PCIe BAR1 P2P Ring + AllReduce CUDA Driver API parity test
* for comparing stock nvidia.ko (580.178.04) against nvidia_rs.ko on N >= 2
* Tesla V100 GPUs inside a disposable VFIO VM.
*
* Build: cc -O2 -Wall -Wextra tools/cuda_multigpu_parity.c -ldl -o target/hardware-hello/cuda-multigpu-parity
* Run: target/hardware-hello/cuda-multigpu-parity
*/
#include <dlfcn.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_GPUS 16
typedef int CUresult;
typedef int CUdevice;
typedef void *CUcontext;
typedef void *CUmodule;
typedef void *CUfunction;
typedef uint64_t CUdeviceptr;
typedef struct {
unsigned char bytes[16];
} CUuuid_local;
/* SM70 PTX module containing:
* 1. vec_transform: out[tid] = (in[tid] ^ xor_mask) + add_val
* 2. vec_add: acc[tid] = acc[tid] + src[tid]
* 3. p2p_ring_accum: val = peer_in[tid]; acc[tid] += val; local_fwd[tid] = val;
* 4. p2p_direct_store: peer_out[tid] = local_in[tid];
*/
static const char PTX_MODULE[] =
".version 6.0\n"
".target sm_70\n"
".address_size 64\n"
"\n"
".visible .entry vec_transform(\n"
" .param .u64 in_ptr,\n"
" .param .u64 out_ptr,\n"
" .param .u32 xor_mask,\n"
" .param .u32 add_val,\n"
" .param .u32 count\n"
") {\n"
" .reg .u32 %r<10>;\n"
" .reg .u64 %rd<6>;\n"
" .reg .pred %p<2>;\n"
" mov.u32 %r1, %ctaid.x;\n"
" mov.u32 %r2, %ntid.x;\n"
" mov.u32 %r3, %tid.x;\n"
" mad.lo.u32 %r4, %r1, %r2, %r3;\n"
" ld.param.u32 %r5, [count];\n"
" setp.ge.u32 %p1, %r4, %r5;\n"
" @%p1 bra DONE;\n"
" ld.param.u64 %rd1, [in_ptr];\n"
" ld.param.u64 %rd2, [out_ptr];\n"
" ld.param.u32 %r6, [xor_mask];\n"
" ld.param.u32 %r7, [add_val];\n"
" mul.wide.u32 %rd3, %r4, 4;\n"
" add.u64 %rd4, %rd1, %rd3;\n"
" add.u64 %rd5, %rd2, %rd3;\n"
" ld.global.u32 %r8, [%rd4];\n"
" xor.b32 %r9, %r8, %r6;\n"
" add.u32 %r9, %r9, %r7;\n"
" st.global.u32 [%rd5], %r9;\n"
"DONE:\n"
" ret;\n"
"}\n"
"\n"
".visible .entry vec_add(\n"
" .param .u64 acc_ptr,\n"
" .param .u64 src_ptr,\n"
" .param .u32 count\n"
") {\n"
" .reg .u32 %r<9>;\n"
" .reg .u64 %rd<6>;\n"
" .reg .pred %p<2>;\n"
" mov.u32 %r1, %ctaid.x;\n"
" mov.u32 %r2, %ntid.x;\n"
" mov.u32 %r3, %tid.x;\n"
" mad.lo.u32 %r4, %r1, %r2, %r3;\n"
" ld.param.u32 %r5, [count];\n"
" setp.ge.u32 %p1, %r4, %r5;\n"
" @%p1 bra DONE_ADD;\n"
" ld.param.u64 %rd1, [acc_ptr];\n"
" ld.param.u64 %rd2, [src_ptr];\n"
" mul.wide.u32 %rd3, %r4, 4;\n"
" add.u64 %rd4, %rd1, %rd3;\n"
" add.u64 %rd5, %rd2, %rd3;\n"
" ld.global.u32 %r6, [%rd4];\n"
" ld.global.u32 %r7, [%rd5];\n"
" add.u32 %r8, %r6, %r7;\n"
" st.global.u32 [%rd4], %r8;\n"
"DONE_ADD:\n"
" ret;\n"
"}\n"
"\n"
".visible .entry p2p_ring_accum(\n"
" .param .u64 peer_in_ptr,\n"
" .param .u64 local_acc_ptr,\n"
" .param .u64 local_fwd_ptr,\n"
" .param .u32 count\n"
") {\n"
" .reg .u32 %r<9>;\n"
" .reg .u64 %rd<8>;\n"
" .reg .pred %p<2>;\n"
" mov.u32 %r1, %ctaid.x;\n"
" mov.u32 %r2, %ntid.x;\n"
" mov.u32 %r3, %tid.x;\n"
" mad.lo.u32 %r4, %r1, %r2, %r3;\n"
" ld.param.u32 %r5, [count];\n"
" setp.ge.u32 %p1, %r4, %r5;\n"
" @%p1 bra DONE_P2P;\n"
" ld.param.u64 %rd1, [peer_in_ptr];\n"
" ld.param.u64 %rd2, [local_acc_ptr];\n"
" ld.param.u64 %rd3, [local_fwd_ptr];\n"
" mul.wide.u32 %rd4, %r4, 4;\n"
" add.u64 %rd5, %rd1, %rd4;\n"
" add.u64 %rd6, %rd2, %rd4;\n"
" add.u64 %rd7, %rd3, %rd4;\n"
" ld.global.u32 %r6, [%rd5];\n"
" ld.global.u32 %r7, [%rd6];\n"
" add.u32 %r8, %r7, %r6;\n"
" st.global.u32 [%rd6], %r8;\n"
" st.global.u32 [%rd7], %r6;\n"
"DONE_P2P:\n"
" ret;\n"
"}\n"
"\n"
".visible .entry p2p_direct_store(\n"
" .param .u64 local_in_ptr,\n"
" .param .u64 peer_out_ptr,\n"
" .param .u32 count\n"
") {\n"
" .reg .u32 %r<7>;\n"
" .reg .u64 %rd<6>;\n"
" .reg .pred %p<2>;\n"
" mov.u32 %r1, %ctaid.x;\n"
" mov.u32 %r2, %ntid.x;\n"
" mov.u32 %r3, %tid.x;\n"
" mad.lo.u32 %r4, %r1, %r2, %r3;\n"
" ld.param.u32 %r5, [count];\n"
" setp.ge.u32 %p1, %r4, %r5;\n"
" @%p1 bra DONE_STORE;\n"
" ld.param.u64 %rd1, [local_in_ptr];\n"
" ld.param.u64 %rd2, [peer_out_ptr];\n"
" mul.wide.u32 %rd3, %r4, 4;\n"
" add.u64 %rd4, %rd1, %rd3;\n"
" add.u64 %rd5, %rd2, %rd3;\n"
" ld.global.u32 %r6, [%rd4];\n"
" st.global.u32 [%rd5], %r6;\n"
"DONE_STORE:\n"
" ret;\n"
"}\n";
static uint64_t fnv1a_64(const void *data, size_t len) {
const uint8_t *p = (const uint8_t *)data;
uint64_t h = 14695981039346656037ULL;
for (size_t i = 0; i < len; i++) {
h ^= (uint64_t)p[i];
h *= 1099511628211ULL;
}
return h;
}
static void format_uuid(const CUuuid_local *u, char out[40]) {
snprintf(out, 40,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
u->bytes[0], u->bytes[1], u->bytes[2], u->bytes[3],
u->bytes[4], u->bytes[5], u->bytes[6], u->bytes[7],
u->bytes[8], u->bytes[9], u->bytes[10], u->bytes[11],
u->bytes[12], u->bytes[13], u->bytes[14], u->bytes[15]);
}
static void *must_malloc(size_t bytes) {
void *p = malloc(bytes ? bytes : 1);
if (!p) {
fprintf(stderr, "malloc(%zu) failed\n", bytes);
exit(2);
}
return p;
}
static void *must_calloc(size_t nmemb, size_t size) {
void *p = calloc(nmemb ? nmemb : 1, size ? size : 1);
if (!p) {
fprintf(stderr, "calloc(%zu, %zu) failed\n", nmemb, size);
exit(2);
}
return p;
}
#define LOAD_SYM(handle, name, ret, args) \
typedef ret (*fn_##name##_t) args; \
fn_##name##_t p_##name = (fn_##name##_t)dlsym(handle, #name); \
if (!p_##name) { \
fprintf(stderr, "missing symbol %s\n", #name); \
return 2; \
}
int main(void) {
void *lib = dlopen("libcuda.so.1", RTLD_NOW | RTLD_LOCAL);
if (!lib) {
fprintf(stderr, "dlopen libcuda.so.1 failed: %s\n", dlerror());
return 2;
}
LOAD_SYM(lib, cuInit, CUresult, (unsigned int));
LOAD_SYM(lib, cuDeviceGetCount, CUresult, (int *));
LOAD_SYM(lib, cuDeviceGet, CUresult, (CUdevice *, int));
LOAD_SYM(lib, cuDeviceGetName, CUresult, (char *, int, CUdevice));
LOAD_SYM(lib, cuDeviceGetUuid, CUresult, (CUuuid_local *, CUdevice));
LOAD_SYM(lib, cuDeviceTotalMem_v2, CUresult, (size_t *, CUdevice));
LOAD_SYM(lib, cuDeviceGetPCIBusId, CUresult, (char *, int, CUdevice));
LOAD_SYM(lib, cuCtxCreate_v2, CUresult, (CUcontext *, unsigned int, CUdevice));
LOAD_SYM(lib, cuCtxDestroy_v2, CUresult, (CUcontext));
LOAD_SYM(lib, cuCtxSetCurrent, CUresult, (CUcontext));
LOAD_SYM(lib, cuCtxSynchronize, CUresult, (void));
LOAD_SYM(lib, cuMemAlloc_v2, CUresult, (CUdeviceptr *, size_t));
LOAD_SYM(lib, cuMemFree_v2, CUresult, (CUdeviceptr));
LOAD_SYM(lib, cuMemcpyHtoD_v2, CUresult, (CUdeviceptr, const void *, size_t));
LOAD_SYM(lib, cuMemcpyDtoH_v2, CUresult, (void *, CUdeviceptr, size_t));
LOAD_SYM(lib, cuMemcpyDtoD_v2, CUresult, (CUdeviceptr, CUdeviceptr, size_t));
LOAD_SYM(lib, cuModuleLoadData, CUresult, (CUmodule *, const void *));
LOAD_SYM(lib, cuModuleGetFunction, CUresult, (CUfunction *, CUmodule, const char *));
LOAD_SYM(lib, cuModuleUnload, CUresult, (CUmodule));
LOAD_SYM(lib, cuLaunchKernel, CUresult,
(CUfunction, unsigned int, unsigned int, unsigned int,
unsigned int, unsigned int, unsigned int, unsigned int,
void *, void **, void **));
LOAD_SYM(lib, cuDeviceCanAccessPeer, CUresult, (int *, CUdevice, CUdevice));
LOAD_SYM(lib, cuCtxEnablePeerAccess, CUresult, (CUcontext, unsigned int));
LOAD_SYM(lib, cuCtxDisablePeerAccess, CUresult, (CUcontext));
LOAD_SYM(lib, cuMemcpyPeer, CUresult, (CUdeviceptr, CUcontext, CUdeviceptr, CUcontext, size_t));
CUresult rc_init = p_cuInit(0);
int dev_count = 0;
CUresult rc_count = (rc_init == 0) ? p_cuDeviceGetCount(&dev_count) : rc_init;
int ngpus = (rc_init == 0 && rc_count == 0 && dev_count >= 2 && dev_count <= MAX_GPUS)
? dev_count
: 0;
char name[MAX_GPUS][128];
char uuid_str[MAX_GPUS][40];
char bdf_str[MAX_GPUS][32];
size_t total_mem[MAX_GPUS];
CUdevice devs[MAX_GPUS];
CUcontext ctxs[MAX_GPUS];
CUmodule mods[MAX_GPUS];
CUfunction fn_transform[MAX_GPUS];
CUfunction fn_add[MAX_GPUS];
CUfunction fn_p2p_accum[MAX_GPUS];
CUfunction fn_p2p_store[MAX_GPUS];
CUdeviceptr d_in[MAX_GPUS];
CUdeviceptr d_out[MAX_GPUS];
CUresult rc_ctx[MAX_GPUS];
CUresult rc_launch[MAX_GPUS];
int gpu_kernel_ok[MAX_GPUS];
uint64_t gpu_digest[MAX_GPUS];
int gpu_mismatches[MAX_GPUS];
memset(name, 0, sizeof(name));
memset(uuid_str, 0, sizeof(uuid_str));
memset(bdf_str, 0, sizeof(bdf_str));
memset(total_mem, 0, sizeof(total_mem));
memset(devs, 0, sizeof(devs));
memset(ctxs, 0, sizeof(ctxs));
memset(mods, 0, sizeof(mods));
memset(fn_transform, 0, sizeof(fn_transform));
memset(fn_add, 0, sizeof(fn_add));
memset(fn_p2p_accum, 0, sizeof(fn_p2p_accum));
memset(fn_p2p_store, 0, sizeof(fn_p2p_store));
memset(d_in, 0, sizeof(d_in));
memset(d_out, 0, sizeof(d_out));
memset(gpu_kernel_ok, 0, sizeof(gpu_kernel_ok));
memset(gpu_digest, 0, sizeof(gpu_digest));
memset(gpu_mismatches, 0, sizeof(gpu_mismatches));
for (int g = 0; g < MAX_GPUS; g++) {
rc_ctx[g] = -1;
rc_launch[g] = -1;
}
const uint32_t N = 4096;
const size_t BYTES = (size_t)N * sizeof(uint32_t);
uint32_t *h_in[MAX_GPUS];
uint32_t *h_out[MAX_GPUS];
uint32_t *h_tmp = (uint32_t *)must_malloc(BYTES);
uint32_t *h_expected_sum = (uint32_t *)must_calloc(N, sizeof(uint32_t));
memset(h_in, 0, sizeof(h_in));
memset(h_out, 0, sizeof(h_out));
static const uint32_t XOR_MASKS[8] = {
0xA5A55A5AU, 0x3C3CC3C3U, 0x55AA33CCU, 0xF0F00F0FU,
0x12345678U, 0x9ABCDEF0U, 0xCAFEBABEU, 0xDEADBEEFU,
};
static const uint32_t ADD_VALS[8] = {
0x13579BDFU, 0x2468ACE0U, 0x11223344U, 0x55667788U,
0x01020304U, 0x05060708U, 0x090A0B0CU, 0x0D0E0F10U,
};
static const uint32_t MULT_SEEDS[8] = {
2654435761U, 2246822519U, 3266489917U, 668265263U,
374761393U, 1597334677U, 2051212867U, 3037000493U,
};
for (int g = 0; g < ngpus; g++) {
h_in[g] = (uint32_t *)must_malloc(BYTES);
h_out[g] = (uint32_t *)must_malloc(BYTES);
uint32_t base = ((uint32_t)(g + 1)) << 28;
uint32_t mult = MULT_SEEDS[g & 7];
for (uint32_t i = 0; i < N; i++) {
h_in[g][i] = base + i * mult;
}
}
int enum_ok = (ngpus >= 2);
int uuids_distinct = 0;
if (enum_ok) {
for (int g = 0; g < ngpus; g++) {
CUuuid_local u;
memset(&u, 0, sizeof(u));
if (p_cuDeviceGet(&devs[g], g) != 0) enum_ok = 0;
if (p_cuDeviceGetName(name[g], sizeof(name[g]), devs[g]) != 0) enum_ok = 0;
if (p_cuDeviceGetUuid(&u, devs[g]) != 0) enum_ok = 0;
format_uuid(&u, uuid_str[g]);
if (p_cuDeviceTotalMem_v2(&total_mem[g], devs[g]) != 0) enum_ok = 0;
if (total_mem[g] == 0) enum_ok = 0;
if (p_cuDeviceGetPCIBusId(bdf_str[g], sizeof(bdf_str[g]), devs[g]) != 0) enum_ok = 0;
}
uuids_distinct = 1;
for (int i = 0; i < ngpus; i++) {
for (int j = i + 1; j < ngpus; j++) {
if (strcmp(uuid_str[i], uuid_str[j]) == 0) {
uuids_distinct = 0;
}
}
}
}
/* Create all N contexts concurrently before launching work on any GPU. */
int all_ctx_ok = enum_ok;
if (enum_ok) {
for (int g = 0; g < ngpus; g++) {
rc_ctx[g] = p_cuCtxCreate_v2(&ctxs[g], 0, devs[g]);
if (rc_ctx[g] != 0) all_ctx_ok = 0;
}
}
/* Allocate + load PTX + launch vec_transform concurrently on all N GPUs. */
int all_kernels_ok = all_ctx_ok;
if (all_ctx_ok) {
for (int g = 0; g < ngpus; g++) {
CUresult r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) r = p_cuMemAlloc_v2(&d_in[g], BYTES);
if (r == 0) r = p_cuMemAlloc_v2(&d_out[g], BYTES);
if (r == 0) r = p_cuMemcpyHtoD_v2(d_in[g], h_in[g], BYTES);
if (r == 0) r = p_cuModuleLoadData(&mods[g], PTX_MODULE);
if (r == 0) r = p_cuModuleGetFunction(&fn_transform[g], mods[g], "vec_transform");
if (r == 0) r = p_cuModuleGetFunction(&fn_add[g], mods[g], "vec_add");
if (r == 0) r = p_cuModuleGetFunction(&fn_p2p_accum[g], mods[g], "p2p_ring_accum");
if (r == 0) r = p_cuModuleGetFunction(&fn_p2p_store[g], mods[g], "p2p_direct_store");
if (r == 0) {
uint32_t xm = XOR_MASKS[g & 7];
uint32_t av = ADD_VALS[g & 7];
uint32_t cnt = N;
void *params[5] = {&d_in[g], &d_out[g], &xm, &av, &cnt};
r = p_cuLaunchKernel(fn_transform[g], N / 256, 1, 1, 256, 1, 1, 0, NULL, params, NULL);
}
rc_launch[g] = r;
}
/* Synchronize and verify all N GPUs. */
for (int g = 0; g < ngpus; g++) {
if (rc_launch[g] == 0) {
CUresult r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) r = p_cuCtxSynchronize();
if (r == 0) r = p_cuMemcpyDtoH_v2(h_out[g], d_out[g], BYTES);
rc_launch[g] = r;
if (r == 0) {
int bad = 0;
uint32_t xm = XOR_MASKS[g & 7];
uint32_t av = ADD_VALS[g & 7];
for (uint32_t i = 0; i < N; i++) {
uint32_t exp = (h_in[g][i] ^ xm) + av;
if (h_out[g][i] != exp) bad++;
h_expected_sum[i] += exp;
}
gpu_mismatches[g] = bad;
gpu_digest[g] = fnv1a_64(h_out[g], BYTES);
gpu_kernel_ok[g] = (bad == 0);
}
}
if (!gpu_kernel_ok[g]) all_kernels_ok = 0;
}
}
uint64_t expected_allreduce_digest = fnv1a_64(h_expected_sum, BYTES);
/* Bidirectional ring P2P setup: i -> (i+1)%N and (i+1)%N -> i. */
int can_fwd[MAX_GPUS], can_bwd[MAX_GPUS];
CUresult rc_can_fwd[MAX_GPUS], rc_can_bwd[MAX_GPUS];
CUresult rc_en_fwd[MAX_GPUS], rc_en_bwd[MAX_GPUS];
int peer_enabled[MAX_GPUS][MAX_GPUS];
memset(can_fwd, -1, sizeof(can_fwd));
memset(can_bwd, -1, sizeof(can_bwd));
memset(peer_enabled, 0, sizeof(peer_enabled));
for (int g = 0; g < MAX_GPUS; g++) {
rc_can_fwd[g] = -1;
rc_can_bwd[g] = -1;
rc_en_fwd[g] = -1;
rc_en_bwd[g] = -1;
}
/* Ring cuMemcpyPeer results (fwd: g -> next, bwd: next -> g). */
CUresult rc_ring_cpy_fwd[MAX_GPUS], rc_ring_cpy_bwd[MAX_GPUS];
int ring_cpy_fwd_ok[MAX_GPUS], ring_cpy_bwd_ok[MAX_GPUS];
uint64_t ring_cpy_fwd_digest[MAX_GPUS], ring_cpy_bwd_digest[MAX_GPUS];
memset(ring_cpy_fwd_ok, 0, sizeof(ring_cpy_fwd_ok));
memset(ring_cpy_bwd_ok, 0, sizeof(ring_cpy_bwd_ok));
memset(ring_cpy_fwd_digest, 0, sizeof(ring_cpy_fwd_digest));
memset(ring_cpy_bwd_digest, 0, sizeof(ring_cpy_bwd_digest));
for (int g = 0; g < MAX_GPUS; g++) {
rc_ring_cpy_fwd[g] = -1;
rc_ring_cpy_bwd[g] = -1;
}
/* Ring AllReduce via cuMemcpyPeer results. */
int memcpy_allreduce_ok = 0;
CUresult rc_memcpy_allreduce = -1;
uint64_t memcpy_allreduce_digest[MAX_GPUS];
int memcpy_allreduce_mismatches[MAX_GPUS];
memset(memcpy_allreduce_digest, 0, sizeof(memcpy_allreduce_digest));
memset(memcpy_allreduce_mismatches, 0, sizeof(memcpy_allreduce_mismatches));
/* Direct cross-GPU SM70 kernel P2P Ring AllReduce (direct ld.global) results. */
int direct_p2p_allreduce_ok = 0;
CUresult rc_direct_p2p_allreduce = -1;
uint64_t direct_p2p_allreduce_digest[MAX_GPUS];
int direct_p2p_allreduce_mismatches[MAX_GPUS];
memset(direct_p2p_allreduce_digest, 0, sizeof(direct_p2p_allreduce_digest));
memset(direct_p2p_allreduce_mismatches, 0, sizeof(direct_p2p_allreduce_mismatches));
/* Direct cross-GPU SM70 kernel P2P direct st.global ring verification results. */
int direct_p2p_store_ok = 0;
CUresult rc_direct_p2p_store = -1;
uint64_t direct_p2p_store_digest[MAX_GPUS];
int direct_p2p_store_mismatches[MAX_GPUS];
memset(direct_p2p_store_digest, 0, sizeof(direct_p2p_store_digest));
memset(direct_p2p_store_mismatches, 0, sizeof(direct_p2p_store_mismatches));
int all_peer_enabled = 0;
int all_ring_cpy_ok = 0;
if (all_kernels_ok) {
all_peer_enabled = 1;
for (int g = 0; g < ngpus; g++) {
int next = (g + 1) % ngpus;
rc_can_fwd[g] = p_cuDeviceCanAccessPeer(&can_fwd[g], devs[g], devs[next]);
rc_can_bwd[g] = p_cuDeviceCanAccessPeer(&can_bwd[g], devs[next], devs[g]);
if (rc_can_fwd[g] == 0 && can_fwd[g]) {
if (!peer_enabled[g][next]) {
p_cuCtxSetCurrent(ctxs[g]);
rc_en_fwd[g] = p_cuCtxEnablePeerAccess(ctxs[next], 0);
if (rc_en_fwd[g] == 0) peer_enabled[g][next] = 1;
} else {
rc_en_fwd[g] = 0;
}
}
if (rc_can_bwd[g] == 0 && can_bwd[g]) {
if (!peer_enabled[next][g]) {
p_cuCtxSetCurrent(ctxs[next]);
rc_en_bwd[g] = p_cuCtxEnablePeerAccess(ctxs[g], 0);
if (rc_en_bwd[g] == 0) peer_enabled[next][g] = 1;
} else {
rc_en_bwd[g] = 0;
}
}
if (rc_can_fwd[g] != 0 || !can_fwd[g] || rc_en_fwd[g] != 0 ||
rc_can_bwd[g] != 0 || !can_bwd[g] || rc_en_bwd[g] != 0) {
all_peer_enabled = 0;
}
}
/* 1. Bidirectional Ring cuMemcpyPeer Copy (fwd: g -> next, bwd: next -> g). */
all_ring_cpy_ok = 1;
for (int g = 0; g < ngpus; g++) {
int next = (g + 1) % ngpus;
CUdeviceptr d_dst = 0;
/* Forward: g -> next */
p_cuCtxSetCurrent(ctxs[next]);
if (p_cuMemAlloc_v2(&d_dst, BYTES) == 0) {
rc_ring_cpy_fwd[g] = p_cuMemcpyPeer(d_dst, ctxs[next], d_out[g], ctxs[g], BYTES);
if (rc_ring_cpy_fwd[g] == 0) {
p_cuCtxSynchronize();
if (p_cuMemcpyDtoH_v2(h_tmp, d_dst, BYTES) == 0) {
ring_cpy_fwd_ok[g] = (memcmp(h_tmp, h_out[g], BYTES) == 0);
ring_cpy_fwd_digest[g] = fnv1a_64(h_tmp, BYTES);
}
}
p_cuMemFree_v2(d_dst);
}
/* Backward: next -> g */
d_dst = 0;
p_cuCtxSetCurrent(ctxs[g]);
if (p_cuMemAlloc_v2(&d_dst, BYTES) == 0) {
rc_ring_cpy_bwd[g] = p_cuMemcpyPeer(d_dst, ctxs[g], d_out[next], ctxs[next], BYTES);
if (rc_ring_cpy_bwd[g] == 0) {
p_cuCtxSynchronize();
if (p_cuMemcpyDtoH_v2(h_tmp, d_dst, BYTES) == 0) {
ring_cpy_bwd_ok[g] = (memcmp(h_tmp, h_out[next], BYTES) == 0);
ring_cpy_bwd_digest[g] = fnv1a_64(h_tmp, BYTES);
}
}
p_cuMemFree_v2(d_dst);
}
if (!ring_cpy_fwd_ok[g] || !ring_cpy_bwd_ok[g]) all_ring_cpy_ok = 0;
}
/* 2. Ring AllReduce Sum via cuMemcpyPeer + local vec_add kernel. */
{
CUdeviceptr d_acc[MAX_GPUS], d_cur[MAX_GPUS], d_nxt[MAX_GPUS];
memset(d_acc, 0, sizeof(d_acc));
memset(d_cur, 0, sizeof(d_cur));
memset(d_nxt, 0, sizeof(d_nxt));
CUresult r = 0;
for (int g = 0; g < ngpus && r == 0; g++) {
r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) r = p_cuMemAlloc_v2(&d_acc[g], BYTES);
if (r == 0) r = p_cuMemAlloc_v2(&d_cur[g], BYTES);
if (r == 0) r = p_cuMemAlloc_v2(&d_nxt[g], BYTES);
if (r == 0) r = p_cuMemcpyDtoD_v2(d_acc[g], d_out[g], BYTES);
if (r == 0) r = p_cuMemcpyDtoD_v2(d_cur[g], d_out[g], BYTES);
if (r == 0) r = p_cuCtxSynchronize();
}
for (int step = 0; step < ngpus - 1 && r == 0; step++) {
for (int g = 0; g < ngpus && r == 0; g++) {
int peer = (g + 1) % ngpus;
r = p_cuMemcpyPeer(d_nxt[g], ctxs[g], d_cur[peer], ctxs[peer], BYTES);
}
for (int g = 0; g < ngpus && r == 0; g++) {
r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) r = p_cuCtxSynchronize();
}
for (int g = 0; g < ngpus && r == 0; g++) {
r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) {
uint32_t cnt = N;
void *params[3] = {&d_acc[g], &d_nxt[g], &cnt};
r = p_cuLaunchKernel(fn_add[g], N / 256, 1, 1, 256, 1, 1, 0, NULL, params, NULL);
}
}
for (int g = 0; g < ngpus && r == 0; g++) {
r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) r = p_cuCtxSynchronize();
CUdeviceptr tmp = d_cur[g];
d_cur[g] = d_nxt[g];
d_nxt[g] = tmp;
}
}
rc_memcpy_allreduce = r;
if (r == 0) {
memcpy_allreduce_ok = 1;
for (int g = 0; g < ngpus; g++) {
p_cuCtxSetCurrent(ctxs[g]);
if (p_cuMemcpyDtoH_v2(h_tmp, d_acc[g], BYTES) == 0) {
int bad = 0;
for (uint32_t i = 0; i < N; i++) {
if (h_tmp[i] != h_expected_sum[i]) bad++;
}
memcpy_allreduce_mismatches[g] = bad;
memcpy_allreduce_digest[g] = fnv1a_64(h_tmp, BYTES);
if (bad != 0) memcpy_allreduce_ok = 0;
} else {
memcpy_allreduce_ok = 0;
}
}
}
for (int g = 0; g < ngpus; g++) {
p_cuCtxSetCurrent(ctxs[g]);
if (d_acc[g]) p_cuMemFree_v2(d_acc[g]);
if (d_cur[g]) p_cuMemFree_v2(d_cur[g]);
if (d_nxt[g]) p_cuMemFree_v2(d_nxt[g]);
}
}
/* 3. Direct Cross-GPU SM70 Kernel P2P Ring AllReduce (direct BAR1 P2P ld.global)
* + Direct Cross-GPU SM70 Kernel P2P Store (direct BAR1 P2P st.global). */
if (all_peer_enabled) {
CUdeviceptr d_acc[MAX_GPUS], d_buf_a[MAX_GPUS], d_buf_b[MAX_GPUS], d_store_recv[MAX_GPUS];
memset(d_acc, 0, sizeof(d_acc));
memset(d_buf_a, 0, sizeof(d_buf_a));
memset(d_buf_b, 0, sizeof(d_buf_b));
memset(d_store_recv, 0, sizeof(d_store_recv));
CUresult r = 0;
for (int g = 0; g < ngpus && r == 0; g++) {
r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) r = p_cuMemAlloc_v2(&d_acc[g], BYTES);
if (r == 0) r = p_cuMemAlloc_v2(&d_buf_a[g], BYTES);
if (r == 0) r = p_cuMemAlloc_v2(&d_buf_b[g], BYTES);
if (r == 0) r = p_cuMemAlloc_v2(&d_store_recv[g], BYTES);
if (r == 0) r = p_cuMemcpyDtoD_v2(d_acc[g], d_out[g], BYTES);
if (r == 0) r = p_cuMemcpyDtoD_v2(d_buf_a[g], d_out[g], BYTES);
if (r == 0) r = p_cuCtxSynchronize();
}
/* N-1 concurrent direct-P2P load+accumulate steps around the ring. */
for (int step = 0; step < ngpus - 1 && r == 0; step++) {
CUdeviceptr *src_buf = (step % 2 == 0) ? d_buf_a : d_buf_b;
CUdeviceptr *dst_buf = (step % 2 == 0) ? d_buf_b : d_buf_a;
for (int g = 0; g < ngpus && r == 0; g++) {
int peer = (g + 1) % ngpus;
r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) {
uint32_t cnt = N;
CUdeviceptr peer_ptr = src_buf[peer];
void *params[4] = {&peer_ptr, &d_acc[g], &dst_buf[g], &cnt};
r = p_cuLaunchKernel(fn_p2p_accum[g], N / 256, 1, 1, 256, 1, 1, 0, NULL, params, NULL);
}
}
for (int g = 0; g < ngpus && r == 0; g++) {
r = p_cuCtxSetCurrent(ctxs[g]);
if (r == 0) r = p_cuCtxSynchronize();
}
}
rc_direct_p2p_allreduce = r;
if (r == 0) {
direct_p2p_allreduce_ok = 1;
for (int g = 0; g < ngpus; g++) {
p_cuCtxSetCurrent(ctxs[g]);
if (p_cuMemcpyDtoH_v2(h_tmp, d_acc[g], BYTES) == 0) {
int bad = 0;
for (uint32_t i = 0; i < N; i++) {
if (h_tmp[i] != h_expected_sum[i]) bad++;
}
direct_p2p_allreduce_mismatches[g] = bad;
direct_p2p_allreduce_digest[g] = fnv1a_64(h_tmp, BYTES);
if (bad != 0) direct_p2p_allreduce_ok = 0;
} else {
direct_p2p_allreduce_ok = 0;
}
}
}
/* Direct-P2P store ring: GPU g writes its d_acc[g] directly into GPU (g+1)%N's d_store_recv. */
if (r == 0) {
CUresult rs = 0;
for (int g = 0; g < ngpus && rs == 0; g++) {
int peer = (g + 1) % ngpus;
rs = p_cuCtxSetCurrent(ctxs[g]);
if (rs == 0) {
uint32_t cnt = N;
CUdeviceptr peer_dst = d_store_recv[peer];
void *params[3] = {&d_acc[g], &peer_dst, &cnt};
rs = p_cuLaunchKernel(fn_p2p_store[g], N / 256, 1, 1, 256, 1, 1, 0, NULL, params, NULL);
}
}
for (int g = 0; g < ngpus && rs == 0; g++) {
rs = p_cuCtxSetCurrent(ctxs[g]);
if (rs == 0) rs = p_cuCtxSynchronize();
}
rc_direct_p2p_store = rs;
if (rs == 0) {
direct_p2p_store_ok = 1;
for (int g = 0; g < ngpus; g++) {
p_cuCtxSetCurrent(ctxs[g]);
if (p_cuMemcpyDtoH_v2(h_tmp, d_store_recv[g], BYTES) == 0) {
int bad = 0;
for (uint32_t i = 0; i < N; i++) {
if (h_tmp[i] != h_expected_sum[i]) bad++;
}
direct_p2p_store_mismatches[g] = bad;
direct_p2p_store_digest[g] = fnv1a_64(h_tmp, BYTES);
if (bad != 0) direct_p2p_store_ok = 0;
} else {
direct_p2p_store_ok = 0;
}
}
}
}
for (int g = 0; g < ngpus; g++) {
p_cuCtxSetCurrent(ctxs[g]);
if (d_acc[g]) p_cuMemFree_v2(d_acc[g]);
if (d_buf_a[g]) p_cuMemFree_v2(d_buf_a[g]);
if (d_buf_b[g]) p_cuMemFree_v2(d_buf_b[g]);
if (d_store_recv[g]) p_cuMemFree_v2(d_store_recv[g]);
}
}
/* Disable all enabled peer access links cleanly. */
for (int i = 0; i < ngpus; i++) {
for (int j = 0; j < ngpus; j++) {
if (peer_enabled[i][j]) {
p_cuCtxSetCurrent(ctxs[i]);
p_cuCtxDisablePeerAccess(ctxs[j]);
}
}
}
}
for (int g = 0; g < ngpus; g++) {
if (ctxs[g]) {
p_cuCtxSetCurrent(ctxs[g]);
if (d_in[g]) p_cuMemFree_v2(d_in[g]);
if (d_out[g]) p_cuMemFree_v2(d_out[g]);
if (mods[g]) p_cuModuleUnload(mods[g]);
p_cuCtxDestroy_v2(ctxs[g]);
}
}
int passed = enum_ok && uuids_distinct && all_kernels_ok && all_peer_enabled &&
all_ring_cpy_ok && memcpy_allreduce_ok &&
direct_p2p_allreduce_ok && direct_p2p_store_ok;
printf("{\"passed\":%s,\"rc_init\":%d,\"rc_count\":%d,\"device_count\":%d,\"uuids_distinct\":%s,",
passed ? "true" : "false", rc_init, rc_count, dev_count,
uuids_distinct ? "true" : "false");
printf("\"gpus\":[");
for (int g = 0; g < ngpus; g++) {
if (g > 0) printf(",");
printf("{\"index\":%d,\"name\":\"%s\",\"uuid\":\"%s\",\"bdf\":\"%s\",\"total_mem\":%zu,"
"\"rc_ctx\":%d,\"rc_launch\":%d,\"kernel_ok\":%s,\"mismatches\":%d,\"digest\":\"0x%016llx\"}",
g, name[g], uuid_str[g], bdf_str[g], total_mem[g],
rc_ctx[g], rc_launch[g], gpu_kernel_ok[g] ? "true" : "false",
gpu_mismatches[g], (unsigned long long)gpu_digest[g]);
}
printf("],");
printf("\"p2p\":{\"all_peer_enabled\":%s,\"all_ring_memcpy_ok\":%s,\"ring_links\":[",
all_peer_enabled ? "true" : "false",
all_ring_cpy_ok ? "true" : "false");
for (int g = 0; g < ngpus; g++) {
int next = (g + 1) % ngpus;
if (g > 0) printf(",");
printf("{\"src\":%d,\"dst\":%d,"
"\"rc_can_fwd\":%d,\"can_fwd\":%d,\"rc_en_fwd\":%d,"
"\"rc_can_bwd\":%d,\"can_bwd\":%d,\"rc_en_bwd\":%d,"
"\"rc_memcpy_fwd\":%d,\"ok_fwd\":%s,\"digest_fwd\":\"0x%016llx\","
"\"rc_memcpy_bwd\":%d,\"ok_bwd\":%s,\"digest_bwd\":\"0x%016llx\"}",
g, next,
rc_can_fwd[g], can_fwd[g], rc_en_fwd[g],
rc_can_bwd[g], can_bwd[g], rc_en_bwd[g],
rc_ring_cpy_fwd[g], ring_cpy_fwd_ok[g] ? "true" : "false",
(unsigned long long)ring_cpy_fwd_digest[g],
rc_ring_cpy_bwd[g], ring_cpy_bwd_ok[g] ? "true" : "false",
(unsigned long long)ring_cpy_bwd_digest[g]);
}
printf("],");
printf("\"expected_allreduce_digest\":\"0x%016llx\",",
(unsigned long long)expected_allreduce_digest);
printf("\"memcpy_allreduce\":{\"rc\":%d,\"ok\":%s,\"digest\":\"0x%016llx\",\"per_gpu_digests\":[",
rc_memcpy_allreduce, memcpy_allreduce_ok ? "true" : "false",
(unsigned long long)(ngpus > 0 ? memcpy_allreduce_digest[0] : 0ULL));
for (int g = 0; g < ngpus; g++) {
if (g > 0) printf(",");
printf("\"0x%016llx\"", (unsigned long long)memcpy_allreduce_digest[g]);
}
printf("],\"per_gpu_mismatches\":[");
for (int g = 0; g < ngpus; g++) {
if (g > 0) printf(",");
printf("%d", memcpy_allreduce_mismatches[g]);
}
printf("]},");
printf("\"direct_p2p_allreduce\":{\"rc\":%d,\"ok\":%s,\"digest\":\"0x%016llx\",\"per_gpu_digests\":[",
rc_direct_p2p_allreduce, direct_p2p_allreduce_ok ? "true" : "false",
(unsigned long long)(ngpus > 0 ? direct_p2p_allreduce_digest[0] : 0ULL));
for (int g = 0; g < ngpus; g++) {
if (g > 0) printf(",");
printf("\"0x%016llx\"", (unsigned long long)direct_p2p_allreduce_digest[g]);
}
printf("],\"per_gpu_mismatches\":[");
for (int g = 0; g < ngpus; g++) {
if (g > 0) printf(",");
printf("%d", direct_p2p_allreduce_mismatches[g]);
}
printf("]},");
printf("\"direct_p2p_store\":{\"rc\":%d,\"ok\":%s,\"digest\":\"0x%016llx\",\"per_gpu_digests\":[",
rc_direct_p2p_store, direct_p2p_store_ok ? "true" : "false",
(unsigned long long)(ngpus > 0 ? direct_p2p_store_digest[0] : 0ULL));
for (int g = 0; g < ngpus; g++) {
if (g > 0) printf(",");
printf("\"0x%016llx\"", (unsigned long long)direct_p2p_store_digest[g]);
}
printf("],\"per_gpu_mismatches\":[");
for (int g = 0; g < ngpus; g++) {
if (g > 0) printf(",");
printf("%d", direct_p2p_store_mismatches[g]);
}
printf("]}}}\n");
for (int g = 0; g < ngpus; g++) {
free(h_in[g]);
free(h_out[g]);
}
free(h_tmp);
free(h_expected_sum);
return passed ? 0 : 1;
}