Hard
Ping-pong buffers driven by repeated cudaGraphLaunch calls
An iterative solver used to launch step 1000 times, swapping d_a and d_b after each launch. To cut launch overhead, the engineer captures one launch into a CUDA graph and replays it, keeping the swap. step reads its first argument and writes its second, and the solver expects the final state in d_a after the loop. Every API call returns cudaSuccess. What does d_a hold after the synchronize?
solver.cu
float *d_a, *d_b; // d_a holds the initial state
// ... allocate and initialize ...
cudaGraph_t graph;
cudaGraphExec_t exec;
cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal);
step<<<grid, block, 0, stream>>>(d_a, d_b); // reads d_a, writes d_b
cudaStreamEndCapture(stream, &graph);
cudaGraphInstantiate(&exec, graph, 0);
for (int it = 0; it < 1000; ++it) {
cudaGraphLaunch(exec, stream);
std::swap(d_a, d_b); // next step should read what this one wrote
}
cudaStreamSynchronize(stream);39 of 46 in GPU / CUDA