Skip to content

Commit fb7ad40

Browse files
committed
Windows fixes
Mostly for msys2 and mingw64 builds, which are different from each other and different from standard Visual Studio builds. Isn't Windows fun? - Define _GNU_SOURCE in more files (it's already used in ggml.c for Linux's sake). - Don't use PrefetchVirtualMemory if not building for Windows 8 or later (mingw64 doesn't by default). But warn the user about this situation since it's probably not intended. - Check for NOMINMAX already being defined, which it is on mingw64. - Actually use the `increment` variable (bug in my `pizza` PR). - Suppress unused variable warnings in the fake pthread_create and pthread_join implementations for Windows. - (not Windows-related) Remove mention of `asprintf` from comment; `asprintf` is no longer used. Fixes ggml-org#871.
1 parent a0caa34 commit fb7ad40

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

examples/main/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Defines sigaction on msys:
2+
#ifndef _GNU_SOURCE
3+
#define _GNU_SOURCE
4+
#endif
5+
16
#include "common.h"
27
#include "llama.h"
38

ggml.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Defines CLOCK_MONOTONIC and asprintf on Linux
1+
// Defines CLOCK_MONOTONIC on Linux
22
#define _GNU_SOURCE
33

44
#include "ggml.h"
@@ -50,6 +50,7 @@ typedef HANDLE pthread_t;
5050

5151
typedef DWORD thread_ret_t;
5252
static int pthread_create(pthread_t* out, void* unused, thread_ret_t(*func)(void*), void* arg) {
53+
(void) unused;
5354
HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL);
5455
if (handle == NULL)
5556
{
@@ -61,6 +62,7 @@ static int pthread_create(pthread_t* out, void* unused, thread_ret_t(*func)(void
6162
}
6263

6364
static int pthread_join(pthread_t thread, void* unused) {
65+
(void) unused;
6466
return (int) WaitForSingleObject(thread, INFINITE);
6567
}
6668

llama.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Defines fileno on msys:
2+
#ifndef _GNU_SOURCE
3+
#define _GNU_SOURCE
4+
#endif
5+
16
#include "llama_util.h"
27
#include "llama.h"
38
#include "llama_internal.h"

llama_util.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
#if defined(_WIN32)
2828
#define WIN32_LEAN_AND_MEAN
29-
#define NOMINMAX
29+
#ifndef NOMINMAX
30+
#define NOMINMAX
31+
#endif
3032
#include <windows.h>
3133
#include <io.h>
3234
#include <stdio.h> // for _fseeki64
@@ -209,6 +211,7 @@ struct llama_mmap {
209211
throw format("MapViewOfFile failed: %s", llama_format_win_err(error).c_str());
210212
}
211213

214+
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
212215
// Advise the kernel to preload the mapped memory
213216
WIN32_MEMORY_RANGE_ENTRY range;
214217
range.VirtualAddress = addr;
@@ -217,6 +220,9 @@ struct llama_mmap {
217220
fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n",
218221
llama_format_win_err(GetLastError()).c_str());
219222
}
223+
#else
224+
#pragma message("warning: You are building for pre-Windows 8; prefetch not supported")
225+
#endif // _WIN32_WINNT >= _WIN32_WINNT_WIN8
220226
}
221227

222228
~llama_mmap() {
@@ -338,8 +344,8 @@ struct llama_mlock {
338344
// Hopefully a megabyte is enough overhead:
339345
size_t increment = size + 1048576;
340346
// The minimum must be <= the maximum, so we need to increase both:
341-
min_ws_size += size;
342-
max_ws_size += size;
347+
min_ws_size += increment;
348+
max_ws_size += increment;
343349
if (!SetProcessWorkingSetSize(GetCurrentProcess(), min_ws_size, max_ws_size)) {
344350
fprintf(stderr, "warning: SetProcessWorkingSetSize failed: %s\n",
345351
llama_format_win_err(GetLastError()).c_str());

0 commit comments

Comments
 (0)