| 1 |
/* Test 'nohost' clause via 'acc_on_device'. |
| 2 |
|
| 3 |
With optimizations disabled, we currently don't expect that 'acc_on_device' "evaluates at compile time to a constant". |
| 4 |
{ dg-skip-if "TODO PR82391" { *-*-* } { "-O0" } } |
| 5 |
*/ |
| 6 |
|
| 7 |
/* { dg-additional-options "-fdump-tree-oaccloops" } */ |
| 8 |
|
| 9 |
/* { dg-additional-options "-fno-inline" } for stable results regarding OpenACC 'routine'. */ |
| 10 |
|
| 11 |
#include <assert.h> |
| 12 |
#include <openacc.h> |
| 13 |
|
| 14 |
#pragma acc routine |
| 15 |
static int fact(int n) |
| 16 |
{ |
| 17 |
if (n == 0 || n == 1) |
| 18 |
return 1; |
| 19 |
else |
| 20 |
return n * fact(n - 1); |
| 21 |
} |
| 22 |
|
| 23 |
#pragma acc routine nohost |
| 24 |
static int fact_nohost(int n) |
| 25 |
{ |
| 26 |
/* Make sure this fails host compilation. */ |
| 27 |
#if defined ACC_DEVICE_TYPE_host |
| 28 |
asm ("IT'S A TRAP"); |
| 29 |
#elif defined ACC_DEVICE_TYPE_nvidia |
| 30 |
asm ("{\n\t .reg .u32 %tid_x;\n\t mov.u32 %tid_x, %tid.x;\n\t}"); |
| 31 |
#elif defined ACC_DEVICE_TYPE_radeon |
| 32 |
asm ("s_nop 0"); |
| 33 |
#else |
| 34 |
# error Not ported to this ACC_DEVICE_TYPE |
| 35 |
#endif |
| 36 |
|
| 37 |
return fact(n); |
| 38 |
} |
| 39 |
/* { dg-final { scan-tree-dump-times {(?n)^OpenACC routine 'fact_nohost' has 'nohost' clause\.$} 1 oaccloops { target c } } } |
| 40 |
{ dg-final { scan-tree-dump-times {(?n)^OpenACC routine 'int fact_nohost\(int\)' has 'nohost' clause\.$} 1 oaccloops { target { c++ && { ! offloading_enabled } } } } } |
| 41 |
{ dg-final { scan-tree-dump-times {(?n)^OpenACC routine 'fact_nohost\(int\)' has 'nohost' clause\.$} 1 oaccloops { target { c++ && offloading_enabled } } } } |
| 42 |
TODO See PR101551 for 'offloading_enabled' differences. */ |
| 43 |
|
| 44 |
int main() |
| 45 |
{ |
| 46 |
#define N 10 |
| 47 |
int x[N]; |
| 48 |
|
| 49 |
#pragma acc parallel loop copyout(x) |
| 50 |
for (int i = 0; i < N; ++i) |
| 51 |
/*TODO PR82391: '(int) acc_device_*' cast to avoid the C++ 'acc_on_device' wrapper. */ |
| 52 |
x[i] = acc_on_device((int) acc_device_not_host) ? fact_nohost(i) : 0; |
| 53 |
|
| 54 |
for (int i = 0; i < N; ++i) |
| 55 |
{ |
| 56 |
if (acc_get_device_type() == acc_device_host) |
| 57 |
assert(x[i] == 0); |
| 58 |
else |
| 59 |
assert(x[i] == fact(i)); |
| 60 |
} |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |