From: Andrew Stubbs <[email protected]>
The normal behaviour, when the chunk-size is not given explicitly, is to give
each thread a contiguous portion of the iteration space to work on. This works
for SIMT code, but gives horrible cache performance.
A chunk size of "1" makes the threads stride over the data, which means that
adjacent threads get adjacent data, in many cases, which is much more
cache-friendly.
This only applies to the "full" SIMT mode, not the "directed" mode where the
SIMT code does not look threaded to libgomp.
Assisted-by: Claude Opus 5.5
Claude generate the testcase and assisted with the code.
gcc/ChangeLog:
* omp-expand.cc (expand_omp_for): In "simt" offload mode, use a
chunk size of one for a static schedule without a schedule clause.
libgomp/ChangeLog:
* testsuite/libgomp.c-c++-common/simt-loop-default-schedule-amdgcn-1.c:
New test.
---
gcc/omp-expand.cc | 13 +++++
.../simt-loop-default-schedule-amdgcn-1.c | 57 +++++++++++++++++++
2 files changed, 70 insertions(+)
create mode 100644
libgomp/testsuite/libgomp.c-c++-common/simt-loop-default-schedule-amdgcn-1.c
diff --git a/gcc/omp-expand.cc b/gcc/omp-expand.cc
index 9304b16f4681a..7fec0d1c3240d 100644
--- a/gcc/omp-expand.cc
+++ b/gcc/omp-expand.cc
@@ -8482,6 +8482,19 @@ expand_omp_for (struct omp_region *region, gimple
*inner_stmt)
else if (fd.sched_kind == OMP_CLAUSE_SCHEDULE_STATIC
&& !fd.have_ordered)
{
+ /* In "simt" offload mode each lane of a wavefront is a thread. If
+ the schedule is not specified then it is implementation defined,
+ so give consecutive iterations to consecutive lanes, which lets
+ their memory accesses coalesce. */
+ if (param_offload_gpu_mode == OFFLOAD_GPU_MODE_SIMT
+ && fd.chunk_size == NULL
+ && gimple_omp_for_kind (fd.for_stmt) == GF_OMP_FOR_KIND_FOR
+ && !fd.non_rect
+ && !fd.have_scantemp
+ && !omp_find_clause (gimple_omp_for_clauses (fd.for_stmt),
+ OMP_CLAUSE_SCHEDULE)
+ && is_in_offload_region (region))
+ fd.chunk_size = integer_one_node;
if (fd.chunk_size == NULL)
expand_omp_for_static_nochunk (region, &fd, inner_stmt);
else
diff --git
a/libgomp/testsuite/libgomp.c-c++-common/simt-loop-default-schedule-amdgcn-1.c
b/libgomp/testsuite/libgomp.c-c++-common/simt-loop-default-schedule-amdgcn-1.c
new file mode 100644
index 0000000000000..95c95bf6a079e
--- /dev/null
+++
b/libgomp/testsuite/libgomp.c-c++-common/simt-loop-default-schedule-amdgcn-1.c
@@ -0,0 +1,57 @@
+/* Check the loop schedules on an AMD GCN device in "simt" offload mode,
+ where each lane of a wavefront is an OpenMP thread. Without a schedule
+ clause, consecutive iterations go to consecutive threads, so that the
+ lanes' memory accesses coalesce. An explicit schedule(static) still
+ gives each thread one contiguous block. */
+
+/* { 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
+
+int tid[2][N];
+
+int
+main (void)
+{
+ for (int k = 0; k < 2; k++)
+ {
+ int nt = k ? 1024 : 64;
+ int nthr = 0;
+
+ #pragma omp target teams num_teams(1) map(from: tid, nthr) \
+ firstprivate(nt)
+ #pragma omp parallel num_threads(nt)
+ {
+ int t = omp_get_thread_num ();
+
+ #pragma omp single
+ nthr = omp_get_num_threads ();
+
+ #pragma omp for
+ for (int i = 0; i < N; i++)
+ tid[0][i] = t;
+
+ #pragma omp for schedule(static)
+ for (int i = 0; i < N; i++)
+ tid[1][i] = t;
+ }
+
+ if (nthr != nt)
+ abort ();
+
+ int q = N / nthr, r = N % nthr;
+ for (int i = 0; i < N; i++)
+ {
+ int block = i < r * (q + 1) ? i / (q + 1) : r + (i - r * (q + 1)) / q;
+ if (tid[0][i] != i % nthr || tid[1][i] != block)
+ abort ();
+ }
+ }
+
+ return 0;
+}
--
2.56.0