Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advise the kernel to preload the mapped memory #740

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down