Skip to content

Commit

Permalink
disable mmap prefetch/readahead for NUMA systems
Browse files Browse the repository at this point in the history
  • Loading branch information
zrm committed May 21, 2023
1 parent 6fc5f17 commit 0d23f8c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion llama-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ static std::string llama_format_win_err(DWORD err) {
}
#endif

extern "C" {
bool ggml_is_numa();
}
struct llama_mmap {
void * addr;
size_t size;
Expand All @@ -176,8 +179,10 @@ struct llama_mmap {
size = file->size;
int fd = fileno(file->fp);
int flags = MAP_SHARED;
// prefetch/readahead impairs performance on NUMA systems
if (ggml_is_numa()) prefetch = 0;
#ifdef __linux__
flags |= MAP_POPULATE;
if (prefetch) flags |= MAP_POPULATE;
#endif
addr = mmap(NULL, file->size, PROT_READ, flags, fd, 0);
if (addr == MAP_FAILED) {
Expand All @@ -191,6 +196,14 @@ struct llama_mmap {
strerror(errno));
}
}
if (ggml_is_numa()) {
// advise the kernel not to use readahead
// (because the next page might not belong on the same node)
if (madvise(addr, file->size, MADV_RANDOM)) {
fprintf(stderr, "warning: madvise(.., MADV_RANDOM) failed: %s\n",
strerror(errno));
}
}
}

~llama_mmap() {
Expand Down

0 comments on commit 0d23f8c

Please sign in to comment.