Files
UnbiasedStrata/tests/core/host_graph_launch_test.cu
Robert Karl 0197ae86eb Import PolyStrata v0.1.45-poly.1 (d0cd84dc) unmodified
The upstream release tree, every tracked file byte for byte, from
https://github.com/VecSzn/PolyStrata at tag v0.1.45-poly.1, commit
d0cd84dc6bd009d1d9e2191e4e28cc76b80f1ee3. Nothing added, removed or edited;
git diff against this commit is the whole story of the fork.

Verified: the commit's tree hash equals the upstream commit's tree hash.
2026-10-17 19:34:32 -07:00

83 lines
3.5 KiB
Plaintext

#include "strata/core/host_graph_launch.hpp"
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <atomic>
#include <thread>
static void check(bool ok, const char* what) {
if (!ok) { std::fprintf(stderr, "FAIL: %s\n", what); std::exit(1); }
}
static void gpu(cudaError_t e) { check(e == cudaSuccess, cudaGetErrorString(e)); }
__global__ void wait_for_cpu(volatile unsigned* flag) {
while (*flag == 0) { }
}
__global__ void tick(unsigned* value) { ++*value; }
int main(int argc, char** argv) {
const bool threaded = !(argc > 1 && std::strcmp(argv[1], "--synchronous") == 0);
cudaStream_t stream;
gpu(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
unsigned *host, *flag, *value;
gpu(cudaHostAlloc(&host, sizeof(unsigned), cudaHostAllocMapped));
gpu(cudaHostGetDevicePointer(&flag, host, 0));
gpu(cudaMalloc(&value, sizeof(unsigned)));
gpu(cudaMemset(value, 0, sizeof(unsigned)));
*host = 0;
cudaGraph_t graph;
cudaGraphExec_t exec;
gpu(cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal));
wait_for_cpu<<<1, 1, 0, stream>>>(flag);
// Enough nodes to exercise launch backpressure under Compute Sanitizer.
for (int i = 0; i < 4096; ++i) tick<<<1, 1, 0, stream>>>(value);
gpu(cudaStreamEndCapture(stream, &graph));
gpu(cudaGraphInstantiate(&exec, graph, nullptr, nullptr, 0));
std::atomic<bool> done{false}, timed_out{false};
std::thread watchdog([&] {
const auto start = std::chrono::steady_clock::now();
while (!done && std::chrono::steady_clock::now() - start < std::chrono::seconds(8))
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (!done) { timed_out = true; *(volatile unsigned*)host = 1; }
});
{
strata::core::HostGraphLaunch launch(exec, stream, 0, threaded,
[&] { *(volatile unsigned*)host = 1; });
gpu(launch.status());
check(launch.query() == cudaErrorNotReady, "graph waits for CPU service");
*(volatile unsigned*)host = 1;
gpu(launch.finish());
gpu(cudaStreamSynchronize(stream));
}
done = true;
watchdog.join();
check(!timed_out, "CPU service proceeds without launch deadlock");
unsigned count = 0;
gpu(cudaMemcpy(&count, value, sizeof(unsigned), cudaMemcpyDeviceToHost));
check(count == 4096, "all graph nodes executed");
if (threaded) {
// An early service failure must release the graph before joining.
*host = 0;
std::atomic<bool> released{false};
{
strata::core::HostGraphLaunch launch(exec, stream, 0, true,
[&] { released = true; *(volatile unsigned*)host = 1; });
}
check(released, "early return releases device waits");
gpu(cudaStreamSynchronize(stream));
gpu(cudaMemcpy(&count, value, sizeof(unsigned), cudaMemcpyDeviceToHost));
check(count == 8192, "early return drains the actual graph");
if (argc > 1 && std::strcmp(argv[1], "--errors") == 0) {
strata::core::HostGraphLaunch invalid(exec, stream, -1, true, [] {});
check(invalid.finish() == cudaErrorInvalidDevice, "launch-thread device error propagated");
(void)cudaGetLastError();
}
}
gpu(cudaGraphExecDestroy(exec));
gpu(cudaGraphDestroy(graph));
gpu(cudaFree(value));
gpu(cudaFreeHost(host));
gpu(cudaStreamDestroy(stream));
std::puts("PASS: graph launch, CPU doorbell service and cleanup");
}