// fault_probe.cu: one CUDA process that faults on purpose, for the GPU-sharing article. // What does an illegal-address fault in one client do to a serving engine on the same GPU, // with separate contexts (time-slicing) and under MPS? // // nvcc -O2 -arch=sm_121 -o fault_probe fault_probe.cu // ./fault_probe ok # control: a valid kernel, exits 0 // ./fault_probe fault # writes to an unmapped device address: expect cudaErrorIllegalAddress (exit 2) // // Prints the error returned by the kernel's synchronize and by a later call (sticky or not), then exits. #include #include #include __global__ void touch(int *p, int n) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < n) p[i] = i; } int main(int argc, char **argv) { bool fault = argc > 1 && strcmp(argv[1], "fault") == 0; int *buf = nullptr; cudaError_t e = cudaMalloc(&buf, 1 << 20); printf("cudaMalloc: %s\n", cudaGetErrorString(e)); if (e != cudaSuccess) return 1; // Fault case: a pointer far outside any allocation (low addresses are never mapped for the GPU). int *target = fault ? reinterpret_cast(0x10) : buf; touch<<<256, 256>>>(target, 256 * 256); cudaError_t launch = cudaGetLastError(); cudaError_t sync = cudaDeviceSynchronize(); printf("launch: %s | sync: %s\n", cudaGetErrorString(launch), cudaGetErrorString(sync)); cudaError_t after = cudaMalloc(&buf, 1 << 20); // sticky errors poison the context printf("next call: %s\n", cudaGetErrorString(after)); fflush(stdout); return sync == cudaSuccess ? 0 : 2; }