Skip to content

Commit a809518

Browse files
threadpool: improve abort handling
Do not use threadpool->ec (exit code) to decide whether to exit the compute loop. threadpool->ec is not atomic which makes thread-sanitizer rightfully unhappy about it. Instead introduce atomic threadpool->abort flag used for this. This is consistent with how we handle threadpool->stop or pause. While at it add an explicit atomic_load for n_threads_cur for consistency.
1 parent b9763b3 commit a809518

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

ggml/src/ggml.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,7 @@ struct ggml_threadpool {
20132013
// these are atomic as an annotation for thread-sanitizer
20142014
atomic_bool stop; // Used for stopping the threadpool altogether
20152015
atomic_bool pause; // Used for pausing the threadpool or individual threads
2016+
atomic_bool abort; // Used for aborting processing of a graph
20162017

20172018
struct ggml_compute_state * workers; // per thread state
20182019
int n_threads_max; // number of threads in the pool
@@ -19928,34 +19929,33 @@ struct ggml_cplan ggml_graph_plan(
1992819929

1992919930
static thread_ret_t ggml_graph_compute_thread(void * data) {
1993019931
struct ggml_compute_state * state = (struct ggml_compute_state *) data;
19932+
struct ggml_threadpool * tp = state->threadpool;
1993119933

19932-
const struct ggml_cgraph * cgraph = state->threadpool->cgraph;
19933-
const struct ggml_cplan * cplan = state->threadpool->cplan;
19934+
const struct ggml_cgraph * cgraph = tp->cgraph;
19935+
const struct ggml_cplan * cplan = tp->cplan;
1993419936

1993519937
set_numa_thread_affinity(state->ith);
1993619938

1993719939
struct ggml_compute_params params = {
1993819940
/*.ith =*/ state->ith,
19939-
/*.nth =*/ state->threadpool->n_threads_cur,
19941+
/*.nth =*/ atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed),
1994019942
/*.wsize =*/ cplan->work_size,
1994119943
/*.wdata =*/ cplan->work_data,
19942-
/*.threadpool=*/ state->threadpool,
19944+
/*.threadpool=*/ tp,
1994319945
};
1994419946

19945-
for (int node_n = 0; node_n < cgraph->n_nodes; node_n++) {
19947+
for (int node_n = 0; node_n < cgraph->n_nodes && !tp->abort; node_n++) {
1994619948
struct ggml_tensor * node = cgraph->nodes[node_n];
1994719949

1994819950
ggml_compute_forward(&params, node);
1994919951

19950-
if (state->ith == 0 && cplan->abort_callback && cplan->abort_callback(cplan->abort_callback_data)) {
19951-
state->threadpool->ec = GGML_STATUS_ABORTED;
19952+
if (state->ith == 0 && cplan->abort_callback &&
19953+
cplan->abort_callback(cplan->abort_callback_data)) {
19954+
tp->abort = true;
19955+
tp->ec = GGML_STATUS_ABORTED;
1995219956
}
1995319957

1995419958
ggml_barrier(state->threadpool);
19955-
19956-
if (state->threadpool->ec != GGML_STATUS_SUCCESS) {
19957-
break;
19958-
}
1995919959
}
1996019960

1996119961
return 0;
@@ -20144,6 +20144,7 @@ static struct ggml_threadpool * ggml_threadpool_new_impl(
2014420144
threadpool->current_chunk = 0;
2014520145
threadpool->stop = false;
2014620146
threadpool->pause = tpp->paused;
20147+
threadpool->abort = false;
2014720148
threadpool->workers = NULL;
2014820149
threadpool->n_threads_max = tpp->n_threads;
2014920150
threadpool->n_threads_cur = tpp->n_threads;
@@ -20220,6 +20221,7 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
2022020221
threadpool->cgraph = cgraph;
2022120222
threadpool->cplan = cplan;
2022220223
threadpool->current_chunk = 0;
20224+
threadpool->abort = false;
2022320225
threadpool->ec = GGML_STATUS_SUCCESS;
2022420226
}
2022520227

0 commit comments

Comments
 (0)