mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2025-01-05 16:24:34 +00:00
fix windows build
This commit is contained in:
parent
a65d37ad36
commit
d3bc4df97d
89
ggml.c
89
ggml.c
@ -52,81 +52,11 @@ static LONG atomic_fetch_sub(atomic_int* ptr, LONG dec) {
|
|||||||
return atomic_fetch_add(ptr, -(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) {
|
static int sched_yield (void) {
|
||||||
Sleep (0);
|
Sleep (0);
|
||||||
return 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
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
@ -9042,14 +8972,10 @@ typedef int ggml_lock_t;
|
|||||||
|
|
||||||
#define GGML_LOCK_INITIALIZER 0
|
#define GGML_LOCK_INITIALIZER 0
|
||||||
|
|
||||||
typedef pthread_t ggml_thread_t;
|
|
||||||
|
|
||||||
#define ggml_thread_create pthread_create
|
#define ggml_thread_create pthread_create
|
||||||
#define ggml_thread_join pthread_join
|
#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_init pthread_mutex_init
|
||||||
#define ggml_mutex_destroy pthread_mutex_destroy
|
#define ggml_mutex_destroy pthread_mutex_destroy
|
||||||
#define ggml_cond_init pthread_cond_init
|
#define ggml_cond_init pthread_cond_init
|
||||||
@ -9063,19 +8989,10 @@ typedef pthread_cond_t ggml_cond_t;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct ggml_compute_state_shared {
|
struct ggml_compute_state_shared {
|
||||||
|
|
||||||
int n_threads;
|
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 {
|
struct ggml_compute_state {
|
||||||
ggml_thread_t thrd;
|
|
||||||
|
|
||||||
struct ggml_compute_params params;
|
struct ggml_compute_params params;
|
||||||
struct ggml_tensor * node;
|
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;
|
const int n_threads = cgraph->n_threads;
|
||||||
struct ggml_compute_state_shared state_shared = {
|
struct ggml_compute_state_shared state_shared = {
|
||||||
/*.n_threads =*/ n_threads,
|
/*.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;
|
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);
|
ctx->tpool = thpool_init(n_threads);
|
||||||
for (int j = 0; j < n_threads - 1; j++) {
|
for (int j = 0; j < n_threads - 1; j++) {
|
||||||
workers[j] = (struct ggml_compute_state) {
|
workers[j] = (struct ggml_compute_state) {
|
||||||
.thrd = 0,
|
|
||||||
.params = {
|
.params = {
|
||||||
.type = GGML_TASK_COMPUTE,
|
.type = GGML_TASK_COMPUTE,
|
||||||
.ith = j + 1,
|
.ith = j + 1,
|
||||||
|
118
thpool.c
118
thpool.c
@ -15,11 +15,111 @@
|
|||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#endif
|
#endif
|
||||||
#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 <Windows.h>
|
||||||
|
#else
|
||||||
|
// ref: https://github.com/ggerganov/whisper.cpp/issues/168
|
||||||
|
#include <windows.h>
|
||||||
|
#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 <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
@ -247,16 +347,6 @@ void thpool_destroy(thpool_* thpool_p){
|
|||||||
free(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 */
|
/* Resume all threads in threadpool */
|
||||||
void thpool_resume(thpool_* thpool_p) {
|
void thpool_resume(thpool_* thpool_p) {
|
||||||
// resuming a single threadpool hasn't been
|
// resuming a single threadpool hasn't been
|
||||||
@ -332,20 +422,22 @@ static void* thread_do(struct thread* thread_p){
|
|||||||
#elif defined(__APPLE__) && defined(__MACH__)
|
#elif defined(__APPLE__) && defined(__MACH__)
|
||||||
pthread_setname_np(thread_name);
|
pthread_setname_np(thread_name);
|
||||||
#else
|
#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
|
#endif
|
||||||
|
|
||||||
/* Assure all threads have been created before starting serving */
|
/* Assure all threads have been created before starting serving */
|
||||||
thpool_* thpool_p = thread_p->thpool_p;
|
thpool_* thpool_p = thread_p->thpool_p;
|
||||||
|
|
||||||
/* Register signal handler */
|
/* Register signal handler */
|
||||||
|
/*
|
||||||
|
///// HACK
|
||||||
struct sigaction act;
|
struct sigaction act;
|
||||||
sigemptyset(&act.sa_mask);
|
sigemptyset(&act.sa_mask);
|
||||||
act.sa_flags = 0;
|
act.sa_flags = 0;
|
||||||
act.sa_handler = thread_hold;
|
act.sa_handler = thread_hold;
|
||||||
if (sigaction(SIGUSR1, &act, NULL) == -1) {
|
if (sigaction(SIGUSR1, &act, NULL) == -1) {
|
||||||
err("thread_do(): cannot handle SIGUSR1");
|
err("thread_do(): cannot handle SIGUSR1");
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/* Mark thread as alive (initialized) */
|
/* Mark thread as alive (initialized) */
|
||||||
pthread_mutex_lock(&thpool_p->thcount_lock);
|
pthread_mutex_lock(&thpool_p->thcount_lock);
|
||||||
|
Loading…
Reference in New Issue
Block a user