blob: a52124643c4fca62f75b463fdb543af9d1a3205f [file]
/* Check reductions on offloaded composite simd collapse(n) loops: every
reduction operator on int, unsigned, long long, float and double, two
reductions in one loop, collapse(3), a non-unit inner step, safelen,
num_teams and num_threads, iteration variables that are lastprivate, and
complex addition. Inputs are chosen so that every result is exact in any
evaluation order, and each result is compared with a serial host loop.
Forms that the lowering does not give a SIMT region (user-defined and
array section reductions, complex multiplication, && and || on floating
point) take the plain simd path. They must still compute the right result,
and must not be rejected by simt_collapse_p.
The bounds are constants. A loop whose bounds are firstprivate scalars
needs the live-in scalars to be correct in every lane, which the SIMT
series does not yet guarantee on GCN, with or without a reduction. The
reductions that simt_collapse_p rejects (on a non-composite loop, or
inscan) are in simt-collapse-fallback-1.c. */
/* { dg-do run } */
/* The - reduction operator is deprecated in OpenMP 5.2, but still has to work. */
/* { dg-additional-options "-O2 -fdump-tree-omplower-details -Wno-deprecated-openmp" } */
/* { 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>
#define N 37
#define M 101
static int nfail;
/* Input values, as functions of the flat index K. No division or modulo:
those keep a collapsed loop off the SIMT path. */
#define v_add(k) (((k) & 7) - 3)
#define v_mul(k) (((k) & 511) == 0 ? 2 : (((k) & 1023) == 513 ? -1 : 1))
#define v_mm(k) ((((k) * 37) & 1023) - 512 + ((k) == 2345 ? 900 : 0) \
- ((k) == 77 ? 900 : 0))
#define v_and(k) (~(1 << ((k) & 15)) | (((k) & 15) == 5 ? (1 << 5) : 0))
#define v_or(k) (((k) & 15) == 12 ? 0 : 1 << ((k) & 15))
#define v_xor(k) (((k) * 2654435761u) >> 7)
#define v_land(k) ((k) != 1234)
#define v_lor(k) ((k) == 2222)
#define DO_PRAGMA(X) _Pragma (#X)
#define U_ADD(s, x) s += x
#define U_MUL(s, x) s *= x
#define U_MAX(s, x) s = s > x ? s : x
#define U_MIN(s, x) s = s < x ? s : x
#define U_AND(s, x) s &= x
#define U_OR(s, x) s |= x
#define U_XOR(s, x) s ^= x
#define U_LAND(s, x) s = s && x
#define U_LOR(s, x) s = s || x
#define FAIL(NAME) \
do { __builtin_printf ("FAIL %s\n", NAME); nfail++; } while (0)
/* A reduction with operator OP of type T over the nest, starting from INIT,
with input VAL and update UPD. */
#define RED(T, NAME, OP, INIT, VAL, UPD) \
static void \
test_##NAME##_##T (void) \
{ \
T s = INIT, h = INIT; \
DO_PRAGMA (omp target teams distribute parallel for simd collapse(2) \
reduction(OP: s)) \
for (int i = 0; i < N; i++) \
for (int j = 0; j < M; j++) \
{ \
T x = (T) VAL (i * M + j); \
UPD (s, x); \
} \
for (int k = 0; k < N * M; k++) \
{ \
T x = (T) VAL (k); \
UPD (h, x); \
} \
if (s != h) \
FAIL (#NAME " " #T); \
}
typedef unsigned int uint;
typedef long long llong;
/* && and || on float and double are not supported by the SIMT reduction,
and take the plain simd path. */
#define ARITH(T) \
RED (T, add, +, 0, v_add, U_ADD) \
RED (T, sub, -, 0, v_add, U_ADD) \
RED (T, mul, *, 1, v_mul, U_MUL) \
RED (T, max, max, (T) -1000, v_mm, U_MAX) \
RED (T, min, min, (T) 1000, v_mm, U_MIN) \
RED (T, land, &&, 1, v_land, U_LAND) \
RED (T, lor, ||, 0, v_lor, U_LOR)
#define BITS(T) \
RED (T, and, &, (T) ~0, v_and, U_AND) \
RED (T, or, |, 0, v_or, U_OR) \
RED (T, xor, ^, 0, v_xor, U_XOR)
ARITH (int) BITS (int)
ARITH (uint) BITS (uint)
ARITH (llong) BITS (llong)
ARITH (float)
ARITH (double)
#define CALLA(T) \
test_add_##T (); test_sub_##T (); test_mul_##T (); test_max_##T (); \
test_min_##T (); test_land_##T (); test_lor_##T ();
#define CALLB(T) test_and_##T (); test_or_##T (); test_xor_##T ();
/* Two reductions of different types and operators in one loop. */
static void
test_two (void)
{
int s = 0, hs = 0;
double m = -1000.0, hm = -1000.0;
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(+: s) reduction(max: m)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
s += v_add (i * M + j);
m = m > v_mm (i * M + j) ? m : v_mm (i * M + j);
}
for (int k = 0; k < N * M; k++)
{
hs += v_add (k);
hm = hm > v_mm (k) ? hm : v_mm (k);
}
if (s != hs || m != hm)
FAIL ("two reductions");
}
static void
test_collapse3 (void)
{
int s = 0, h = 0;
#pragma omp target teams distribute parallel for simd collapse(3) \
reduction(+: s)
for (int i = 0; i < 5; i++)
for (int j = 0; j < 7; j++)
for (int k = 0; k < 67; k++)
s += v_add ((i * 7 + j) * 67 + k);
for (int k = 0; k < 5 * 7 * 67; k++)
h += v_add (k);
if (s != h)
FAIL ("collapse(3)");
}
static void
test_inner_step3 (void)
{
int s = 0, h = 0;
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(+: s)
for (int i = 0; i < N; i++)
for (int j = 0; j < 200; j += 3)
s += v_add (i * 200 + j);
for (int i = 0; i < N; i++)
for (int j = 0; j < 200; j += 3)
h += v_add (i * 200 + j);
if (s != h)
FAIL ("inner step 3");
}
static void
test_safelen (void)
{
int s = 0, h = 0;
#pragma omp target teams distribute parallel for simd collapse(2) safelen(8) \
reduction(+: s)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
s += v_add (i * M + j);
for (int k = 0; k < N * M; k++)
h += v_add (k);
if (s != h)
FAIL ("safelen");
}
static void
test_num_teams_threads (void)
{
int s = 0, h = 0;
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(+: s) num_teams(3) num_threads(5)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
s += v_add (i * M + j);
for (int k = 0; k < N * M; k++)
h += v_add (k);
if (s != h)
FAIL ("num_teams and num_threads");
}
/* Iteration variables declared outside the loop are lastprivate. */
static void
test_lastprivate_iv (void)
{
int s = 0, h = 0, i, j;
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(+: s) map(tofrom: i, j)
for (i = 0; i < N; i++)
for (j = 0; j < M; j++)
s += v_add (i * M + j);
for (int k = 0; k < N * M; k++)
h += v_add (k);
if (s != h || i != N || j != M)
FAIL ("lastprivate iteration variables");
}
static void
test_complex_add (void)
{
_Complex double s = 0, h = 0;
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(+: s)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
s += v_add (i * M + j) + 1.0i * v_add (i * M + j + 1);
for (int k = 0; k < N * M; k++)
h += v_add (k) + 1.0i * v_add (k + 1);
if (s != h)
FAIL ("complex add");
}
/* The next three get no SIMT region from the lowering. */
static void
test_complex_mul (void)
{
_Complex double s = 1, h = 1;
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(*: s)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
s *= ((i * M + j) == 100 || (i * M + j) == 900) ? 1.0i : 1.0;
for (int k = 0; k < N * M; k++)
h *= (k == 100 || k == 900) ? 1.0i : 1.0;
if (s != h)
FAIL ("complex mul");
}
#pragma omp declare reduction (mymax: int : \
omp_out = omp_out > omp_in ? omp_out : omp_in) \
initializer (omp_priv = -1000000)
static void
test_user_defined (void)
{
int s = -1000000, h = -1000000;
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(mymax: s)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
int x = v_mm (i * M + j);
s = s > x ? s : x;
}
for (int k = 0; k < N * M; k++)
h = h > v_mm (k) ? h : v_mm (k);
if (s != h)
FAIL ("user-defined reduction");
}
static void
test_array_section (void)
{
int a[4] = { 0, 0, 0, 0 }, h[4] = { 0, 0, 0, 0 };
#pragma omp target teams distribute parallel for simd collapse(2) \
reduction(+: a[:4])
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
a[(i + j) & 3] += v_add (i * M + j);
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
h[(i + j) & 3] += v_add (i * M + j);
for (int x = 0; x < 4; x++)
if (a[x] != h[x])
FAIL ("array section");
}
int
main (void)
{
CALLA (int) CALLB (int)
CALLA (uint) CALLB (uint)
CALLA (llong) CALLB (llong)
CALLA (float)
CALLA (double)
test_two ();
test_collapse3 ();
test_inner_step3 ();
test_safelen ();
test_num_teams_threads ();
test_lastprivate_iv ();
test_complex_add ();
test_complex_mul ();
test_user_defined ();
test_array_section ();
if (nfail)
abort ();
return 0;
}
/* No composite loop of this file is rejected by simt_collapse_p. */
/* { dg-final { scan-tree-dump-not "collapsed loop cannot take the SIMT path" "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-offload-tree-dump "GOMP_SIMT_ENTER" "optimized" { target { offload_target_amdgcn || offload_target_nvptx } } } } */