From: Andrew Stubbs <[email protected]>
In full SIMT mode, each lane of each wavefront thread is considered it's own
OpenMP thread. For a lot of libgomp this does not make any difference -- the
internal "GOMP" thread numbers remain 0..15 -- but all the user-visible APIs
need to use modified "OMP" thread numbers, including ICVs, in the range
0..1023.
Barriers, atomics, and spinlocks must be limited to executing in one thread per
wave, or else they hang, or trigger multiple times.
Worksharing constructs need to divide the work differently, such that the
results don't duplicate effort and conform to OpenMP semantics. I've made a
start on these in this patch -- there's enough in this PoC implementation to
support my development testcases -- but there's more items on my to-do list for
future projects.
The patch includes an optimization feature that wraps gomp_aligned_alloc so
that all the threads (within a wave) are combined into a single-threaded call
to malloc. This avoids serializing 64 threads into 64 independent allocations.
The optimization is only suitable for usecases where the gomp_free call is
guaranteed be called by the exact same set of threads.
The target-independent code changes are guarded by a new "LIBGOMP_SIMT" macro
that is intended to be usable by all full-SIMT targets (just AMD GCN, for now,
but NVPTX could support this, in theory).
Assisted-by: Claude Opus 5.5
Claude generated the testcases and assisted with some of the code.
libgomp/ChangeLog:
* alloc.c (gomp_simt_max): New function.
(gomp_aligned_alloc_lanes, gomp_aligned_free_lanes): New functions.
* config/accel/mutex.h (gomp_mutex_lock, gomp_mutex_unlock): Access
the mutex from the first lane only.
* config/gcn/bar.c (gomp_team_barrier_wait_end): Skip s_barrier if
the barrier has only one thread.
* config/gcn/bar.h (gomp_barrier_wait_start)
(gomp_barrier_wait_final_start): Count the wavefront in the first
lane only, and give the result to every lane.
* config/gcn/team.c: Update the file comment.
(__gcn_simt_mode): New variable.
(gomp_gcn_enter_kernel): In SIMT builds, take a simt_mode parameter
and store it in __gcn_simt_mode. Count the nthreads and thread_limit
ICVs in OpenMP threads. Rename threadid and numthreads to
wavefrontid and numwavefronts.
(gomp_simt_lane_in_team_p): New function.
(gomp_master_progress): New function.
(gomp_thread_start): Rename to ...
(gomp_wavefront_start): ... this. Reset the idle limit whenever the
master wavefront makes progress. In "simt" mode, call the team
function in SIMT mode, in the lanes that belong to the team.
(gomp_simt_launch_master): New function.
(gomp_team_start): Return early in all but the first lane. In SIMT
builds, count the team's wavefronts.
* iter.c (gomp_iter_simt_lanes_alloc): New function.
(gomp_iter_static_next): Use the per-lane ranges of a nested SIMT
team. Use OpenMP thread numbers and counts. Make the lanes of a
wavefront agree on the static trip.
(gomp_iter_dynamic_next_locked, gomp_iter_dynamic_next)
(gomp_iter_guided_next_locked, gomp_iter_guided_next): Use the
per-lane ranges of a nested SIMT team.
* iter_ull.c (gomp_iter_ull_static_next): Use the per-lane ranges of
a nested SIMT team. Use OpenMP thread numbers and counts. Make the
lanes of a wavefront agree on the static trip.
(gomp_iter_ull_dynamic_next_locked, gomp_iter_ull_dynamic_next)
(gomp_iter_ull_guided_next_locked, gomp_iter_ull_guided_next): Use
the per-lane ranges of a nested SIMT team.
* libgomp.h: Include limits.h.
(LIBGOMP_SIMT, SIMT_MODE_P, FIRST_LANE_P, SIMT_VOTE_ANY)
(SIMT_BROADCAST_FIRST_LANE, SIMT_LANE_ID, SIMT_LANES): New macros.
(gomp_aligned_alloc_lanes, gomp_aligned_free_lanes): Declare, or
define as gomp_aligned_alloc and gomp_aligned_free.
(struct gomp_work_share): Add simt_lane_ranges.
(struct gomp_team): Add omp_nthreads.
(GOMP_SIMT_LANES_P, GOMP_SIMT_LANE_TEAMS_P, GOMP_TID_TO_OMP_TID_AT)
(GOMP_TID_TO_OMP_TID, OMP_TID_TO_GOMP_TID, GOMP_TEAM_OMP_NTHR)
(OMP_NTHR_TO_GOMP_NTHR, LAUNCH_MASTER_THREAD): New macros.
(gomp_simt_launch_master, gomp_iter_simt_lanes_alloc): Declare.
(gomp_iter_simt_lanes_init, gomp_iter_ull_simt_lanes_next)
(gomp_iter_simt_lanes_next, gomp_iter_simt_lanes_next_one): New
functions.
(GOMP_ITER_SIMT_LANES_P): New macro.
* loop.c (gomp_loop_init): Record each lane's bounds in a nested SIMT
team. Use the OpenMP thread count.
(gomp_loop_ordered_dynamic_next): Make the lanes agree on the result.
(gomp_parallel_loop_start): Initialize the work share in the first
lane only, then record each lane's bounds in a nested SIMT team.
(GOMP_parallel_loop_static, GOMP_parallel_loop_dynamic)
(GOMP_parallel_loop_guided, GOMP_parallel_loop_runtime)
(GOMP_parallel_loop_nonmonotonic_dynamic)
(GOMP_parallel_loop_nonmonotonic_guided)
(GOMP_parallel_loop_nonmonotonic_runtime)
(GOMP_parallel_loop_maybe_nonmonotonic_runtime): Use
LAUNCH_MASTER_THREAD.
(GOMP_loop_static_worksharing, GOMP_loop_static_worksharing_start):
Use OpenMP thread numbers and counts.
* loop_ull.c (gomp_loop_ull_init): Record each lane's bounds in a
nested SIMT team. Use the OpenMP thread count.
(gomp_loop_ull_ordered_dynamic_next): Make the lanes agree on the
result.
* ordered.c (gomp_ordered_first, gomp_ordered_last)
(gomp_ordered_next, gomp_ordered_static_init)
(gomp_ordered_static_next, gomp_ordered_sync): Return early in all
but the first lane.
(gomp_doacross_init, gomp_doacross_ull_init): Likewise. Use one
static schedule entry per OpenMP thread.
(GOMP_doacross_post, GOMP_doacross_wait, GOMP_doacross_ull_post)
(GOMP_doacross_ull_wait): Use OpenMP thread numbers and counts.
* parallel.c (gomp_resolve_num_threads): Return 1 for a nested team
in SIMT mode. Read thread_limit_var once.
(GOMP_parallel_end): Use the OpenMP thread count.
(GOMP_parallel): Use LAUNCH_MASTER_THREAD.
(GOMP_parallel_reductions): Likewise. Free the taskgroup with
gomp_aligned_free_lanes.
(GOMP_has_masked_thread_num, GOMP_has_masked_thread_num_with_end)
(omp_get_num_threads, omp_get_thread_num, omp_get_team_size): Use
OpenMP thread numbers and counts.
(omp_get_ancestor_thread_num): Likewise, except for the initial
thread.
* sections.c (gomp_sections_init): Record each lane's range in a
nested SIMT team.
(GOMP_sections_start, GOMP_sections2_start, GOMP_sections_next): Use
the per-lane ranges of a nested SIMT team.
(GOMP_parallel_sections_start): Initialize the work share in the first
lane only, then record each lane's range in a nested SIMT team.
(GOMP_parallel_sections): Likewise. Use LAUNCH_MASTER_THREAD.
* single.c (GOMP_single_start, GOMP_single_start_with_end): Let only
the first lane compete for the construct, except in a nested SIMT
team, where every lane executes it.
* task.c (gomp_taskgroup_init): Split out ...
(gomp_taskgroup_init_at): ... this new function.
(GOMP_REDUCTION_LANES_ALLOCATOR): New macro.
(gomp_reduction_register): Add a LANES parameter, to allocate the
private copies of all the lanes together.
(gomp_create_artificial_team): Return early in all but the first lane.
(GOMP_taskgroup_reduction_register): Use the OpenMP thread count.
Update the gomp_reduction_register call.
(GOMP_taskgroup_reduction_unregister): Free lane allocations with
gomp_aligned_free_lanes.
(GOMP_task_reduction_remap): Use the OpenMP thread number.
(gomp_parallel_reduction_register): Allocate the taskgroup with
gomp_aligned_alloc_lanes, and pass SIMT_MODE_P to
gomp_reduction_register.
(gomp_workshare_task_reduction_register): Use the OpenMP thread count.
Update the gomp_reduction_register call.
* team.c (gomp_new_team): Return NULL in all but the first lane. In
SIMT builds, allocate per wavefront, and record the OpenMP thread
count in omp_nthreads.
(gomp_team_end): Return early in all but the first lane.
* work.c (gomp_init_work_share): Initialize simt_lane_ranges.
(gomp_fini_work_share): Free simt_lane_ranges.
(gomp_work_share_start): Rename to ...
(gomp_work_share_start_1): ... this, and make it static.
(gomp_work_share_start): New function. Call gomp_work_share_start_1
in the first lane only, and give the result to every lane.
(gomp_work_share_end, gomp_work_share_end_nowait): Return early in
all but the first lane.
(gomp_work_share_end_cancel): Free the last work share from the first
lane only.
* testsuite/libgomp.c-c++-common/simt-ancestor-thread-num-amdgcn-1.c:
New test.
* testsuite/libgomp.c-c++-common/simt-idle-waves-amdgcn-1.c: New test.
* testsuite/libgomp.c-c++-common/simt-loop-ull-static-amdgcn-1.c: New
test.
* testsuite/libgomp.c-c++-common/simt-nested-active-level-amdgcn-1.c:
New test.
* testsuite/libgomp.c-c++-common/simt-nested-parallel-amdgcn-1.c: New
test.
* testsuite/libgomp.c-c++-common/simt-nested-parallel-for-amdgcn-1.c:
New test.
* testsuite/libgomp.c-c++-common/simt-nested-sections-amdgcn-1.c: New
test.
* testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-1.c:
New test.
* testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-2.c:
New test.
* testsuite/libgomp.c-c++-common/simt-task-reduction-remap-amdgcn-1.c:
New test.
* testsuite/libgomp.c-c++-common/simt-team-size-amdgcn-1.c: New test.
---
libgomp/alloc.c | 53 ++++++
libgomp/config/accel/mutex.h | 8 +-
libgomp/config/gcn/bar.c | 3 +-
libgomp/config/gcn/bar.h | 13 +-
libgomp/config/gcn/team.c | 155 +++++++++++++---
libgomp/iter.c | 41 ++++-
libgomp/iter_ull.c | 27 ++-
libgomp/libgomp.h | 167 ++++++++++++++++++
libgomp/loop.c | 47 +++--
libgomp/loop_ull.c | 12 +-
libgomp/ordered.c | 50 +++++-
libgomp/parallel.c | 47 +++--
libgomp/sections.c | 34 +++-
libgomp/single.c | 16 ++
libgomp/task.c | 65 +++++--
libgomp/team.c | 19 ++
.../simt-ancestor-thread-num-amdgcn-1.c | 62 +++++++
.../simt-idle-waves-amdgcn-1.c | 44 +++++
.../simt-loop-ull-static-amdgcn-1.c | 77 ++++++++
.../simt-nested-active-level-amdgcn-1.c | 66 +++++++
.../simt-nested-parallel-amdgcn-1.c | 86 +++++++++
.../simt-nested-parallel-for-amdgcn-1.c | 132 ++++++++++++++
.../simt-nested-sections-amdgcn-1.c | 124 +++++++++++++
.../simt-parallel-reductions-amdgcn-1.c | 88 +++++++++
.../simt-parallel-reductions-amdgcn-2.c | 58 ++++++
.../simt-task-reduction-remap-amdgcn-1.c | 48 +++++
.../simt-team-size-amdgcn-1.c | 134 ++++++++++++++
libgomp/work.c | 36 +++-
28 files changed, 1613 insertions(+), 99 deletions(-)
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-ancestor-thread-num-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-idle-waves-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-loop-ull-static-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-nested-active-level-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-for-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-nested-sections-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-2.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-task-reduction-remap-amdgcn-1.c
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-team-size-amdgcn-1.c
diff --git a/libgomp/alloc.c b/libgomp/alloc.c
index 34ece6248465c..55eca79818583 100644
--- a/libgomp/alloc.c
+++ b/libgomp/alloc.c
@@ -123,3 +123,56 @@ gomp_aligned_free (void *ptr)
free (((void **) ptr)[-1]);
#endif
}
+
+#ifdef LIBGOMP_SIMT
+/* Return the largest X of the active lanes, in every lane. */
+
+static size_t
+gomp_simt_max (size_t x)
+{
+ /* Usually every lane has the same X. */
+ size_t max = SIMT_BROADCAST_FIRST_LANE (x);
+ if (!SIMT_VOTE_ANY (x != max))
+ return max;
+
+ max = 0;
+ for (int bit = sizeof (size_t) * 8 - 1; bit >= 0; bit--)
+ {
+ size_t c = max | ((size_t) 1 << bit);
+ if (SIMT_VOTE_ANY (x >= c))
+ max = c;
+ }
+ return max;
+}
+
+/* Allocate SIZE bytes, aligned to AL, for each active lane of the wave, with
+ one allocation for all of them. The first active lane allocates a slot for
+ itself and each higher lane, large enough for any of the lanes, and its own
+ slot is the start of the block. Every active lane must call this at once,
+ and the same lanes must later free the memory at once, with
+ gomp_aligned_free_lanes, which frees the block from the first lane. */
+
+void *
+gomp_aligned_alloc_lanes (size_t al, size_t size)
+{
+ /* There is nothing to share with a single lane. */
+ if (!SIMT_VOTE_ANY (!FIRST_LANE_P))
+ return gomp_aligned_alloc (al, size);
+
+ al = gomp_simt_max (al);
+ size_t slot = (gomp_simt_max (size) + al - 1) & -al;
+ int first = SIMT_BROADCAST_FIRST_LANE (SIMT_LANE_ID);
+ uintptr_t block = 0;
+ if (FIRST_LANE_P)
+ block = (uintptr_t) gomp_aligned_alloc (al, slot * (SIMT_LANES - first));
+ block = SIMT_BROADCAST_FIRST_LANE (block);
+ return (void *) (block + (SIMT_LANE_ID - first) * slot);
+}
+
+void
+gomp_aligned_free_lanes (void *ptr)
+{
+ if (FIRST_LANE_P)
+ gomp_aligned_free (ptr);
+}
+#endif
diff --git a/libgomp/config/accel/mutex.h b/libgomp/config/accel/mutex.h
index 8c6a6985814ea..ce0f0b717de90 100644
--- a/libgomp/config/accel/mutex.h
+++ b/libgomp/config/accel/mutex.h
@@ -48,13 +48,15 @@ gomp_mutex_destroy (gomp_mutex_t *mutex)
static inline void
gomp_mutex_lock (gomp_mutex_t *mutex)
{
- while (__sync_lock_test_and_set (mutex, 1))
- /* spin */ ;
+ if (FIRST_LANE_P)
+ while (__sync_lock_test_and_set (mutex, 1))
+ /* spin */ ;
}
static inline void
gomp_mutex_unlock (gomp_mutex_t *mutex)
{
- __sync_lock_release (mutex);
+ if (FIRST_LANE_P)
+ __sync_lock_release (mutex);
}
#endif /* GOMP_MUTEX_H */
diff --git a/libgomp/config/gcn/bar.c b/libgomp/config/gcn/bar.c
index d3e4464347b12..ea96671863707 100644
--- a/libgomp/config/gcn/bar.c
+++ b/libgomp/config/gcn/bar.c
@@ -123,7 +123,8 @@ gomp_team_barrier_wait_end (gomp_barrier_t *bar,
gomp_barrier_state_t state,
abort();
}
- asm ("s_barrier" ::: "memory");
+ if (bar->total > 1)
+ asm ("s_barrier" ::: "memory");
gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
{
diff --git a/libgomp/config/gcn/bar.h b/libgomp/config/gcn/bar.h
index 471ad88ca7151..9b3afd8334366 100644
--- a/libgomp/config/gcn/bar.h
+++ b/libgomp/config/gcn/bar.h
@@ -107,7 +107,12 @@ gomp_barrier_wait_start (gomp_barrier_t *bar,
2.8.6 flush Construct, which says there is an implicit flush during
a barrier region. This is a convenient place to add the barrier,
so we use MEMMODEL_ACQ_REL here rather than MEMMODEL_ACQUIRE. */
- if (__atomic_add_fetch (&bar->awaited, -1, MEMMODEL_RELAXED) == 0)
+ /* In SIMT mode a gomp_thread is a whole wavefront, so only one lane
+ may count it, but every lane must see the result. */
+ int last = 0;
+ if (FIRST_LANE_P)
+ last = __atomic_add_fetch (&bar->awaited, -1, MEMMODEL_RELAXED) == 0;
+ if (SIMT_VOTE_ANY (last))
ret |= BAR_WAS_LAST;
return ret;
}
@@ -128,7 +133,11 @@ gomp_barrier_wait_final_start (gomp_barrier_t *bar,
unsigned int ret = __atomic_load_n (&bar->generation, MEMMODEL_RELAXED);
ret &= -BAR_INCR | BAR_CANCELLED;
/* See above gomp_barrier_wait_start comment. */
- if (__atomic_add_fetch (&bar->awaited_final, -1, MEMMODEL_RELAXED) == 0)
+ int last = 0;
+ if (FIRST_LANE_P)
+ last = (__atomic_add_fetch (&bar->awaited_final, -1, MEMMODEL_RELAXED)
+ == 0);
+ if (SIMT_VOTE_ANY (last))
ret |= BAR_WAS_LAST;
return ret;
}
diff --git a/libgomp/config/gcn/team.c b/libgomp/config/gcn/team.c
index f9bfa93fe6317..9a9345d2b3a34 100644
--- a/libgomp/config/gcn/team.c
+++ b/libgomp/config/gcn/team.c
@@ -23,7 +23,18 @@
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
-/* This file handles maintenance of threads on AMD GCN. */
+/* This file handles maintenance of wavefronts and threads on AMD GCN.
+
+ In SIMD mode, and in SIMT-DIRECTED mode, and OpenMP "thread" corresponds
+ 1-to-1 with a GPU "wavefront".
+
+ In SIMT mode (no "directed"), an OpenMP "thread" corresponds to a GPU
+ "work-item" (vector lane), conceptually. However, the internal
+ implementation (i.e. here) still maps gomp_thread_pool to wavefronts,
+ since this is the smallest unit that can be assigned to a task.
+
+ The thread numbers are translated from wavefront/work-item to a flat
+ thread number only when presented to the user code. */
#include <assert.h>
#include "libgomp.h"
@@ -39,25 +50,47 @@
/* Defined in basic-allocator.c via config/amdgcn/allocator.c. */
void __gcn_lowlat_init (void *heap, size_t size);
-static void gomp_thread_start (struct gomp_thread_pool *);
+static void gomp_wavefront_start (struct gomp_thread_pool *);
/* This externally visible function handles target region entry. It
sets up a per-team thread pool and transfers control by returning to
- the kernel in the master thread or gomp_thread_start in other threads.
+ the kernel in the master thread (wavefront) or gomp_wavefront_start in
+ other threads (wavefronts).
The name of this function is part of the interface with the compiler: for
each OpenMP kernel the compiler configures the stack, then calls here.
Likewise, gomp_gcn_exit_kernel is called during the kernel epilogue. */
+#ifdef __SIMT__
+/* TODO: implement SIMT-DIRECTED vs. SIMT mode switching per kernel.
+ The global-variable implementation is simpler but requires all kernels
+ make the same choice. There's not actually a way to compile per kernel
+ anyway, yet.
+
+ 0 - SIMT-DIRECTED (vector-level)
+ 1 - SIMT (thread-level) */
+int __gcn_simt_mode = 0;
+#endif
+
void
-gomp_gcn_enter_kernel (void)
+gomp_gcn_enter_kernel (
+#ifdef __SIMT__
+ int simt_mode
+#else
+ void
+#endif
+ )
{
- int threadid = __builtin_gcn_dim_pos (1);
+#ifdef __SIMT__
+ __gcn_simt_mode = simt_mode;
+#endif
+
+ int wavefrontid = __builtin_gcn_dim_pos (1);
- if (threadid == 0)
+ if (wavefrontid == 0)
{
- int numthreads = __builtin_gcn_dim_size (1);
+ int numwavefronts = __builtin_gcn_dim_size (1);
int teamid = __builtin_gcn_dim_pos(0);
/* Initialize indirect function support for older libgomp. */
@@ -66,8 +99,14 @@ gomp_gcn_enter_kernel (void)
/* Set up the global state.
Every team will do this, but that should be harmless. */
+#ifdef __SIMT__
+ gomp_global_icv.nthreads_var = (simt_mode ? 1024 : 16);
+ gomp_global_icv.thread_limit_var = (numwavefronts
+ * (simt_mode ? 64 : 1));
+#else
gomp_global_icv.nthreads_var = 16;
- gomp_global_icv.thread_limit_var = numthreads;
+ gomp_global_icv.thread_limit_var = numwavefronts;
+#endif
/* Starting additional threads is not supported. */
gomp_global_icv.dyn_var = true;
@@ -96,22 +135,22 @@ gomp_gcn_enter_kernel (void)
/* Allocate and initialize the team-local-storage data. */
struct gomp_thread *thrs = team_malloc_cleared (sizeof (*thrs)
- * numthreads);
+ * numwavefronts);
set_gcn_thrs (thrs);
- /* Allocate and initialize a pool of threads in the team.
- The threads are already running, of course, we just need to manage
+ /* Allocate and initialize a pool of threads (wavefronts) in the team.
+ The wavefronts are already running, of course, we just need to manage
the communication between them. */
struct gomp_thread_pool *pool = team_malloc (sizeof (*pool));
- pool->threads = team_malloc (sizeof (void *) * numthreads);
- for (int tid = 0; tid < numthreads; tid++)
+ pool->threads = team_malloc (sizeof (void *) * numwavefronts);
+ for (int tid = 0; tid < numwavefronts; tid++)
pool->threads[tid] = &thrs[tid];
- pool->threads_size = numthreads;
- pool->threads_used = numthreads;
+ pool->threads_size = numwavefronts;
+ pool->threads_used = numwavefronts;
pool->threads_busy = 1;
pool->last_team = NULL;
pool->prev_barrier = NULL;
- gomp_simple_barrier_init (&pool->threads_dock, numthreads);
+ gomp_simple_barrier_init (&pool->threads_dock, numwavefronts);
thrs->thread_pool = pool;
asm ("s_barrier" ::: "memory");
@@ -120,8 +159,8 @@ gomp_gcn_enter_kernel (void)
else
{
asm ("s_barrier" ::: "memory");
- gomp_thread_start (gcn_thrs ()[0].thread_pool);
- /* gomp_thread_start does not return. */
+ gomp_wavefront_start (gcn_thrs ()[0].thread_pool);
+ /* gomp_wavefront_start does not return. */
}
}
@@ -159,11 +198,36 @@ gomp_prep_our_thread (struct gomp_thread *thr,
thr->task->taskgroup = start_data->taskgroup;
}
-/* This function contains the idle loop in which a thread waits
+#ifdef __SIMT__
+/* True if this lane of wave TEAM_ID is one of the OpenMP threads of TEAM,
+ an outermost team in SIMT mode. The team's last wave may be partial. */
+
+static inline bool
+gomp_simt_lane_in_team_p (struct gomp_team *team, unsigned team_id)
+{
+ return team_id * SIMT_LANES + SIMT_LANE_ID < team->omp_nthreads;
+}
+#endif
+
+/* Return a value that changes whenever the master wavefront passes a barrier
+ of its current team, or starts or ends a team. */
+
+static inline uintptr_t
+gomp_master_progress (void)
+{
+ struct gomp_team *team = __atomic_load_n (&gcn_thrs ()[0].ts.team,
+ MEMMODEL_RELAXED);
+ if (!team)
+ return 0;
+ return ((uintptr_t) team
+ + __atomic_load_n (&team->barrier.generation, MEMMODEL_RELAXED));
+}
+
+/* This function contains the idle loop in which a wavefront (thread) waits
to be called up to become part of a team. */
static void
-gomp_thread_start (struct gomp_thread_pool *pool)
+gomp_wavefront_start (struct gomp_thread_pool *pool)
{
struct gomp_thread *thr = gomp_thread ();
@@ -174,11 +238,22 @@ gomp_thread_start (struct gomp_thread_pool *pool)
which contains "s_endpgm", or an infinite no-op loop is
suspected (this happens when the thread master crashes). */
int nul_limit = 99;
+ uintptr_t progress = 0;
do
{
gomp_simple_barrier_wait (&pool->threads_dock);
if (!thr->fn)
{
+ /* GCN barriers stop every wavefront, so the barriers of a team that
+ does not use every wavefront also release the idle ones. Count
+ only the wake-ups during which the master made no progress. */
+ uintptr_t now = gomp_master_progress ();
+ if (now != progress)
+ {
+ progress = now;
+ nul_limit = 99;
+ continue;
+ }
if (nul_limit-- > 0)
continue;
else
@@ -190,6 +265,11 @@ gomp_thread_start (struct gomp_thread_pool *pool)
}
}
+#if __SIMT__
+ /* Cleanup helpers have no team. */
+ struct gomp_team *team = thr->start_data ? thr->start_data->team : NULL;
+#endif
+
/* Perform rest of task initialization. Populated from
gomp_team_start. */
if (thr->start_data)
@@ -202,7 +282,17 @@ gomp_thread_start (struct gomp_thread_pool *pool)
thr->start_data = NULL;
}
- thr->fn (thr->data);
+#if __SIMT__
+ if (__gcn_simt_mode)
+ {
+ int _ = __builtin_gcn_simt_enter (1, 2);
+ if (!team || gomp_simt_lane_in_team_p (team, thr->ts.team_id))
+ thr->fn (thr->data);
+ __builtin_gcn_simt_exit (_);
+ }
+ else
+#endif
+ thr->fn (thr->data);
thr->fn = NULL;
struct gomp_task *task = thr->task;
@@ -212,6 +302,20 @@ gomp_thread_start (struct gomp_thread_pool *pool)
while (1);
}
+#ifdef __SIMT__
+/* See LAUNCH_MASTER_THREAD. */
+
+void __attribute__((noinline))
+gomp_simt_launch_master (void (*fn) (void *), void *data)
+{
+ struct gomp_team *team = gomp_thread ()->ts.team;
+ int _ = __builtin_gcn_simt_enter (1, 2);
+ if (gomp_simt_lane_in_team_p (team, 0))
+ fn (data);
+ __builtin_gcn_simt_exit (_);
+}
+#endif
+
/* Launch a team. */
void
@@ -224,6 +328,10 @@ gomp_team_start (void (*fn) (void *), void *data, unsigned
nthreads,
struct gomp_task_icv *icv;
struct gomp_thread_pool *pool;
+ /* A nested team is started from SIMT code, but THR is per wave. */
+ if (!FIRST_LANE_P)
+ return;
+
thr = gomp_thread ();
pool = thr->thread_pool;
prev_task = thr->task;
@@ -244,6 +352,11 @@ gomp_team_start (void (*fn) (void *), void *data, unsigned
nthreads,
.taskgroup = taskgroup
};
+#ifdef __SIMT__
+ /* NTHREADS counts OpenMP threads; from here on it counts waves. */
+ nthreads = team->nthreads;
+#endif
+
if (nthreads != 1)
{
/* When there's more than one thread, we expect that we're operating on
diff --git a/libgomp/iter.c b/libgomp/iter.c
index 029c1780adf36..b48cda4a8ac73 100644
--- a/libgomp/iter.c
+++ b/libgomp/iter.c
@@ -30,6 +30,20 @@
#include <stdlib.h>
+#ifdef LIBGOMP_SIMT
+/* See gomp_iter_simt_lanes_init. */
+
+void
+gomp_iter_simt_lanes_alloc (struct gomp_work_share *ws)
+{
+ if (!FIRST_LANE_P)
+ return;
+
+ ws->simt_lane_ranges
+ = team_malloc (SIMT_LANES * 2 * sizeof (*ws->simt_lane_ranges));
+}
+#endif
+
/* This function implements the STATIC scheduling method. The caller should
iterate *pstart <= x < *pend. Return zero if there are more iterations
to perform; nonzero if not. Return less than 0 if this thread had
@@ -41,7 +55,10 @@ gomp_iter_static_next (long *pstart, long *pend)
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
struct gomp_work_share *ws = thr->ts.work_share;
- unsigned long nthreads = team ? team->nthreads : 1;
+ unsigned long nthreads = team ? GOMP_TEAM_OMP_NTHR (team) : 1;
+
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return !gomp_iter_simt_lanes_next (ws, pstart, pend);
if (thr->ts.static_trip == -1)
return -1;
@@ -70,7 +87,7 @@ gomp_iter_static_next (long *pstart, long *pend)
/* Compute the total number of iterations. */
s = ws->incr + (ws->incr > 0 ? -1 : 1);
n = (ws->end - ws->next + s) / ws->incr;
- i = thr->ts.team_id;
+ i = GOMP_TID_TO_OMP_TID (thr->ts.team_id);
/* Compute the "zero-based" start and end points. That is, as
if the loop began at zero and incremented by one. */
@@ -97,7 +114,7 @@ gomp_iter_static_next (long *pstart, long *pend)
*pstart = s;
*pend = e;
- thr->ts.static_trip = (e0 == n ? -1 : 1);
+ thr->ts.static_trip = (SIMT_VOTE_ANY (e0 == n) ? -1 : 1);
return 0;
}
else
@@ -110,7 +127,7 @@ gomp_iter_static_next (long *pstart, long *pend)
s = ws->incr + (ws->incr > 0 ? -1 : 1);
n = (ws->end - ws->next + s) / ws->incr;
- i = thr->ts.team_id;
+ i = GOMP_TID_TO_OMP_TID (thr->ts.team_id);
c = ws->chunk_size;
/* Initial guess is a C sized chunk positioned nthreads iterations
@@ -131,9 +148,9 @@ gomp_iter_static_next (long *pstart, long *pend)
*pstart = s;
*pend = e;
- if (e0 == n)
+ if (SIMT_VOTE_ANY (e0 == n))
thr->ts.static_trip = -1;
- else
+ else if (FIRST_LANE_P)
thr->ts.static_trip++;
return 0;
}
@@ -151,6 +168,9 @@ gomp_iter_dynamic_next_locked (long *pstart, long *pend)
struct gomp_work_share *ws = thr->ts.work_share;
long start, end, chunk, left;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_simt_lanes_next (ws, pstart, pend);
+
start = ws->next;
if (start == ws->end)
return false;
@@ -187,6 +207,9 @@ gomp_iter_dynamic_next (long *pstart, long *pend)
struct gomp_work_share *ws = thr->ts.work_share;
long start, end, nend, chunk, incr;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_simt_lanes_next (ws, pstart, pend);
+
end = ws->end;
incr = ws->incr;
chunk = ws->chunk_size;
@@ -267,6 +290,9 @@ gomp_iter_guided_next_locked (long *pstart, long *pend)
unsigned long n, q;
long start, end;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_simt_lanes_next (ws, pstart, pend);
+
if (ws->next == ws->end)
return false;
@@ -301,6 +327,9 @@ gomp_iter_guided_next (long *pstart, long *pend)
long start, end, nend, incr;
unsigned long chunk_size;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_simt_lanes_next (ws, pstart, pend);
+
start = __atomic_load_n (&ws->next, MEMMODEL_RELAXED);
end = ws->end;
incr = ws->incr;
diff --git a/libgomp/iter_ull.c b/libgomp/iter_ull.c
index bf112dfaadf2f..75e3b629d7308 100644
--- a/libgomp/iter_ull.c
+++ b/libgomp/iter_ull.c
@@ -42,7 +42,10 @@ gomp_iter_ull_static_next (gomp_ull *pstart, gomp_ull *pend)
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
struct gomp_work_share *ws = thr->ts.work_share;
- unsigned long nthreads = team ? team->nthreads : 1;
+ unsigned long nthreads = team ? GOMP_TEAM_OMP_NTHR (team) : 1;
+
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return !gomp_iter_ull_simt_lanes_next (ws, pstart, pend);
if (thr->ts.static_trip == -1)
return -1;
@@ -71,7 +74,7 @@ gomp_iter_ull_static_next (gomp_ull *pstart, gomp_ull *pend)
n = (ws->end_ull - ws->next_ull + ws->incr_ull - 1) / ws->incr_ull;
else
n = (ws->next_ull - ws->end_ull - ws->incr_ull - 1) / -ws->incr_ull;
- i = thr->ts.team_id;
+ i = GOMP_TID_TO_OMP_TID (thr->ts.team_id);
/* Compute the "zero-based" start and end points. That is, as
if the loop began at zero and incremented by one. */
@@ -98,7 +101,7 @@ gomp_iter_ull_static_next (gomp_ull *pstart, gomp_ull *pend)
*pstart = s;
*pend = e;
- thr->ts.static_trip = (e0 == n ? -1 : 1);
+ thr->ts.static_trip = (SIMT_VOTE_ANY (e0 == n) ? -1 : 1);
return 0;
}
else
@@ -112,7 +115,7 @@ gomp_iter_ull_static_next (gomp_ull *pstart, gomp_ull *pend)
n = (ws->end_ull - ws->next_ull + ws->incr_ull - 1) / ws->incr_ull;
else
n = (ws->next_ull - ws->end_ull - ws->incr_ull - 1) / -ws->incr_ull;
- i = thr->ts.team_id;
+ i = GOMP_TID_TO_OMP_TID (thr->ts.team_id);
c = ws->chunk_size_ull;
/* Initial guess is a C sized chunk positioned nthreads iterations
@@ -133,9 +136,9 @@ gomp_iter_ull_static_next (gomp_ull *pstart, gomp_ull *pend)
*pstart = s;
*pend = e;
- if (e0 == n)
+ if (SIMT_VOTE_ANY (e0 == n))
thr->ts.static_trip = -1;
- else
+ else if (FIRST_LANE_P)
thr->ts.static_trip++;
return 0;
}
@@ -153,6 +156,9 @@ gomp_iter_ull_dynamic_next_locked (gomp_ull *pstart,
gomp_ull *pend)
struct gomp_work_share *ws = thr->ts.work_share;
gomp_ull start, end, chunk, left;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_ull_simt_lanes_next (ws, pstart, pend);
+
start = ws->next_ull;
if (start == ws->end_ull)
return false;
@@ -189,6 +195,9 @@ gomp_iter_ull_dynamic_next (gomp_ull *pstart, gomp_ull
*pend)
struct gomp_work_share *ws = thr->ts.work_share;
gomp_ull start, end, nend, chunk;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_ull_simt_lanes_next (ws, pstart, pend);
+
end = ws->end_ull;
chunk = ws->chunk_size_ull;
@@ -268,6 +277,9 @@ gomp_iter_ull_guided_next_locked (gomp_ull *pstart,
gomp_ull *pend)
gomp_ull n, q;
gomp_ull start, end;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_ull_simt_lanes_next (ws, pstart, pend);
+
if (ws->next_ull == ws->end_ull)
return false;
@@ -305,6 +317,9 @@ gomp_iter_ull_guided_next (gomp_ull *pstart, gomp_ull *pend)
gomp_ull start, end, nend, incr;
gomp_ull chunk_size;
+ if (GOMP_ITER_SIMT_LANES_P (ws))
+ return gomp_iter_ull_simt_lanes_next (ws, pstart, pend);
+
start = __atomic_load_n (&ws->next_ull, MEMMODEL_RELAXED);
end = ws->end_ull;
incr = ws->incr_ull;
diff --git a/libgomp/libgomp.h b/libgomp/libgomp.h
index 8cec73c4e6056..8dcbb1180a363 100644
--- a/libgomp/libgomp.h
+++ b/libgomp/libgomp.h
@@ -43,6 +43,7 @@
#include "config.h"
#include <stdint.h>
+#include <limits.h>
/* Include omp.h by parts. */
#include "omp-lock.h"
@@ -260,6 +261,38 @@ spin_count ()
return count;
}
+/* SIMT devices, on which the OpenMP threads of a team may be the lanes of
+ a wave, define LIBGOMP_SIMT and the lane primitives below. The generic
+ code tests LIBGOMP_SIMT, never the target. SIMT_MODE_P is true if this
+ kernel runs threads as lanes ("simt" offload mode), and false in
+ "simt-directed" mode. These must be defined before the following
+ headers. */
+#if defined(__AMDGCN__) && defined(__SIMT__)
+#define LIBGOMP_SIMT 1
+extern int __gcn_simt_mode;
+#define SIMT_MODE_P __gcn_simt_mode
+#define FIRST_LANE_P __builtin_gcn_simt_first_lane_p ()
+#define SIMT_VOTE_ANY(COND) __builtin_gcn_simt_vote_any (COND)
+#define SIMT_BROADCAST_FIRST_LANE(X) __builtin_gcn_simt_broadcast_first_lane
(X)
+#define SIMT_LANE_ID __builtin_gcn_dim_pos (2)
+#define SIMT_LANES 64
+#else
+#define SIMT_MODE_P false
+#define FIRST_LANE_P true
+#define SIMT_VOTE_ANY(COND) (COND)
+#define SIMT_BROADCAST_FIRST_LANE(X) (X)
+#define SIMT_LANE_ID 0
+#define SIMT_LANES 1
+#endif
+
+#ifdef LIBGOMP_SIMT
+extern void *gomp_aligned_alloc_lanes (size_t, size_t);
+extern void gomp_aligned_free_lanes (void *);
+#else
+#define gomp_aligned_alloc_lanes gomp_aligned_alloc
+#define gomp_aligned_free_lanes gomp_aligned_free
+#endif
+
#include "priority_queue.h"
#include "sem.h"
#include "mutex.h"
@@ -450,6 +483,12 @@ struct gomp_work_share
/* Task reductions for this work-sharing construct. */
uintptr_t *task_reductions;
+#ifdef LIBGOMP_SIMT
+ /* In a nested SIMT team, each lane's remaining iterations, as NEXT, END
+ pairs indexed by lane (see gomp_iter_simt_lanes_init). */
+ unsigned long long *simt_lane_ranges;
+#endif
+
/* If only few threads are in the team, ordered_team_ids can point
to this array which fills the padding at the end of this struct. */
unsigned inline_ordered_team_ids[0];
@@ -873,6 +912,13 @@ struct gomp_team
/* This is the number of threads in the current team. */
unsigned nthreads;
+#ifdef LIBGOMP_SIMT
+ /* The number of OpenMP threads in the team. NTHREADS counts waves, and
+ in SIMT mode the threads of an outermost team are the first
+ OMP_NTHREADS lanes of those waves. */
+ unsigned omp_nthreads;
+#endif
+
/* This is number of gomp_work_share structs that have been allocated
as a block last time. */
unsigned work_share_chunk;
@@ -1030,6 +1076,57 @@ struct gomp_thread
#endif
};
+#ifdef LIBGOMP_SIMT
+/* True if the OpenMP threads at nesting LEVEL are the lanes of the team's
+ waves. The outermost team (level 1) enters SIMT mode, so its lanes are
+ threads. A parallel region nested inside it is reached from SIMT code,
+ and each lane becomes a team of one, sharing its wave's team structure.
+ Level 0, the initial thread, runs on lane 0 alone, so it maps the same
+ either way. */
+#define GOMP_SIMT_LANES_P(LEVEL) (SIMT_MODE_P && (LEVEL) <= 1)
+/* True if each lane is a team of one at nesting LEVEL (a nested team). */
+#define GOMP_SIMT_LANE_TEAMS_P(LEVEL) (SIMT_MODE_P && (LEVEL) > 1)
+
+/* Map gomp_thread IDs to the user-visible IDs. These are the same
+ for non-SIMT devices, and also in "simt-directed" mode. The _AT forms
+ are for the team at nesting LEVEL, the others for the current team. */
+#define GOMP_TID_TO_OMP_TID_AT(LEVEL, GTID) \
+ (GOMP_SIMT_LANES_P (LEVEL) ? (GTID) * SIMT_LANES + SIMT_LANE_ID \
+ : (GTID))
+#define GOMP_TID_TO_OMP_TID(GTID) \
+ GOMP_TID_TO_OMP_TID_AT (gomp_thread ()->ts.level, GTID)
+#define OMP_TID_TO_GOMP_TID(OTID) \
+ (SIMT_MODE_P ? (OTID) / SIMT_LANES : (OTID))
+/* The number of OpenMP threads in TEAM. */
+#define GOMP_TEAM_OMP_NTHR(TEAM) ((TEAM)->omp_nthreads)
+/* The number of waves needed for ONUM OpenMP threads. */
+#define OMP_NTHR_TO_GOMP_NTHR(ONUM) \
+ (SIMT_MODE_P ? ((ONUM) + SIMT_LANES - 1) / SIMT_LANES : (ONUM))
+
+/* Call FN (DATA) on the master thread of a new team, switching the backend
+ into SIMT mode if the team's threads are lanes (an outermost team in a
+ kernel built that way). The SIMT entry is out of line, in
+ gomp_simt_launch_master, so that the GOMP_parallel entry points contain
+ no simt_enter and are compiled SIMT throughout: a nested parallel region
+ calls them from SIMT code, with every lane of the team active, and then
+ FN is called directly. */
+extern void gomp_simt_launch_master (void (*) (void *), void *);
+#define LAUNCH_MASTER_THREAD(FN, DATA) \
+ if (GOMP_SIMT_LANES_P (gomp_thread ()->ts.level)) \
+ gomp_simt_launch_master (FN, DATA); \
+ else \
+ (FN) (DATA)
+
+#else
+#define GOMP_SIMT_LANE_TEAMS_P(LEVEL) false
+#define GOMP_TID_TO_OMP_TID_AT(LEVEL, GTID) (GTID)
+#define GOMP_TID_TO_OMP_TID(GTID) (GTID)
+#define OMP_TID_TO_GOMP_TID(OTID) (OTID)
+#define GOMP_TEAM_OMP_NTHR(TEAM) ((TEAM)->nthreads)
+#define OMP_NTHR_TO_GOMP_NTHR(ONUM) (ONUM)
+#define LAUNCH_MASTER_THREAD(FN, DATA) (FN) (DATA)
+#endif
+
struct gomp_thread_pool
{
@@ -1170,6 +1267,76 @@ extern bool gomp_get_icv_flag (uint32_t value, enum
gomp_icvs icv);
/* iter.c */
+#ifdef LIBGOMP_SIMT
+/* In a nested SIMT team each lane is a team of one, with its own loop
+ bounds, but the work share belongs to the wave. The loop initialization
+ therefore records each lane's iterations, and the gomp_iter_* routines
+ give each lane all of them at once, which is a valid schedule of any kind
+ for a team of one. END must already be canonicalized. */
+
+extern void gomp_iter_simt_lanes_alloc (struct gomp_work_share *);
+
+static inline void
+gomp_iter_simt_lanes_init (struct gomp_work_share *ws,
+ unsigned long long start, unsigned long long end)
+{
+ unsigned long long *r;
+
+ /* Every lane calls this, and only reads the pointer after the call. An
+ inline "if (FIRST_LANE_P)" store followed by the load here lets the
+ compiler move the load of the other lanes ahead of the store. */
+ gomp_iter_simt_lanes_alloc (ws);
+ r = ws->simt_lane_ranges + 2 * SIMT_LANE_ID;
+ r[0] = start;
+ r[1] = end;
+}
+
+#define GOMP_ITER_SIMT_LANES_P(WS) ((WS)->simt_lane_ranges != NULL)
+
+static inline bool
+gomp_iter_ull_simt_lanes_next (struct gomp_work_share *ws,
+ unsigned long long *pstart,
+ unsigned long long *pend)
+{
+ unsigned long long *r = ws->simt_lane_ranges + 2 * SIMT_LANE_ID;
+
+ *pstart = r[0];
+ *pend = r[1];
+ r[0] = r[1];
+ return *pstart != *pend;
+}
+
+static inline bool
+gomp_iter_simt_lanes_next (struct gomp_work_share *ws, long *pstart,
+ long *pend)
+{
+ unsigned long long start, end;
+ bool ret = gomp_iter_ull_simt_lanes_next (ws, &start, &end);
+
+ *pstart = start;
+ *pend = end;
+ return ret;
+}
+
+/* Likewise, but give the lane a single iteration (for sections). */
+
+static inline bool
+gomp_iter_simt_lanes_next_one (struct gomp_work_share *ws, long *pstart)
+{
+ unsigned long long *r = ws->simt_lane_ranges + 2 * SIMT_LANE_ID;
+
+ if (r[0] == r[1])
+ return false;
+ *pstart = r[0]++;
+ return true;
+}
+#else
+#define GOMP_ITER_SIMT_LANES_P(WS) false
+#define gomp_iter_simt_lanes_next(WS, PSTART, PEND) false
+#define gomp_iter_ull_simt_lanes_next(WS, PSTART, PEND) false
+#define gomp_iter_simt_lanes_next_one(WS, PSTART) false
+#endif
+
extern int gomp_iter_static_next (long *, long *);
extern bool gomp_iter_dynamic_next_locked (long *, long *);
extern bool gomp_iter_guided_next_locked (long *, long *);
diff --git a/libgomp/loop.c b/libgomp/loop.c
index ce0073966582d..96a256bdb93ee 100644
--- a/libgomp/loop.c
+++ b/libgomp/loop.c
@@ -47,6 +47,14 @@ gomp_loop_init (struct gomp_work_share *ws, long start, long
end, long incr,
? start : end;
ws->incr = incr;
ws->next = start;
+#ifdef LIBGOMP_SIMT
+ /* Every lane gets here, each with its own bounds in a nested team. */
+ if (GOMP_SIMT_LANE_TEAMS_P (gomp_thread ()->ts.level)
+ && ws == gomp_thread ()->ts.work_share)
+ gomp_iter_simt_lanes_init (ws, start,
+ ((incr > 0 && start > end)
+ || (incr < 0 && start < end)) ? start : end);
+#endif
if (sched == GFS_DYNAMIC)
{
ws->chunk_size *= incr;
@@ -57,7 +65,7 @@ gomp_loop_init (struct gomp_work_share *ws, long start, long
end, long incr,
faster. */
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
- long nthreads = team ? team->nthreads : 1;
+ long nthreads = team ? GOMP_TEAM_OMP_NTHR (team) : 1;
if (__builtin_expect (incr > 0, 1))
{
@@ -737,7 +745,7 @@ gomp_loop_ordered_dynamic_next (long *istart, long *iend)
gomp_ordered_sync ();
gomp_mutex_lock (&thr->ts.work_share->lock);
ret = gomp_iter_dynamic_next_locked (istart, iend);
- if (ret)
+ if (SIMT_VOTE_ANY (ret))
gomp_ordered_next ();
else
gomp_ordered_last ();
@@ -796,8 +804,17 @@ gomp_parallel_loop_start (void (*fn) (void *), void *data,
num_threads = gomp_resolve_num_threads (num_threads, 0);
team = gomp_new_team (num_threads);
- gomp_loop_init (&team->work_shares[0], start, end, incr, sched, chunk_size);
+ if (FIRST_LANE_P)
+ gomp_loop_init (&team->work_shares[0], start, end, incr, sched,
+ chunk_size);
gomp_team_start (fn, data, num_threads, flags, team, NULL);
+#ifdef LIBGOMP_SIMT
+ /* The work share was set up for the first lane only. */
+ if (GOMP_SIMT_LANE_TEAMS_P (gomp_thread ()->ts.level))
+ gomp_iter_simt_lanes_init (gomp_thread ()->ts.work_share, start,
+ ((incr > 0 && start > end)
+ || (incr < 0 && start < end)) ? start : end);
+#endif
}
void
@@ -847,7 +864,7 @@ GOMP_parallel_loop_static (void (*fn) (void *), void *data,
{
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
GFS_STATIC, chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
@@ -858,7 +875,7 @@ GOMP_parallel_loop_dynamic (void (*fn) (void *), void *data,
{
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
GFS_DYNAMIC, chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
@@ -869,7 +886,7 @@ GOMP_parallel_loop_guided (void (*fn) (void *), void *data,
{
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
GFS_GUIDED, chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
@@ -882,7 +899,7 @@ GOMP_parallel_loop_runtime (void (*fn) (void *), void *data,
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
icv->run_sched_var & ~GFS_MONOTONIC,
icv->run_sched_chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
@@ -904,7 +921,7 @@ GOMP_parallel_loop_nonmonotonic_dynamic (void (*fn) (void
*), void *data,
{
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
GFS_DYNAMIC, chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
@@ -916,7 +933,7 @@ GOMP_parallel_loop_nonmonotonic_guided (void (*fn) (void
*), void *data,
{
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
GFS_GUIDED, chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
@@ -929,7 +946,7 @@ GOMP_parallel_loop_nonmonotonic_runtime (void (*fn) (void
*), void *data,
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
icv->run_sched_var & ~GFS_MONOTONIC,
icv->run_sched_chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
@@ -943,7 +960,7 @@ GOMP_parallel_loop_maybe_nonmonotonic_runtime (void (*fn)
(void *), void *data,
gomp_parallel_loop_start (fn, data, num_threads, start, end, incr,
icv->run_sched_var & ~GFS_MONOTONIC,
icv->run_sched_chunk_size, flags);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
#endif
@@ -1196,8 +1213,8 @@ GOMP_loop_static_worksharing (unsigned long long niter
__attribute__ ((unused)))
{
struct gomp_team *team = gomp_thread ()->ts.team;
- unsigned tid = gomp_thread ()->ts.team_id;
- unsigned nthreads = team ? team->nthreads : 1;
+ unsigned tid = GOMP_TID_TO_OMP_TID (gomp_thread ()->ts.team_id);
+ unsigned nthreads = team ? GOMP_TEAM_OMP_NTHR (team) : 1;
return nthreads + tid * 1I;
}
@@ -1209,8 +1226,8 @@ GOMP_loop_static_worksharing_start (unsigned long long
niter
__attribute__ ((unused)))
{
struct gomp_team *team = gomp_thread ()->ts.team;
- unsigned tid = gomp_thread ()->ts.team_id;
- unsigned nthreads = team ? team->nthreads : 1;
+ unsigned tid = GOMP_TID_TO_OMP_TID (gomp_thread ()->ts.team_id);
+ unsigned nthreads = team ? GOMP_TEAM_OMP_NTHR (team) : 1;
return nthreads + tid * 1I;
}
diff --git a/libgomp/loop_ull.c b/libgomp/loop_ull.c
index cb110c785deb2..6fde8f1ffa962 100644
--- a/libgomp/loop_ull.c
+++ b/libgomp/loop_ull.c
@@ -50,6 +50,14 @@ gomp_loop_ull_init (struct gomp_work_share *ws, bool up,
gomp_ull start,
ws->incr_ull = incr;
ws->next_ull = start;
ws->mode = 0;
+#ifdef LIBGOMP_SIMT
+ /* Every lane gets here, each with its own bounds in a nested team. */
+ if (GOMP_SIMT_LANE_TEAMS_P (gomp_thread ()->ts.level)
+ && ws == gomp_thread ()->ts.work_share)
+ gomp_iter_simt_lanes_init (ws, start,
+ ((up && start > end) || (!up && start < end))
+ ? start : end);
+#endif
if (sched == GFS_DYNAMIC)
{
ws->chunk_size_ull *= incr;
@@ -60,7 +68,7 @@ gomp_loop_ull_init (struct gomp_work_share *ws, bool up,
gomp_ull start,
faster. */
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
- long nthreads = team ? team->nthreads : 1;
+ long nthreads = team ? GOMP_TEAM_OMP_NTHR (team) : 1;
if (__builtin_expect (up, 1))
{
@@ -743,7 +751,7 @@ gomp_loop_ull_ordered_dynamic_next (gomp_ull *istart,
gomp_ull *iend)
gomp_ordered_sync ();
gomp_mutex_lock (&thr->ts.work_share->lock);
ret = gomp_iter_ull_dynamic_next_locked (istart, iend);
- if (ret)
+ if (SIMT_VOTE_ANY (ret))
gomp_ordered_next ();
else
gomp_ordered_last ();
diff --git a/libgomp/ordered.c b/libgomp/ordered.c
index aa432f692ac1e..aff1e43485d77 100644
--- a/libgomp/ordered.c
+++ b/libgomp/ordered.c
@@ -47,6 +47,10 @@ gomp_ordered_first (void)
if (team == NULL || team->nthreads == 1)
return;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
index = ws->ordered_cur + ws->ordered_num_used;
if (index >= team->nthreads)
index -= team->nthreads;
@@ -78,6 +82,10 @@ gomp_ordered_last (void)
if (team == NULL || team->nthreads == 1)
return;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
/* We're no longer the owner. */
ws->ordered_owner = -1;
@@ -113,6 +121,10 @@ gomp_ordered_next (void)
if (team == NULL || team->nthreads == 1)
return;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
/* We're no longer the owner. */
ws->ordered_owner = -1;
@@ -158,6 +170,10 @@ gomp_ordered_static_init (void)
if (team == NULL || team->nthreads == 1)
return;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
gomp_sem_post (team->ordered_release[0]);
}
@@ -178,6 +194,10 @@ gomp_ordered_static_next (void)
if (team == NULL || team->nthreads == 1)
return;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
ws->ordered_owner = -1;
/* This thread currently owns the lock. Increment the owner. */
@@ -203,6 +223,10 @@ gomp_ordered_sync (void)
if (team == NULL || team->nthreads == 1)
return;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
/* ??? I believe it to be safe to access this data without taking the
ws->lock. The only presumed race condition is with the previous
thread on the queue incrementing ordered_cur such that it points
@@ -269,6 +293,10 @@ gomp_doacross_init (unsigned ncounts, long *counts, long
chunk_size,
unsigned long ent, num_ents, elt_sz, shift_sz;
struct gomp_doacross_work_share *doacross;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
if (team == NULL || team->nthreads == 1)
{
empty:
@@ -308,7 +336,9 @@ gomp_doacross_init (unsigned ncounts, long *counts, long
chunk_size,
}
if (ws->sched == GFS_STATIC)
- num_ents = team->nthreads;
+ /* One entry per user-visible thread, matching the static schedule in
+ iter.c. In SIMT mode each lane posts its own progress. */
+ num_ents = GOMP_TEAM_OMP_NTHR (team);
else if (ws->sched == GFS_GUIDED)
num_ents = counts[0];
else
@@ -387,7 +417,7 @@ GOMP_doacross_post (long *counts)
}
if (__builtin_expect (ws->sched == GFS_STATIC, 1))
- ent = thr->ts.team_id;
+ ent = GOMP_TID_TO_OMP_TID (thr->ts.team_id);
else if (ws->sched == GFS_GUIDED)
ent = counts[0];
else
@@ -449,7 +479,8 @@ GOMP_doacross_wait (long first, ...)
+ doacross->t;
}
else
- ent = first / ws->chunk_size % thr->ts.team->nthreads;
+ ent = (first / ws->chunk_size
+ % GOMP_TEAM_OMP_NTHR (thr->ts.team));
}
else if (ws->sched == GFS_GUIDED)
ent = first;
@@ -519,6 +550,10 @@ gomp_doacross_ull_init (unsigned ncounts, gomp_ull *counts,
unsigned long ent, num_ents, elt_sz, shift_sz;
struct gomp_doacross_work_share *doacross;
+ /* SIMT work share is per-wave. */
+ if (!FIRST_LANE_P)
+ return;
+
if (team == NULL || team->nthreads == 1)
{
empty:
@@ -558,7 +593,9 @@ gomp_doacross_ull_init (unsigned ncounts, gomp_ull *counts,
}
if (ws->sched == GFS_STATIC)
- num_ents = team->nthreads;
+ /* One entry per user-visible thread, matching the static schedule in
+ iter.c. In SIMT mode each lane posts its own progress. */
+ num_ents = GOMP_TEAM_OMP_NTHR (team);
else if (ws->sched == GFS_GUIDED)
num_ents = counts[0];
else
@@ -643,7 +680,7 @@ GOMP_doacross_ull_post (gomp_ull *counts)
}
if (__builtin_expect (ws->sched == GFS_STATIC, 1))
- ent = thr->ts.team_id;
+ ent = GOMP_TID_TO_OMP_TID (thr->ts.team_id);
else if (ws->sched == GFS_GUIDED)
ent = counts[0];
else
@@ -726,7 +763,8 @@ GOMP_doacross_ull_wait (gomp_ull first, ...)
+ doacross->t;
}
else
- ent = first / ws->chunk_size_ull % thr->ts.team->nthreads;
+ ent = (first / ws->chunk_size_ull
+ % GOMP_TEAM_OMP_NTHR (thr->ts.team));
}
else if (ws->sched == GFS_GUIDED)
ent = first;
diff --git a/libgomp/parallel.c b/libgomp/parallel.c
index 286798ff90276..18483d02b3fa6 100644
--- a/libgomp/parallel.c
+++ b/libgomp/parallel.c
@@ -28,7 +28,6 @@
#include "libgomp.h"
#include <limits.h>
-
/* Determine the number of threads to be launched for a PARALLEL construct.
This algorithm is explicitly described in OpenMP 3.0 section 2.4.1.
SPECIFIED is a combination of the NUM_THREADS clause and the IF clause.
@@ -49,6 +48,11 @@ gomp_resolve_num_threads (unsigned specified, unsigned count)
if (specified == 1)
return 1;
+ /* In SIMT mode a nested team is a team of one per lane, even inside an
+ inactive team. */
+ if (GOMP_SIMT_LANE_TEAMS_P (thr->ts.level + 1))
+ return 1;
+
if (thr->ts.active_level >= 1
/* Accelerators with fixed thread counts require this to return 1 for
nested parallel regions. */
@@ -81,8 +85,10 @@ gomp_resolve_num_threads (unsigned specified, unsigned count)
max_num_threads = count;
}
+ unsigned thread_limit = icv->thread_limit_var;
+
/* UINT_MAX stands for infinity. */
- if (__builtin_expect (icv->thread_limit_var == UINT_MAX, 1)
+ if (__builtin_expect (thread_limit == UINT_MAX, 1)
|| max_num_threads == 1)
return max_num_threads;
@@ -95,8 +101,8 @@ gomp_resolve_num_threads (unsigned specified, unsigned count)
if (thr->ts.team == NULL || pool == NULL)
{
num_threads = max_num_threads;
- if (num_threads > icv->thread_limit_var)
- num_threads = icv->thread_limit_var;
+ if (num_threads > thread_limit)
+ num_threads = thread_limit;
if (pool)
pool->threads_busy = num_threads;
return num_threads;
@@ -107,8 +113,8 @@ gomp_resolve_num_threads (unsigned specified, unsigned
count)
{
busy = pool->threads_busy;
num_threads = max_num_threads;
- if (icv->thread_limit_var - busy + 1 < num_threads)
- num_threads = icv->thread_limit_var - busy + 1;
+ if (thread_limit - busy + 1 < num_threads)
+ num_threads = thread_limit - busy + 1;
}
while (__sync_val_compare_and_swap (&pool->threads_busy,
busy, busy + num_threads - 1)
@@ -117,8 +123,8 @@ gomp_resolve_num_threads (unsigned specified, unsigned
count)
gomp_mutex_lock (&gomp_managed_threads_lock);
num_threads = max_num_threads;
busy = pool->threads_busy;
- if (icv->thread_limit_var - busy + 1 < num_threads)
- num_threads = icv->thread_limit_var - busy + 1;
+ if (thread_limit - busy + 1 < num_threads)
+ num_threads = thread_limit - busy + 1;
pool->threads_busy += num_threads - 1;
gomp_mutex_unlock (&gomp_managed_threads_lock);
#endif
@@ -142,7 +148,7 @@ GOMP_parallel_end (void)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
- unsigned int nthreads = team ? team->nthreads : 1;
+ unsigned int nthreads = team ? GOMP_TEAM_OMP_NTHR (team) : 1;
gomp_team_end ();
if (nthreads > 1)
{
@@ -175,7 +181,7 @@ GOMP_parallel (void (*fn) (void *), void *data, unsigned
num_threads,
num_threads = gomp_resolve_num_threads (num_threads, 0);
gomp_team_start (fn, data, num_threads, flags, gomp_new_team (num_threads),
NULL);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
ialias_call (GOMP_parallel_end) ();
}
@@ -189,10 +195,10 @@ GOMP_parallel_reductions (void (*fn) (void *), void *data,
taskgroup = gomp_parallel_reduction_register (rdata, num_threads);
gomp_team_start (fn, data, num_threads, flags, gomp_new_team (num_threads),
taskgroup);
- fn (data);
+ LAUNCH_MASTER_THREAD (fn, data);
ialias_call (GOMP_parallel_end) ();
gomp_sem_destroy (&taskgroup->taskgroup_sem);
- free (taskgroup);
+ gomp_aligned_free_lanes (taskgroup);
return num_threads;
}
@@ -277,7 +283,7 @@ GOMP_cancel (int which, bool do_cancel)
bool
GOMP_has_masked_thread_num (int tid)
{
- return tid == gomp_thread ()->ts.team_id;
+ return tid == GOMP_TID_TO_OMP_TID (gomp_thread ()->ts.team_id);
}
/* OMPT variant enabled by -fopenmp-ompt. */
@@ -285,7 +291,7 @@ GOMP_has_masked_thread_num (int tid)
bool
GOMP_has_masked_thread_num_with_end (int tid)
{
- return tid == gomp_thread ()->ts.team_id;
+ return tid == GOMP_TID_TO_OMP_TID (gomp_thread ()->ts.team_id);
}
/* Stub for OMPT callback enabled by -fopenmp-ompt. */
@@ -294,13 +300,14 @@ void
GOMP_masked_end (void)
{}
+
/* The public OpenMP API for thread and team related inquiries. */
int
omp_get_num_threads (void)
{
struct gomp_team *team = gomp_thread ()->ts.team;
- return team ? team->nthreads : 1;
+ return team ? GOMP_TEAM_OMP_NTHR (team) : 1;
}
int
@@ -314,7 +321,7 @@ omp_get_num_threads_dim (int dim)
int
omp_get_thread_num (void)
{
- return gomp_thread ()->ts.team_id;
+ return GOMP_TID_TO_OMP_TID (gomp_thread ()->ts.team_id);
}
int
@@ -349,7 +356,11 @@ omp_get_ancestor_thread_num (int level)
return -1;
for (level = ts->level - level; level > 0; --level)
ts = &ts->team->prev_ts;
- return ts->team_id;
+ /* The initial thread (level 0) is not a SIMT team, so every lane
+ shares its thread number. */
+ if (ts->team == NULL)
+ return ts->team_id;
+ return GOMP_TID_TO_OMP_TID_AT (ts->level, ts->team_id);
}
int
@@ -363,7 +374,7 @@ omp_get_team_size (int level)
if (ts->team == NULL)
return 1;
else
- return ts->team->nthreads;
+ return GOMP_TEAM_OMP_NTHR (ts->team);
}
int
diff --git a/libgomp/sections.c b/libgomp/sections.c
index f47a4483f8f26..196cc4c80f71f 100644
--- a/libgomp/sections.c
+++ b/libgomp/sections.c
@@ -41,6 +41,12 @@ gomp_sections_init (struct gomp_work_share *ws, unsigned
count)
ws->end = count + 1L;
ws->incr = 1;
ws->next = 1;
+#ifdef LIBGOMP_SIMT
+ /* Every lane gets here. In a nested team each runs every section. */
+ if (GOMP_SIMT_LANE_TEAMS_P (gomp_thread ()->ts.level)
+ && ws == gomp_thread ()->ts.work_share)
+ gomp_iter_simt_lanes_init (ws, 1, count + 1L);
+#endif
#ifdef HAVE_SYNC_BUILTINS
/* Prepare things to make each iteration faster. */
if (sizeof (long) > sizeof (unsigned))
@@ -81,6 +87,9 @@ GOMP_sections_start (unsigned count)
gomp_work_share_init_done ();
}
+ if (GOMP_ITER_SIMT_LANES_P (thr->ts.work_share))
+ return gomp_iter_simt_lanes_next_one (thr->ts.work_share, &s) ? s : 0;
+
#ifdef HAVE_SYNC_BUILTINS
if (gomp_iter_dynamic_next (&s, &e))
ret = s;
@@ -158,6 +167,9 @@ GOMP_sections2_start (unsigned count, uintptr_t
*reductions, void **mem)
}
}
+ if (GOMP_ITER_SIMT_LANES_P (thr->ts.work_share))
+ return gomp_iter_simt_lanes_next_one (thr->ts.work_share, &s) ? s : 0;
+
#ifdef HAVE_SYNC_BUILTINS
if (gomp_iter_dynamic_next (&s, &e))
ret = s;
@@ -189,6 +201,10 @@ GOMP_sections_next (void)
{
long s, e, ret;
+ if (GOMP_ITER_SIMT_LANES_P (gomp_thread ()->ts.work_share))
+ return (gomp_iter_simt_lanes_next_one (gomp_thread ()->ts.work_share, &s)
+ ? s : 0);
+
#ifdef HAVE_SYNC_BUILTINS
if (gomp_iter_dynamic_next (&s, &e))
ret = s;
@@ -219,8 +235,14 @@ GOMP_parallel_sections_start (void (*fn) (void *), void
*data,
num_threads = gomp_resolve_num_threads (num_threads, count);
team = gomp_new_team (num_threads);
- gomp_sections_init (&team->work_shares[0], count);
+ if (FIRST_LANE_P)
+ gomp_sections_init (&team->work_shares[0], count);
gomp_team_start (fn, data, num_threads, 0, team, NULL);
+#ifdef LIBGOMP_SIMT
+ /* The work share was set up for the first lane only. */
+ if (GOMP_SIMT_LANE_TEAMS_P (gomp_thread ()->ts.level))
+ gomp_iter_simt_lanes_init (gomp_thread ()->ts.work_share, 1, count + 1L);
+#endif
}
ialias_redirect (GOMP_parallel_end)
@@ -233,9 +255,15 @@ GOMP_parallel_sections (void (*fn) (void *), void *data,
num_threads = gomp_resolve_num_threads (num_threads, count);
team = gomp_new_team (num_threads);
- gomp_sections_init (&team->work_shares[0], count);
+ if (FIRST_LANE_P)
+ gomp_sections_init (&team->work_shares[0], count);
gomp_team_start (fn, data, num_threads, flags, team, NULL);
- fn (data);
+#ifdef LIBGOMP_SIMT
+ /* The work share was set up for the first lane only. */
+ if (GOMP_SIMT_LANE_TEAMS_P (gomp_thread ()->ts.level))
+ gomp_iter_simt_lanes_init (gomp_thread ()->ts.work_share, 1, count + 1L);
+#endif
+ LAUNCH_MASTER_THREAD (fn, data);
GOMP_parallel_end ();
}
diff --git a/libgomp/single.c b/libgomp/single.c
index cfafe55aa679e..cc5d27b84f7da 100644
--- a/libgomp/single.c
+++ b/libgomp/single.c
@@ -43,6 +43,14 @@ GOMP_single_start (void)
if (__builtin_expect (team == NULL, 0))
return true;
+ /* In SIMT mode a gomp_thread is a whole wavefront: one lane counts the
+ construct and competes for it, and only that lane can win. In a nested
+ team each lane is a team of one, so every lane wins. */
+ if (GOMP_SIMT_LANE_TEAMS_P (thr->ts.level))
+ return true;
+ if (!FIRST_LANE_P)
+ return false;
+
single_count = thr->ts.single_count++;
return __sync_bool_compare_and_swap (&team->single_count, single_count,
single_count + 1L);
@@ -68,6 +76,14 @@ GOMP_single_start_with_end (void)
if (__builtin_expect (team == NULL, 0))
return true;
+ /* In SIMT mode a gomp_thread is a whole wavefront: one lane counts the
+ construct and competes for it, and only that lane can win. In a nested
+ team each lane is a team of one, so every lane wins. */
+ if (GOMP_SIMT_LANE_TEAMS_P (thr->ts.level))
+ return true;
+ if (!FIRST_LANE_P)
+ return false;
+
single_count = thr->ts.single_count++;
return __sync_bool_compare_and_swap (&team->single_count, single_count,
single_count + 1L);
diff --git a/libgomp/task.c b/libgomp/task.c
index 9ff300d576a35..de0c4f473804f 100644
--- a/libgomp/task.c
+++ b/libgomp/task.c
@@ -2241,10 +2241,9 @@ GOMP_taskyield (void)
}
static inline struct gomp_taskgroup *
-gomp_taskgroup_init (struct gomp_taskgroup *prev)
+gomp_taskgroup_init_at (struct gomp_taskgroup *taskgroup,
+ struct gomp_taskgroup *prev)
{
- struct gomp_taskgroup *taskgroup
- = gomp_malloc (sizeof (struct gomp_taskgroup));
taskgroup->prev = prev;
priority_queue_init (&taskgroup->taskgroup_queue);
taskgroup->reductions = prev ? prev->reductions : NULL;
@@ -2256,6 +2255,13 @@ gomp_taskgroup_init (struct gomp_taskgroup *prev)
return taskgroup;
}
+static inline struct gomp_taskgroup *
+gomp_taskgroup_init (struct gomp_taskgroup *prev)
+{
+ return gomp_taskgroup_init_at (gomp_malloc (sizeof (struct gomp_taskgroup)),
+ prev);
+}
+
void
GOMP_taskgroup_start (void)
{
@@ -2447,9 +2453,19 @@ GOMP_taskgroup_end (void)
free (taskgroup);
}
+/* NTHREADS counts OpenMP threads, so in SIMT mode it is lanes at level 1.
+
+ With LANES, every active lane registers its own reductions at once, and
+ the same lanes unregister them at once. The private copies of all the
+ lanes are then allocated together, and data[3], the allocator, which is
+ otherwise unused, is set to GOMP_REDUCTION_LANES_ALLOCATOR to tell
+ GOMP_taskgroup_reduction_unregister to free them together. */
+
+#define GOMP_REDUCTION_LANES_ALLOCATOR ((uintptr_t) -2)
+
static inline __attribute__((always_inline)) void
gomp_reduction_register (uintptr_t *data, uintptr_t *old, uintptr_t *orig,
- unsigned nthreads)
+ unsigned nthreads, bool lanes)
{
size_t total_cnt = 0;
uintptr_t *d = data;
@@ -2469,7 +2485,14 @@ gomp_reduction_register (uintptr_t *data, uintptr_t
*old, uintptr_t *orig,
{
size_t sz = d[1] * nthreads;
/* Should use omp_alloc if d[3] is not -1. */
- void *ptr = gomp_aligned_alloc (d[2], sz);
+ void *ptr;
+ if (lanes)
+ {
+ ptr = gomp_aligned_alloc_lanes (d[2], sz);
+ d[3] = GOMP_REDUCTION_LANES_ALLOCATOR;
+ }
+ else
+ ptr = gomp_aligned_alloc (d[2], sz);
memset (ptr, '\0', sz);
d[2] = (uintptr_t) ptr;
d[6] = d[2] + sz;
@@ -2535,6 +2558,9 @@ gomp_reduction_register (uintptr_t *data, uintptr_t *old,
uintptr_t *orig,
static void
gomp_create_artificial_team (void)
{
+ if (!FIRST_LANE_P)
+ return;
+
struct gomp_thread *thr = gomp_thread ();
struct gomp_task_icv *icv;
struct gomp_team *team = gomp_new_team (1);
@@ -2606,9 +2632,10 @@ GOMP_taskgroup_reduction_register (uintptr_t *data)
ialias_call (GOMP_taskgroup_start) ();
team = thr->ts.team;
}
- nthreads = team->nthreads;
+ nthreads = GOMP_TEAM_OMP_NTHR (team);
task = thr->task;
- gomp_reduction_register (data, task->taskgroup->reductions, NULL, nthreads);
+ gomp_reduction_register (data, task->taskgroup->reductions, NULL, nthreads,
+ false);
task->taskgroup->reductions = data;
}
@@ -2619,7 +2646,10 @@ GOMP_taskgroup_reduction_unregister (uintptr_t *data)
htab_free ((struct htab *) data[5]);
do
{
- gomp_aligned_free ((void *) d[2]);
+ if (d[3] == GOMP_REDUCTION_LANES_ALLOCATOR)
+ gomp_aligned_free_lanes ((void *) d[2]);
+ else
+ gomp_aligned_free ((void *) d[2]);
d = (uintptr_t *) d[4];
}
while (d && !d[5]);
@@ -2637,7 +2667,8 @@ GOMP_task_reduction_remap (size_t cnt, size_t cntorig,
void **ptrs)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_task *task = thr->task;
- unsigned id = thr->ts.team_id;
+ /* The private copies are per OpenMP thread. */
+ unsigned id = GOMP_TID_TO_OMP_TID (thr->ts.team_id);
uintptr_t *data = task->taskgroup->reductions;
uintptr_t *d;
struct htab *reduction_htab = (struct htab *) data[5];
@@ -2698,11 +2729,18 @@ GOMP_task_reduction_remap (size_t cnt, size_t cntorig,
void **ptrs)
}
}
+/* In SIMT mode, every lane may start a team with reductions at once, and
+ those lanes finish at once, so allocate their memory together. The
+ caller frees the taskgroup with gomp_aligned_free_lanes. */
+
struct gomp_taskgroup *
gomp_parallel_reduction_register (uintptr_t *data, unsigned nthreads)
{
- struct gomp_taskgroup *taskgroup = gomp_taskgroup_init (NULL);
- gomp_reduction_register (data, NULL, NULL, nthreads);
+ struct gomp_taskgroup *taskgroup
+ = gomp_aligned_alloc_lanes (__alignof__ (struct gomp_taskgroup),
+ sizeof (struct gomp_taskgroup));
+ gomp_taskgroup_init_at (taskgroup, NULL);
+ gomp_reduction_register (data, NULL, NULL, nthreads, SIMT_MODE_P);
taskgroup->reductions = data;
return taskgroup;
}
@@ -2713,8 +2751,9 @@ gomp_workshare_task_reduction_register (uintptr_t *data,
uintptr_t *orig)
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
struct gomp_task *task = thr->task;
- unsigned nthreads = team->nthreads;
- gomp_reduction_register (data, task->taskgroup->reductions, orig, nthreads);
+ unsigned nthreads = GOMP_TEAM_OMP_NTHR (team);
+ gomp_reduction_register (data, task->taskgroup->reductions, orig, nthreads,
+ false);
task->taskgroup->reductions = data;
}
diff --git a/libgomp/team.c b/libgomp/team.c
index 3d38b7f958732..afcda9e0b7456 100644
--- a/libgomp/team.c
+++ b/libgomp/team.c
@@ -284,6 +284,19 @@ gomp_new_team (unsigned nthreads)
struct gomp_team *team;
int i;
+ /* In SIMT mode every lane calls this, but the team belongs to the wave, so
+ only the first lane gets it; the others get NULL. gomp_team_start
+ ignores the other lanes. */
+ if (!FIRST_LANE_P)
+ return NULL;
+
+#ifdef LIBGOMP_SIMT
+ /* NTHREADS counts OpenMP threads, but the team's barrier and per-thread
+ arrays are per wave. */
+ unsigned omp_nthreads = nthreads;
+ nthreads = OMP_NTHR_TO_GOMP_NTHR (nthreads);
+#endif
+
team = get_last_team (nthreads);
if (team == NULL)
{
@@ -332,6 +345,9 @@ gomp_new_team (unsigned nthreads)
}
team->work_share_chunk = 8;
+#ifdef LIBGOMP_SIMT
+ team->omp_nthreads = omp_nthreads;
+#endif
#ifdef HAVE_SYNC_BUILTINS
team->single_count = 0;
#endif
@@ -1271,6 +1287,9 @@ gomp_team_start (void (*fn) (void *), void *data,
unsigned nthreads,
void
gomp_team_end (void)
{
+ if (!FIRST_LANE_P)
+ return;
+
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
unsigned team_id = thr->ts.team_id;
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-ancestor-thread-num-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-ancestor-thread-num-amdgcn-1.c
new file mode 100644
index 0000000000000..e4159fa21f2e3
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-ancestor-thread-num-amdgcn-1.c
@@ -0,0 +1,62 @@
+/* Check omp_get_ancestor_thread_num and omp_get_team_size in a parallel
+ region on an AMD GCN device in "simt" offload mode, where each lane of a
+ wavefront is an OpenMP thread. The initial thread (level 0) is not a
+ SIMT team, so every lane must see ancestor thread 0 there, not its own
+ lane number. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define MAXT 1024
+
+int
+main (void)
+{
+ int seen[MAXT], bad = 0, nthr = 0;
+
+ for (int t = 0; t < MAXT; t++)
+ seen[t] = 0;
+
+ #pragma omp target teams num_teams(1) map(tofrom: seen, bad, nthr)
+ #pragma omp parallel
+ {
+ int t = omp_get_thread_num ();
+ int n = omp_get_num_threads ();
+
+ #pragma omp single
+ nthr = n;
+
+ if (t < 0 || t >= MAXT
+ || omp_get_level () != 1
+ || omp_get_ancestor_thread_num (-1) != -1
+ || omp_get_ancestor_thread_num (0) != 0
+ || omp_get_ancestor_thread_num (1) != t
+ || omp_get_ancestor_thread_num (2) != -1
+ || omp_get_team_size (0) != 1
+ || omp_get_team_size (1) != n
+ || omp_get_team_size (2) != -1)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ else
+ {
+ #pragma omp atomic
+ seen[t]++;
+ }
+ }
+
+ if (nthr < 1 || nthr > MAXT)
+ abort ();
+ if (bad != 0)
+ abort ();
+ for (int t = 0; t < MAXT; t++)
+ if (seen[t] != (t < nthr))
+ abort ();
+
+ return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/simt-idle-waves-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-idle-waves-amdgcn-1.c
new file mode 100644
index 0000000000000..0407ff952962d
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-idle-waves-amdgcn-1.c
@@ -0,0 +1,44 @@
+/* Check that the wavefronts of an AMD GCN device in "simt" offload mode that
+ are not part of the current team survive many wake-ups with no work. A
+ GCN barrier stops every wavefront of the work-group, so the barriers of a
+ team smaller than the launch also release the idle wavefronts, which used
+ to give up with "team master not responding" after 100 of them. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define N 300
+
+int
+main (void)
+{
+ int count = 0, nthr = 0;
+
+ /* A team of two wavefronts; the others stay idle. */
+ #pragma omp target teams num_teams(1) map(tofrom: count, nthr)
+ {
+ #pragma omp parallel num_threads(100)
+ {
+ for (int i = 0; i < N; i++)
+ {
+ #pragma omp barrier
+ }
+ if (omp_get_thread_num () == 0)
+ nthr = omp_get_num_threads ();
+ }
+ for (int i = 0; i < N; i++)
+ {
+ #pragma omp parallel num_threads(100)
+ if (omp_get_thread_num () == 0)
+ count++;
+ }
+ }
+ if (count != N || nthr != 100)
+ abort ();
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-loop-ull-static-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-loop-ull-static-amdgcn-1.c
new file mode 100644
index 0000000000000..6f6ae9f04196e
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-loop-ull-static-amdgcn-1.c
@@ -0,0 +1,77 @@
+/* Check the static schedule of "unsigned long long" loops on an AMD GCN
+ device in "simt" offload mode, where each lane of a wavefront is an
+ OpenMP thread. libgomp's static schedule for these loops is used by
+ ordered loops and by schedule(runtime). Every iteration must run exactly
+ once, with a team of one wavefront and with several. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define N 3001
+/* Above LONG_MAX, so that the compiler uses the GOMP_loop_ull_* entry
+ points. */
+#define BASE (~0ULL - (1ULL << 33))
+
+int
+main (void)
+{
+ int cnt[N], bad = 0;
+
+ for (int nt = 64; nt <= 1024; nt *= 16)
+ for (int k = 0; k < 4; k++)
+ {
+ for (int i = 0; i < N; i++)
+ cnt[i] = 0;
+
+ #pragma omp target map(tofrom: cnt, bad) firstprivate(nt, k)
+ {
+ if (k == 2)
+ omp_set_schedule (omp_sched_static, 0);
+ else if (k == 3)
+ omp_set_schedule (omp_sched_static, 3);
+
+ #pragma omp parallel num_threads(nt)
+ {
+ if (k == 0)
+ {
+ #pragma omp for ordered schedule(static)
+ for (unsigned long long i = BASE; i < BASE + N; i++)
+ {
+ #pragma omp atomic
+ cnt[i - BASE]++;
+ }
+ }
+ else if (k == 1)
+ {
+ #pragma omp for ordered schedule(static, 2)
+ for (unsigned long long i = BASE + N - 1; i >= BASE; i--)
+ {
+ #pragma omp atomic
+ cnt[i - BASE]++;
+ }
+ }
+ else
+ {
+ #pragma omp for schedule(runtime)
+ for (unsigned long long i = BASE; i < BASE + N; i++)
+ {
+ #pragma omp atomic
+ cnt[i - BASE]++;
+ }
+ }
+ }
+ }
+
+ for (int i = 0; i < N; i++)
+ if (cnt[i] != 1)
+ bad++;
+ if (bad)
+ abort ();
+ }
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-nested-active-level-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-active-level-amdgcn-1.c
new file mode 100644
index 0000000000000..18706a87bbbf4
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-active-level-amdgcn-1.c
@@ -0,0 +1,66 @@
+/* Check the active level on an AMD GCN device in "simt" offload mode, where
+ each lane of a wavefront is an OpenMP thread. A team of one wavefront
+ has 64 threads, so it is active, and a parallel region nested inside it
+ must get one thread. Repeat the nested region to check that no other
+ wavefronts are woken for it. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define N 100
+
+int
+main (void)
+{
+ for (int k = 0; k < 2; k++)
+ {
+ int nt = k ? 1024 : 64;
+ int nthr = 0, bad = 0;
+
+ #pragma omp target teams num_teams(1) map(tofrom: nthr, bad) \
+ firstprivate(nt)
+ #pragma omp parallel num_threads(nt)
+ {
+ int t = omp_get_thread_num ();
+ int b = 0;
+
+ #pragma omp single
+ nthr = omp_get_num_threads ();
+
+ if (!omp_in_parallel () || omp_get_active_level () != 1
+ || omp_get_level () != 1)
+ b++;
+
+ for (int i = 0; i < N; i++)
+ {
+ int r = 0, n = 0, lvl = 0, alvl = 0;
+
+ #pragma omp parallel reduction(+: r, n)
+ {
+ r += t + i;
+ n++;
+ lvl = omp_get_level ();
+ alvl = omp_get_active_level ();
+ }
+
+ if (r != t + i || n != 1 || lvl != 2 || alvl != 1)
+ b++;
+ }
+
+ if (b)
+ {
+ #pragma omp atomic
+ bad += b;
+ }
+ }
+
+ if (nthr != nt || bad != 0)
+ abort ();
+ }
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-amdgcn-1.c
new file mode 100644
index 0000000000000..f5f062fa27b3a
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-amdgcn-1.c
@@ -0,0 +1,86 @@
+/* Check a parallel region nested inside a parallel region on an AMD GCN
+ device in "simt" offload mode, where each lane of a wavefront is an
+ OpenMP thread. The nested region is inactive, so each outer thread
+ becomes a team of one: it must run the body once, with the right thread
+ numbers and team sizes at both levels, and must run "single" bodies and
+ all the iterations of a loop itself. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define MAXT 1024
+#define N 100
+
+int
+main (void)
+{
+ int count[MAXT], sgl[MAXT], iters[MAXT], bad = 0, nthr = 0;
+
+ for (int t = 0; t < MAXT; t++)
+ count[t] = sgl[t] = iters[t] = 0;
+
+ #pragma omp target teams num_teams(1) \
+ map(tofrom: count, sgl, iters, bad, nthr)
+ #pragma omp parallel
+ {
+ int t = omp_get_thread_num ();
+ int n = omp_get_num_threads ();
+
+ #pragma omp single
+ nthr = n;
+
+ if (t < 0 || t >= MAXT)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ else
+ {
+ #pragma omp parallel
+ {
+ #pragma omp atomic
+ count[t]++;
+
+ if (omp_get_level () != 2
+ || omp_get_active_level () != 1
+ || omp_get_num_threads () != 1
+ || omp_get_thread_num () != 0
+ || omp_get_ancestor_thread_num (0) != 0
+ || omp_get_ancestor_thread_num (1) != t
+ || omp_get_ancestor_thread_num (2) != 0
+ || omp_get_team_size (0) != 1
+ || omp_get_team_size (1) != n
+ || omp_get_team_size (2) != 1)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+
+ #pragma omp single
+ sgl[t]++;
+
+ #pragma omp barrier
+
+ #pragma omp for
+ for (int i = 0; i < N; i++)
+ iters[t]++;
+ }
+ }
+ }
+
+ if (nthr < 1 || nthr > MAXT)
+ abort ();
+ if (bad != 0)
+ abort ();
+ for (int t = 0; t < MAXT; t++)
+ if (count[t] != (t < nthr)
+ || sgl[t] != (t < nthr)
+ || iters[t] != (t < nthr ? N : 0))
+ abort ();
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-for-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-for-amdgcn-1.c
new file mode 100644
index 0000000000000..84277b9b0e706
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-parallel-for-amdgcn-1.c
@@ -0,0 +1,132 @@
+/* Check a combined "parallel for" nested inside a parallel region on an
+ AMD GCN device in "simt" offload mode, where each lane of a wavefront is
+ an OpenMP thread. The nested region is inactive, so each outer thread
+ is a team of one and must run all the iterations of its own loop, whose
+ bounds differ between the lanes of a wavefront. A loop with constant
+ bounds uses the combined GOMP_parallel_loop_* entry points instead. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+/* Each nested team, and each nested loop's per-lane ranges, comes from the
+ team arena and is not returned to it, so the default arena runs out. */
+/* { dg-set-target-env-var GCN_TEAM_ARENA_SIZE "4194304" } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define MAXT 1024
+#define NSCHED 8
+#define CONSTN 17
+
+/* The loop for outer thread T runs from T % 7 to T % 7 + T % 13 + 5, so
+ both bounds vary. */
+#define LO(T) ((T) % 7)
+#define HI(T) ((T) % 7 + (T) % 13 + 5)
+
+int
+main (void)
+{
+ int iters[NSCHED][MAXT], sum[NSCHED][MAXT], bad = 0, nthr = 0;
+
+ for (int s = 0; s < NSCHED; s++)
+ for (int t = 0; t < MAXT; t++)
+ iters[s][t] = sum[s][t] = 0;
+
+ #pragma omp target teams num_teams(1) map(tofrom: iters, sum, bad, nthr)
+ #pragma omp parallel
+ {
+ int t = omp_get_thread_num ();
+
+ #pragma omp single
+ nthr = omp_get_num_threads ();
+
+ if (t < 0 || t >= MAXT)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ else
+ {
+ int lo = LO (t), hi = HI (t);
+
+ #pragma omp parallel for schedule(static)
+ for (int i = lo; i < hi; i++)
+ {
+ iters[0][t]++;
+ sum[0][t] += i;
+ }
+
+ #pragma omp parallel for schedule(static, 2)
+ for (int i = lo; i < hi; i++)
+ {
+ iters[1][t]++;
+ sum[1][t] += i;
+ }
+
+ #pragma omp parallel for schedule(dynamic)
+ for (int i = lo; i < hi; i++)
+ {
+ iters[2][t]++;
+ sum[2][t] += i;
+ }
+
+ #pragma omp parallel for schedule(dynamic, 3)
+ for (int i = lo; i < hi; i++)
+ {
+ iters[3][t]++;
+ sum[3][t] += i;
+ }
+
+ #pragma omp parallel for schedule(guided)
+ for (int i = hi - 1; i >= lo; i--)
+ {
+ iters[4][t]++;
+ sum[4][t] += i;
+ }
+
+ #pragma omp parallel for schedule(runtime)
+ for (int i = lo; i < hi; i += 2)
+ {
+ iters[5][t]++;
+ sum[5][t] += i;
+ }
+
+ #pragma omp parallel for schedule(dynamic, 2)
+ for (int i = 0; i < CONSTN; i++)
+ {
+ iters[6][t]++;
+ sum[6][t] += i;
+ }
+
+ unsigned long long ulo = lo, uhi = hi;
+ #pragma omp parallel for schedule(dynamic)
+ for (unsigned long long i = ulo; i < uhi; i++)
+ {
+ iters[7][t]++;
+ sum[7][t] += i;
+ }
+ }
+ }
+
+ if (nthr < 1 || nthr > MAXT)
+ abort ();
+ if (bad != 0)
+ abort ();
+ for (int t = 0; t < MAXT; t++)
+ for (int s = 0; s < NSCHED; s++)
+ {
+ int n = 0, total = 0;
+ if (t < nthr)
+ for (int i = s == 6 ? 0 : LO (t); i < (s == 6 ? CONSTN : HI (t));
+ i += (s == 5 ? 2 : 1))
+ {
+ n++;
+ total += i;
+ }
+ if (iters[s][t] != n || sum[s][t] != total)
+ abort ();
+ }
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-nested-sections-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-sections-amdgcn-1.c
new file mode 100644
index 0000000000000..b0c2ecaf85d52
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-nested-sections-amdgcn-1.c
@@ -0,0 +1,124 @@
+/* Check "sections" nested inside a parallel region on an AMD GCN device in
+ "simt" offload mode, where each lane of a wavefront is an OpenMP thread.
+ The nested region is inactive, so each outer thread is a team of one and
+ must run every section itself, exactly once. This covers the separate
+ GOMP_sections_start/next entry points, the combined GOMP_parallel_sections,
+ consecutive constructs with nowait, and a reduction. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+/* Each nested team, and each nested construct's per-lane ranges, comes from
+ the team arena and is not returned to it, so the default arena runs out. */
+/* { dg-set-target-env-var GCN_TEAM_ARENA_SIZE "4194304" } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define MAXT 1024
+#define NCASE 5
+
+int
+main (void)
+{
+ int mask[NCASE][MAXT], cnt[NCASE][MAXT], red[MAXT], bad = 0, nthr = 0;
+
+ for (int c = 0; c < NCASE; c++)
+ for (int t = 0; t < MAXT; t++)
+ mask[c][t] = cnt[c][t] = red[t] = 0;
+
+ #pragma omp target teams num_teams(1) map(tofrom: mask, cnt, red, bad, nthr)
+ #pragma omp parallel
+ {
+ int t = omp_get_thread_num ();
+
+ #pragma omp single
+ nthr = omp_get_num_threads ();
+
+ if (t < 0 || t >= MAXT)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ else
+ {
+ #pragma omp parallel
+ {
+ #pragma omp sections
+ {
+ #pragma omp section
+ { mask[0][t] |= 1; cnt[0][t]++; }
+ #pragma omp section
+ { mask[0][t] |= 2; cnt[0][t]++; }
+ #pragma omp section
+ { mask[0][t] |= 4; cnt[0][t]++; }
+ }
+ }
+
+ #pragma omp parallel sections
+ {
+ #pragma omp section
+ { mask[1][t] |= 1; cnt[1][t]++; }
+ #pragma omp section
+ { mask[1][t] |= 2; cnt[1][t]++; }
+ #pragma omp section
+ { mask[1][t] |= 4; cnt[1][t]++; }
+ #pragma omp section
+ { mask[1][t] |= 8; cnt[1][t]++; }
+ }
+
+ #pragma omp parallel
+ {
+ #pragma omp sections nowait
+ {
+ #pragma omp section
+ { mask[2][t] |= 1; cnt[2][t]++; }
+ #pragma omp section
+ { mask[2][t] |= 2; cnt[2][t]++; }
+ }
+ #pragma omp sections nowait
+ {
+ #pragma omp section
+ { mask[3][t] |= 1; cnt[3][t]++; }
+ #pragma omp section
+ { mask[3][t] |= 2; cnt[3][t]++; }
+ #pragma omp section
+ { mask[3][t] |= 4; cnt[3][t]++; }
+ #pragma omp section
+ { mask[3][t] |= 8; cnt[3][t]++; }
+ #pragma omp section
+ { mask[3][t] |= 16; cnt[3][t]++; }
+ }
+ }
+
+ int r = 0;
+ #pragma omp parallel sections reduction(+: r)
+ {
+ #pragma omp section
+ { r += t; mask[4][t] |= 1; cnt[4][t]++; }
+ #pragma omp section
+ { r += 1000; mask[4][t] |= 2; cnt[4][t]++; }
+ }
+ red[t] = r;
+ }
+ }
+
+ static const int full[NCASE] = { 7, 15, 3, 31, 3 };
+ static const int count[NCASE] = { 3, 4, 2, 5, 2 };
+
+ if (nthr < 1 || nthr > MAXT)
+ abort ();
+ if (bad != 0)
+ abort ();
+ for (int t = 0; t < MAXT; t++)
+ {
+ for (int c = 0; c < NCASE; c++)
+ if (mask[c][t] != (t < nthr ? full[c] : 0)
+ || cnt[c][t] != (t < nthr ? count[c] : 0))
+ abort ();
+ if (red[t] != (t < nthr ? t + 1000 : 0))
+ abort ();
+ }
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-1.c
new file mode 100644
index 0000000000000..f5d0c2910659a
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-1.c
@@ -0,0 +1,88 @@
+/* Check "parallel" with task reductions (GOMP_parallel_reductions) on an
+ AMD GCN device in "simt" offload mode, where each lane of a wavefront is
+ an OpenMP thread. At the top level, the private copies of every lane
+ must be combined. Nested inside a parallel region, each outer thread is
+ a team of one with its own reductions, which every lane allocates at
+ once; this needs newlib's SIMT malloc. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define MAXT 1024
+
+int
+main (void)
+{
+ int ires[MAXT], top_n = 0, nthr = 0, bad = 0;
+ long top_r = 0;
+ double dres[MAXT], top_d = 0;
+
+ for (int t = 0; t < MAXT; t++)
+ {
+ ires[t] = 0;
+ dres[t] = 0;
+ }
+
+ #pragma omp target map(tofrom: top_r, top_d, top_n)
+ #pragma omp parallel reduction(task, +: top_r, top_d)
+ {
+ int t = omp_get_thread_num ();
+ top_r += t + 1;
+ top_d += 0.5;
+ #pragma omp single
+ top_n = omp_get_num_threads ();
+ }
+
+ if (top_n < 1 || top_n > MAXT
+ || top_r != (long) top_n * (top_n + 1) / 2
+ || top_d != top_n * 0.5)
+ abort ();
+
+ #pragma omp target teams num_teams(1) map(tofrom: ires, dres, nthr, bad)
+ #pragma omp parallel
+ {
+ int t = omp_get_thread_num ();
+
+ #pragma omp single
+ nthr = omp_get_num_threads ();
+
+ if (t < 0 || t >= MAXT)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ else
+ {
+ int r = 100;
+ double d = 0.25;
+
+ #pragma omp parallel reduction(task, +: r) reduction(task, *: d)
+ {
+ if (omp_get_num_threads () != 1)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ r += t + 1;
+ d *= 4 + (t & 3);
+ }
+ ires[t] = r;
+ dres[t] = d;
+ }
+ }
+
+ if (nthr < 1 || nthr > MAXT)
+ abort ();
+ if (bad != 0)
+ abort ();
+ for (int t = 0; t < MAXT; t++)
+ if (ires[t] != (t < nthr ? t + 101 : 0)
+ || dres[t] != (t < nthr ? 0.25 * (4 + (t & 3)) : 0))
+ abort ();
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-2.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-2.c
new file mode 100644
index 0000000000000..9135efcafb088
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-parallel-reductions-amdgcn-2.c
@@ -0,0 +1,58 @@
+/* Check the size of the private copies for "parallel" with task reductions
+ nested inside a parallel region on an AMD GCN device in "simt" offload
+ mode. Each outer thread (a lane) is a team of one, so it needs one copy,
+ not one per lane of its wavefront. With 64 copies per lane the 1024
+ threads would need 256 MiB, more than the default device heap. Every
+ lane allocates its hash table at once; this needs newlib's SIMT malloc. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define NT 1024
+#define N 1024
+
+int buf[NT][N];
+#pragma omp declare target (buf)
+
+int
+main (void)
+{
+ int nthr = 0, bad = 0;
+
+ #pragma omp target teams num_teams(1) map(tofrom: nthr, bad)
+ #pragma omp parallel num_threads(NT)
+ {
+ int t = omp_get_thread_num ();
+ int *p = buf[t];
+ int b = 0;
+
+ #pragma omp single
+ nthr = omp_get_num_threads ();
+
+ for (int i = 0; i < N; i++)
+ p[i] = 1;
+
+ #pragma omp parallel reduction(task, +: p[:N])
+ for (int i = 0; i < N; i++)
+ p[i] += t + i;
+
+ for (int i = 0; i < N; i++)
+ if (p[i] != 1 + t + i)
+ b++;
+
+ if (b)
+ {
+ #pragma omp atomic
+ bad += b;
+ }
+ }
+
+ if (nthr != NT || bad != 0)
+ abort ();
+
+ return 0;
+}
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-task-reduction-remap-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-task-reduction-remap-amdgcn-1.c
new file mode 100644
index 0000000000000..c2c6bf0790b30
--- /dev/null
+++
b/libgomp/testsuite/libgomp.c-c++-common/simt-task-reduction-remap-amdgcn-1.c
@@ -0,0 +1,48 @@
+/* Check "in_reduction" on an AMD GCN device in "simt" offload mode, where
+ each lane of a wavefront is an OpenMP thread. GOMP_task_reduction_remap
+ must give each thread its own private copy of a "parallel" task
+ reduction, so the lanes of a wavefront must not share one. The tasks are
+ undeferred; deferred tasks are not supported in this mode yet. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+int
+main (void)
+{
+ for (int nt = 64; nt <= 1024; nt *= 16)
+ {
+ long r = 0;
+ double d = 1;
+ int n = 0;
+
+ #pragma omp target map(tofrom: r, d, n) firstprivate(nt)
+ #pragma omp parallel num_threads(nt) reduction(task, +: r) \
+ reduction(task, *: d)
+ {
+ int t = omp_get_thread_num ();
+
+ #pragma omp single
+ n = omp_get_num_threads ();
+
+ #pragma omp task in_reduction(+: r) in_reduction(*: d) if(0)
+ {
+ r += t + 1;
+ d *= (t & 1) ? 2 : 0.5;
+ }
+ #pragma omp task in_reduction(+: r) if(0)
+ r += 1000;
+ }
+
+ if (n != nt
+ || r != (long) n * (n + 1) / 2 + 1000L * n
+ || d != 1)
+ abort ();
+ }
+
+ return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/simt-team-size-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-team-size-amdgcn-1.c
new file mode 100644
index 0000000000000..91bf9f0b56b15
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/simt-team-size-amdgcn-1.c
@@ -0,0 +1,134 @@
+/* Check teams whose size is not a multiple of the wavefront size on an AMD
+ GCN device in "simt" offload mode, where each lane of a wavefront is a
+ thread. The last wavefront of such a team is partial, and only the team's
+ threads may run the region. Also check "if (0)", a "thread_limit" that
+ is not a multiple of the wavefront size, and a nested region inside a
+ team of one. */
+
+/* { dg-do run } */
+/* { dg-require-effective-target offload_device_gcn } */
+/* { dg-additional-options "--param=offload-gpu-mode=simt" { target
offload_target_amdgcn } } */
+
+#include <stdlib.h>
+#include <omp.h>
+
+#define N 1000
+#define MAXT 256
+
+static const int sizes[] = { 1, 2, 10, 63, 64, 65, 100, 129, 200, 256 };
+
+int
+main (void)
+{
+ for (int k = 0; k < (int) (sizeof (sizes) / sizeof (sizes[0])); k++)
+ {
+ int nt = sizes[k];
+ int count = 0, nthr = 0, single = 0, bad = 0;
+ int seen[MAXT] = { 0 }, iters[N] = { 0 }, iters2[N] = { 0 };
+ long sum = 0;
+
+ #pragma omp target teams num_teams(1) firstprivate(nt) \
+ map(tofrom: count, nthr, single, bad, seen, iters, iters2, \
+ sum)
+ #pragma omp parallel num_threads(nt)
+ {
+ int t = omp_get_thread_num ();
+ #pragma omp atomic
+ count++;
+ if (t < MAXT)
+ {
+ #pragma omp atomic
+ seen[t]++;
+ }
+ if (omp_get_num_threads () != nt || omp_get_team_size (1) != nt)
+ {
+ #pragma omp atomic
+ bad++;
+ }
+ if (t == 0)
+ nthr = omp_get_num_threads ();
+ #pragma omp single
+ single++;
+ #pragma omp for
+ for (int i = 0; i < N; i++)
+ {
+ #pragma omp atomic
+ iters[i]++;
+ }
+ #pragma omp for schedule(static)
+ for (int i = 0; i < N; i++)
+ {
+ #pragma omp atomic
+ iters2[i]++;
+ }
+ #pragma omp for reduction(+: sum)
+ for (int i = 0; i < N; i++)
+ sum += i;
+ }
+
+ if (count != nt || nthr != nt || single != 1 || bad
+ || sum != (long) N * (N - 1) / 2)
+ abort ();
+ for (int t = 0; t < MAXT; t++)
+ if (seen[t] != (t < nt))
+ abort ();
+ for (int i = 0; i < N; i++)
+ if (iters[i] != 1 || iters2[i] != 1)
+ abort ();
+ }
+
+ /* "if (0)" runs one thread, and a nested region inside it is a team of
+ one too. */
+ int count = 0, nthr = 0, level = 0, active = -1, ncount = 0, nnthr = 0;
+ #pragma omp target teams num_teams(1) \
+ map(tofrom: count, nthr, level, active, ncount, nnthr)
+ #pragma omp parallel if (0)
+ {
+ #pragma omp atomic
+ count++;
+ nthr = omp_get_num_threads ();
+ active = omp_get_active_level ();
+ #pragma omp parallel num_threads(4)
+ {
+ #pragma omp atomic
+ ncount++;
+ nnthr = omp_get_num_threads ();
+ level = omp_get_level ();
+ }
+ }
+ if (count != 1 || nthr != 1 || active != 0
+ || ncount != 1 || nnthr != 1 || level != 2)
+ abort ();
+
+ /* "thread_limit" caps the default team size. */
+ count = 0;
+ nthr = 0;
+ #pragma omp target teams num_teams(1) thread_limit(100) \
+ map(tofrom: count, nthr)
+ #pragma omp parallel
+ {
+ #pragma omp atomic
+ count++;
+ if (omp_get_thread_num () == 0)
+ nthr = omp_get_num_threads ();
+ }
+ if (count != nthr || nthr < 1 || nthr > 100)
+ abort ();
+
+ /* Sections size the team by the number of sections. */
+ int s1 = 0, s2 = 0, s3 = 0;
+ #pragma omp target teams num_teams(1) map(tofrom: s1, s2, s3)
+ #pragma omp parallel sections
+ {
+ #pragma omp section
+ { s1++; }
+ #pragma omp section
+ { s2++; }
+ #pragma omp section
+ { s3++; }
+ }
+ if (s1 != 1 || s2 != 1 || s3 != 1)
+ abort ();
+
+ return 0;
+}
diff --git a/libgomp/work.c b/libgomp/work.c
index 280fa242fd530..615a67212df9c 100644
--- a/libgomp/work.c
+++ b/libgomp/work.c
@@ -134,6 +134,9 @@ gomp_init_work_share (struct gomp_work_share *ws, size_t
ordered,
ws->ordered_team_ids = ws->inline_ordered_team_ids;
gomp_ptrlock_init (&ws->next_ws, NULL);
ws->threads_completed = 0;
+#ifdef LIBGOMP_SIMT
+ ws->simt_lane_ranges = NULL;
+#endif
}
/* Do any needed destruction of gomp_work_share fields before it
@@ -145,6 +148,10 @@ gomp_fini_work_share (struct gomp_work_share *ws)
gomp_mutex_destroy (&ws->lock);
if (ws->ordered_team_ids != ws->inline_ordered_team_ids)
team_free (ws->ordered_team_ids);
+#ifdef LIBGOMP_SIMT
+ if (ws->simt_lane_ranges != NULL)
+ team_free (ws->simt_lane_ranges);
+#endif
gomp_ptrlock_destroy (&ws->next_ws);
}
@@ -183,8 +190,8 @@ free_work_share (struct gomp_team *team, struct
gomp_work_share *ws)
structure. In all cases the work_share lock is locked. Return true
if this was the first thread to reach this point. */
-bool
-gomp_work_share_start (size_t ordered)
+static bool
+gomp_work_share_start_1 (size_t ordered)
{
struct gomp_thread *thr = gomp_thread ();
struct gomp_team *team = thr->ts.team;
@@ -222,6 +229,20 @@ gomp_work_share_start (size_t ordered)
}
}
+/* In SIMT mode a gomp_thread is a whole wavefront, and the work share
+ belongs to the wavefront, so only one lane may take the ptrlock (a lane
+ that waited for another lane of its own wavefront would deadlock), but
+ every lane needs the result. */
+
+bool
+gomp_work_share_start (size_t ordered)
+{
+ bool first = false;
+ if (FIRST_LANE_P)
+ first = gomp_work_share_start_1 (ordered);
+ return SIMT_VOTE_ANY (first);
+}
+
/* The current thread is done with its current work sharing construct.
This version does imply a barrier at the end of the work-share. */
@@ -232,6 +253,10 @@ gomp_work_share_end (void)
struct gomp_team *team = thr->ts.team;
gomp_barrier_state_t bstate;
+ /* In SIMT mode the work share and barrier are per wavefront. */
+ if (!FIRST_LANE_P)
+ return;
+
/* Work sharing constructs can be orphaned. */
if (team == NULL)
{
@@ -268,7 +293,8 @@ gomp_work_share_end_cancel (void)
/* Cancellable work sharing constructs cannot be orphaned. */
bstate = gomp_barrier_wait_cancel_start (&team->barrier, thr->ts.team_id);
- if (gomp_barrier_last_thread (bstate))
+ /* In SIMT mode every lane of the last wavefront sees BAR_WAS_LAST. */
+ if (gomp_barrier_last_thread (bstate) && FIRST_LANE_P)
{
if (__builtin_expect (thr->ts.last_work_share != NULL, 1))
{
@@ -293,6 +319,10 @@ gomp_work_share_end_nowait (void)
struct gomp_work_share *ws = thr->ts.work_share;
unsigned completed;
+ /* In SIMT mode the work share is per wavefront. */
+ if (!FIRST_LANE_P)
+ return;
+
/* Work sharing constructs can be orphaned. */
if (team == NULL)
{
--
2.56.0