From d3bc4df97db470d4e719bab387bb546bc5dabc70 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sat, 1 Apr 2023 19:45:33 +0200 Subject: [PATCH] fix windows build --- ggml.c | 89 ----------------------------------------- thpool.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 105 insertions(+), 102 deletions(-) diff --git a/ggml.c b/ggml.c index 58e51159c..1c3e28d37 100644 --- a/ggml.c +++ b/ggml.c @@ -52,81 +52,11 @@ static LONG atomic_fetch_sub(atomic_int* ptr, LONG dec) { return atomic_fetch_add(ptr, -(dec)); } -typedef HANDLE pthread_t; - -typedef DWORD thread_ret_t; -static int pthread_create(pthread_t* out, void* unused, thread_ret_t(*func)(void*), void* arg) { - HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL); - if (handle == NULL) - { - return EAGAIN; - } - - *out = handle; - return 0; -} - -static int pthread_join(pthread_t thread, void* unused) { - return (int) WaitForSingleObject(thread, INFINITE); -} - static int sched_yield (void) { Sleep (0); return 0; } -typedef struct pthread_mutex_tag { - CRITICAL_SECTION critical_section; -} pthread_mutex_t; - -typedef struct pthread_mutexattr_tag { - int attr; -} pthread_mutexattr_t; - -int pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * attr) { - InitializeCriticalSection (&mutex->critical_section); - return 0; -} - -int pthread_mutex_destroy(pthread_mutex_t * mutex) { - DeleteCriticalSection(&mutex->critical_section); - return 0; -} - - -int pthread_mutex_lock(pthread_mutex_t * mutex) { - EnterCriticalSection(&mutex->critical_section); - return 0; -} - -int pthread_mutex_unlock(pthread_mutex_t * mutex) { - LeaveCriticalSection(&mutex->critical_section); - return 0; -} - -typedef struct pthread_cond_tag { - CONDITION_VARIABLE cond; -} pthread_cond_t; - -int pthread_cond_init(pthread_cond_t * cond, void * unused) { - InitializeConditionVariable (&cond->cond); - return 0; -} - -int pthread_cond_destroy(pthread_cond_t * cond) { - return 0; -} - -int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex) { - SleepConditionVariableCS(&cond->cond, &mutex->critical_section, INFINITE); - return 0; -} - -int pthread_cond_broadcast(pthread_cond_t * cond) { - WakeAllConditionVariable(&cond->cond); - return 0; -} - #else #include #include @@ -9042,14 +8972,10 @@ typedef int ggml_lock_t; #define GGML_LOCK_INITIALIZER 0 -typedef pthread_t ggml_thread_t; #define ggml_thread_create pthread_create #define ggml_thread_join pthread_join -typedef pthread_mutex_t ggml_mutex_t; -typedef pthread_cond_t ggml_cond_t; - #define ggml_mutex_init pthread_mutex_init #define ggml_mutex_destroy pthread_mutex_destroy #define ggml_cond_init pthread_cond_init @@ -9063,19 +8989,10 @@ typedef pthread_cond_t ggml_cond_t; #endif struct ggml_compute_state_shared { - int n_threads; - - // synchronization primitives - int n_ready; - bool has_work; - bool stop; // stop all threads - ggml_mutex_t mutex; - ggml_cond_t cond; }; struct ggml_compute_state { - ggml_thread_t thrd; struct ggml_compute_params params; struct ggml_tensor * node; @@ -9097,11 +9014,6 @@ void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph) const int n_threads = cgraph->n_threads; struct ggml_compute_state_shared state_shared = { /*.n_threads =*/ n_threads, - /*.n_ready =*/ 0, - /*.has_work =*/ false, - /*.stop =*/ false, - /*.mutex =*/ {0}, - /*.cond =*/ {0}, }; struct ggml_compute_state * workers = n_threads > 1 ? alloca(sizeof(struct ggml_compute_state)*(n_threads - 1)) : NULL; @@ -9110,7 +9022,6 @@ void ggml_graph_compute(struct ggml_context * ctx, struct ggml_cgraph * cgraph) ctx->tpool = thpool_init(n_threads); for (int j = 0; j < n_threads - 1; j++) { workers[j] = (struct ggml_compute_state) { - .thrd = 0, .params = { .type = GGML_TASK_COMPUTE, .ith = j + 1, diff --git a/thpool.c b/thpool.c index 59e2e5556..76cf5fc3f 100644 --- a/thpool.c +++ b/thpool.c @@ -15,11 +15,111 @@ #define _POSIX_C_SOURCE 200809L #endif #endif +/* +this is not part of original thpool, thats me hacking it to work on windows +*/ +#if defined _MSC_VER || defined(__MINGW32__) +#if !defined(__MINGW32__) +#include +#else +// ref: https://github.com/ggerganov/whisper.cpp/issues/168 +#include +#endif + +unsigned int sleep(unsigned int seconds) { + Sleep(seconds * 1000); + return 0; +} + +typedef HANDLE pthread_t; + +typedef DWORD thread_ret_t; +static int pthread_create(pthread_t* out, void* unused, thread_ret_t(*func)(void*), void* arg) { + HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL); + if (handle == NULL) + { + return EAGAIN; + } + + *out = handle; + return 0; +} + +static int pthread_join(pthread_t thread, void* unused) { + return (int) WaitForSingleObject(thread, INFINITE); +} + +static int pthread_detach(pthread_t thread) { + CloseHandle(thread); + return 0; +} + +typedef struct pthread_mutex_tag { + CRITICAL_SECTION critical_section; +} pthread_mutex_t; + +typedef struct pthread_mutexattr_tag { + int attr; +} pthread_mutexattr_t; + +int pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * attr) { + InitializeCriticalSection (&mutex->critical_section); + return 0; +} + +int pthread_mutex_destroy(pthread_mutex_t * mutex) { + DeleteCriticalSection(&mutex->critical_section); + return 0; +} + + +int pthread_mutex_lock(pthread_mutex_t * mutex) { + EnterCriticalSection(&mutex->critical_section); + return 0; +} + +int pthread_mutex_unlock(pthread_mutex_t * mutex) { + LeaveCriticalSection(&mutex->critical_section); + return 0; +} + +typedef struct pthread_cond_tag { + CONDITION_VARIABLE cond; +} pthread_cond_t; + +int pthread_cond_init(pthread_cond_t * cond, void * unused) { + InitializeConditionVariable (&cond->cond); + return 0; +} + +int pthread_cond_destroy(pthread_cond_t * cond) { + return 0; +} + +int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex) { + SleepConditionVariableCS(&cond->cond, &mutex->critical_section, INFINITE); + return 0; +} + +int pthread_cond_broadcast(pthread_cond_t * cond) { + WakeAllConditionVariable(&cond->cond); + return 0; +} + +int pthread_cond_signal(pthread_cond_t * cond) { + WakeConditionVariable(&cond->cond); + return 0; +} +#else #include +#include +#endif + #include #include #include -#include + + #include #include #if defined(__linux__) @@ -247,16 +347,6 @@ void thpool_destroy(thpool_* thpool_p){ free(thpool_p); } - -/* Pause all threads in threadpool */ -void thpool_pause(thpool_* thpool_p) { - int n; - for (n=0; n < thpool_p->num_threads_alive; n++){ - pthread_kill(thpool_p->threads[n]->pthread, SIGUSR1); - } -} - - /* Resume all threads in threadpool */ void thpool_resume(thpool_* thpool_p) { // resuming a single threadpool hasn't been @@ -332,20 +422,22 @@ static void* thread_do(struct thread* thread_p){ #elif defined(__APPLE__) && defined(__MACH__) pthread_setname_np(thread_name); #else - err("thread_do(): pthread_setname_np is not supported on this system"); + // err("thread_do(): pthread_setname_np is not supported on this system"); #endif /* Assure all threads have been created before starting serving */ thpool_* thpool_p = thread_p->thpool_p; /* Register signal handler */ + /* + ///// HACK struct sigaction act; sigemptyset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = thread_hold; if (sigaction(SIGUSR1, &act, NULL) == -1) { err("thread_do(): cannot handle SIGUSR1"); - } + }*/ /* Mark thread as alive (initialized) */ pthread_mutex_lock(&thpool_p->thcount_lock);