diff --git a/src/core/vmm.cpp b/src/core/vmm.cpp index 2ae5af6a..3336a49d 100644 --- a/src/core/vmm.cpp +++ b/src/core/vmm.cpp @@ -151,7 +151,153 @@ VmmChunk VmmRange::unmap(int64_t i) { } // namespace strata::core -#else // HIP: no virtual memory here; the elastic K/V stays off +#else // not CUDA +// HIP (ROCm 6+): the same elastic range through hipMemAddressReserve / hipMemCreate / hipMemMap / hipMemSetAccess. Available +// only where the device reports virtual memory management support (STRATA_HIP_VMM=0 turns it off): --kv-grow stays off +// otherwise, as before. Everything else (Metal, gfx906, ROCm older than 6): no virtual memory, the elastic K/V stays off. +#if defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) +#include +#include + +#include +#include +#include +#endif + +#if defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906) && defined(HIP_VERSION_MAJOR) && HIP_VERSION_MAJOR >= 6 +namespace strata::core { +namespace { +struct Api { + bool ok = false; + uint64_t gran = 0; + int dev = -1; +}; +void fail(const char* what, hipError_t e) { + static int n = 0; + if (n++ < 8) std::fprintf(stderr, "strata: HIP virtual memory: %s failed: %s\n", what, hipGetErrorString(e)); + (void) hipGetLastError(); +} +hipMemAllocationProp alloc_prop(int dev) { + hipMemAllocationProp p{}; + p.type = hipMemAllocationTypePinned; + p.requestedHandleType = hipMemHandleTypeNone; + p.location.type = hipMemLocationTypeDevice; + p.location.id = dev; + return p; +} +const Api& api() { + static Api& a = *new Api; // never destroyed: the K/V pools and the cache release their chunks from static destructors + static std::once_flag once; + std::call_once(once, [] { + const char* off = std::getenv("STRATA_HIP_VMM"); + if (off != nullptr && off[0] == '0') return; + if (hipGetDevice(&a.dev) != hipSuccess) { (void) hipGetLastError(); return; } + int vmm = 0; + if (hipDeviceGetAttribute(&vmm, hipDeviceAttributeVirtualMemoryManagementSupported, a.dev) != hipSuccess || !vmm) { + (void) hipGetLastError(); + return; + } + const hipMemAllocationProp prop = alloc_prop(a.dev); + size_t g = 0; + if (hipMemGetAllocationGranularity(&g, &prop, hipMemAllocationGranularityMinimum) != hipSuccess || g == 0) { + (void) hipGetLastError(); + return; + } + // gfx1201 reports a 4 KiB minimum granularity; the elastic range works in 2 MiB chunks like CUDA's (any multiple of the minimum) + const uint64_t want = 2ull << 20; + a.gran = g >= want ? g : (want + g - 1) / g * g; + a.ok = true; + }); + return a; +} +} // namespace + +bool vmm_available() { return api().ok; } +uint64_t vmm_granularity() { return api().ok ? api().gran : 0; } + +VmmChunk vmm_chunk_new() { + const Api& a = api(); + if (!a.ok) return 0; + const hipMemAllocationProp prop = alloc_prop(a.dev); + hipMemGenericAllocationHandle_t h = nullptr; + if (const hipError_t e = hipMemCreate(&h, (size_t) a.gran, &prop, 0); e != hipSuccess) { fail("hipMemCreate", e); return 0; } + return (VmmChunk) (uintptr_t) h; +} + +void vmm_chunk_free(VmmChunk h) { + if (h != 0 && api().ok) (void) hipMemRelease((hipMemGenericAllocationHandle_t) (uintptr_t) h); +} + +bool VmmRange::reserve(uint64_t bytes) { + release(); + const Api& a = api(); + if (!a.ok || bytes == 0) return false; + const uint64_t n = (bytes + a.gran - 1) / a.gran; + void* p = nullptr; + if (const hipError_t e = hipMemAddressReserve(&p, (size_t) (n * a.gran), 0, nullptr, 0); e != hipSuccess) { fail("hipMemAddressReserve", e); return false; } + base_ = (unsigned long long) (uintptr_t) p; + h_.assign((size_t) n, 0); + return true; +} + +void VmmRange::release() { + if (base_ == 0) return; + const Api& a = api(); + for (int64_t i = 0; i < chunks(); ++i) vmm_chunk_free(unmap(i)); + (void) hipMemAddressFree((void*) (uintptr_t) base_, (size_t) ((uint64_t) h_.size() * a.gran)); + base_ = 0; + h_.clear(); +} + +int64_t VmmRange::mapped_count() const { + int64_t n = 0; + for (const VmmChunk h : h_) n += h != 0; + return n; +} + +bool VmmRange::map_one(int64_t i, VmmChunk h) { + const Api& a = api(); + if (const hipError_t e = hipMemMap((void*) (uintptr_t) (base_ + (uint64_t) i * a.gran), (size_t) a.gran, 0, + (hipMemGenericAllocationHandle_t) (uintptr_t) h, 0); + e != hipSuccess) { + fail("hipMemMap", e); + return false; + } + h_[(size_t) i] = h; + return true; +} + +bool VmmRange::set_access(int64_t lo, int64_t hi) { + if (hi <= lo) return true; + const Api& a = api(); + hipMemAccessDesc d{}; + d.location.type = hipMemLocationTypeDevice; + d.location.id = a.dev; + d.flags = hipMemAccessFlagsProtReadWrite; + const hipError_t e = hipMemSetAccess((void*) (uintptr_t) (base_ + (uint64_t) lo * a.gran), (size_t) ((uint64_t) (hi - lo) * a.gran), + &d, 1); + const bool ok = e == hipSuccess; + if (!ok) fail("hipMemSetAccess", e); + return ok; +} + +bool VmmRange::commit_run(int64_t lo, int64_t hi) { + if (set_access(lo, hi)) return true; + for (int64_t c = lo; c < hi; ++c) vmm_chunk_free(unmap(c)); + return false; +} + +VmmChunk VmmRange::unmap(int64_t i) { + if (!mapped(i)) return 0; + const Api& a = api(); + const VmmChunk h = h_[(size_t) i]; + if (hipMemUnmap((void*) (uintptr_t) (base_ + (uint64_t) i * a.gran), (size_t) a.gran) != hipSuccess) { (void) hipGetLastError(); return 0; } + h_[(size_t) i] = 0; + return h; +} +} // namespace strata::core +#else + namespace strata::core { bool vmm_available() { return false; } @@ -168,3 +314,4 @@ VmmChunk VmmRange::unmap(int64_t) { return 0; } } // namespace strata::core #endif +#endif