okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0216-core into f-0216-allbd0e878f4

main
6.1 KiB125 linesraw
1// Flag B behind queued copies (PR #5096): an empty plan must not release a layer whose uploads are still in flight.
2// A delayed host function stands in for a slow DMA on the copy stream (it writes the "staging" word a while after it was
3// queued); a kernel plays the graph (waits for flag B >= value, then reads staging).  Run on every device found.
4#include "strata/core/flag_order.hpp"
5
6#include <chrono>
7#include <cstdio>
8#include <cstdlib>
9#include <thread>
10
11using strata::core::FlagRaise;
12using strata::core::flag_raise_max;
13using strata::core::raise_flag_b_ordered;
14
15static int failed = 0;
16#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
17#define CK(call) do { const cudaError_t e_ = (call); if (e_ != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", #call, cudaGetErrorString(e_)); return 1; } } while (0)
18
19struct SlowCopy { volatile uint32_t* staging; uint32_t word; int ms; };
20static void slow_copy(void* p) {
21    SlowCopy* c = (SlowCopy*) p;
22    std::this_thread::sleep_for(std::chrono::milliseconds(c->ms));
23    *c->staging = c->word;   // the final writer of the buffer
24}
25
26__global__ void consumer(volatile uint32_t* flag, uint32_t want, volatile uint32_t* staging, volatile uint32_t* seen) {
27    while (*flag < want) {}
28    *seen = *staging;        // what the graph's PCIe group would read
29}
30
31static uint32_t rd(volatile uint32_t* p) { return __atomic_load_n((uint32_t*) p, __ATOMIC_SEQ_CST); }
32
33static int run_device(int dev) {
34    CK(cudaSetDevice(dev));
35    cudaStream_t copy, cs;
36    CK(cudaStreamCreateWithFlags(&copy, cudaStreamNonBlocking));
37    CK(cudaStreamCreateWithFlags(&cs, cudaStreamNonBlocking));
38    volatile uint32_t *flag_h, *staging_h, *seen_h;
39    CK(cudaHostAlloc((void**) &flag_h, 64, cudaHostAllocMapped));
40    CK(cudaHostAlloc((void**) &staging_h, 64, cudaHostAllocMapped));
41    CK(cudaHostAlloc((void**) &seen_h, 64, cudaHostAllocMapped));
42    volatile uint32_t *flag_d, *staging_d, *seen_d;
43    CK(cudaHostGetDevicePointer((void**) &flag_d, (void*) flag_h, 0));
44    CK(cudaHostGetDevicePointer((void**) &staging_d, (void*) staging_h, 0));
45    CK(cudaHostGetDevicePointer((void**) &seen_d, (void*) seen_h, 0));
46    FlagRaise slots[4] = {};
47    SlowCopy c0{staging_h, 0xAAu, 150};
48
49    // 1. nonempty plan (layer 0) followed by an empty plan (layer 1) while the copy is delayed
50    *flag_h = 0; *staging_h = 0; *seen_h = 0;
51    CK(cudaLaunchHostFunc(copy, slow_copy, &c0));                                // layer 0's upload, in flight
52    CK(raise_flag_b_ordered(true, copy, &slots[0], (uint32_t*) flag_h, 1));      // its completion flag (non-empty plan)
53    CK(raise_flag_b_ordered(true, copy, &slots[1], (uint32_t*) flag_h, 2));      // layer 1: empty plan, same copy stream
54    CHECK(rd(flag_h) == 0);                                                      // neither flag may rise before the delayed copy
55    consumer<<<1, 1, 0, cs>>>(flag_d, 1, staging_d, seen_d);                     // the graph's layer-0 group
56    CK(cudaStreamSynchronize(cs));
57    CHECK(rd(seen_h) == 0xAAu);                                                  // the consumer saw the final writer's bytes
58    CK(cudaStreamSynchronize(copy));
59    CHECK(rd(flag_h) == 2);
60
61    // 2. the negative control: a direct raise (the old empty-plan path) does release the consumer early
62    *flag_h = 0; *staging_h = 0; *seen_h = 0;
63    CK(cudaLaunchHostFunc(copy, slow_copy, &c0));
64    flag_raise_max((uint32_t*) flag_h, 2);                                       // what an unordered empty plan did
65    consumer<<<1, 1, 0, cs>>>(flag_d, 1, staging_d, seen_d);
66    CK(cudaStreamSynchronize(cs));
67    CHECK(rd(seen_h) == 0);                                                      // read before the copy landed: the bug the ordered raise removes
68    CK(cudaStreamSynchronize(copy));
69
70    // 3. nothing queued in this window: the flag rises at once
71    *flag_h = 0;
72    CK(raise_flag_b_ordered(false, copy, &slots[2], (uint32_t*) flag_h, 3));
73    CHECK(rd(flag_h) == 3);
74
75    // 4. the flag only rises: a late host function of an earlier layer does not lower it
76    *flag_h = 5;
77    CK(raise_flag_b_ordered(true, copy, &slots[3], (uint32_t*) flag_h, 4));
78    CK(cudaStreamSynchronize(copy));
79    CHECK(rd(flag_h) == 5);
80
81    // 5. an idle copy stream with copies_queued still set (the window's earlier copies finished): raised through the stream
82    *flag_h = 0;
83    CK(raise_flag_b_ordered(true, copy, &slots[0], (uint32_t*) flag_h, 6));
84    CK(cudaStreamSynchronize(copy));
85    CHECK(rd(flag_h) == 6);
86
87    // 6. through a graph: the consumer is captured and replayed twice, a fresh delayed copy each time
88    {
89        cudaGraph_t graph; cudaGraphExec_t exec;
90        CK(cudaStreamBeginCapture(cs, cudaStreamCaptureModeThreadLocal));
91        consumer<<<1, 1, 0, cs>>>(flag_d, 7, staging_d, seen_d);
92        CK(cudaStreamEndCapture(cs, &graph));
93        CK(cudaGraphInstantiate(&exec, graph, nullptr, nullptr, 0));
94        for (int rep = 0; rep < 2; ++rep) {
95            *flag_h = 0; *staging_h = 0; *seen_h = 0;
96            SlowCopy c{staging_h, 0x55u + (uint32_t) rep, 60};
97            CK(cudaLaunchHostFunc(copy, slow_copy, &c));
98            CK(raise_flag_b_ordered(true, copy, &slots[1], (uint32_t*) flag_h, 7));
99            CK(cudaGraphLaunch(exec, cs));
100            CK(cudaStreamSynchronize(cs));
101            CHECK(rd(seen_h) == 0x55u + (uint32_t) rep);
102            CK(cudaStreamSynchronize(copy));
103        }
104        CK(cudaGraphExecDestroy(exec));
105        CK(cudaGraphDestroy(graph));
106    }
107    CK(cudaStreamDestroy(copy));
108    CK(cudaStreamDestroy(cs));
109    CK(cudaFreeHost((void*) flag_h)); CK(cudaFreeHost((void*) staging_h)); CK(cudaFreeHost((void*) seen_h));
110    return 0;
111}
112
113int main() {
114    int n = 0;
115    CK(cudaGetDeviceCount(&n));
116    if (n == 0) { std::puts("flag_order_test: no device, skipped"); return 0; }
117    for (int d = 0; d < n; ++d) {
118        const int rc = run_device(d);
119        if (rc != 0) return rc;
120        std::printf("flag_order_test: device %d ok\n", d);
121    }
122    if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; }
123    std::puts("flag_order_test: ok");
124    return 0;
125}