summaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c/examples-4/e.53.5.c
blob: 3bcd753dbbb15ee48954d6de798026a50c714b35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* { dg-do run } */
/* { dg-options "-O2" } */
/* { dg-additional-options "-msse2" { target sse2_runtime } } */
/* { dg-additional-options "-mavx" { target avx_runtime } } */

#include <stdlib.h>

#define EPS 0.00001
#define N 10000
#define M 1024

#pragma omp declare target
float Q[N][N];
#pragma omp declare simd uniform(i) linear(k) notinbranch
float Pfun (const int i, const int k)
{
  return Q[i][k] * Q[k][i];
}
#pragma omp end declare target

void init ()
{
  int i, j;
  for (i = 0; i < N; i++)
    for (j = 0; j < N; j++)
      Q[i][j] = 0.001 * i * j;
}

float accum_ref ()
{
  int i, k;
  float tmp = 0.0;

  for (i = 0; i < N; i++)
    {
      float tmp1 = 0.0;

      for (k = 0; k < M; k++)
	tmp1 += Pfun(i,k);

      tmp += tmp1;
    }

  return tmp;
}

float accum ()
{
  int i, k;
  float tmp = 0.0;

  #pragma omp target
    #pragma omp parallel for reduction(+:tmp)
      for (i = 0; i < N; i++)
	{
	  float tmp1 = 0.0;

	  #pragma omp simd reduction(+:tmp1)
	    for (k = 0; k < M; k++)
	      tmp1 += Pfun(i,k);

	  tmp += tmp1;
	}

  return tmp;
}

void check (float a, float b)
{
  float err = (b == 0.0) ? a : (a - b) / b;
  if (((err > 0) ? err : -err) > EPS)
    abort ();
}

int main ()
{
  init ();

  #pragma omp target update to(Q)

  check (accum (), accum_ref ());

  return 0;
}