okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bd0e878f4
main
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 { 14size_t total ; 15 std::vector < int > copied ,marked ; 16explicit Round (size_t n ) :total (n ),copied (n ,0 ),marked (n ,0 ) {} 17static 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 ) { 22r .clear (); 23r .next_batch (rd .total ,batch ); 24for (;;) { 25for (size_t i = r .lo ;i < r .hi ;++ i )++ rd .copied [i ]; 26for (size_t i = r .landed_begin (false);i < r .landed_end (false,rd .total );++ i )++ rd .marked [i ]; 27if (!r .more (rd .total ))break ; 28r .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 34for (size_t total : {(size_t )0 , (size_t )1 , (size_t )2 , (size_t )5 , (size_t )8 , (size_t )64 }) 35for (size_t batch : {(size_t )0 , (size_t )1 , (size_t )2 , (size_t )3 , (size_t )7 , (size_t )8 , (size_t )100 }) { 36AdaptiveCopyRange r ; 37Round rd (total ); 38serial_round (r ,rd ,batch ); 39CHECK (Round ::all (rd .copied )&& Round ::all (rd .marked )); 40CHECK (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 { 45AdaptiveCopyRange r ; 46r .next_batch (5 , (size_t )-1 ); 47CHECK (r .lo == 0 && r .hi == 5 && r .bi == 5 ); 48r .next_batch (5 ,2 );// asked again after the end: an empty batch at the end 49CHECK (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 { 55AdaptiveCopyRange r ; 56Round first (5 ); 57serial_round (r ,first ,2 );// leaves lo = 4, hi = 5, bi = 5 58CHECK (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 { 62AdaptiveCopyRange stale = r ; 63Round pl (7 ); 64for (size_t i = 0 ;i < pl .total ;++ i )++ pl .copied [i ];// a_in copied all of them at the gaps 65for (size_t i = stale .landed_begin (false);i < stale .landed_end (false,pl .total );++ i )++ pl .marked [i ]; 66CHECK (!Round ::all (pl .marked ));// the bug this test pins 67 } 68 69// the fix: the pipelined plan names the whole round 70Round pl (7 ); 71r .whole_round (pl .total ); 72for (size_t i = 0 ;i < pl .total ;++ i )++ pl .copied [i ]; 73for (size_t i = r .landed_begin (false);i < r .landed_end (false,pl .total );++ i )++ pl .marked [i ];// the drained tick: serial rule 74CHECK (Round ::all (pl .marked )); 75CHECK (!r .more (pl .total ));// and no second batch is posted 76// the same round finished on the pipelined rule marks the same range 77CHECK (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 { 82AdaptiveCopyRange r ; 83r .next_batch (9 ,4 ); 84r .whole_round (1 ); 85CHECK (r .landed_begin (false)== 0 && r .landed_end (false,1 )== 1 && !r .more (1 )); 86r .whole_round (0 ); 87CHECK (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 { 92AdaptiveCopyRange r ; 93r .whole_round (6 ); 94Round rd (3 ); 95serial_round (r ,rd ,2 ); 96CHECK (Round ::all (rd .copied )&& Round ::all (rd .marked )); 97 } 98 99if (failed ) { std::fprintf (stderr ,"%d check(s) failed\n" ,failed );return 1 ; } 100 std::puts ("adaptive_range_test: ok" ); 101return 0 ; 102}