Skip to content

Commit 0d23f8c

Browse files
committed
disable mmap prefetch/readahead for NUMA systems
1 parent 6fc5f17 commit 0d23f8c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

llama-util.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ static std::string llama_format_win_err(DWORD err) {
163163
}
164164
#endif
165165

166+
extern "C" {
167+
bool ggml_is_numa();
168+
}
166169
struct llama_mmap {
167170
void * addr;
168171
size_t size;
@@ -176,8 +179,10 @@ struct llama_mmap {
176179
size = file->size;
177180
int fd = fileno(file->fp);
178181
int flags = MAP_SHARED;
182+
// prefetch/readahead impairs performance on NUMA systems
183+
if (ggml_is_numa()) prefetch = 0;
179184
#ifdef __linux__
180-
flags |= MAP_POPULATE;
185+
if (prefetch) flags |= MAP_POPULATE;
181186
#endif
182187
addr = mmap(NULL, file->size, PROT_READ, flags, fd, 0);
183188
if (addr == MAP_FAILED) {
@@ -191,6 +196,14 @@ struct llama_mmap {
191196
strerror(errno));
192197
}
193198
}
199+
if (ggml_is_numa()) {
200+
// advise the kernel not to use readahead
201+
// (because the next page might not belong on the same node)
202+
if (madvise(addr, file->size, MADV_RANDOM)) {
203+
fprintf(stderr, "warning: madvise(.., MADV_RANDOM) failed: %s\n",
204+
strerror(errno));
205+
}
206+
}
194207
}
195208

196209
~llama_mmap() {

0 commit comments

Comments
 (0)