okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fe57a0cb5
main
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) { 28PfRingOp o {k ,part ,hop ,step }; 29o .fin = fin ; 30return 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 ) { 36const 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 )}; 38if (n == 2 ) { 39for (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 ), 40op (K ::WAIT ,-1 ,-1 ,1 ),op (K ::OUT )}) 41v .push_back (o ); 42return v ; 43 } 44for (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 ), 45op (K ::WAIT ,-1 ,-1 ,1 ),op (K ::ADD ,c ,1 ,-1 , true),op (K ::PASS ,c ),op (K ::SIGNAL ,-1 ,-1 ,2 ), 46op (K ::WAIT ,-1 ,-1 ,2 ),op (K ::PASS ,a ),op (K ::SIGNAL ,-1 ,-1 ,3 ),op (K ::WAIT ,-1 ,-1 ,3 ),op (K ::OUT )}) 47v .push_back (o ); 48return 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 { 55bool full = false; 56unsigned seq = 0 ; 57int part = -1 ; 58unsigned addends = 0 ; 59int step = -1 ; 60bool 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 ) { 68const std::string tag = std::to_string (n )+ " cards, order " + std::to_string (seed )+ ": " ; 69const unsigned all = (1u <<n )- 1 ; 70 std::vector < std::vector < PfRingOp >> plan (n ); 71for (int g = 0 ;g < n ;++ g )plan [g ]= fnx::pf_ring_plan (n ,g ); 72const int steps = fnx::pf_ring_steps (n ); 73struct Copied { 74bool ag ; 75int i ; 76 }; 77struct Card { 78size_t pc = 0 ; 79unsigned 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] 82Slot 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 ); 86for (Card & c :cd ) { 87c .flag .assign (2 * steps ,0 ); 88c .waited .assign (2 * steps ,0 ); 89c .in .assign (2 * (n - 1 ), {}); 90c .ag .assign (2 * n , {}); 91 } 92 std::mt19937 rng (seed ); 93auto readable = [& ](const Card & c ,const Slot & s ,int set ) { 94return 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 97auto writable = [& ](const Slot & s ,unsigned seq ,const std::string & at ,const char * what ) { 98if (s .full && s .seq == seq ) {fail (at + what + " written twice in one sum" );return false; } 99if (s .full && !s .consumed ) { 100fail (at + what + " of sum " + std::to_string (s .seq )+ " written over by sum " + std::to_string (seq )+ " before it was read" ); 101return false; 102 } 103return true; 104 }; 105for (;;) { 106 std::vector < int > ready ; 107bool left = false; 108for (int g = 0 ;g < n ;++ g ) { 109const Card & c = cd [g ]; 110if (c .seq > (unsigned )sums )continue ; 111left = true; 112const PfRingOp & o = plan [g ][c .pc ]; 113if (o .k == K ::WAIT ) { 114if (o .step < 0 || o .step >=steps ) {fail (tag + "a flag past PF_RING_STEPS" );return false; } 115if (c .flag [(size_t ) ((c .seq & 1 )* steps + o .step )]< c .seq )continue ; 116 } 117ready .push_back (g ); 118 } 119if (!left )break ; 120if (ready .empty ()) { 121for (int g = 0 ;g < n ;++ g ) 122if (cd [g ].seq <= (unsigned )sums ) 123fail (tag + "card " + std::to_string (g )+ " stuck in sum " + std::to_string (cd [g ].seq )+ " at op " + std::to_string (cd [g ].pc )); 124return false; 125 } 126const int g = ready [rng () %ready .size ()]; 127Card & c = cd [g ]; 128Card & nx = cd [(g + 1 ) %n ]; 129const int set = (int ) (c .seq & 1u ); 130const PfRingOp & o = plan [g ][c .pc ]; 131const std::string at = tag + "card " + std::to_string (g )+ " sum " + std::to_string (c .seq )+ " op " + std::to_string (c .pc )+ ": " ; 132if ((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; } 133auto in = [& ](Card & k ,int hop )-> Slot & {return k .in [(size_t ) (set * (n - 1 )+ hop )]; }; 134auto ag = [& ](Card & k ,int part )-> Slot & {return k .ag [(size_t ) (set * n + part )]; }; 135if (o .k == K ::WAIT ) { 136c .waited [(size_t ) (set * steps + o .step )]= c .seq ; 137 }else if (o .k == K ::PACK ) { 138c .send = {true,c .seq ,o .part ,1u <<g ,-1 , false}; 139 }else if (o .k == K ::ADD ) { 140Slot & r = in (c ,o .hop ); 141if (!readable (c ,r ,set )) {fail (at + "adds a receive slot not yet covered by a flag it waited on" );return false; } 142if (r .part != o .part )fail (at + "the receive slot holds another part" ); 143if (r .addends & (1u <<g ))fail (at + "adds its own addend twice" ); 144r .consumed = true; 145const Slot s {true,c .seq ,o .part ,r .addends | (1u <<g ),-1 , false}; 146if (o .fin ) { 147if (s .addends != all )fail (at + "finishes a part without every card's addend" ); 148if (!writable (ag (c ,o .part ),c .seq ,at ,"a gathered part" ))return false; 149ag (c ,o .part )= s ; 150 }else { 151c .send = s ; 152 } 153 }else if (o .k == K ::SEND ) { 154if (!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; } 155if (!writable (in (nx ,o .hop ),c .seq ,at ,"the next card's receive slot" ))return false; 156Slot s = c .send ; 157s .step = -2 ; 158in (nx ,o .hop )= s ; 159c .pending .push_back ({false,o .hop }); 160 }else if (o .k == K ::PASS ) { 161const Slot & f = ag (c ,o .part ); 162if (!readable (c ,f ,set )) {fail (at + "passes a part not here yet" );return false; } 163if (f .addends != all )fail (at + "passes an unfinished part" ); 164if (!writable (ag (nx ,o .part ),c .seq ,at ,"the next card's gathered part" ))return false; 165Slot s = f ; 166s .step = -2 ; 167s .consumed = false; 168ag (nx ,o .part )= s ; 169c .pending .push_back ({true,o .part }); 170 }else if (o .k == K ::SIGNAL ) { 171if (o .step < 0 || o .step >=steps ) {fail (at + "a flag past PF_RING_STEPS" );return false; } 172nx .flag [(size_t ) (set * steps + o .step )]= c .seq ; 173for (const Copied & p :c .pending ) (p .ag ?ag (nx ,p .i ) :in (nx ,p .i )).step = o .step ; 174c .pending .clear (); 175 }else if (o .k == K ::OUT ) { 176for (int k = 0 ;k < n ;++ k ) { 177Slot & s = ag (c ,k ); 178if (!readable (c ,s ,set )|| s .addends != all ) { 179fail (at + "the sum read out without part " + std::to_string (k )+ " finished and covered" ); 180return false; 181 } 182s .consumed = true; 183 } 184if (!c .pending .empty ()) {fail (at + "copied after its last flag" );return false; } 185 }else { 186fail (at + "a step of unknown kind " + std::to_string ((int )o .k )); 187return false; 188 } 189if (++ c .pc == plan [g ].size ()) { 190c .pc = 0 ; 191++ c .seq ; 192 } 193 } 194return true; 195} 196 197int main () { 198for (int n : {2 ,3 }) 199for (int g = 0 ;g < n ;++ g ) 200if (fnx::pf_ring_plan (n ,g )!= written (n ,g )) 201fail (std::to_string (n )+ " cards, card " + std::to_string (g )+ ": the plan is not the written sequence" ); 202for (const auto & [n ,g ] : std::vector < std::pair < int ,int >> {{1 ,0 }, {0 ,0 }, {3 ,3 }, {3 ,-1 }}) { 203try { 204 fnx::pf_ring_plan (n ,g ); 205fail ("pf_ring_plan(" + std::to_string (n )+ ", " + std::to_string (g )+ ") made a plan" ); 206 }catch (const std::invalid_argument & ) { 207 } 208 } 209constexpr int SUMS = 8 ,ORDERS = 500 ; 210int counts = 0 ; 211for (int n = 2 ;n <= fnx::NCARD_MAX ;++ n ) 212if (fnx::ncard_ok (n )) { 213for (unsigned seed = 0 ;seed < ORDERS ;++ seed ) 214if (!simulate (n ,SUMS ,seed ))break ; 215++ counts ; 216 } 217if (fails ) { 218 std::printf ("%d failures\n" ,fails ); 219return 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" , 223counts , fnx::NCARD_MAX ,SUMS ,ORDERS ); 224return 0 ; 225}