summaryrefslogtreecommitdiff
path: root/llama.cpp
diff options
context:
space:
mode:
authorDannyDaemonic <DannyDaemonic@gmail.com>2023-08-31 04:21:45 -0700
committerGitHub <noreply@github.com>2023-08-31 04:21:45 -0700
commite8422de39e4aa2f7e50574124b060a80607e654a (patch)
treebbec67823ab9242a72238a3a26c91ed6a9462acd /llama.cpp
parent92d0b751a77a089e650983e9f1564ef4d31b32b9 (diff)
@vxiiduu's fix for PrefetchVirtualMemory (#2930)
Reimplement fix for `PrefetchVirtualMemory`. Co-authored-by: vxiiduu <73044267+vxiiduu@users.noreply.github.com>
Diffstat (limited to 'llama.cpp')
-rw-r--r--llama.cpp27
1 files changed, 16 insertions, 11 deletions
diff --git a/llama.cpp b/llama.cpp
index 95ee6ffe..98a5da96 100644
--- a/llama.cpp
+++ b/llama.cpp
@@ -611,20 +611,25 @@ struct llama_mmap {
throw std::runtime_error(format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str()));
}
- #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
if (prefetch) {
- // Advise the kernel to preload the mapped memory
- WIN32_MEMORY_RANGE_ENTRY range;
- range.VirtualAddress = addr;
- range.NumberOfBytes = (SIZE_T)size;
- if (!PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
- fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n",
- llama_format_win_err(GetLastError()).c_str());
+ // PrefetchVirtualMemory is only present on Windows 8 and above, so we dynamically load it
+ BOOL (WINAPI *pPrefetchVirtualMemory) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG);
+ HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
+
+ // may fail on pre-Windows 8 systems
+ pPrefetchVirtualMemory = reinterpret_cast<decltype(pPrefetchVirtualMemory)> (GetProcAddress(hKernel32, "PrefetchVirtualMemory"));
+
+ if (pPrefetchVirtualMemory) {
+ // advise the kernel to preload the mapped memory
+ WIN32_MEMORY_RANGE_ENTRY range;
+ range.VirtualAddress = addr;
+ range.NumberOfBytes = (SIZE_T)size;
+ if (!pPrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) {
+ fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n",
+ llama_format_win_err(GetLastError()).c_str());
+ }
}
}
- #else
- #pragma message("warning: You are building for pre-Windows 8; prefetch not supported")
- #endif // _WIN32_WINNT >= _WIN32_WINNT_WIN8
}
~llama_mmap() {