okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1// metal/tests/runtime_hello.cpp - the Metal runtime end to end, with the shared src/core/graph.cpp built unchanged: 2// 1. gpu::malloc_device / host_alloc (mapped) / memcpy_async / a kernel through metal::dispatch / events / D2H 3// 2. the SHARED host file src/core/graph.cpp (CapturedGraph / GraphRegistry), compiled unchanged for Metal: 4// capture a 3-kernel "window", replay it 5 times, wait with wait_ms (event query polling), check the data path 5// is live (each replay reads the buffers' current contents, as a CUDA graph does) 6// 3. host_device_pointer: a kernel writes a mapped host buffer that the host reads without a copy 7// 4. the cost of replaying a 1,000-dispatch window 8#include "strata/core/graph.hpp" 9#include "strata/gpu/metal.hpp" 10#include "strata/gpu/runtime.hpp" 11 12#include <chrono> 13#include <cstdio> 14#include <cstring> 15#include <string> 16#include <vector> 17 18namespace gpu= strata::gpu; 19 20static const char * kSrc = R"MSL( 21#include <metal_stdlib> 22using namespace metal; 23struct Axpy { device const float* x; device float* y; float a; uint n; }; 24kernel void axpy(constant Axpy& p [[buffer(0)]], uint i [[thread_position_in_grid]]) { 25if (i < p.n) p.y[i] = p.a * p.x[i] + p.y[i]; 26} 27struct Scale { device float* y; device const float* s; uint n; }; // the factor is DATA (a device buffer), not an argument 28kernel void scale(constant Scale& p [[buffer(0)]], uint i [[thread_position_in_grid]]) { 29if (i < p.n) p.y[i] *= p.s[0]; 30} 31)MSL" ; 32struct Axpy {uint64_t x ,y ;float a ;uint32_t n ; }; 33struct Scale {uint64_t y ,s ;uint32_t n ; }; 34 35static int g_fail = 0 ; 36#define CHECK (c , ...) do { if (!(c)) { std::printf("FAIL %s:%d: ", __FILE__, __LINE__); std::printf(__VA_ARGS__); std::printf("\n"); ++g_fail; } } while (0) 37 38static double now_ms () { 39return std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ().time_since_epoch ()).count (); 40} 41 42int main () { 43 std::string err ; 44MTL ::ComputePipelineState * p_axpy = gpu::metal::pipeline ("hello" ,kSrc ,"axpy" ,err ); 45MTL ::ComputePipelineState * p_scale = gpu::metal::pipeline ("hello" ,kSrc ,"scale" ,err ); 46CHECK (p_axpy && p_scale ,"pipelines: %s" ,err .c_str ()); 47if (!p_axpy || !p_scale )return 1 ; 48const uint32_t N = 1 <<16 ; 49const gpu::metal::Grid groups {(N + 255 ) /256 ,1 ,1 },threads {256 ,1 ,1 }; 50 51 gpu::Stream s = nullptr ,s2 = nullptr ; 52CHECK (gpu::stream_create (& s , gpu::kStreamNonBlocking )== gpu::kSuccess ,"stream" ); 53CHECK (gpu::stream_create (& s2 , gpu::kStreamNonBlocking )== gpu::kSuccess ,"stream 2" ); 54float * dx = nullptr ,* dy = nullptr ,* ds = nullptr ,* hx = nullptr ; 55CHECK (gpu::malloc_device ((void ** )& dx ,N * 4 )== gpu::kSuccess ,"malloc x" ); 56CHECK (gpu::malloc_device ((void ** )& dy ,N * 4 )== gpu::kSuccess ,"malloc y" ); 57CHECK (gpu::malloc_device ((void ** )& ds ,4 )== gpu::kSuccess ,"malloc s" ); 58CHECK (gpu::host_alloc ((void ** )& hx ,N * 4 , gpu::kHostAllocMapped )== gpu::kSuccess ,"host_alloc" ); 59CHECK (gpu::host_view (dx )!= nullptr && gpu::host_view (dx )!= (void * )dx ,"a device pointer is a GPU address, not the CPU view" ); 60 61// ---- 1. copies, a kernel, an event across two streams, D2H 62 std::vector < float > x (N ),y (N ,1.0f ),got (N ); 63for (uint32_t i = 0 ;i < N ;++ i )x [i ]= (float ) (i %1000 ); 64 std::memcpy (hx ,x .data (),N * 4 ); 65 gpu::memcpy_async (dx ,hx ,N * 4 , gpu::kHostToDevice ,s );// mapped host buffer -> device: a blit 66 gpu::memcpy_async (dy ,y .data (),N * 4 , gpu::kHostToDevice ,s );// pageable -> device 67Axpy a {(uint64_t )dx , (uint64_t )dy ,2.0f ,N }; 68 gpu::metal::dispatch (s ,p_axpy ,& a ,sizeof a ,groups ,threads ); 69 gpu::Event ev = nullptr ; 70 gpu::event_create (& ev , gpu::kEventDisableTiming ); 71 gpu::event_record (ev ,s ); 72 gpu::stream_wait_event (s2 ,ev );// s2's kernel runs after s's 73Axpy a2 {(uint64_t )dx , (uint64_t )dy ,1.0f ,N }; 74 gpu::metal::dispatch (s2 ,p_axpy ,& a2 ,sizeof a2 ,groups ,threads ); 75 gpu::memcpy_async (got .data (),dy ,N * 4 , gpu::kDeviceToHost ,s2 ); 76 gpu::stream_synchronize (s2 ); 77int bad = 0 ; 78for (uint32_t i = 0 ;i < N ;++ i )bad += got [i ]!= 3.0f * x [i ]+ 1.0f ; 79CHECK (bad == 0 ,"step 1: %d wrong values" ,bad ); 80 std::printf ("1. copies + kernel + cross-stream event + D2H: %s\n" ,bad ?"FAIL" :"OK" ); 81 82// ---- 2. the shared CapturedGraph from src/core/graph.cpp 83 { 84float one = 1.0f ; 85 gpu::memcpy_sync (ds ,& one ,4 , gpu::kHostToDevice ); 86 strata::core::CapturedGraph g ; 87CHECK (g .begin (s ,err ),"begin: %s" ,err .c_str ()); 88Axpy w {(uint64_t )dx , (uint64_t )dy ,1.0f ,N }; 89Scale sc {(uint64_t )dy , (uint64_t )ds ,N }; 90 gpu::metal::dispatch (s ,p_axpy ,& w ,sizeof w ,groups ,threads ); 91 gpu::metal::dispatch (s ,p_scale ,& sc ,sizeof sc ,groups ,threads ); 92 gpu::metal::dispatch (s ,p_axpy ,& w ,sizeof w ,groups ,threads ); 93CHECK (g .end (s ,err ),"end: %s" ,err .c_str ()); 94CHECK (g .nodes ()== 3 ,"nodes %zu" ,g .nodes ()); 95// host model of y 96 std::vector < float > m (N ); 97for (uint32_t i = 0 ;i < N ;++ i )m [i ]= got [i ]; 98bool all = true; 99for (int rep = 0 ;rep < 5 ;++ rep ) { 100const float f = rep %2 ?0.5f :2.0f ;// the per-step value changes as DATA between replays 101float * sv = (float * ) gpu::host_view (ds ); 102 gpu::stream_synchronize (s ); 103* sv = f ; 104CHECK (g .launch (s ,err ),"launch: %s" ,err .c_str ()); 105CHECK (g .wait_ms (2000 ),"wait_ms timed out" ); 106for (uint32_t i = 0 ;i < N ;++ i )m [i ]= ((m [i ]+ x [i ])* f )+ x [i ]; 107 } 108 gpu::memcpy_sync (got .data (),dy ,N * 4 , gpu::kDeviceToHost ); 109int wrong = 0 ; 110for (uint32_t i = 0 ;i < N ;++ i )wrong += got [i ]!= m [i ]; 111all = wrong == 0 ; 112CHECK (all ,"graph replays: %d wrong values" ,wrong ); 113 std::printf ("2. shared CapturedGraph (src/core/graph.cpp): 3-kernel window, 5 replays with live data: %s\n" ,all ?"OK" :"FAIL" ); 114 115 strata::core::GraphRegistry reg (s ); 116int bodies = 0 ; 117for (int k = 0 ;k < 4 ;++ k ) 118CHECK (reg .record (strata::core::LayerType ::GDN ,1 , [& ] {++ bodies ; gpu::metal::dispatch (s ,p_axpy ,& w ,sizeof w ,groups ,threads ); },err ), 119"record: %s" ,err .c_str ()); 120CHECK (bodies == 1 && reg .size ()== 1 ,"registry captured %d bodies" ,bodies ); 121CHECK (reg .launch (strata::core::LayerType ::GDN ,1 ,2000 ,err ),"registry launch: %s" ,err .c_str ()); 122 std::printf (" GraphRegistry: body captured once over 4 records, launch+wait: %s\n" ,bodies == 1 ?"OK" :"FAIL" ); 123 } 124 125// ---- 3. a mapped host buffer written by a kernel, read by the host with no copy 126 { 127float * dview = nullptr ; 128CHECK (gpu::host_device_pointer ((void ** )& dview ,hx )== gpu::kSuccess ,"host_device_pointer" ); 129 std::memset (hx ,0 ,N * 4 ); 130Axpy w {(uint64_t )dx , (uint64_t )dview ,1.0f ,N }; 131 gpu::metal::dispatch (s ,p_axpy ,& w ,sizeof w ,groups ,threads ); 132 gpu::stream_synchronize (s ); 133int wrong = 0 ; 134for (uint32_t i = 0 ;i < N ;++ i )wrong += hx [i ]!= x [i ]; 135CHECK (wrong == 0 ,"mapped: %d wrong" ,wrong ); 136 std::printf ("3. kernel -> mapped host buffer, host reads it in place: %s\n" ,wrong ?"FAIL" :"OK" ); 137 } 138 139// ---- 4. replay cost of a 1,000-dispatch window 140 { 141 strata::core::CapturedGraph g ; 142Axpy w {(uint64_t )dx , (uint64_t )dy ,0.0f ,256 }; 143g .begin (s ,err ); 144for (int k = 0 ;k < 1000 ;++ k ) gpu::metal::dispatch (s ,p_axpy ,& w ,sizeof w , gpu::metal::Grid {1 ,1 ,1 },threads ); 145g .end (s ,err ); 146for (int rep = 0 ;rep < 5 ;++ rep ) { 147const double t0 = now_ms (); 148g .launch (s ,err ); 149const double t1 = now_ms (); 150g .wait_ms (5000 ); 151 std::printf ("4. 1000-dispatch window: replay (encode+commit) %.3f ms, until complete %.3f ms\n" ,t1 - t0 ,now_ms ()- t0 ); 152 } 153 } 154const auto st = gpu::metal::stats (); 155 std::printf ("stats: %llu dispatches, %llu command buffers, %llu blits, %llu host syncs\n" , (unsigned long long )st .dispatches , 156 (unsigned long long )st .command_buffers , (unsigned long long )st .blits , (unsigned long long )st .host_syncs ); 157 gpu::free_device (dx ); gpu::free_device (dy ); gpu::free_device (ds ); gpu::free_host (hx ); 158 gpu::event_destroy (ev ); 159 gpu::stream_destroy (s ); gpu::stream_destroy (s2 ); 160 std::printf (g_fail ?"RESULT: %d FAILED\n" :"RESULT: all passed\n" ,g_fail ); 161return g_fail ?1 :0 ; 162}