Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
slaren committed Sep 16, 2024
1 parent 23e0d70 commit 695c448
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 141 deletions.
26 changes: 18 additions & 8 deletions ggml/src/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -3168,16 +3168,32 @@ struct ggml_state {

// global state
static struct ggml_state g_state;
static atomic_flag g_state_critical = ATOMIC_FLAG_INIT;

#if !defined(_MSC_VER)
// critical section via pthread mutex
static pthread_mutex_t g_state_mutex = PTHREAD_MUTEX_INITIALIZER;
static void ggml_critical_section_start(void) {
pthread_mutex_lock(&g_state_mutex);
}

static void ggml_critical_section_end(void) {
pthread_mutex_unlock(&g_state_mutex);
}
#else
// critical section via spin lock
inline static void ggml_critical_section_start(void) {
static atomic_flag g_state_critical = ATOMIC_FLAG_INIT;
static void ggml_critical_section_start(void) {
while (atomic_flag_test_and_set(&g_state_critical)) {
// spin
sched_yield();
}
}

static void ggml_critical_section_end(void) {
atomic_flag_clear(&g_state_critical);
}
#endif

#ifdef GGML_USE_OPENMP
static void ggml_barrier(struct ggml_threadpool * threadpool) {
if (threadpool->n_threads_cur == 1) {
Expand Down Expand Up @@ -3214,12 +3230,6 @@ static void ggml_barrier(struct ggml_threadpool * threadpool) {
}
#endif

// TODO: make this somehow automatically executed
// some sort of "sentry" mechanism
inline static void ggml_critical_section_end(void) {
atomic_flag_clear(&g_state_critical);
}

#if defined(__gnu_linux__)
static cpu_set_t ggml_get_numa_affinity(void) {
cpu_set_t cpuset;
Expand Down
Loading

0 comments on commit 695c448

Please sign in to comment.