Skip to content

[OpenMP] Fix nested parallel with tasking #87309

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

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions openmp/runtime/src/kmp_tasking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3219,7 +3219,7 @@ static kmp_task_t *__kmp_remove_my_task(kmp_info_t *thread, kmp_int32 gtid,
// __kmp_steal_task: remove a task from another thread's deque
// Assume that calling thread has already checked existence of
// task_team thread_data before calling this routine.
static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
static kmp_task_t *__kmp_steal_task(kmp_int32 victim_tid, kmp_int32 gtid,
kmp_task_team_t *task_team,
std::atomic<kmp_int32> *unfinished_threads,
int *thread_finished,
Expand All @@ -3229,15 +3229,18 @@ static kmp_task_t *__kmp_steal_task(kmp_info_t *victim_thr, kmp_int32 gtid,
kmp_taskdata_t *current;
kmp_thread_data_t *victim_td, *threads_data;
kmp_int32 target;
kmp_int32 victim_tid;
kmp_info_t *victim_thr;

KMP_DEBUG_ASSERT(__kmp_tasking_mode != tskm_immediate_exec);

threads_data = task_team->tt.tt_threads_data;
KMP_DEBUG_ASSERT(threads_data != NULL); // Caller should check this condition
KMP_DEBUG_ASSERT(victim_tid >= 0);
KMP_DEBUG_ASSERT(victim_tid < task_team->tt.tt_nproc);

victim_tid = victim_thr->th.th_info.ds.ds_tid;
victim_td = &threads_data[victim_tid];
victim_thr = victim_td->td.td_thr;
(void)victim_thr; // Use in TRACE messages which aren't always enabled.

KA_TRACE(10, ("__kmp_steal_task(enter): T#%d try to steal from T#%d: "
"task_team=%p ntasks=%d head=%u tail=%u\n",
Expand Down Expand Up @@ -3452,9 +3455,9 @@ static inline int __kmp_execute_tasks_template(

if (!asleep) {
// We have a victim to try to steal from
task = __kmp_steal_task(other_thread, gtid, task_team,
unfinished_threads, thread_finished,
is_constrained);
task =
__kmp_steal_task(victim_tid, gtid, task_team, unfinished_threads,
thread_finished, is_constrained);
}
if (task != NULL) { // set last stolen to victim
if (threads_data[tid].td.td_deque_last_stolen != victim_tid) {
Expand Down
43 changes: 43 additions & 0 deletions openmp/runtime/test/tasking/issue-87307.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %libomp-compile-and-run
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int a;

void inc_a() {
#pragma omp task
{
#pragma omp atomic
a++;
}
}

int main() {
int n;
int nth_outer;
omp_set_max_active_levels(2);
omp_set_dynamic(0);

for (n = 0; n < 200; ++n) {
a = 0;
#pragma omp parallel num_threads(8)
{
if (omp_get_thread_num() == 0)
nth_outer = omp_get_num_threads();
#pragma omp parallel num_threads(2)
{
int i;
#pragma omp master
for (i = 0; i < 50; ++i)
inc_a();
}
}
if (a != nth_outer * 50) {
fprintf(stderr, "error: a (%d) != %d\n", a, nth_outer * 50);
return EXIT_FAILURE;
}
}

return EXIT_SUCCESS;
}