okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d8684360b
main
1#include "strata/core/host_graph_launch.hpp" 2#include <chrono> 3#include <cstdio> 4#include <cstdlib> 5#include <cstring> 6#include <atomic> 7#include <thread> 8 9static void check(bool ok, const char* what) { 10 if (!ok) { std::fprintf(stderr, "FAIL: %s\n", what); std::exit(1); } 11} 12static void gpu(cudaError_t e) { check(e == cudaSuccess, cudaGetErrorString(e)); } 13__global__ void wait_for_cpu(volatile unsigned* flag) { 14 while (*flag == 0) { } 15} 16__global__ void tick(unsigned* value) { ++*value; } 17 18int main(int argc, char** argv) { 19 const bool threaded = !(argc > 1 && std::strcmp(argv[1], "--synchronous") == 0); 20 cudaStream_t stream; 21 gpu(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking)); 22 unsigned *host, *flag, *value; 23 gpu(cudaHostAlloc(&host, sizeof(unsigned), cudaHostAllocMapped)); 24 gpu(cudaHostGetDevicePointer(&flag, host, 0)); 25 gpu(cudaMalloc(&value, sizeof(unsigned))); 26 gpu(cudaMemset(value, 0, sizeof(unsigned))); 27 *host = 0; 28 cudaGraph_t graph; 29 cudaGraphExec_t exec; 30 gpu(cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal)); 31 wait_for_cpu<<<1, 1, 0, stream>>>(flag); 32 // Enough nodes to exercise launch backpressure under Compute Sanitizer. 33 for (int i = 0; i < 4096; ++i) tick<<<1, 1, 0, stream>>>(value); 34 gpu(cudaStreamEndCapture(stream, &graph)); 35 gpu(cudaGraphInstantiate(&exec, graph, nullptr, nullptr, 0)); 36 std::atomic<bool> done{false}, timed_out{false}; 37 std::thread watchdog([&] { 38 const auto start = std::chrono::steady_clock::now(); 39 while (!done && std::chrono::steady_clock::now() - start < std::chrono::seconds(8)) 40 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 41 if (!done) { timed_out = true; *(volatile unsigned*)host = 1; } 42 }); 43 { 44 strata::core::HostGraphLaunch launch(exec, stream, 0, threaded, 45 [&] { *(volatile unsigned*)host = 1; }); 46 gpu(launch.status()); 47 check(launch.query() == cudaErrorNotReady, "graph waits for CPU service"); 48 *(volatile unsigned*)host = 1; 49 gpu(launch.finish()); 50 gpu(cudaStreamSynchronize(stream)); 51 } 52 done = true; 53 watchdog.join(); 54 check(!timed_out, "CPU service proceeds without launch deadlock"); 55 unsigned count = 0; 56 gpu(cudaMemcpy(&count, value, sizeof(unsigned), cudaMemcpyDeviceToHost)); 57 check(count == 4096, "all graph nodes executed"); 58 if (threaded) { 59 // An early service failure must release the graph before joining. 60 *host = 0; 61 std::atomic<bool> released{false}; 62 { 63 strata::core::HostGraphLaunch launch(exec, stream, 0, true, 64 [&] { released = true; *(volatile unsigned*)host = 1; }); 65 } 66 check(released, "early return releases device waits"); 67 gpu(cudaStreamSynchronize(stream)); 68 gpu(cudaMemcpy(&count, value, sizeof(unsigned), cudaMemcpyDeviceToHost)); 69 check(count == 8192, "early return drains the actual graph"); 70 if (argc > 1 && std::strcmp(argv[1], "--errors") == 0) { 71 strata::core::HostGraphLaunch invalid(exec, stream, -1, true, [] {}); 72 check(invalid.finish() == cudaErrorInvalidDevice, "launch-thread device error propagated"); 73 (void)cudaGetLastError(); 74 } 75 } 76 gpu(cudaGraphExecDestroy(exec)); 77 gpu(cudaGraphDestroy(graph)); 78 gpu(cudaFree(value)); 79 gpu(cudaFreeHost(host)); 80 gpu(cudaStreamDestroy(stream)); 81 std::puts("PASS: graph launch, CPU doorbell service and cleanup"); 82}