@@ -2015,7 +2015,7 @@ struct ggml_threadpool {
2015
2015
2016
2016
struct ggml_compute_state * workers; // per thread state
2017
2017
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
2019
2019
2020
2020
int32_t prio; // Scheduling priority
2021
2021
uint32_t poll; // Polling level (0 - no polling)
@@ -3179,22 +3179,23 @@ inline static void ggml_critical_section_start(void) {
3179
3179
3180
3180
#ifdef GGML_USE_OPENMP
3181
3181
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) {
3183
3184
return;
3184
3185
}
3185
3186
3186
3187
#pragma omp barrier
3187
3188
}
3188
3189
#else
3189
3190
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) {
3191
3193
return;
3192
3194
}
3193
3195
3194
3196
atomic_int * n_barrier = &threadpool->n_barrier;
3195
3197
atomic_int * n_barrier_passed = &threadpool->n_barrier_passed;
3196
3198
3197
- int n_threads = threadpool->n_threads_cur;
3198
3199
int passed_old = atomic_load_explicit(n_barrier_passed, memory_order_relaxed);
3199
3200
3200
3201
if (atomic_fetch_add(n_barrier, 1) == n_threads - 1) {
@@ -19967,15 +19968,21 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
19967
19968
19968
19969
#ifndef GGML_USE_OPENMP
19969
19970
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) {
19971
19978
struct ggml_threadpool * threadpool = state->threadpool;
19972
19979
19973
19980
if (state->pending || threadpool->stop || threadpool->pause) { return true; }
19974
19981
19975
19982
// check for new graph/work
19976
19983
int new_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed);
19977
19984
if (new_graph != state->last_graph) {
19978
- state->pending = (state->ith < threadpool->n_threads_cur );
19985
+ state->pending = ggml_graph_compute_thread_active (state);
19979
19986
state->last_graph = new_graph;
19980
19987
}
19981
19988
@@ -19985,11 +19992,16 @@ static inline bool ggml_graph_compute_ready(struct ggml_compute_state * state) {
19985
19992
static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) {
19986
19993
struct ggml_threadpool * threadpool = state->threadpool;
19987
19994
19995
+ // Skip polling for unused threads
19996
+ if (!ggml_graph_compute_thread_active(state)) {
19997
+ return state->pending;
19998
+ }
19999
+
19988
20000
// This seems to make 0 ... 100 a decent range for polling level across modern processors.
19989
20001
// Perhaps, we can adjust it dynamically based on load and things.
19990
20002
const uint64_t n_rounds = 1024UL * 128 * threadpool->poll;
19991
20003
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++) {
19993
20005
// No new work. Keep polling.
19994
20006
ggml_thread_cpu_relax();
19995
20007
}
@@ -20005,9 +20017,9 @@ static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state *
20005
20017
}
20006
20018
20007
20019
ggml_mutex_lock_shared(&threadpool->mutex);
20008
- while (!ggml_graph_compute_ready (state)) {
20020
+ while (!ggml_graph_compute_thread_ready (state)) {
20009
20021
// 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);
20011
20023
ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
20012
20024
}
20013
20025
ggml_mutex_unlock_shared(&threadpool->mutex);
@@ -20054,12 +20066,17 @@ static thread_ret_t ggml_graph_compute_secondary_thread(void* data) {
20054
20066
}
20055
20067
20056
20068
// 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 )
20058
20070
{
20059
20071
// always take the mutex here because the worker threads are doing hybrid poll/wait
20060
20072
20061
20073
ggml_mutex_lock(&threadpool->mutex);
20062
20074
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
+
20063
20080
atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_relaxed);
20064
20081
20065
20082
if (threadpool->pause) {
@@ -20194,15 +20211,10 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
20194
20211
// No worker threads should be accessing the parameters below at this stage
20195
20212
threadpool->cgraph = cgraph;
20196
20213
threadpool->cplan = cplan;
20197
- threadpool->n_threads_cur = n_threads;
20198
20214
threadpool->current_chunk = 0;
20199
20215
threadpool->ec = GGML_STATUS_SUCCESS;
20200
20216
}
20201
20217
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
-
20206
20218
#ifdef GGML_USE_OPENMP
20207
20219
if (n_threads > 1) {
20208
20220
#pragma omp parallel num_threads(n_threads)
@@ -20211,7 +20223,7 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
20211
20223
{
20212
20224
// update the number of threads from the actual number of threads that we got from OpenMP
20213
20225
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) ;
20215
20227
}
20216
20228
20217
20229
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
20220
20232
ggml_graph_compute_thread(&threadpool->workers[0]);
20221
20233
}
20222
20234
#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
+
20223
20240
// Kick all threads to start the new graph
20224
- ggml_graph_compute_kickoff(threadpool);
20241
+ ggml_graph_compute_kickoff(threadpool, n_threads );
20225
20242
20226
20243
// This is a work thread too
20227
20244
ggml_graph_compute_thread(&threadpool->workers[0]);
0 commit comments