okami.mom/strata

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

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

main
4.5 KiB102 linesraw
1// The adaptive tier's copy-in range (PR #4844): which swaps of a round step 3 marks resident.  No GPU: the rounds are
2// walked as generate.cpp walks them, and each swap is a counter that says whether it was copied in and whether it was marked.
3#include <cstdio>
4#include <vector>
5
6#include "strata/program/adaptive_range.hpp"
7
8using strata::program::AdaptiveCopyRange;
9
10static int failed = 0;
11#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
12
13struct Round {
14    size_t total;
15    std::vector<int> copied, marked;
16    explicit Round(size_t n) : total(n), copied(n, 0), marked(n, 0) {}
17    static bool all(const std::vector<int>& v) { for (int x : v) if (x != 1) return false; return true; }
18};
19
20// the serial loop: plan (clear + next_batch), copy the batch, step 3 marks it, repeat while more() (the arena's cap)
21static void serial_round(AdaptiveCopyRange& r, Round& rd, size_t batch) {
22    r.clear();
23    r.next_batch(rd.total, batch);
24    for (;;) {
25        for (size_t i = r.lo; i < r.hi; ++i) ++rd.copied[i];
26        for (size_t i = r.landed_begin(false); i < r.landed_end(false, rd.total); ++i) ++rd.marked[i];
27        if (!r.more(rd.total)) break;
28        r.next_batch(rd.total, batch);
29    }
30}
31
32int main() {
33    // the serial loop covers every swap once, whatever the cap, including zero swaps and one swap
34    for (size_t total : {(size_t) 0, (size_t) 1, (size_t) 2, (size_t) 5, (size_t) 8, (size_t) 64})
35        for (size_t batch : {(size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 7, (size_t) 8, (size_t) 100}) {
36            AdaptiveCopyRange r;
37            Round rd(total);
38            serial_round(r, rd, batch);
39            CHECK(Round::all(rd.copied) && Round::all(rd.marked));
40            CHECK(r.bi == total && r.hi == total);
41        }
42
43    // a batch never runs past the list, even for a cap near the size_t limit
44    {
45        AdaptiveCopyRange r;
46        r.next_batch(5, (size_t) -1);
47        CHECK(r.lo == 0 && r.hi == 5 && r.bi == 5);
48        r.next_batch(5, 2);                     // asked again after the end: an empty batch at the end
49        CHECK(r.lo == 5 && r.hi == 5 && !r.more(5));
50    }
51
52    // the lost experts of #4844: a round planned on the pipelined loop whose loop drained before step 3 is marked on the
53    // serial rule.  An earlier serial round left [lo, hi) at its last batch.
54    {
55        AdaptiveCopyRange r;
56        Round first(5);
57        serial_round(r, first, 2);              // leaves lo = 4, hi = 5, bi = 5
58        CHECK(r.lo == 4 && r.hi == 5);
59
60        // the old plan (nothing set for a pipelined round): step 3 on the serial rule marks only the stale range
61        {
62            AdaptiveCopyRange stale = r;
63            Round pl(7);
64            for (size_t i = 0; i < pl.total; ++i) ++pl.copied[i];                       // a_in copied all of them at the gaps
65            for (size_t i = stale.landed_begin(false); i < stale.landed_end(false, pl.total); ++i) ++pl.marked[i];
66            CHECK(!Round::all(pl.marked));                                              // the bug this test pins
67        }
68
69        // the fix: the pipelined plan names the whole round
70        Round pl(7);
71        r.whole_round(pl.total);
72        for (size_t i = 0; i < pl.total; ++i) ++pl.copied[i];
73        for (size_t i = r.landed_begin(false); i < r.landed_end(false, pl.total); ++i) ++pl.marked[i];   // the drained tick: serial rule
74        CHECK(Round::all(pl.marked));
75        CHECK(!r.more(pl.total));                                                       // and no second batch is posted
76        // the same round finished on the pipelined rule marks the same range
77        CHECK(r.landed_begin(true) == r.landed_begin(false) && r.landed_end(true, pl.total) == r.landed_end(false, pl.total));
78    }
79
80    // one swap, and an empty round, on the pipelined plan
81    {
82        AdaptiveCopyRange r;
83        r.next_batch(9, 4);
84        r.whole_round(1);
85        CHECK(r.landed_begin(false) == 0 && r.landed_end(false, 1) == 1 && !r.more(1));
86        r.whole_round(0);
87        CHECK(r.landed_begin(false) == 0 && r.landed_end(false, 0) == 0 && !r.more(0));
88    }
89
90    // a serial round that follows a pipelined one starts clean
91    {
92        AdaptiveCopyRange r;
93        r.whole_round(6);
94        Round rd(3);
95        serial_round(r, rd, 2);
96        CHECK(Round::all(rd.copied) && Round::all(rd.marked));
97    }
98
99    if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; }
100    std::puts("adaptive_range_test: ok");
101    return 0;
102}