okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1// metal/tests/gridbar_bench.cpp - phases of a streaming read separated by dispatch boundaries vs by an in-kernel 2// grid barrier (gridbar.metal), and whether a phase sees the previous phase's writes of the other threadgroups. 3// gridbar_bench [MiB per phase] [phases] 4#include "mtl_rt.h" 5 6#include <algorithm> 7#include <cstdio> 8#include <cstdlib> 9#include <vector> 10 11int main (int argc ,char ** argv ) { 12const double mib = argc > 1 ? std::atof (argv [1 ]) :4.5 ; 13const int phases = argc > 2 ? std::atoi (argv [2 ]) :96 ; 14 mtl::Context cx ; 15const int lib = cx .compile ({"gridbar.metal" }, std::getenv ("GB_FENCE" ) ?"#define NO_GGML_COMMON 1\n#define GB_FENCE 1\n" 16 :"#define NO_GGML_COMMON 1\n" ); 17 mtl::Pipeline pp = cx .pipeline (lib ,"k_phases" ),p1 = cx .pipeline (lib ,"k_phase1" ); 18const uint32_t n = (uint32_t )(mib * 1048576 /16 ); 19 mtl::Buffer src = cx .alloc ((size_t )n * 16 * phases ),out = cx .alloc (4u <<20 ),bar = cx .alloc (4 ), 20flag = cx .alloc (4 * 64 * (size_t )phases ),bad = cx .alloc (4 ); 21for (size_t i = 0 ;i < src .bytes /4 ;i += 1024 )src .as < uint32_t > ()[i ]= (uint32_t )i ; 22 std::printf ("device %s: %d phases of %.1f MiB\n" ,cx .device_name ().c_str (),phases ,mib ); 23// dispatch boundaries 24for (uint32_t groups : {40u ,160u ,640u }) { 25 std::vector < mtl::Dispatch > ds ; 26for (int p = 0 ;p < phases ;++ p ) { 27 mtl::Dispatch d ; 28d .p = & p1 ; 29const uint32_t pu = p ; 30d .args = {mtl::Arg ::B (src ), mtl::Arg ::B (out ), mtl::Arg ::V (n ), mtl::Arg ::V (pu )}; 31d .grid [0 ]= groups ; 32d .tg [0 ]= 1024 ; 33ds .push_back (d ); 34 } 35cx .warm (ds ,0.2 ); 36 std::vector < double > t ; 37for (int i = 0 ;i < 5 ;++ i )t .push_back (cx .run (ds )); 38 std::sort (t .begin (),t .end ()); 39 std::printf ("dispatch per phase, %4u x 1024 threads: %7.2f us a phase %6.1f GB/s\n" ,groups , 40t [2 ] /phases * 1e6 ,n * 16.0 * phases /t [2 ] /1e9 ); 41 } 42// grid barrier, at most one threadgroup a core 43for (uint32_t groups : {10u ,20u ,40u }) { 44 mtl::Dispatch d ; 45d .p = & pp ; 46const uint32_t ph = phases ; 47d .args = {mtl::Arg ::B (src ), mtl::Arg ::B (out ), mtl::Arg ::B (bar ), mtl::Arg ::B (flag ), mtl::Arg ::B (bad ), 48 mtl::Arg ::V (n ), mtl::Arg ::V (ph )}; 49d .grid [0 ]= groups ; 50d .tg [0 ]= 1024 ; 51 std::vector < double > t ; 52uint32_t badc = 0 ; 53for (int i = 0 ;i < 6 ;++ i ) { 54bar .as < uint32_t > ()[0 ]= 0 ; 55bad .as < uint32_t > ()[0 ]= 0 ; 56for (size_t k = 0 ;k < flag .bytes /4 ;++ k )flag .as < uint32_t > ()[k ]= 0xFFFFFFFFu ; 57const double s = cx .run ({d }); 58if (i )t .push_back (s ); 59badc += bad .as < uint32_t > ()[0 ]; 60 } 61 std::sort (t .begin (),t .end ()); 62 std::printf ("grid barrier, %4u x 1024 threads: %7.2f us a phase %6.1f GB/s stale flags seen: %u\n" , 63groups ,t [2 ] /phases * 1e6 ,n * 16.0 * phases /t [2 ] /1e9 ,badc ); 64 } 65return 0 ; 66}