| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-additional-options "-lm" } */ |
| 3 |
|
| 4 |
/* Note: At the time this program was written, Nvptx was not asynchronous |
| 5 |
enough to trigger the issue (with a 'nowait' added); however, one |
| 6 |
AMD GPUs, it triggered. */ |
| 7 |
|
| 8 |
/* Test whether nowait / dependency is handled correctly. |
| 9 |
Motivated by OpenMP_VV's 5.1/interop/test_interop_target.c |
| 10 |
|
| 11 |
The code actually only creates a streaming object without actually using it, |
| 12 |
except for dependency tracking. |
| 13 |
|
| 14 |
Note that there is a difference between having a steaming (targetsync) object |
| 15 |
and not (= omp_interop_none); at least if one assumes that omp_interop_none |
| 16 |
does not include 'targetsync' as (effective) interop type - in that case, |
| 17 |
'nowait' has no effect and the 'depend' is active as included task, otherwise |
| 18 |
the code continues with the depend being active only for the about to be |
| 19 |
destroyed or used thread. |
| 20 |
|
| 21 |
The OpenMP spec states (here 6.0): |
| 22 |
"If the interop-type set includes 'targetsync', an empty mergeable task is |
| 23 |
generated. If the 'nowait' clause is not present on the construct then |
| 24 |
the task is also an included task. If the interop-type set does not |
| 25 |
include 'targetsync', the 'nowait' clause has no effect. Any depend |
| 26 |
clauses that are present on the construct apply to the generated task. */ |
| 27 |
|
| 28 |
#include <omp.h> |
| 29 |
|
| 30 |
void |
| 31 |
test_async (const int dev) |
| 32 |
{ |
| 33 |
constexpr int N = 2048; |
| 34 |
constexpr int ulp = 4; |
| 35 |
constexpr double M_PI = 2.0 * __builtin_acos (0.0); |
| 36 |
omp_interop_t obj1, obj2; |
| 37 |
double A[N] = { }; |
| 38 |
int B[N] = { }; |
| 39 |
|
| 40 |
/* Create interop object. */ |
| 41 |
#pragma omp interop device(dev) init(targetsync : obj1, obj2) |
| 42 |
|
| 43 |
if (dev == omp_initial_device || dev == omp_get_num_devices ()) |
| 44 |
{ |
| 45 |
if (obj1 != omp_interop_none || obj2 != omp_interop_none) |
| 46 |
__builtin_abort (); |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
if (obj1 == omp_interop_none || obj2 == omp_interop_none) |
| 51 |
__builtin_abort (); |
| 52 |
} |
| 53 |
|
| 54 |
/* DOUBLE */ |
| 55 |
|
| 56 |
/* Now in the background update it, slowly enough that the |
| 57 |
code afterwards is reached while still running asynchronously. |
| 58 |
As OpenMP_VV's Issue #863 shows, the overhead is high enough to |
| 59 |
fail even when only doing an atomic integer increment. */ |
| 60 |
|
| 61 |
#pragma omp target device(dev) map(A) depend(out: A[:N]) nowait |
| 62 |
for (int i = 0; i < N; i++) |
| 63 |
#pragma omp atomic update |
| 64 |
A[i] += __builtin_sin (2*i*M_PI/N); |
| 65 |
|
| 66 |
/* DESTROY take care of the dependeny such that ... */ |
| 67 |
|
| 68 |
if (obj1 == omp_interop_none) |
| 69 |
{ |
| 70 |
// Same as below as 'nowait' is ignored. |
| 71 |
#pragma omp interop destroy(obj1) depend(in: A[:N]) nowait |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
#pragma omp interop destroy(obj1) depend(in: A[:N]) |
| 76 |
} |
| 77 |
|
| 78 |
/* ... this code is only executed once the dependency as been fulfilled. */ |
| 79 |
|
| 80 |
/* Check the value - part I: quick, avoid A[0] == sin(0) = 0. */ |
| 81 |
for (int i = 1; i < N; i++) |
| 82 |
if (A[i] == 0.0) |
| 83 |
__builtin_abort (); |
| 84 |
|
| 85 |
/* Check the value - part II: throughly */ |
| 86 |
for (int i = 0; i < N; i++) |
| 87 |
{ |
| 88 |
double x = A[i]; |
| 89 |
double y = __builtin_sin (2*i*M_PI/N); |
| 90 |
if (__builtin_fabs (x - y) > ulp * __builtin_fabs (x+y) * __DBL_EPSILON__) |
| 91 |
__builtin_abort (); |
| 92 |
} |
| 93 |
|
| 94 |
/* Integer */ |
| 95 |
|
| 96 |
#pragma omp target device(dev) map(B) depend(out: B[:N]) nowait |
| 97 |
for (int i = 0; i < N; i++) |
| 98 |
#pragma omp atomic update |
| 99 |
B[i] += 42; |
| 100 |
|
| 101 |
/* Same - but using USE. */ |
| 102 |
if (obj2 == omp_interop_none) |
| 103 |
{ |
| 104 |
// Same as below as 'nowait' is ignored. |
| 105 |
#pragma omp interop use(obj2) depend(in: B[:N]) nowait |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
#pragma omp interop use(obj2) depend(in: B[:N]) |
| 110 |
} |
| 111 |
|
| 112 |
for (int i = 0; i < N; i++) |
| 113 |
if (B[i] != 42) |
| 114 |
__builtin_abort (); |
| 115 |
|
| 116 |
#pragma omp interop destroy(obj2) |
| 117 |
} |
| 118 |
|
| 119 |
int |
| 120 |
main () |
| 121 |
{ |
| 122 |
int ndev = omp_get_num_devices (); |
| 123 |
|
| 124 |
for (int dev = 0; dev <= ndev; dev++) |
| 125 |
test_async (dev); |
| 126 |
test_async (omp_initial_device); |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |