Windows: cap the sliced arena pin at the GPU's shared-memory budget (#243)

When cudaHostRegister refuses the whole expert arena, the sliced fallback
pinned layer after layer until the driver refused one (28 GiB on a 63 GB
single-GPU PC), and WDDM then refused every later device allocation
("native head upload: out of memory").  On Windows the sliced pass now stops
at the adapter's DXGI non-local budget - the process's current usage - 4 GiB
(IDXGIAdapter3::QueryVideoMemoryInfo for the CUDA device's LUID; dxgi.dll is
loaded at run time), or RAM/2 - 8 GiB when that query fails.  The arena line
names the cap and where it came from.

STRATA_ARENA_PIN_GIB: unset = the above; 0 = no cap (the old behaviour);
N > 0 = at most N GiB from the start.  arena_pin_cap_gib() (pinned.hpp)
reads it, -1 when unset, for the multi-GPU cap in generate.cpp as well.
A machine whose whole-arena registration succeeds is unchanged, and a
caller's cap (multi-GPU) still takes precedence.

This PC's whole registration fails too: IQ3_XXS now pins 24 GiB (32 slices;
budget 30.8 GiB - 2.0 in use - 4) instead of 28 GiB (36).
This commit is contained in:
Niko1221 committed 2026-09-30 21:02:17 +02:00
1 parent 4b8db25763
commit 00ec1d081d
4 files changed
+139 -5

No files matched your search

+62
View File
@@ -5,8 +5,11 @@
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <dxgi1_4.h>
#include <cstring>
#else
#include <sys/mman.h>
#include <unistd.h>
#endif
namespace strata::platform {
@@ -57,6 +60,54 @@ void unlock_resident(void* p, uint64_t bytes) {
for (uint64_t off = 0; off < bytes; off += chunk)
VirtualUnlock((uint8_t*) p + off, (SIZE_T) (bytes - off < chunk ? bytes - off : chunk));
}
bool gpu_shared_memory_budget(const void* luid, uint64_t& budget, uint64_t& usage, std::string& why) {
budget = usage = 0;
// dxgi.dll is loaded when asked, not linked: a start that never needs this keeps the imports it had
HMODULE dxgi = LoadLibraryA("dxgi.dll");
if (dxgi == nullptr) { why = "dxgi.dll not found"; return false; }
using CreateFactory = HRESULT(WINAPI*)(REFIID, void**);
const auto create = (CreateFactory) (void*) GetProcAddress(dxgi, "CreateDXGIFactory1");
IDXGIFactory1* factory = nullptr;
if (create == nullptr || FAILED(create(__uuidof(IDXGIFactory1), (void**) &factory)) || factory == nullptr) {
why = "CreateDXGIFactory1 failed";
FreeLibrary(dxgi);
return false;
}
bool ok = false;
why = "no DXGI adapter has the CUDA device's LUID";
for (UINT i = 0; !ok; ++i) {
IDXGIAdapter1* a = nullptr;
if (factory->EnumAdapters1(i, &a) == DXGI_ERROR_NOT_FOUND || a == nullptr) break;
DXGI_ADAPTER_DESC1 d{};
if (SUCCEEDED(a->GetDesc1(&d)) && std::memcmp(&d.AdapterLuid, luid, sizeof d.AdapterLuid) == 0) {
IDXGIAdapter3* a3 = nullptr;
DXGI_QUERY_VIDEO_MEMORY_INFO info{};
if (SUCCEEDED(a->QueryInterface(__uuidof(IDXGIAdapter3), (void**) &a3)) && a3 != nullptr &&
SUCCEEDED(a3->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &info))) {
budget = info.Budget;
usage = info.CurrentUsage;
ok = budget > 0;
why = ok ? "" : "the adapter reports no shared-memory budget";
} else {
why = "QueryVideoMemoryInfo failed";
}
if (a3 != nullptr) a3->Release();
a->Release();
break;
}
a->Release();
}
factory->Release();
FreeLibrary(dxgi);
return ok;
}
uint64_t total_physical_memory() {
MEMORYSTATUSEX ms{};
ms.dwLength = sizeof ms;
return GlobalMemoryStatusEx(&ms) ? (uint64_t) ms.ullTotalPhys : 0;
}
#else
LockResult lock_resident(void* p, uint64_t bytes) {
LockResult r;
@@ -71,6 +122,17 @@ LockResult lock_resident(void* p, uint64_t bytes) {
void unlock_resident(void* p, uint64_t bytes) {
if (p != nullptr && bytes != 0) munlock(p, bytes);
}
bool gpu_shared_memory_budget(const void*, uint64_t& budget, uint64_t& usage, std::string& why) {
budget = usage = 0;
why = "DXGI is Windows-only";
return false;
}
uint64_t total_physical_memory() {
const long pages = sysconf(_SC_PHYS_PAGES), page = sysconf(_SC_PAGE_SIZE);
return pages > 0 && page > 0 ? (uint64_t) pages * (uint64_t) page : 0;
}
#endif
} // namespace strata::platform