Skip to content

Commit f94b6f3

Browse files
[OpenMP] Remove optimization skipping reduction struct initialization (#65697)
This commit removes an optimization that skips the initialization of the reduction struct if the number of threads in a team is 1. This optimization caused a bug with Hidden Helper Threads. When the task group is initially initialized by the master thread but a Hidden Helper Thread executes a target nowait region, it requires the reduction struct initialization to properly accumulate the data. This commit also adds a LIT test for issue #57522 to ensure that the issue is properly addressed and that the optimization removal does not introduce any regressions. Fixes: #57522
1 parent 4c4bdf0 commit f94b6f3

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %libomptarget-compile-generic && \
2+
// RUN: %libomptarget-run-generic
3+
4+
#include <omp.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
int main(int argc, char *argv[]) {
9+
10+
int num_devices = omp_get_num_devices();
11+
12+
// No target devices, just return
13+
if (num_devices == 0) {
14+
printf("PASS\n");
15+
return 0;
16+
}
17+
18+
double sum = 999;
19+
double A = 311;
20+
21+
#pragma omp taskgroup task_reduction(+ : sum)
22+
{
23+
#pragma omp target map(to : A) in_reduction(+ : sum) device(0) nowait
24+
{ sum += A; }
25+
}
26+
27+
printf("PASS\n");
28+
return EXIT_SUCCESS;
29+
}
30+
31+
// CHECK: PASS

openmp/runtime/src/kmp_tasking.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,7 @@ void *__kmp_task_reduction_init(int gtid, int num, T *data) {
25122512
KMP_ASSERT(tg != NULL);
25132513
KMP_ASSERT(data != NULL);
25142514
KMP_ASSERT(num > 0);
2515-
if (nth == 1) {
2515+
if (nth == 1 && !__kmp_enable_hidden_helper) {
25162516
KA_TRACE(10, ("__kmpc_task_reduction_init: T#%d, tg %p, exiting nth=1\n",
25172517
gtid, tg));
25182518
return (void *)tg;
@@ -2699,6 +2699,7 @@ void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
26992699
return p_priv[tid];
27002700
}
27012701
}
2702+
KMP_ASSERT(tg->parent);
27022703
tg = tg->parent;
27032704
arr = (kmp_taskred_data_t *)(tg->reduce_data);
27042705
num = tg->reduce_num_data;
@@ -2711,7 +2712,10 @@ void *__kmpc_task_reduction_get_th_data(int gtid, void *tskgrp, void *data) {
27112712
// Called from __kmpc_end_taskgroup()
27122713
static void __kmp_task_reduction_fini(kmp_info_t *th, kmp_taskgroup_t *tg) {
27132714
kmp_int32 nth = th->th.th_team_nproc;
2714-
KMP_DEBUG_ASSERT(nth > 1); // should not be called if nth == 1
2715+
KMP_DEBUG_ASSERT(
2716+
nth > 1 ||
2717+
__kmp_enable_hidden_helper); // should not be called if nth == 1 unless we
2718+
// are using hidden helper threads
27152719
kmp_taskred_data_t *arr = (kmp_taskred_data_t *)tg->reduce_data;
27162720
kmp_int32 num = tg->reduce_num_data;
27172721
for (int i = 0; i < num; ++i) {

0 commit comments

Comments
 (0)