| 52 | } |
| 53 | |
| 54 | bool CapturedGraph::end(void* stream, std::string& err) { |
| 55 | cudaError_t e = cudaStreamEndCapture((cudaStream_t) stream, &graph_); |
| 56 | if (e != cudaSuccess) { graph_ = nullptr; return fail(err, "cudaStreamEndCapture", e); } |
| 57 | |
| 58 | nodes_ = 0; |
| 59 | e = cudaGraphGetNodes(graph_, nullptr, &nodes_); |
| 60 | if (e != cudaSuccess) return fail(err, "cudaGraphGetNodes", e); |
| 61 | // A capture that recorded NOTHING is a wiring mistake, and a graph that replays nothing produces no error |
| 62 | // and no output - the silent kind of failure this project keeps paying for. |
| 63 | if (nodes_ == 0) { err = "end: the capture recorded ZERO nodes - the body launched nothing"; reset(); return false; } |
| 64 | |
| 65 | e = cudaGraphInstantiate(&exec_, graph_, nullptr, nullptr, 0); |
| 66 | if (e != cudaSuccess) return fail(err, "cudaGraphInstantiate", e); |
| 67 | |
| 68 | // The completion event is recorded ONCE and reused: `launch` records it again after each replay, which is |
| 69 | // what makes `wait_ms` a query rather than a sync. |
| 70 | e = cudaEventCreateWithFlags(&done_, cudaEventDisableTiming); |
| 71 | if (e != cudaSuccess) return fail(err, "cudaEventCreateWithFlags", e); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool CapturedGraph::launch(void* stream, std::string& err) const { |
| 76 | if (!exec_) { err = "launch: not recorded"; return false; } |