okami.mom/strata

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

Niko1221rc1 merge split (1ba231217)fe57a0cb5

main
10.6 KiB225 linesraw
1// The prompt path's copy-engine ring plan (src/pf_ring_plan.hpp), on the CPU alone:
2//   1. two and three cards: the plan is exactly the step sequence pf_ring_run (pf_common.cuh) runs for them, written
3//      out below, for every card;
4//   2. every count card_split.hpp allows: the n cards' plans run against each other for several sums in a row, over
5//      the two buffer sets and the sequence-numbered flags the streams use, in many random interleavings: every sum's
6//      parts hold every card's addend once, nothing is read on a card before the flag that covers it for that sum, no
7//      slot is written twice in a sum or over a previous sum's contents before they were read, no deadlock, and no
8//      more flags than PF_RING_STEPS holds.
9// Exit 0 when all pass; each failure printed.
10#include <cstdio>
11#include <random>
12#include <string>
13#include <vector>
14
15#include "../src/model.hpp"   // card_split.hpp: the supported counts
16#include "../src/pf_ring_plan.hpp"
17
18using fnx::PfRingOp;
19using K = PfRingOp::Kind;
20
21static int fails = 0;
22static void fail(const std::string& m) {
23    std::printf("FAIL: %s\n", m.c_str());
24    ++fails;
25}
26
27static PfRingOp op(K k, int part = -1, int hop = -1, int step = -1, bool fin = false) {
28    PfRingOp o{k, part, hop, step};
29    o.fin = fin;
30    return o;
31}
32
33// the ring as pf_common.cuh's pf_ring_run was written to run it for three cards (a = g, b = g - 1, c = g + 1) and two
34// (c = b = the other)
35static std::vector<PfRingOp> written(int n, int g) {
36    const int a = g, b = (g + n - 1) % n, c = (g + 1) % n;
37    std::vector<PfRingOp> v = {op(K::PACK, a), op(K::SEND, a, 0), op(K::SIGNAL, -1, -1, 0)};
38    if (n == 2) {
39        for (const PfRingOp& o : {op(K::WAIT, -1, -1, 0), op(K::ADD, c, 0, -1, true), op(K::PASS, c), op(K::SIGNAL, -1, -1, 1),
40                                  op(K::WAIT, -1, -1, 1), op(K::OUT)})
41            v.push_back(o);
42        return v;
43    }
44    for (const PfRingOp& o : {op(K::WAIT, -1, -1, 0), op(K::ADD, b, 0), op(K::SEND, b, 1), op(K::SIGNAL, -1, -1, 1),
45                              op(K::WAIT, -1, -1, 1), op(K::ADD, c, 1, -1, true), op(K::PASS, c), op(K::SIGNAL, -1, -1, 2),
46                              op(K::WAIT, -1, -1, 2), op(K::PASS, a), op(K::SIGNAL, -1, -1, 3), op(K::WAIT, -1, -1, 3), op(K::OUT)})
47        v.push_back(o);
48    return v;
49}
50
51// a buffer's contents in the simulation: the sum it belongs to (its sequence number), which part, whose addends (bit
52// per card), the flag that publishes it on the card holding it (-1: written there by the card itself, -2: copied in,
53// its flag not yet written), and whether this card is through reading it (a later sum may then write over it)
54struct Slot {
55    bool full = false;
56    unsigned seq = 0;
57    int part = -1;
58    unsigned addends = 0;
59    int step = -1;
60    bool consumed = false;
61};
62
63// `sums` sums one after another on every card, as pf_ring runs them: sum seq (from 1, after pf_seq_reset's 0) uses buffer
64// set seq & 1, a signal writes seq into the flag (cuStreamWriteValue32) and a wait passes once the flag is >= seq
65// (CU_STREAM_WAIT_VALUE_GEQ). Each card's steps are one stream (in order); the cards interleave as the random order
66// picks, so a card may run sums ahead of a slower one as far as the flags let it. Returns false after a failure.
67static bool simulate(int n, int sums, unsigned seed) {
68    const std::string tag = std::to_string(n) + " cards, order " + std::to_string(seed) + ": ";
69    const unsigned all = (1u << n) - 1;
70    std::vector<std::vector<PfRingOp>> plan(n);
71    for (int g = 0; g < n; ++g) plan[g] = fnx::pf_ring_plan(n, g);
72    const int steps = fnx::pf_ring_steps(n);
73    struct Copied {
74        bool ag;
75        int i;
76    };
77    struct Card {
78        size_t pc = 0;
79        unsigned seq = 1;
80        std::vector<unsigned> flag, waited;   // [set][step]: the flag's value; the sum that last waited on it
81        std::vector<Slot> in, ag;             // [set][hop], [set][part]
82        Slot send;
83        std::vector<Copied> pending;          // copied into the next card since this card's last signal (this sum's set)
84    };
85    std::vector<Card> cd(n);
86    for (Card& c : cd) {
87        c.flag.assign(2 * steps, 0);
88        c.waited.assign(2 * steps, 0);
89        c.in.assign(2 * (n - 1), {});
90        c.ag.assign(2 * n, {});
91    }
92    std::mt19937 rng(seed);
93    auto readable = [&](const Card& c, const Slot& s, int set) {
94        return s.full && s.seq == c.seq && (s.step == -1 || (s.step >= 0 && c.waited[(size_t) (set * steps + s.step)] == c.seq));
95    };
96    // a write into a slot: the sum before on this set must have been read out of it, and this sum not yet written
97    auto writable = [&](const Slot& s, unsigned seq, const std::string& at, const char* what) {
98        if (s.full && s.seq == seq) { fail(at + what + " written twice in one sum"); return false; }
99        if (s.full && !s.consumed) {
100            fail(at + what + " of sum " + std::to_string(s.seq) + " written over by sum " + std::to_string(seq) + " before it was read");
101            return false;
102        }
103        return true;
104    };
105    for (;;) {
106        std::vector<int> ready;
107        bool left = false;
108        for (int g = 0; g < n; ++g) {
109            const Card& c = cd[g];
110            if (c.seq > (unsigned) sums) continue;
111            left = true;
112            const PfRingOp& o = plan[g][c.pc];
113            if (o.k == K::WAIT) {
114                if (o.step < 0 || o.step >= steps) { fail(tag + "a flag past PF_RING_STEPS"); return false; }
115                if (c.flag[(size_t) ((c.seq & 1) * steps + o.step)] < c.seq) continue;
116            }
117            ready.push_back(g);
118        }
119        if (!left) break;
120        if (ready.empty()) {
121            for (int g = 0; g < n; ++g)
122                if (cd[g].seq <= (unsigned) sums)
123                    fail(tag + "card " + std::to_string(g) + " stuck in sum " + std::to_string(cd[g].seq) + " at op " + std::to_string(cd[g].pc));
124            return false;
125        }
126        const int g = ready[rng() % ready.size()];
127        Card& c = cd[g];
128        Card& nx = cd[(g + 1) % n];
129        const int set = (int) (c.seq & 1u);
130        const PfRingOp& o = plan[g][c.pc];
131        const std::string at = tag + "card " + std::to_string(g) + " sum " + std::to_string(c.seq) + " op " + std::to_string(c.pc) + ": ";
132        if ((o.k == K::ADD || o.k == K::SEND) && (o.hop < 0 || o.hop >= n - 1)) { fail(at + "a receive slot past n - 1 hops"); return false; }
133        auto in = [&](Card& k, int hop) -> Slot& { return k.in[(size_t) (set * (n - 1) + hop)]; };
134        auto ag = [&](Card& k, int part) -> Slot& { return k.ag[(size_t) (set * n + part)]; };
135        if (o.k == K::WAIT) {
136            c.waited[(size_t) (set * steps + o.step)] = c.seq;
137        } else if (o.k == K::PACK) {
138            c.send = {true, c.seq, o.part, 1u << g, -1, false};
139        } else if (o.k == K::ADD) {
140            Slot& r = in(c, o.hop);
141            if (!readable(c, r, set)) { fail(at + "adds a receive slot not yet covered by a flag it waited on"); return false; }
142            if (r.part != o.part) fail(at + "the receive slot holds another part");
143            if (r.addends & (1u << g)) fail(at + "adds its own addend twice");
144            r.consumed = true;
145            const Slot s{true, c.seq, o.part, r.addends | (1u << g), -1, false};
146            if (o.fin) {
147                if (s.addends != all) fail(at + "finishes a part without every card's addend");
148                if (!writable(ag(c, o.part), c.seq, at, "a gathered part")) return false;
149                ag(c, o.part) = s;
150            } else {
151                c.send = s;
152            }
153        } else if (o.k == K::SEND) {
154            if (!c.send.full || c.send.seq != c.seq || c.send.part != o.part) { fail(at + "sends what the send buffer does not hold"); return false; }
155            if (!writable(in(nx, o.hop), c.seq, at, "the next card's receive slot")) return false;
156            Slot s = c.send;
157            s.step = -2;
158            in(nx, o.hop) = s;
159            c.pending.push_back({false, o.hop});
160        } else if (o.k == K::PASS) {
161            const Slot& f = ag(c, o.part);
162            if (!readable(c, f, set)) { fail(at + "passes a part not here yet"); return false; }
163            if (f.addends != all) fail(at + "passes an unfinished part");
164            if (!writable(ag(nx, o.part), c.seq, at, "the next card's gathered part")) return false;
165            Slot s = f;
166            s.step = -2;
167            s.consumed = false;
168            ag(nx, o.part) = s;
169            c.pending.push_back({true, o.part});
170        } else if (o.k == K::SIGNAL) {
171            if (o.step < 0 || o.step >= steps) { fail(at + "a flag past PF_RING_STEPS"); return false; }
172            nx.flag[(size_t) (set * steps + o.step)] = c.seq;
173            for (const Copied& p : c.pending) (p.ag ? ag(nx, p.i) : in(nx, p.i)).step = o.step;
174            c.pending.clear();
175        } else if (o.k == K::OUT) {
176            for (int k = 0; k < n; ++k) {
177                Slot& s = ag(c, k);
178                if (!readable(c, s, set) || s.addends != all) {
179                    fail(at + "the sum read out without part " + std::to_string(k) + " finished and covered");
180                    return false;
181                }
182                s.consumed = true;
183            }
184            if (!c.pending.empty()) { fail(at + "copied after its last flag"); return false; }
185        } else {
186            fail(at + "a step of unknown kind " + std::to_string((int) o.k));
187            return false;
188        }
189        if (++c.pc == plan[g].size()) {
190            c.pc = 0;
191            ++c.seq;
192        }
193    }
194    return true;
195}
196
197int main() {
198    for (int n : {2, 3})
199        for (int g = 0; g < n; ++g)
200            if (fnx::pf_ring_plan(n, g) != written(n, g))
201                fail(std::to_string(n) + " cards, card " + std::to_string(g) + ": the plan is not the written sequence");
202    for (const auto& [n, g] : std::vector<std::pair<int, int>>{{1, 0}, {0, 0}, {3, 3}, {3, -1}}) {
203        try {
204            fnx::pf_ring_plan(n, g);
205            fail("pf_ring_plan(" + std::to_string(n) + ", " + std::to_string(g) + ") made a plan");
206        } catch (const std::invalid_argument&) {
207        }
208    }
209    constexpr int SUMS = 8, ORDERS = 500;
210    int counts = 0;
211    for (int n = 2; n <= fnx::NCARD_MAX; ++n)
212        if (fnx::ncard_ok(n)) {
213            for (unsigned seed = 0; seed < ORDERS; ++seed)
214                if (!simulate(n, SUMS, seed)) break;
215            ++counts;
216        }
217    if (fails) {
218        std::printf("%d failures\n", fails);
219        return 1;
220    }
221    std::printf("pf ring plan: two and three cards as written; %d counts (2 to %d): %d sums in a row over the two buffer "
222                "sets complete, %d random orders each\n",
223                counts, fnx::NCARD_MAX, SUMS, ORDERS);
224    return 0;
225}