From 888db62c8070ca71f56026437a0a305c58b03ae2 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 3 Apr 2023 12:28:49 +0200 Subject: [PATCH] Advise the kernel to preload the mapped memory --- llama.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/llama.cpp b/llama.cpp index 854bb8993fbc5..dad4f9fa1fc89 100644 --- a/llama.cpp +++ b/llama.cpp @@ -327,13 +327,24 @@ static void *mmap_file(const char *fname, uint64_t *mm_length) { void *addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); CloseHandle(hMapping); if (!addr) return 0; + // Advise the kernel to preload the mapped memory + WIN32_MEMORY_RANGE_ENTRY range; + range.VirtualAddress = addr; + range.NumberOfBytes = (SIZE_T)length; + PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0); #else int fd = open(fname, O_RDONLY); if (fd == -1) return 0; int64_t length = lseek(fd, 0, SEEK_END); +#ifdef __linux__ + void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0); +#else // MAP_POPULATE is only supported on Linux void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0); +#endif close(fd); if (addr == MAP_FAILED) return 0; + // Advise the kernel to preload the mapped memory + madvise(addr, length, MADV_WILLNEED); #endif *mm_length = length; return addr;