Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
Windows: cap the sliced arena pin at the GPU's shared-memory budget (#243)
Niko122100ec1d081dce
diff --git a/include/strata/core/pinned.hpp b/include/strata/core/pinned.hppindex 530acd4d1..d1039da52 100644--- a/include/strata/core/pinned.hpp+++ b/include/strata/core/pinned.hpp@@ -22,6 +22,10 @@ namespace strata::core {enum class PageBacking { LargePages, NormalPages, PinnedByCuda };+/// #243: STRATA_ARENA_PIN_GIB, the cap on the expert arena's CUDA registration in GiB. -1 when unset (the engine+/// decides), 0 = no cap (the whole arena, or as many slices as the driver takes), N > 0 = at most N GiB.+int arena_pin_cap_gib();+struct PinnedArena {void* base = nullptr;uint64_t capacity = 0;diff --git a/include/strata/platform/memory.hpp b/include/strata/platform/memory.hppindex a350a78c5..ece6c9e4a 100644--- a/include/strata/platform/memory.hpp+++ b/include/strata/platform/memory.hpp@@ -23,4 +23,13 @@ LockResult lock_resident(void* p, uint64_t bytes);/// Undo lock_resident for the same region (best effort).void unlock_resident(void* p, uint64_t bytes);+/// #243, Windows: the GPU's shared (non-local) memory budget and this process's use of it, from DXGI+/// (IDXGIAdapter3::QueryVideoMemoryInfo, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL) for the adapter whose LUID is the+/// 8 bytes at `luid` (cudaDeviceProp::luid). Page-locked host memory the GPU maps is charged there. False (and+/// `why` says so) when the query is not possible - always elsewhere than Windows.+bool gpu_shared_memory_budget(const void* luid, uint64_t& budget, uint64_t& usage, std::string& why);++/// The machine's physical RAM in bytes (0 when unknown).+uint64_t total_physical_memory();+} // namespace strata::platformdiff --git a/src/core/pinned.cu b/src/core/pinned.cuindex b64eb52a7..4b1602481 100644--- a/src/core/pinned.cu+++ b/src/core/pinned.cu@@ -238,6 +238,46 @@ namespace {bool clear_error() { (void) cudaGetLastError(); return true; }} // namespace+int arena_pin_cap_gib() {+ const char* e = std::getenv("STRATA_ARENA_PIN_GIB");+ if (e == nullptr || *e == '\0') return -1;+ const int v = std::atoi(e);+ return v < 0 ? -1 : v;+}++namespace {+#ifdef _WIN32+// #243: how much of the arena the sliced registration may pin on Windows when the whole arena was refused.+// Page-locked memory the GPU maps is charged to its shared (non-local) WDDM segment; pinned slice by slice until+// the driver refused one (28 GiB of a 63 GB PC), that segment was left full and every later cudaMalloc failed+// "out of memory". 4 GiB of the budget stay free for what the engine allocates after the arena; without the DXGI+// numbers, RAM/2 - 8 GiB (the budget is about half the RAM). False: no limit could be worked out.+bool sliced_pin_limit(uint64_t& limit, std::string& why) {+ constexpr uint64_t GiB = 1ull << 30;+ char buf[256];+ int dev = 0;+ cudaDeviceProp p{};+ uint64_t budget = 0, usage = 0;+ std::string err = "no CUDA device properties";+ if (cudaGetDevice(&dev) == cudaSuccess && cudaGetDeviceProperties(&p, dev) == cudaSuccess &&+ strata::platform::gpu_shared_memory_budget(p.luid, budget, usage, err)) {+ limit = budget > usage + 4 * GiB ? budget - usage - 4 * GiB : 0;+ std::snprintf(buf, sizeof buf, "the GPU's shared-memory budget %.1f GiB - %.1f GiB in use - 4 GiB",+ (double) budget / GiB, (double) usage / GiB);+ why = buf;+ return true;+ }+ (void) cudaGetLastError();+ const uint64_t ram = strata::platform::total_physical_memory();+ if (ram == 0) return false;+ limit = ram / 2 > 8 * GiB ? ram / 2 - 8 * GiB : 0;+ std::snprintf(buf, sizeof buf, "%s: RAM/2 - 8 GiB of %.1f GiB", err.c_str(), (double) ram / GiB);+ why = buf;+ return true;+}+#endif+} // namespace+namespace {std::vector<uint64_t> uniform_bounds(uint64_t bytes, uint64_t slice) {std::vector<uint64_t> b;@@ -265,7 +305,15 @@ PinnedArena::PinnedArena(uint64_t bytes, const std::vector<uint64_t>& bounds,// Register with CUDA BEFORE any page is touched: cudaHostRegister pins what is resident now, and a region// that has already been faulted in page by page is far more expensive to register and may fail outright.if (base) {- const bool capped = max_pinned_bytes > 0 && max_pinned_bytes < bytes && bounds.size() >= 2;+ // #243: STRATA_ARENA_PIN_GIB=N caps the registration from the start where the caller set no cap+ const int env_gib = arena_pin_cap_gib();+ uint64_t cap = max_pinned_bytes;+ std::string cap_why = "for CUDA1";+ if (cap == 0 && env_gib > 0) {+ cap = (uint64_t) env_gib << 30;+ cap_why = "by STRATA_ARENA_PIN_GIB";+ }+ const bool capped = cap > 0 && cap < bytes && bounds.size() >= 2;const cudaError_t e = capped ? cudaSuccess :cudaHostRegister(base, (size_t) bytes, cudaHostRegisterPortable | cudaHostRegisterMapped);if (!capped && e == cudaSuccess) {@@ -275,9 +323,17 @@ PinnedArena::PinnedArena(uint64_t bytes, const std::vector<uint64_t>& bounds,// Plan v0.3 P5: the whole range is refused, so pin it slice by slice from the start. The rest stays// resident through the working-set lock below. (P6: slices may differ in size, one per layer.)slice_bytes = 1; // sliced; the uniform constructor records the size+ bool limited = capped;+ uint64_t limit = cap;+ std::string limit_why;+#ifdef _WIN32+ // #243: not up to the driver's refusal - that leaves WDDM refusing every later allocation (unless+ // STRATA_ARENA_PIN_GIB says otherwise; 0 is the old behaviour)+ if (!capped && env_gib < 0) limited = sliced_pin_limit(limit, limit_why);+#endiffor (size_t i = 0; i + 1 < bounds.size(); ++i) {const uint64_t off = bounds[i], n = bounds[i + 1] - bounds[i];- if (capped && (off > max_pinned_bytes || n > max_pinned_bytes - off)) break;+ if (limited && (off > limit || n > limit - off)) break;if (cudaHostRegister((uint8_t*) base + off, (size_t) n, cudaHostRegisterPortable | cudaHostRegisterMapped) != cudaSuccess) {(void) cudaGetLastError();break;@@ -286,9 +342,12 @@ PinnedArena::PinnedArena(uint64_t bytes, const std::vector<uint64_t>& bounds,registered_bytes = off + n;++registered_slices;}- note = (capped ? "cudaHostRegister limited to " + std::to_string(max_pinned_bytes >> 30) +- " GiB for CUDA1; " :- "cudaHostRegister of the whole arena FAILED (" + std::string(cudaGetErrorString(e)) + "); ") ++ char gib[32];+ std::snprintf(gib, sizeof gib, "%.1f", (double) limit / (double) (1ull << 30));+ note = (capped ? "cudaHostRegister limited to " + std::to_string(cap >> 30) + " GiB " + cap_why + "; " :+ "cudaHostRegister of the whole arena FAILED (" + std::string(cudaGetErrorString(e)) + "); " ++ (limited ? "slices capped at " + std::string(gib) + " GiB (" + limit_why ++ "; STRATA_ARENA_PIN_GIB=N sets it, 0 = none; #243); " : std::string())) +std::to_string(registered_slices) + " slices pinned (" + std::to_string(registered_bytes >> 30) +" GiB); " + note;if (registered_bytes < bytes) {diff --git a/src/platform/memory.cpp b/src/platform/memory.cppindex 317c6d835..f3b40e8d6 100644--- a/src/platform/memory.cpp+++ b/src/platform/memory.cpp@@ -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>#endifnamespace 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;+}#elseLockResult 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