aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c/examples-4/e.51.3.c
blob: abb283801f8ad26e1c1ab7c0c7aed232014ba919 (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
/* { dg-do run } */

#include <stdlib.h>

const int ROWS = 5;
const int COLS = 5;

void init (int Q[][COLS], const int rows, const int cols)
{
  int i, j;
  for (i = 0; i < rows; i++)
    for (j = 0; j < cols; j++)
      Q[i][j] = (i + 1) * 100 + (j + 1);
}

void check (int a[][COLS], int b[][COLS], const int rows, const int cols)
{
  int i, j;
  for (i = 0; i < rows; i++)
    for (j = 0; j < cols; j++)
      if (a[i][j] != b[i][j])
	abort ();
}

void gramSchmidt_ref (int Q[][COLS], const int rows, const int cols)
{
  int i, k;

  for (k = 0; k < cols; k++)
    {
      int tmp = 0;

      for (i = 0; i < rows; i++)
	tmp += (Q[i][k] * Q[i][k]);

      for (i = 0; i < rows; i++)
	Q[i][k] *= tmp;
    }
}

void gramSchmidt (int Q[][COLS], const int rows, const int cols)
{
  int i, k;

  #pragma omp target data map(Q[0:rows][0:cols]) map(to:COLS)
    for (k = 0; k < cols; k++)
      {
	int tmp = 0;

	#pragma omp target
	  #pragma omp parallel for reduction(+:tmp)
	    for (i = 0; i < rows; i++)
	      tmp += (Q[i][k] * Q[i][k]);

	#pragma omp target
	  #pragma omp parallel for
	    for (i = 0; i < rows; i++)
	      Q[i][k] *= tmp;
      }
}

int main ()
{
  int (*Q1)[COLS] = (int(*)[COLS]) malloc (ROWS * COLS * sizeof (int));
  int (*Q2)[COLS] = (int(*)[COLS]) malloc (ROWS * COLS * sizeof (int));

  init (Q1, ROWS, COLS);
  init (Q2, ROWS, COLS);

  gramSchmidt_ref (Q1, ROWS, COLS);
  gramSchmidt (Q2, ROWS, COLS);

  check (Q1, Q2, ROWS, COLS);

  free (Q1);
  free (Q2);

  return 0;
}