| /* Check that offloaded composite simd collapse(n) loops get a SIMT region | |
| and compute the right result: partial last rows, inner trip counts below | |
| and above the vector length, thread shares that start in the middle of a | |
| row, shares that span many rows, collapse(3), a loop nested deeper than | |
| the collapse, a non-unit, a negative and a runtime inner step, schedule, | |
| dist_schedule, safelen and simdlen clauses, a target parallel construct, | |
| and iteration variables of several types. | |
| Each loop declares its iteration variables in the for-init, so that they | |
| are private, not implicitly lastprivate. Every result array has guard | |
| elements on both sides, which catch writes outside the iteration space. */ | |
| /* { dg-do run } */ | |
| /* { dg-additional-options "-O2" } */ | |
| /* { dg-xfail-if "--param=offload-gpu-mode=simt-directed needs the amdgcn SIMT series" { offload_target_amdgcn } } */ | |
| /* { dg-additional-options "--param=offload-gpu-mode=simt-directed" { target offload_target_amdgcn } } */ | |
| /* { dg-additional-options "-foffload-options=-fdump-tree-optimized" { target { offload_target_amdgcn || offload_target_nvptx } } } */ | |
| #include <stdlib.h> | |
| #define PAD 256 | |
| #define MAXN 8192 | |
| /* simt-collapse-2.c runs the same loops with num_teams and num_threads. */ | |
| #ifndef SIMT_CLAUSES | |
| #define SIMT_CLAUSES(TEAMS, THREADS) | |
| #endif | |
| int buf[MAXN + 2 * PAD]; | |
| static void | |
| clear (void) | |
| { | |
| for (int x = 0; x < MAXN + 2 * PAD; x++) | |
| buf[x] = -1; | |
| } | |
| /* BUF[PAD + I] must be EXPECT (I) for the I in [0, N), and -1 elsewhere. */ | |
| static void | |
| check (int n, int (*expect) (int)) | |
| { | |
| for (int x = 0; x < MAXN + 2 * PAD; x++) | |
| { | |
| int i = x - PAD; | |
| int want = (i >= 0 && i < n) ? expect (i) : -1; | |
| if (buf[x] != want) | |
| abort (); | |
| } | |
| } | |
| static int | |
| ident (int i) | |
| { | |
| return i * 7 + 3; | |
| } | |
| /* Inner trip count M, not a multiple of the vector length, and N * M not | |
| divisible by the number of threads, so shares start in the middle of a | |
| row. The bounds are runtime values, so one loop covers all shapes. | |
| TEAMS and THREADS are used only by simt-collapse-2.c. Shapes with more | |
| iterations than threads, whose shares span rows, are in | |
| simt-collapse-3.c. */ | |
| static void | |
| c2_gen (int n, int m, int teams, int threads) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) \ | |
| SIMT_CLAUSES (teams, threads) map(tofrom: buf) firstprivate(n, m) | |
| for (int i = 0; i < n; i++) | |
| for (int j = 0; j < m; j++) | |
| buf[PAD + i * m + j] = (i * m + j) * 7 + 3; | |
| check (n * m, ident); | |
| } | |
| /* Inner step S, known only at run time: the lanes must be offset by | |
| S * lane in the inner variable. As in c2_step3 below, the rows have | |
| holes. */ | |
| static int rstep_m, rstep_s; | |
| static int | |
| rstep_expect (int i) | |
| { | |
| return (i % rstep_m) % rstep_s == 0 ? ident (i) : -1; | |
| } | |
| static void | |
| c2_rstep (int n, int m, int s, int teams, int threads) | |
| { | |
| rstep_m = m; | |
| rstep_s = s; | |
| #pragma omp target teams distribute parallel for simd collapse(2) \ | |
| SIMT_CLAUSES (teams, threads) map(tofrom: buf) firstprivate(n, m, s) | |
| for (int i = 0; i < n; i++) | |
| for (int j = 0; j < m; j += s) | |
| buf[PAD + i * m + j] = (i * m + j) * 7 + 3; | |
| check (n * m, rstep_expect); | |
| } | |
| static void | |
| c2_default (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (int i = 0; i < 61; i++) | |
| for (int j = 0; j < 67; j++) | |
| buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3; | |
| check (61 * 67, ident); | |
| } | |
| /* Inner step 3: the lanes must be offset by 3 * lane in the inner variable, | |
| but by 1 * lane in the linear counter. The index has no division, which | |
| would take the one-lane fallback, so the rows have holes. */ | |
| static int | |
| step3_expect (int i) | |
| { | |
| return i % 3 == 0 ? ident (i) : -1; | |
| } | |
| static void | |
| c2_step3 (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) \ | |
| SIMT_CLAUSES (3, 5) map(tofrom: buf) | |
| for (int i = 0; i < 19; i++) | |
| for (int j = 0; j < 100; j += 3) | |
| buf[PAD + i * 102 + j] = (i * 102 + j) * 7 + 3; | |
| check (19 * 102, step3_expect); | |
| } | |
| /* Outer loop with a non-unit start and step. As above, the index has no | |
| division, so the rows have holes. */ | |
| static int | |
| outer_step_expect (int i) | |
| { | |
| return (i / 17) % 5 == 0 ? ident (i) : -1; | |
| } | |
| static void | |
| c2_outer_step (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) \ | |
| SIMT_CLAUSES (3, 5) map(tofrom: buf) | |
| for (int i = 5; i < 50; i += 5) | |
| for (int j = 0; j < 17; j++) | |
| buf[PAD + (i - 5) * 17 + j] = ((i - 5) * 17 + j) * 7 + 3; | |
| check (41 * 17, outer_step_expect); | |
| } | |
| /* Decreasing inner loop. */ | |
| static void | |
| c2_down (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) \ | |
| SIMT_CLAUSES (3, 5) map(tofrom: buf) | |
| for (int i = 0; i < 23; i++) | |
| for (int j = 40; j > 0; j--) | |
| buf[PAD + i * 40 + (40 - j)] = (i * 40 + (40 - j)) * 7 + 3; | |
| check (23 * 40, ident); | |
| } | |
| static void | |
| c3 (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(3) \ | |
| SIMT_CLAUSES (3, 5) map(tofrom: buf) | |
| for (int i = 0; i < 7; i++) | |
| for (int j = 0; j < 5; j++) | |
| for (int k = 0; k < 19; k++) | |
| buf[PAD + (i * 5 + j) * 19 + k] = ((i * 5 + j) * 19 + k) * 7 + 3; | |
| check (7 * 5 * 19, ident); | |
| } | |
| /* Empty and one-iteration nests. */ | |
| static void | |
| c2_empty (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (int i = 0; i < 0; i++) | |
| for (int j = 0; j < 10; j++) | |
| buf[PAD + i * 10 + j] = 0; | |
| check (0, ident); | |
| } | |
| static void | |
| c2_single (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (int i = 0; i < 1; i++) | |
| for (int j = 0; j < 1; j++) | |
| buf[PAD + i * 1 + j] = 3; | |
| check (1, ident); | |
| } | |
| /* Shares that span many rows, so the share end is reached before the end of | |
| the outer loops. */ | |
| static void | |
| c2_many_rows (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (int i = 0; i < 1000; i++) | |
| for (int j = 0; j < 7; j++) | |
| buf[PAD + i * 7 + j] = (i * 7 + j) * 7 + 3; | |
| check (1000 * 7, ident); | |
| } | |
| static void | |
| c3_many_rows (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(3) map(tofrom: buf) | |
| for (int i = 0; i < 50; i++) | |
| for (int j = 0; j < 7; j++) | |
| for (int k = 0; k < 5; k++) | |
| buf[PAD + (i * 7 + j) * 5 + k] = ((i * 7 + j) * 5 + k) * 7 + 3; | |
| check (50 * 7 * 5, ident); | |
| } | |
| /* A third loop that is not collapsed, which each lane runs in full. Its | |
| trip count P is a runtime value, so that it is not unrolled away. */ | |
| static void | |
| c2_inner (int n, int m, int p) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) \ | |
| map(tofrom: buf) firstprivate(n, m, p) | |
| for (int i = 0; i < n; i++) | |
| for (int j = 0; j < m; j++) | |
| for (int k = 0; k < p; k++) | |
| buf[PAD + (i * m + j) * p + k] = ((i * m + j) * p + k) * 7 + 3; | |
| check (n * m * p, ident); | |
| } | |
| /* A 61x67 nest with the clauses in the second argument: shares handed out | |
| by other schedules, and safelen or simdlen, which limit the number of | |
| active lanes. */ | |
| #define DO_PRAGMA(X) _Pragma (#X) | |
| #define C2_CLAUSES(NAME, ...) \ | |
| static void \ | |
| NAME (void) \ | |
| { \ | |
| DO_PRAGMA (omp target teams distribute parallel for simd collapse(2) \ | |
| map(tofrom: buf) __VA_ARGS__) \ | |
| for (int i = 0; i < 61; i++) \ | |
| for (int j = 0; j < 67; j++) \ | |
| buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3; \ | |
| check (61 * 67, ident); \ | |
| } | |
| C2_CLAUSES (c2_sched_static_1, schedule(static, 1)) | |
| C2_CLAUSES (c2_dist_static_7, dist_schedule(static, 7)) | |
| C2_CLAUSES (c2_dist_static_500, dist_schedule(static, 500)) | |
| C2_CLAUSES (c2_safelen_2, safelen(2)) | |
| C2_CLAUSES (c2_safelen_8, safelen(8)) | |
| C2_CLAUSES (c2_safelen_33, safelen(33)) | |
| C2_CLAUSES (c2_simdlen_4, simdlen(4)) | |
| static void | |
| c3_safelen_8 (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(3) \ | |
| safelen(8) map(tofrom: buf) | |
| for (int i = 0; i < 9; i++) | |
| for (int j = 0; j < 13; j++) | |
| for (int k = 0; k < 19; k++) | |
| buf[PAD + (i * 13 + j) * 19 + k] = ((i * 13 + j) * 19 + k) * 7 + 3; | |
| check (9 * 13 * 19, ident); | |
| } | |
| /* Without teams. */ | |
| static void | |
| c2_parallel (void) | |
| { | |
| #pragma omp target parallel for simd collapse(2) map(tofrom: buf) | |
| for (int i = 0; i < 61; i++) | |
| for (int j = 0; j < 67; j++) | |
| buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3; | |
| check (61 * 67, ident); | |
| } | |
| /* Iteration variables of other types. For the decreasing loops, the | |
| unsigned division path computes the inner trip count. */ | |
| static void | |
| c2_unsigned (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (unsigned i = 0; i < 61; i++) | |
| for (unsigned j = 0; j < 67; j++) | |
| buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3; | |
| check (61 * 67, ident); | |
| } | |
| static void | |
| c2_unsigned_down (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (unsigned i = 0; i < 61; i++) | |
| for (unsigned j = 67; j > 0; j--) | |
| buf[PAD + i * 67 + (67 - j)] = (i * 67 + (67 - j)) * 7 + 3; | |
| check (61 * 67, ident); | |
| } | |
| /* J is 133, 131, ..., 1. The index uses a shift, because a division would | |
| take the one-lane fallback. */ | |
| static void | |
| c2_unsigned_down_step2 (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (unsigned i = 0; i < 61; i++) | |
| for (unsigned j = 133; j > 0; j -= 2) | |
| buf[PAD + i * 67 + ((133 - j) >> 1)] | |
| = (i * 67 + ((133 - j) >> 1)) * 7 + 3; | |
| check (61 * 67, ident); | |
| } | |
| static void | |
| c2_uchar_int (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (unsigned char i = 0; i < 61; i++) | |
| for (int j = -67; j < 0; j++) | |
| buf[PAD + i * 67 + (j + 67)] = (i * 67 + (j + 67)) * 7 + 3; | |
| check (61 * 67, ident); | |
| } | |
| /* Inner values outside the range of int. */ | |
| static void | |
| c2_long_long_inner (void) | |
| { | |
| #pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: buf) | |
| for (int i = 0; i < 61; i++) | |
| for (long long j = 0x100000000LL; j < 0x100000000LL + 67; j++) | |
| buf[PAD + i * 67 + (int) (j - 0x100000000LL)] | |
| = (i * 67 + (int) (j - 0x100000000LL)) * 7 + 3; | |
| check (61 * 67, ident); | |
| } | |
| #define RUN(F) do { clear (); F; } while (0) | |
| int | |
| main (void) | |
| { | |
| RUN (c2_gen (37, 23, 3, 5)); | |
| RUN (c2_gen (41, 3, 3, 5)); | |
| RUN (c2_gen (53, 1, 2, 7)); | |
| RUN (c2_gen (11, 150, 3, 5)); | |
| RUN (c2_gen (9, 128, 2, 4)); | |
| RUN (c2_gen (13, 29, 1, 1)); | |
| RUN (c2_gen (7, 31, 8, 16)); | |
| RUN (c2_gen (3, 200, 3, 5)); | |
| RUN (c2_rstep (7, 65, 3, 3, 5)); | |
| RUN (c2_rstep (11, 200, 7, 2, 4)); | |
| RUN (c2_default ()); | |
| RUN (c2_step3 ()); | |
| RUN (c2_outer_step ()); | |
| RUN (c2_down ()); | |
| RUN (c3 ()); | |
| RUN (c2_empty ()); | |
| RUN (c2_single ()); | |
| RUN (c2_many_rows ()); | |
| RUN (c3_many_rows ()); | |
| RUN (c2_inner (37, 23, 3)); | |
| RUN (c2_inner (11, 150, 4)); | |
| RUN (c2_inner (13, 29, 1)); | |
| RUN (c2_inner (7, 5, 0)); | |
| RUN (c2_sched_static_1 ()); | |
| RUN (c2_dist_static_7 ()); | |
| RUN (c2_dist_static_500 ()); | |
| RUN (c2_safelen_2 ()); | |
| RUN (c2_safelen_8 ()); | |
| RUN (c2_safelen_33 ()); | |
| RUN (c2_simdlen_4 ()); | |
| RUN (c3_safelen_8 ()); | |
| RUN (c2_parallel ()); | |
| RUN (c2_unsigned ()); | |
| RUN (c2_unsigned_down ()); | |
| RUN (c2_unsigned_down_step2 ()); | |
| RUN (c2_uchar_int ()); | |
| RUN (c2_long_long_inner ()); | |
| return 0; | |
| } | |
| /* { dg-final { scan-offload-tree-dump "GOMP_SIMT_ENTER" "optimized" { target { offload_target_amdgcn || offload_target_nvptx } } } } */ |