The STRATA_HIP_GFX906 build defines STRATA_HIP_GFX906, not STRATA_USE_HIP, so three sites that gate on STRATA_USE_HIP still take the CUDA branch and fail to compile: 1. fused_gr.cu: a bare 'return;' in the bool fused_gr_read_multi -> 'return false;'. 2. mtp.cpp: the MTP stream-priority block (cudaDeviceGetStreamPriorityRange) is gated on !defined(STRATA_HIP_GFX906) too. 3. vmm.cpp: the <cuda.h> include and the CUDA driver VMM code get the same gate. All three are inside #if blocks that only the gfx906 build reaches.
165 lines
5.4 KiB
C++
165 lines
5.4 KiB
C++
#include "strata/core/vmm.hpp"
|
|
|
|
#if !defined(STRATA_USE_HIP) && !defined(STRATA_HIP_GFX906)
|
|
#include <cuda.h>
|
|
#include <cuda_runtime.h>
|
|
|
|
#include <mutex>
|
|
|
|
namespace strata::core {
|
|
namespace {
|
|
|
|
struct Api {
|
|
bool ok = false;
|
|
uint64_t gran = 0;
|
|
int dev = -1;
|
|
decltype(&cuMemAddressReserve) reserve = nullptr;
|
|
decltype(&cuMemAddressFree) free_va = nullptr;
|
|
decltype(&cuMemCreate) create = nullptr;
|
|
decltype(&cuMemRelease) release = nullptr;
|
|
decltype(&cuMemMap) map = nullptr;
|
|
decltype(&cuMemUnmap) unmap = nullptr;
|
|
decltype(&cuMemSetAccess) access = nullptr;
|
|
};
|
|
|
|
template <class F> bool resolve(const char* name, F& f) {
|
|
cudaDriverEntryPointQueryResult q{};
|
|
void* p = nullptr;
|
|
if (cudaGetDriverEntryPointByVersion(name, &p, 12000, cudaEnableDefault, &q) != cudaSuccess ||
|
|
q != cudaDriverEntryPointSuccess || p == nullptr)
|
|
return false;
|
|
f = (F) p;
|
|
return true;
|
|
}
|
|
|
|
// never destroyed: the K/V pools and the cache release their chunks from static destructors at exit
|
|
const Api& api() {
|
|
static Api& a = *new Api;
|
|
static std::once_flag once;
|
|
std::call_once(once, [] {
|
|
decltype(&cuDeviceGetAttribute) attr = nullptr;
|
|
decltype(&cuMemGetAllocationGranularity) granularity = nullptr;
|
|
if (cudaGetDevice(&a.dev) != cudaSuccess) return;
|
|
if (!resolve("cuDeviceGetAttribute", attr) || !resolve("cuMemGetAllocationGranularity", granularity) ||
|
|
!resolve("cuMemAddressReserve", a.reserve) || !resolve("cuMemAddressFree", a.free_va) ||
|
|
!resolve("cuMemCreate", a.create) || !resolve("cuMemRelease", a.release) || !resolve("cuMemMap", a.map) ||
|
|
!resolve("cuMemUnmap", a.unmap) || !resolve("cuMemSetAccess", a.access))
|
|
return;
|
|
int vmm = 0;
|
|
if (attr(&vmm, CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED, (CUdevice) a.dev) != CUDA_SUCCESS || !vmm)
|
|
return;
|
|
CUmemAllocationProp prop{};
|
|
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
|
|
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
|
|
prop.location.id = a.dev;
|
|
size_t g = 0;
|
|
if (granularity(&g, &prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM) != CUDA_SUCCESS || g == 0) return;
|
|
a.gran = 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;
|
|
CUmemAllocationProp prop{};
|
|
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
|
|
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
|
|
prop.location.id = a.dev;
|
|
CUmemGenericAllocationHandle h = 0;
|
|
if (a.create(&h, (size_t) a.gran, &prop, 0) != CUDA_SUCCESS) return 0;
|
|
return (VmmChunk) h;
|
|
}
|
|
|
|
void vmm_chunk_free(VmmChunk h) {
|
|
if (h != 0 && api().ok) api().release((CUmemGenericAllocationHandle) 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;
|
|
CUdeviceptr p = 0;
|
|
if (a.reserve(&p, (size_t) (n * a.gran), 0, 0, 0) != CUDA_SUCCESS) return false;
|
|
base_ = (unsigned long long) 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));
|
|
a.free_va((CUdeviceptr) 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 (a.map((CUdeviceptr) (base_ + (uint64_t) i * a.gran), (size_t) a.gran, 0, (CUmemGenericAllocationHandle) h, 0) !=
|
|
CUDA_SUCCESS)
|
|
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();
|
|
CUmemAccessDesc d{};
|
|
d.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
|
|
d.location.id = a.dev;
|
|
d.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
|
|
return a.access((CUdeviceptr) (base_ + (uint64_t) lo * a.gran), (size_t) ((uint64_t) (hi - lo) * a.gran), &d, 1) ==
|
|
CUDA_SUCCESS;
|
|
}
|
|
|
|
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 (a.unmap((CUdeviceptr) (base_ + (uint64_t) i * a.gran), (size_t) a.gran) != CUDA_SUCCESS) return 0;
|
|
h_[(size_t) i] = 0;
|
|
return h;
|
|
}
|
|
|
|
} // namespace strata::core
|
|
|
|
#else // HIP: no virtual memory here; the elastic K/V stays off
|
|
|
|
namespace strata::core {
|
|
bool vmm_available() { return false; }
|
|
uint64_t vmm_granularity() { return 0; }
|
|
VmmChunk vmm_chunk_new() { return 0; }
|
|
void vmm_chunk_free(VmmChunk) {}
|
|
bool VmmRange::reserve(uint64_t) { return false; }
|
|
void VmmRange::release() {}
|
|
int64_t VmmRange::mapped_count() const { return 0; }
|
|
bool VmmRange::map_one(int64_t, VmmChunk) { return false; }
|
|
bool VmmRange::set_access(int64_t, int64_t) { return false; }
|
|
bool VmmRange::commit_run(int64_t, int64_t) { return false; }
|
|
VmmChunk VmmRange::unmap(int64_t) { return 0; }
|
|
} // namespace strata::core
|
|
|
|
#endif
|