blob: 893b1c2fd2f2c496d9bff63a70dd4d0454437dd5 [file]
/* Check offloaded simd collapse(n) loops whose iteration variables are
private, or partly private and partly lastprivate, and loops with a
lastprivate clause on a variable that is not an iteration variable,
with and without the conditional modifier. Iteration variables that
are only implicitly lastprivate are covered by
simt-collapse-lastprivate-1.c.
Loops whose lastprivate clauses all name iteration variables get a SIMT
region: after the last iteration, all the lanes set every iteration
variable to its final value, but only the lastprivate ones may be
copied out. A lastprivate clause on another variable takes the
one-lane fallback.
Each loop but t3_priv is checked in the non-composite (simd) and the
composite form. A conditional lastprivate clause is not allowed on
distribute, so its composite form is parallel for simd. As in
simt-collapse-lastprivate-1.c, the variables are declare target
variables, not mapped ones. Each nest runs once on the host into REF
and once offloaded into BUF, and the elements visited and the final
values must match. A private variable must keep its value. For a nest
with no iterations, the final value of a lastprivate variable without
the conditional modifier is not checked. On nvptx, the conditional
lastprivate clause on parallel for simd needs
__atomic_compare_exchange_16 from libatomic. */
/* { dg-do run } */
/* { dg-additional-options "-O2 -fdump-tree-omplower-details" } */
/* { 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=nvptx-none=-latomic" { target offload_target_nvptx } } */
/* { dg-additional-options "-foffload-options=-fdump-tree-optimized" { target { offload_target_amdgcn || offload_target_nvptx } } } */
#include <stdlib.h>
#include <string.h>
#define SZ 20000
#pragma omp begin declare target
int buf[SZ], sel[SZ];
int n, m;
int i, j, k, g;
#pragma omp end declare target
int ref[SZ];
#define DO_PRAGMA(X) _Pragma (#X)
/* Check the final value V of a variable against R: always if ALWAYS, else
only if the nest had iterations. */
static void
check (int v, int r, int always, int visits)
{
if ((always || visits) && v != r)
abort ();
}
/* Run the n x m collapse(2) nest on the host and offloaded as
DIR collapse(2) CLAUSES, with BODY after the store to BUF or REF. PI
and PJ are 1 if I or J is private, PG is 1 if G must be checked even
for a nest with no iterations. */
#define TEST2_FORM(NAME, DIR, PI, PJ, PG, BODY, ...) \
static void \
NAME (void) \
{ \
int visits = 0; \
memset (ref, 0, sizeof ref); \
i = 77, j = 77, g = 77; \
for (i = 0; i < n; i++) \
for (j = 0; j < m; j++) \
{ \
ref[i * m + j] = 1, visits++; \
BODY; \
} \
int ri = PI ? 77 : i, rj = PJ ? 77 : j, rg = g; \
memset (buf, 0, sizeof buf); \
i = 77, j = 77, g = 77; \
DO_PRAGMA (omp target update to(buf, i, j, g)) \
DO_PRAGMA (omp target) \
DO_PRAGMA (omp DIR collapse(2) __VA_ARGS__) \
for (i = 0; i < n; i++) \
for (j = 0; j < m; j++) \
{ \
buf[i * m + j] = 1; \
BODY; \
} \
DO_PRAGMA (omp target update from(buf, i, j, g)) \
if (memcmp (buf, ref, sizeof buf) != 0) \
abort (); \
check (i, ri, PI, visits); \
check (j, rj, PJ, visits); \
check (g, rg, PG, visits); \
}
#define TEST2(NAME, PI, PJ, PG, BODY, ...) \
TEST2_FORM (NAME##_nc, simd, PI, PJ, PG, BODY, __VA_ARGS__) \
TEST2_FORM (NAME##_c, teams distribute parallel for simd, PI, PJ, PG, \
BODY, __VA_ARGS__)
/* SIMT region. */
TEST2 (t_priv, 1, 1, 1, (void) 0, private(i, j))
TEST2 (t_lp_i, 0, 1, 1, (void) 0, lastprivate(i) private(j))
TEST2 (t_lp_j, 1, 0, 1, (void) 0, private(i) lastprivate(j))
/* One-lane fallback. */
TEST2 (t_g, 0, 0, 0, g = i * m + j, lastprivate(g))
TEST2 (t_g_priv, 1, 1, 0, g = i * m + j, private(i, j) lastprivate(g))
TEST2_FORM (t_cond_nc, simd, 0, 0, 1, if (sel[i * m + j]) g = i * m + j,
lastprivate(conditional: g))
TEST2_FORM (t_cond_c, parallel for simd, 0, 0, 1,
if (sel[i * m + j]) g = i * m + j, lastprivate(conditional: g))
/* The n x m x 19 collapse(3) nest, offloaded as DIR collapse(3) CLAUSES.
PI and PJK are 1 if I, or J and K, are private, PG is 1 if G is
lastprivate. */
#define TEST3_FORM(NAME, DIR, PI, PJK, PG, ...) \
static void \
NAME (void) \
{ \
int visits = 0; \
memset (ref, 0, sizeof ref); \
i = 77, j = 77, k = 77, g = 77; \
for (i = 0; i < n; i++) \
for (j = 0; j < m; j++) \
for (k = 0; k < 19; k++) \
{ \
ref[(i * m + j) * 19 + k] = 1, visits++; \
if (PG) \
g = i + j + k; \
} \
int ri = PI ? 77 : i, rj = PJK ? 77 : j, rk = PJK ? 77 : k, rg = g; \
memset (buf, 0, sizeof buf); \
i = 77, j = 77, k = 77, g = 77; \
DO_PRAGMA (omp target update to(buf, i, j, k, g)) \
DO_PRAGMA (omp target) \
DO_PRAGMA (omp DIR collapse(3) __VA_ARGS__) \
for (i = 0; i < n; i++) \
for (j = 0; j < m; j++) \
for (k = 0; k < 19; k++) \
{ \
buf[(i * m + j) * 19 + k] = 1; \
if (PG) \
g = i + j + k; \
} \
DO_PRAGMA (omp target update from(buf, i, j, k, g)) \
if (memcmp (buf, ref, sizeof buf) != 0) \
abort (); \
check (i, ri, PI, visits); \
check (j, rj, PJK, visits); \
check (k, rk, PJK, visits); \
check (g, rg, !PG, visits); \
}
#define TEST3(NAME, PI, PJK, PG, ...) \
TEST3_FORM (NAME##_nc, simd, PI, PJK, PG, __VA_ARGS__) \
TEST3_FORM (NAME##_c, teams distribute parallel for simd, PI, PJK, PG, \
__VA_ARGS__)
/* SIMT region. Not composite: with the SIMT series, the composite form
hangs on GCN. */
TEST3_FORM (t3_priv_nc, simd, 1, 1, 0, private(i, j, k))
/* One-lane fallback. */
TEST3 (t3_lp_ig, 0, 0, 1, lastprivate(i, g))
int
main (void)
{
static const int nm[][2] = {
{ 5, 70 }, { 5, 3 }, { 3, 64 }, { 7, 65 }, { 2, 200 }, { 100, 1 },
{ 1, 1 }, { 2, 3 }, { 0, 10 }, { 10, 0 }
};
for (unsigned x = 0; x < sizeof nm / sizeof nm[0]; x++)
{
n = nm[x][0], m = nm[x][1];
#pragma omp target update to(n, m)
t_priv_nc ();
t_priv_c ();
t_lp_i_nc ();
t_lp_i_c ();
t_lp_j_nc ();
t_lp_j_c ();
t_g_nc ();
t_g_c ();
t_g_priv_nc ();
t_g_priv_c ();
t3_priv_nc ();
t3_lp_ig_nc ();
t3_lp_ig_c ();
/* For the conditional lastprivate clause, select every 13th
iteration, then only the first one, then none. */
for (int p = 0; p < 3; p++)
{
for (int y = 0; y < SZ; y++)
sel[y] = p == 0 ? y % 13 == 5 : p == 1 ? y == 0 : 0;
#pragma omp target update to(sel)
t_cond_nc ();
t_cond_c ();
}
}
return 0;
}
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: lastprivate clause on a non-iteration variable" 8 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path" 8 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-offload-tree-dump-times "GOMP_SIMT_ENTER" 7 "optimized" { target { offload_target_amdgcn || offload_target_nvptx } } } } */