Skip to content

Commit 03e97e1

Browse files
threadpool: skip polling for unused threads
Currently all threads do N polling rounds even if only 1 thread is active (n_threads_cur == 1). This commit adds a check to skip the polling for unused threads (ith >= n_threads_cur). n_threads_cur is now an atomic_int to explicitly tell thread sanitizer that it is written from one thread and read from other threads (not a race conditions).
1 parent bd35cb0 commit 03e97e1

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

ggml/src/ggml.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ struct ggml_threadpool {
20152015

20162016
struct ggml_compute_state * workers; // per thread state
20172017
int n_threads_max; // number of threads in the pool
2018-
int n_threads_cur; // number of threads used in the current graph
2018+
atomic_int n_threads_cur; // number of threads used in the current graph
20192019

20202020
int32_t prio; // Scheduling priority
20212021
uint32_t poll; // Polling level (0 - no polling)
@@ -3179,22 +3179,23 @@ inline static void ggml_critical_section_start(void) {
31793179

31803180
#ifdef GGML_USE_OPENMP
31813181
static void ggml_barrier(struct ggml_threadpool * threadpool) {
3182-
if (threadpool->n_threads_cur == 1) {
3182+
int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
3183+
if (n_threads == 1) {
31833184
return;
31843185
}
31853186

31863187
#pragma omp barrier
31873188
}
31883189
#else
31893190
static void ggml_barrier(struct ggml_threadpool * threadpool) {
3190-
if (threadpool->n_threads_cur == 1) {
3191+
int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
3192+
if (n_threads == 1) {
31913193
return;
31923194
}
31933195

31943196
atomic_int * n_barrier = &threadpool->n_barrier;
31953197
atomic_int * n_barrier_passed = &threadpool->n_barrier_passed;
31963198

3197-
int n_threads = threadpool->n_threads_cur;
31983199
int passed_old = atomic_load_explicit(n_barrier_passed, memory_order_relaxed);
31993200

32003201
if (atomic_fetch_add(n_barrier, 1) == n_threads - 1) {
@@ -19967,15 +19968,21 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
1996719968

1996819969
#ifndef GGML_USE_OPENMP
1996919970

19970-
static inline bool ggml_graph_compute_ready(struct ggml_compute_state * state) {
19971+
static inline bool ggml_graph_compute_thread_active(struct ggml_compute_state * state) {
19972+
struct ggml_threadpool * threadpool = state->threadpool;
19973+
int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
19974+
return (state->ith < n_threads);
19975+
}
19976+
19977+
static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * state) {
1997119978
struct ggml_threadpool * threadpool = state->threadpool;
1997219979

1997319980
if (state->pending || threadpool->stop || threadpool->pause) { return true; }
1997419981

1997519982
// check for new graph/work
1997619983
int new_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed);
1997719984
if (new_graph != state->last_graph) {
19978-
state->pending = (state->ith < threadpool->n_threads_cur);
19985+
state->pending = ggml_graph_compute_thread_active(state);
1997919986
state->last_graph = new_graph;
1998019987
}
1998119988

@@ -19985,11 +19992,16 @@ static inline bool ggml_graph_compute_ready(struct ggml_compute_state * state) {
1998519992
static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) {
1998619993
struct ggml_threadpool * threadpool = state->threadpool;
1998719994

19995+
// Skip polling for unused threads
19996+
if (!ggml_graph_compute_thread_active(state)) {
19997+
return state->pending;
19998+
}
19999+
1998820000
// This seems to make 0 ... 100 a decent range for polling level across modern processors.
1998920001
// Perhaps, we can adjust it dynamically based on load and things.
1999020002
const uint64_t n_rounds = 1024UL * 128 * threadpool->poll;
1999120003

19992-
for (uint64_t i=0; !ggml_graph_compute_ready(state) && i<n_rounds; i++) {
20004+
for (uint64_t i=0; !ggml_graph_compute_thread_ready(state) && i < n_rounds; i++) {
1999320005
// No new work. Keep polling.
1999420006
ggml_thread_cpu_relax();
1999520007
}
@@ -20005,9 +20017,9 @@ static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state *
2000520017
}
2000620018

2000720019
ggml_mutex_lock_shared(&threadpool->mutex);
20008-
while (!ggml_graph_compute_ready(state)) {
20020+
while (!ggml_graph_compute_thread_ready(state)) {
2000920021
// No new work. Wait for the signal.
20010-
GGML_PRINT_DEBUG("thread #%d waiting for work\n", state->ith);
20022+
GGML_PRINT_DEBUG("thread #%d waiting for work (sleeping)\n", state->ith);
2001120023
ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
2001220024
}
2001320025
ggml_mutex_unlock_shared(&threadpool->mutex);
@@ -20054,12 +20066,17 @@ static thread_ret_t ggml_graph_compute_secondary_thread(void* data) {
2005420066
}
2005520067

2005620068
// Start processing new graph
20057-
static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool)
20069+
static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool, int n_threads)
2005820070
{
2005920071
// always take the mutex here because the worker threads are doing hybrid poll/wait
2006020072

2006120073
ggml_mutex_lock(&threadpool->mutex);
2006220074

20075+
GGML_PRINT_DEBUG("threadpool: n_threads_cur %d n_threads %d\n", threadpool->n_threads_cur, n_threads);
20076+
20077+
// Update the number of active threads
20078+
atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
20079+
2006320080
atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_relaxed);
2006420081

2006520082
if (threadpool->pause) {
@@ -20194,15 +20211,10 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
2019420211
// No worker threads should be accessing the parameters below at this stage
2019520212
threadpool->cgraph = cgraph;
2019620213
threadpool->cplan = cplan;
20197-
threadpool->n_threads_cur = n_threads;
2019820214
threadpool->current_chunk = 0;
2019920215
threadpool->ec = GGML_STATUS_SUCCESS;
2020020216
}
2020120217

20202-
if (n_threads > threadpool->n_threads_max) {
20203-
GGML_PRINT("WARNING: cplan is requesting more threads than the threadpool contains. Expect a bad time!\n");
20204-
}
20205-
2020620218
#ifdef GGML_USE_OPENMP
2020720219
if (n_threads > 1) {
2020820220
#pragma omp parallel num_threads(n_threads)
@@ -20211,7 +20223,7 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
2021120223
{
2021220224
// update the number of threads from the actual number of threads that we got from OpenMP
2021320225
n_threads = omp_get_num_threads();
20214-
threadpool->n_threads_cur = n_threads;
20226+
atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
2021520227
}
2021620228

2021720229
ggml_graph_compute_thread(&threadpool->workers[omp_get_thread_num()]);
@@ -20220,8 +20232,13 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
2022020232
ggml_graph_compute_thread(&threadpool->workers[0]);
2022120233
}
2022220234
#else
20235+
if (n_threads > threadpool->n_threads_max) {
20236+
GGML_PRINT("WARNING: cplan requested more threads (%d) than available (%d)\n", n_threads, threadpool->n_threads_max);
20237+
n_threads = threadpool->n_threads_max;
20238+
}
20239+
2022320240
// Kick all threads to start the new graph
20224-
ggml_graph_compute_kickoff(threadpool);
20241+
ggml_graph_compute_kickoff(threadpool, n_threads);
2022520242

2022620243
// This is a work thread too
2022720244
ggml_graph_compute_thread(&threadpool->workers[0]);

0 commit comments

Comments
 (0)