okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
58f81f20e
main
1#include "strata/core/vmm.hpp" 2 3#if !defined(STRATA_USE_HIP )&& !defined(STRATA_HIP_GFX906 )&& !defined(STRATA_GPU_METAL ) 4#include <cuda.h> 5#include <cuda_runtime.h> 6 7#include <mutex> 8 9namespace strata::core { 10namespace { 11 12struct Api { 13bool ok = false; 14uint64_t gran = 0 ; 15int dev = -1 ; 16 decltype(& cuMemAddressReserve )reserve = nullptr ; 17 decltype(& cuMemAddressFree )free_va = nullptr ; 18 decltype(& cuMemCreate )create = nullptr ; 19 decltype(& cuMemRelease )release = nullptr ; 20 decltype(& cuMemMap )map = nullptr ; 21 decltype(& cuMemUnmap )unmap = nullptr ; 22 decltype(& cuMemSetAccess )access = nullptr ; 23}; 24 25template < class F > bool resolve (const char * name ,F & f ) { 26cudaDriverEntryPointQueryResult q {}; 27void * p = nullptr ; 28#if CUDART_VERSION >=12050 29const cudaError_t e = cudaGetDriverEntryPointByVersion (name ,& p ,12000 ,cudaEnableDefault ,& q ); 30#else // the versioned query arrived in CUDA 12.5 31const cudaError_t e = cudaGetDriverEntryPoint (name ,& p ,cudaEnableDefault ,& q ); 32#endif 33if (e != cudaSuccess || q != cudaDriverEntryPointSuccess || p == nullptr ) { 34 (void )cudaGetLastError (); 35return false; 36 } 37f = (F )p ; 38return true; 39} 40 41// never destroyed: the K/V pools and the cache release their chunks from static destructors at exit 42const Api & api () { 43static Api & a = * new Api ; 44static std::once_flag once ; 45 std::call_once (once , [] { 46 decltype(& cuDeviceGetAttribute )attr = nullptr ; 47 decltype(& cuMemGetAllocationGranularity )granularity = nullptr ; 48if (cudaGetDevice (& a .dev )!= cudaSuccess )return ; 49if (!resolve ("cuDeviceGetAttribute" ,attr )|| !resolve ("cuMemGetAllocationGranularity" ,granularity )|| 50 !resolve ("cuMemAddressReserve" ,a .reserve )|| !resolve ("cuMemAddressFree" ,a .free_va )|| 51 !resolve ("cuMemCreate" ,a .create )|| !resolve ("cuMemRelease" ,a .release )|| !resolve ("cuMemMap" ,a .map )|| 52 !resolve ("cuMemUnmap" ,a .unmap )|| !resolve ("cuMemSetAccess" ,a .access )) 53return ; 54int vmm = 0 ; 55if (attr (& vmm ,CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED , (CUdevice )a .dev )!= CUDA_SUCCESS || !vmm ) 56return ; 57CUmemAllocationProp prop {}; 58prop .type = CU_MEM_ALLOCATION_TYPE_PINNED ; 59prop .location .type = CU_MEM_LOCATION_TYPE_DEVICE ; 60prop .location .id = a .dev ; 61size_t g = 0 ; 62if (granularity (& g ,& prop ,CU_MEM_ALLOC_GRANULARITY_MINIMUM )!= CUDA_SUCCESS || g == 0 )return ; 63a .gran = g ; 64a .ok = true; 65 }); 66return a ; 67} 68 69}// namespace 70 71bool vmm_available () {return api ().ok ; } 72uint64_t vmm_granularity () {return api ().ok ?api ().gran :0 ; } 73 74VmmChunk vmm_chunk_new () { 75const Api & a = api (); 76if (!a .ok )return 0 ; 77CUmemAllocationProp prop {}; 78prop .type = CU_MEM_ALLOCATION_TYPE_PINNED ; 79prop .location .type = CU_MEM_LOCATION_TYPE_DEVICE ; 80prop .location .id = a .dev ; 81CUmemGenericAllocationHandle h = 0 ; 82if (a .create (& h , (size_t )a .gran ,& prop ,0 )!= CUDA_SUCCESS )return 0 ; 83return (VmmChunk )h ; 84} 85 86void vmm_chunk_free (VmmChunk h ) { 87if (h != 0 && api ().ok )api ().release ((CUmemGenericAllocationHandle )h ); 88} 89 90bool VmmRange ::reserve (uint64_t bytes ) { 91release (); 92const Api & a = api (); 93if (!a .ok || bytes == 0 )return false; 94const uint64_t n = (bytes + a .gran - 1 ) /a .gran ; 95CUdeviceptr p = 0 ; 96if (a .reserve (& p , (size_t ) (n * a .gran ),0 ,0 ,0 )!= CUDA_SUCCESS )return false; 97base_ = (unsigned long long )p ; 98h_ .assign ((size_t )n ,0 ); 99return true; 100} 101 102void VmmRange ::release () { 103if (base_ == 0 )return ; 104const Api & a = api (); 105for (int64_t i = 0 ;i < chunks ();++ i )vmm_chunk_free (unmap (i )); 106a .free_va ((CUdeviceptr )base_ , (size_t ) ((uint64_t )h_ .size ()* a .gran )); 107base_ = 0 ; 108h_ .clear (); 109} 110 111int64_t VmmRange ::mapped_count ()const { 112int64_t n = 0 ; 113for (const VmmChunk h :h_ )n += h != 0 ; 114return n ; 115} 116 117bool VmmRange ::map_one (int64_t i ,VmmChunk h ) { 118const Api & a = api (); 119if (a .map ((CUdeviceptr ) (base_ + (uint64_t )i * a .gran ), (size_t )a .gran ,0 , (CUmemGenericAllocationHandle )h ,0 )!= 120CUDA_SUCCESS ) 121return false; 122h_ [(size_t )i ]= h ; 123return true; 124} 125 126bool VmmRange ::set_access (int64_t lo ,int64_t hi ) { 127if (hi <=lo )return true; 128const Api & a = api (); 129CUmemAccessDesc d {}; 130d .location .type = CU_MEM_LOCATION_TYPE_DEVICE ; 131d .location .id = a .dev ; 132d .flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE ; 133return a .access ((CUdeviceptr ) (base_ + (uint64_t )lo * a .gran ), (size_t ) ((uint64_t ) (hi - lo )* a .gran ),& d ,1 )== 134CUDA_SUCCESS ; 135} 136 137bool VmmRange ::commit_run (int64_t lo ,int64_t hi ) { 138if (set_access (lo ,hi ))return true; 139for (int64_t c = lo ;c < hi ;++ c )vmm_chunk_free (unmap (c )); 140return false; 141} 142 143VmmChunk VmmRange ::unmap (int64_t i ) { 144if (!mapped (i ))return 0 ; 145const Api & a = api (); 146const VmmChunk h = h_ [(size_t )i ]; 147if (a .unmap ((CUdeviceptr ) (base_ + (uint64_t )i * a .gran ), (size_t )a .gran )!= CUDA_SUCCESS )return 0 ; 148h_ [(size_t )i ]= 0 ; 149return h ; 150} 151 152}// namespace strata::core 153 154#else // not CUDA 155// HIP (ROCm 6+): the same elastic range through hipMemAddressReserve / hipMemCreate / hipMemMap / hipMemSetAccess. Available 156// only where the device reports virtual memory management support (STRATA_HIP_VMM=0 turns it off): --kv-grow stays off 157// otherwise, as before. Everything else (Metal, gfx906, ROCm older than 6): no virtual memory, the elastic K/V stays off. 158#if defined(STRATA_USE_HIP )&& !defined(STRATA_HIP_GFX906 ) 159#include <hip/hip_runtime.h> 160#include <hip/hip_version.h> 161 162#include <cstdio> 163#include <cstdlib> 164#include <mutex> 165#endif 166 167#if defined(STRATA_USE_HIP )&& !defined(STRATA_HIP_GFX906 )&& defined(HIP_VERSION_MAJOR )&& HIP_VERSION_MAJOR >=6 168namespace strata::core { 169namespace { 170struct Api { 171bool ok = false; 172uint64_t gran = 0 ; 173int dev = -1 ; 174}; 175void fail (const char * what ,hipError_t e ) { 176static int n = 0 ; 177if (n ++ < 8 ) std::fprintf (stderr ,"strata: HIP virtual memory: %s failed: %s\n" ,what ,hipGetErrorString (e )); 178 (void )hipGetLastError (); 179} 180hipMemAllocationProp alloc_prop (int dev ) { 181hipMemAllocationProp p {}; 182p .type = hipMemAllocationTypePinned ; 183p .requestedHandleType = hipMemHandleTypeNone ; 184p .location .type = hipMemLocationTypeDevice ; 185p .location .id = dev ; 186return p ; 187} 188const Api & api () { 189static Api & a = * new Api ;// never destroyed: the K/V pools and the cache release their chunks from static destructors 190static std::once_flag once ; 191 std::call_once (once , [] { 192const char * off = std::getenv ("STRATA_HIP_VMM" ); 193if (off != nullptr && off [0 ]== '0' )return ; 194if (hipGetDevice (& a .dev )!= hipSuccess ) { (void )hipGetLastError ();return ; } 195int vmm = 0 ; 196if (hipDeviceGetAttribute (& vmm ,hipDeviceAttributeVirtualMemoryManagementSupported ,a .dev )!= hipSuccess || !vmm ) { 197 (void )hipGetLastError (); 198return ; 199 } 200const hipMemAllocationProp prop = alloc_prop (a .dev ); 201size_t g = 0 ; 202if (hipMemGetAllocationGranularity (& g ,& prop ,hipMemAllocationGranularityMinimum )!= hipSuccess || g == 0 ) { 203 (void )hipGetLastError (); 204return ; 205 } 206// gfx1201 reports a 4 KiB minimum granularity; the elastic range works in 2 MiB chunks like CUDA's (any multiple of the minimum) 207const uint64_t want = 2ull <<20 ; 208a .gran = g >=want ?g : (want + g - 1 ) /g * g ; 209a .ok = true; 210 }); 211return a ; 212} 213}// namespace 214 215bool vmm_available () {return api ().ok ; } 216uint64_t vmm_granularity () {return api ().ok ?api ().gran :0 ; } 217 218VmmChunk vmm_chunk_new () { 219const Api & a = api (); 220if (!a .ok )return 0 ; 221const hipMemAllocationProp prop = alloc_prop (a .dev ); 222hipMemGenericAllocationHandle_t h = nullptr ; 223if (const hipError_t e = hipMemCreate (& h , (size_t )a .gran ,& prop ,0 );e != hipSuccess ) {fail ("hipMemCreate" ,e );return 0 ; } 224return (VmmChunk ) (uintptr_t )h ; 225} 226 227void vmm_chunk_free (VmmChunk h ) { 228if (h != 0 && api ().ok ) (void )hipMemRelease ((hipMemGenericAllocationHandle_t ) (uintptr_t )h ); 229} 230 231bool VmmRange ::reserve (uint64_t bytes ) { 232release (); 233const Api & a = api (); 234if (!a .ok || bytes == 0 )return false; 235const uint64_t n = (bytes + a .gran - 1 ) /a .gran ; 236void * p = nullptr ; 237if (const hipError_t e = hipMemAddressReserve (& p , (size_t ) (n * a .gran ),0 ,nullptr ,0 );e != hipSuccess ) {fail ("hipMemAddressReserve" ,e );return false; } 238base_ = (unsigned long long ) (uintptr_t )p ; 239h_ .assign ((size_t )n ,0 ); 240return true; 241} 242 243void VmmRange ::release () { 244if (base_ == 0 )return ; 245const Api & a = api (); 246for (int64_t i = 0 ;i < chunks ();++ i )vmm_chunk_free (unmap (i )); 247 (void )hipMemAddressFree ((void * ) (uintptr_t )base_ , (size_t ) ((uint64_t )h_ .size ()* a .gran )); 248base_ = 0 ; 249h_ .clear (); 250} 251 252int64_t VmmRange ::mapped_count ()const { 253int64_t n = 0 ; 254for (const VmmChunk h :h_ )n += h != 0 ; 255return n ; 256} 257 258bool VmmRange ::map_one (int64_t i ,VmmChunk h ) { 259const Api & a = api (); 260if (const hipError_t e = hipMemMap ((void * ) (uintptr_t ) (base_ + (uint64_t )i * a .gran ), (size_t )a .gran ,0 , 261 (hipMemGenericAllocationHandle_t ) (uintptr_t )h ,0 ); 262e != hipSuccess ) { 263fail ("hipMemMap" ,e ); 264return false; 265 } 266h_ [(size_t )i ]= h ; 267return true; 268} 269 270bool VmmRange ::set_access (int64_t lo ,int64_t hi ) { 271if (hi <=lo )return true; 272const Api & a = api (); 273hipMemAccessDesc d {}; 274d .location .type = hipMemLocationTypeDevice ; 275d .location .id = a .dev ; 276d .flags = hipMemAccessFlagsProtReadWrite ; 277const hipError_t e = hipMemSetAccess ((void * ) (uintptr_t ) (base_ + (uint64_t )lo * a .gran ), (size_t ) ((uint64_t ) (hi - lo )* a .gran ), 278& d ,1 ); 279const bool ok = e == hipSuccess ; 280if (!ok )fail ("hipMemSetAccess" ,e ); 281return ok ; 282} 283 284bool VmmRange ::commit_run (int64_t lo ,int64_t hi ) { 285if (set_access (lo ,hi ))return true; 286for (int64_t c = lo ;c < hi ;++ c )vmm_chunk_free (unmap (c )); 287return false; 288} 289 290VmmChunk VmmRange ::unmap (int64_t i ) { 291if (!mapped (i ))return 0 ; 292const Api & a = api (); 293const VmmChunk h = h_ [(size_t )i ]; 294if (hipMemUnmap ((void * ) (uintptr_t ) (base_ + (uint64_t )i * a .gran ), (size_t )a .gran )!= hipSuccess ) { (void )hipGetLastError ();return 0 ; } 295h_ [(size_t )i ]= 0 ; 296return h ; 297} 298}// namespace strata::core 299#else 300 301 302namespace strata::core { 303bool vmm_available () {return false; } 304uint64_t vmm_granularity () {return 0 ; } 305VmmChunk vmm_chunk_new () {return 0 ; } 306void vmm_chunk_free (VmmChunk ) {} 307bool VmmRange ::reserve (uint64_t ) {return false; } 308void VmmRange ::release () {} 309int64_t VmmRange ::mapped_count ()const {return 0 ; } 310bool VmmRange ::map_one (int64_t ,VmmChunk ) {return false; } 311bool VmmRange ::set_access (int64_t ,int64_t ) {return false; } 312bool VmmRange ::commit_run (int64_t ,int64_t ) {return false; } 313VmmChunk VmmRange ::unmap (int64_t ) {return 0 ; } 314}// namespace strata::core 315 316#endif 317#endif