Skip to content

[OpenMP][GPU][FIX] Enable generic barriers in single threaded contexts #140786

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
May 21, 2025
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
8 changes: 5 additions & 3 deletions offload/DeviceRTL/src/Synchronization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,14 @@ int32_t __kmpc_cancel_barrier(IdentTy *Loc, int32_t TId) {
}

void __kmpc_barrier(IdentTy *Loc, int32_t TId) {
if (mapping::isMainThreadInGenericMode())
return __kmpc_flush(Loc);

if (mapping::isSPMDMode())
return __kmpc_barrier_simple_spmd(Loc, TId);

// Generic parallel regions are run with multiple of the warp size or single
// threaded, in the latter case we need to stop here.
if (omp_get_num_threads() == 1)
return __kmpc_flush(Loc);

impl::namedBarrier();
}

Expand Down
21 changes: 21 additions & 0 deletions offload/test/offloading/single_threaded_for_barrier_hang_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %libomptarget-compile-run-and-check-generic
// RUN: %libomptarget-compileopt-run-and-check-generic

#include <omp.h>
#include <stdio.h>

int main() {
int b = 0;

#pragma omp target map(tofrom : b)
for (int i = 1; i <= 10; ++i) {
#pragma omp parallel num_threads(10) reduction(+ : b)
#pragma omp for
for (int k = 0; k < 10; ++k)
++b;
}

// CHECK: b: 100
printf("b: %i\n", b);
return 0;
}
23 changes: 23 additions & 0 deletions offload/test/offloading/single_threaded_for_barrier_hang_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %libomptarget-compile-run-and-check-generic
// FIXME: This fails with optimization enabled and prints b: 0
// FIXME: RUN: %libomptarget-compileopt-run-and-check-generic

#include <omp.h>
#include <stdio.h>

int main() {
int b = 0;

#pragma omp target map(tofrom : b) thread_limit(256)
for (int i = 1; i <= 1; ++i) {
#pragma omp parallel num_threads(64) reduction(+ : b)
#pragma omp parallel num_threads(10) reduction(+ : b)
#pragma omp for
for (int k = 0; k < 10; ++k)
++b;
}

// CHECK: b: 640
printf("b: %i\n", b);
return 0;
}
Loading