gcc/libgomp/testsuite/libgomp.c-c++-common
Tom de Vries 5ed77fb3ed [libgomp, nvptx] Fix hang in gomp_team_barrier_wait_end
Consider the following omp fragment.
...
  #pragma omp target
  #pragma omp parallel num_threads (2)
  #pragma omp task
    ;
...

This hangs at -O0 for nvptx.

Investigating the behaviour gives us the following trace of events:
- both threads execute GOMP_task, where they:
  - deposit a task, and
  - execute gomp_team_barrier_wake
- thread 1 executes gomp_team_barrier_wait_end and, not being the last thread,
  proceeds to wait at the team barrier
- thread 0 executes gomp_team_barrier_wait_end and, being the last thread, it
  calls gomp_barrier_handle_tasks, where it:
  - executes both tasks and marks the team barrier done
  - executes a gomp_team_barrier_wake which wakes up thread 1
- thread 1 exits the team barrier
- thread 0 returns from gomp_barrier_handle_tasks and goes to wait at
  the team barrier.
- thread 0 hangs.

To understand why there is a hang here, it's good to understand how things
are setup for nvptx.  The libgomp/config/nvptx/bar.c implementation is
a copy of the libgomp/config/linux/bar.c implementation, with uses of both
futex_wake and do_wait replaced with uses of ptx insn bar.sync:
...
  if (bar->total > 1)
    asm ("bar.sync 1, %0;" : : "r" (32 * bar->total));
...

The point where thread 0 goes to wait at the team barrier, corresponds in
the linux implementation with a do_wait.  In the linux case, the call to
do_wait doesn't hang, because it's waiting for bar->generation to become
a certain value, and if bar->generation already has that value, it just
proceeds, without any need for coordination with other threads.

In the nvtpx case, the bar.sync waits until thread 1 joins it in the same
logical barrier, which never happens: thread 1 is lingering in the
thread pool at the thread pool barrier (using a different logical barrier),
waiting to join a new team.

The easiest way to fix this is to revert to the posix implementation for
bar.{c,h}.  That however falls back on a busy-waiting approach, and
does not take advantage of the ptx bar.sync insn.

Instead, we revert to the linux implementation for bar.c,
and implement bar.c local functions futex_wait and futex_wake using the
bar.sync insn.

The bar.sync insn takes an argument specifying how many threads are
participating, and that doesn't play well with the futex syntax where it's
not clear in advance how many threads will be woken up.

This is solved by waking up all waiting threads each time a futex_wait or
futex_wake happens, and possibly going back to sleep with an updated thread
count.

Tested libgomp on x86_64 with nvptx accelerator.

libgomp/ChangeLog:

2021-04-20  Tom de Vries  <tdevries@suse.de>

	PR target/99555
	* config/nvptx/bar.c (generation_to_barrier): New function, copied
	from config/rtems/bar.c.
	(futex_wait, futex_wake): New function.
	(do_spin, do_wait): New function, copied from config/linux/wait.h.
	(gomp_barrier_wait_end, gomp_barrier_wait_last)
	(gomp_team_barrier_wake, gomp_team_barrier_wait_end):
	(gomp_team_barrier_wait_cancel_end, gomp_team_barrier_cancel): Remove
	and replace with include of config/linux/bar.c.
	* config/nvptx/bar.h (gomp_barrier_t): Add fields waiters and lock.
	(gomp_barrier_init): Init new fields.
	* testsuite/libgomp.c-c++-common/task-detach-6.c: Remove nvptx-specific
	workarounds.
	* testsuite/libgomp.c/pr99555-1.c: Same.
	* testsuite/libgomp.fortran/task-detach-6.f90: Same.
2022-02-22 15:48:03 +01:00
..
alloc-1.c
alloc-2.c
alloc-3.c
alloc-4.c
alloc-5.c
alloc-6.c
alloc-7.c
alloc-8.c
alloc-9.c
alloc-10.c
allocate-1.c
allocate-2.c
allocate-3.c
atomic-18.c
atomic-19.c
atomic-20.c
atomic-21.c
cancel-parallel-1.c
cancel-taskgroup-1.c
cancel-taskgroup-2.c
cancel-taskgroup-3.c
cancel-taskgroup-4.c
critical-hint-1.c
critical-hint-2.c
declare_target-1.c
default-1.c
depend-iterator-1.c
depend-iterator-2.c
depend-mutexinout-1.c
depend-mutexinout-2.c
depobj-1.c
display-affinity-1.c
error-1.c
for-1.c
for-1.h
for-2.c
for-2.h
for-3.c
for-4.c
for-5.c
for-6.c
for-7.c
for-8.c
for-9.c
for-10.c
for-11.c
for-12.c
for-13.c
for-14.c
for-15.c
for-16.c
function-not-offloaded-aux.c
function-not-offloaded.c
icv-3.c
icv-4.c
lastprivate-conditional-1.c
lastprivate-conditional-2.c
lastprivate-conditional-3.c
lastprivate-conditional-4.c
lastprivate-conditional-5.c
lastprivate-conditional-6.c
lastprivate-conditional-7.c
lastprivate-conditional-8.c
lastprivate-conditional-9.c
lastprivate-conditional-10.c
loop-1.c
loop-13.c
loop-14.c
loop-15.c
masked-1.c
master-combined-1.c
monotonic-1.c
monotonic-2.c
nested-parallel-unbalanced.c
nonmonotonic-1.c
nonmonotonic-2.c
nothing-1.c
on_device_arch.h Improve Intel MIC offloading XFAILing for 'omp_get_device_num' 2022-01-13 13:09:36 +01:00
order-reproducible-1.c
order-reproducible-2.c
ordered-4.c
pause-1.c
pause-2.c
pr45784.c
pr64824.c
pr64868.c
pr66199-1.c
pr66199-2.c
pr66199-3.c
pr66199-4.c
pr66199-5.c
pr66199-6.c
pr66199-7.c
pr66199-8.c
pr66199-9.c
pr66199-10.c
pr66199-11.c
pr66199-12.c
pr66199-13.c
pr66199-14.c
pr69389.c
pr81875.c
pr83046.c
pr93515.c
pr94366.c
pr96390.c [libgomp, testsuite, nvptx] Fix pr96390.c without CUDA 2022-02-22 10:23:20 +01:00
ptr-attach-1.c
reduction-1.c
reduction-2.c
reduction-3.c
reduction-4.c
reduction-5.c
reduction-6.c
reduction-16.c
reduction-17.c
refcount-1.c
scope-1.c
simd-1.c
simd-14.c
simd-15.c
simd-16.c
simd-17.c
struct-elem-1.c
struct-elem-2.c
struct-elem-3.c
struct-elem-4.c
struct-elem-5.c
target-1.c
target-2.c
target-10.c
target-13.c
target-40.c
target-41.c
target-42.c
target-45.c Improve Intel MIC offloading XFAILing for 'omp_get_device_num' 2022-01-13 13:09:36 +01:00
target-has-device-addr-1.c C, C++, Fortran, OpenMP: Add 'has_device_addr' clause to 'target' construct. 2022-02-09 23:47:12 -08:00
target-implicit-map-1.c openmp: Relax handling of implicit map vs. existing device mappings 2021-11-12 20:29:48 +08:00
target-implicit-map-2.c OpenMP 5.0: Remove array section base-pointer mapping semantics and other front-end adjustments 2021-12-09 00:01:10 +08:00
target-in-reduction-1.c
target-in-reduction-2.c
task-detach-1.c
task-detach-2.c
task-detach-3.c
task-detach-4.c
task-detach-5.c
task-detach-6.c [libgomp, nvptx] Fix hang in gomp_team_barrier_wait_end 2022-02-22 15:48:03 +01:00
task-detach-7.c
task-detach-8.c
task-detach-9.c
task-detach-10.c
task-detach-11.c
task-detach-12.c
task-detach-13.c
task-reduction-1.c
task-reduction-2.c
task-reduction-3.c
task-reduction-4.c
task-reduction-5.c
task-reduction-6.c
task-reduction-7.c
task-reduction-8.c
task-reduction-9.c
task-reduction-11.c
task-reduction-12.c
task-reduction-13.c
task-reduction-14.c
task-reduction-15.c
task-reduction-16.c
taskgroup-1.c
taskloop-1.c
taskloop-2.c
taskloop-3.c
taskloop-4.c
taskloop-5.c
taskloop-reduction-1.c
taskloop-reduction-2.c
taskloop-reduction-3.c
taskloop-reduction-4.c
taskwait-depend-1.c
teams-1.c openmp: Add support for 2 argument num_teams clause 2021-11-11 09:42:47 +01:00
teams-2.c openmp: Honor OpenMP 5.1 num_teams lower bound 2021-11-12 12:41:22 +01:00
thread-limit-1.c openmp: Add support for thread_limit clause on target 2021-11-15 13:20:53 +01:00
udr-1.c
unmap-infinity-2.c
variable-not-offloaded.c