Skip to content

Commit 781cdb9

Browse files
committed
OfferVirtualMemory
1 parent d129edb commit 781cdb9

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

llama.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ struct llama_mmap {
15461546
}
15471547
}
15481548

1549-
void unmap_fragment(size_t first, size_t last) {
1549+
virtual void unmap_fragment(size_t first, size_t last) {
15501550
// not supported
15511551
GGML_UNUSED(first);
15521552
GGML_UNUSED(last);
@@ -1652,6 +1652,29 @@ struct llama_anonymous_mmap : llama_mmap {
16521652
throw std::runtime_error("unexpectedly reached end of file");
16531653
}
16541654
}
1655+
1656+
#if _WIN32_WINNT >= 0x603
1657+
void unmap_fragment(size_t first, size_t last) override {
1658+
DWORD (WINAPI *pOfferVirtualMemory) (PVOID, SIZE_T, DWORD);
1659+
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
1660+
1661+
pOfferVirtualMemory = reinterpret_cast<decltype(pOfferVirtualMemory)> (GetProcAddress(hKernel32, "OfferVirtualMemory"));
1662+
1663+
if (pOfferVirtualMemory) {
1664+
SYSTEM_INFO siSysInfo;
1665+
GetSystemInfo(&siSysInfo);
1666+
DWORD dwPageSize = siSysInfo.dwPageSize;
1667+
1668+
align_to_next_page(&first, dwPageSize);
1669+
align_to_previous_page(&last, dwPageSize);
1670+
1671+
if (pOfferVirtualMemory((char *) addr + first, last - first, 0x00000004 /* VMOfferPriorityNormal */)) {
1672+
LLAMA_LOG_WARN("warning: OfferVirtualMemory failed: %s\n", llama_format_win_err(GetLastError()).c_str());
1673+
}
1674+
}
1675+
}
1676+
#endif
1677+
16551678
#else
16561679
llama_anonymous_mmap(struct llama_file * file) {
16571680
GGML_UNUSED(file);

0 commit comments

Comments
 (0)