Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of Open MP Barrier for thread sync to avoid atomic operations #8002

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -19033,8 +19033,11 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
state->ec = GGML_STATUS_ABORTED;
return 0;
}

#ifdef GGML_USE_OPENMP
if (state->ith == 0) {
#else
if (atomic_fetch_sub(&state->shared->n_active, 1) == 1) {
#endif
// all other threads are finished and spinning
// do finalize and init here so we don't have synchronize again
struct ggml_compute_params params = {
Expand Down Expand Up @@ -19094,13 +19097,23 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
}

task_phase = GGML_TASK_TYPE_INIT;
#ifdef GGML_USE_OPENMP
state->shared->n_active = n_threads;
state->shared->node_n = node_n;
state->shared->node_task = task_phase;
}
#pragma omp barrier
node_n = state->shared->node_n;
task_phase = state->shared->node_task;
#else
atomic_store(&state->shared->n_active, n_threads);
atomic_store(&state->shared->node_n, node_n);
atomic_store(&state->shared->node_task, task_phase);
} else {
ggml_graph_compute_thread_sync_node(&node_n, state, false);
ggml_graph_compute_thread_sync_task(&task_phase, state, false);
}
#endif

// check if we should stop
if (node_n >= cgraph->n_nodes) break;
Expand All @@ -19122,7 +19135,15 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
ggml_compute_forward(&params, node, state);
}
}

#ifdef GGML_USE_OPENMP
if (state->ith == 0) {
task_phase = GGML_TASK_TYPE_COMPUTE;
state->shared->n_active = n_threads;
state->shared->node_task = task_phase;
}
#pragma omp barrier
task_phase = state->shared->node_task;
#else
if (atomic_fetch_sub(&state->shared->n_active, 1) == 1) {
task_phase = GGML_TASK_TYPE_COMPUTE;
atomic_store(&state->shared->n_active, n_threads);
Expand All @@ -19137,12 +19158,21 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
const bool do_yield = node_n < 0 || cgraph->nodes[node_n]->op == GGML_OP_MUL_MAT;
ggml_graph_compute_thread_sync_task(&task_phase, state, do_yield);
}
#endif

if (state->ith < n_tasks) {
params.type = GGML_TASK_TYPE_COMPUTE;
ggml_compute_forward(&params, node, state);
}

#ifdef GGML_USE_OPENMP
if (state->ith == 0) {
task_phase = GGML_TASK_TYPE_FINALIZE;
state->shared->n_active = n_threads;
state->shared->node_task = task_phase;
}
#pragma omp barrier
task_phase = state->shared->node_task;
#else
if (atomic_fetch_sub(&state->shared->n_active, 1) == 1) {
task_phase = GGML_TASK_TYPE_FINALIZE;
atomic_store(&state->shared->n_active, n_threads);
Expand All @@ -19151,6 +19181,7 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
else {
ggml_graph_compute_thread_sync_task(&task_phase, state, false);
}
#endif
}

return 0;
Expand Down