Skip to content

Commit 658f16c

Browse files
max-krasnyanskyfmz
authored andcommitted
threadpool: update calling thread prio and affinity only at start/resume
This avoids extra syscalls for each graph_compute()
1 parent 8186e96 commit 658f16c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

ggml/src/ggml.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19237,6 +19237,12 @@ static void ggml_graph_compute_kickoff(struct ggml_compute_threadpool * threadpo
1923719237
atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_relaxed);
1923819238

1923919239
if (threadpool->pause) {
19240+
// Update main thread prio and affinity to match the threadpool settings
19241+
ggml_thread_apply_thread_priority(threadpool->prio);
19242+
if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
19243+
ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
19244+
}
19245+
1924019246
// resume does cond broadcast
1924119247
ggml_resume_threadpool_locked(threadpool);
1924219248
} else {
@@ -19324,6 +19330,14 @@ static struct ggml_compute_threadpool * ggml_create_threadpool_impl(
1932419330
}
1932519331

1932619332
ggml_thread_cpumask_next(tpp->cpumask, workers[0].cpumask, tpp->strict_cpu, &cpumask_iter);
19333+
19334+
if (!threadpool->pause) {
19335+
// Update main thread prio and affinity at the start, otherwise we'll do it in resume
19336+
ggml_thread_apply_thread_priority(threadpool->prio);
19337+
if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
19338+
ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
19339+
}
19340+
}
1932719341
#endif // GGML_USE_OPENMP
1932819342

1932919343
return threadpool;
@@ -19380,12 +19394,6 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
1938019394
ggml_graph_compute_thread(&threadpool->workers[0]);
1938119395
}
1938219396
#else
19383-
// Update main thread prio and affinity to match the current threadpool
19384-
ggml_thread_apply_thread_priority(threadpool->prio);
19385-
if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
19386-
ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
19387-
}
19388-
1938919397
// Kick all threads to start the new graph
1939019398
ggml_graph_compute_kickoff(threadpool);
1939119399

0 commit comments

Comments
 (0)