aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c/examples-4/simd-5.c
blob: ce037acdaeef6fe7bd5e3d925d2aa4f6a5f503b6 (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
/* { dg-do run } */
/* { dg-additional-options "-msse2" { target sse2_runtime } } */
/* { dg-additional-options "-mavx" { target avx_runtime } } */

#define N 128
#define M 16
#define EPS 0.0000000000000001
#define SAFELEN 16

#include <stdlib.h>

void init(double a[N][N], double b[N][N], int n)
{
  int i, j, s = -1;
  for (i = 0; i < n; i++)
  {
    for (j = 0; j < n; j++)
    {
       a[i][j] = i * j * s;
       b[i][j] = i + j + s;
       s = -s;
    }
  }
}

void work( double a[N][N], double b[N][N], double c[N][N], int n )
{
   int i, j;
   double tmp;
   #pragma omp for simd collapse(2) private(tmp)
   for (i = 0; i < n; i++)
   {
      for (j = 0; j < n; j++)
      {
         tmp = a[i][j] + b[i][j];
         c[i][j] = tmp;
      }
   }
}

void work_ref( double a[N][N], double b[N][N], double c[N][N], int n )
{
   int i, j;
   double tmp;
   for (i = 0; i < n; i++)
   {
      for (j = 0; j < n; j++)
      {
         tmp = a[i][j] + b[i][j];
         c[i][j] = tmp;
      }
   }
}

void check (double a[N][N], double b[N][N])
{
  int i, j;
  for (i = 0; i < N; i++)
    for (j = 0; j < N; j++)
      if (a[i][j] - b[i][j] > EPS || b[i][j] - a[i][j] > EPS)
        abort ();
}

int main ()
{
  double a[N][N], b[N][N], c[N][N], c_ref[N][N];

  init(a, b, N);

  work(a, b, c, N);
  work_ref(a, b, c_ref, N);

  check(c, c_ref);

  return 0;
}