| 1 |
/* { dg-do run } */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
const int ROWS = 5; |
| 6 |
const int COLS = 5; |
| 7 |
|
| 8 |
void init (int Q[][COLS], const int rows, const int cols) |
| 9 |
{ |
| 10 |
int i, j; |
| 11 |
for (i = 0; i < rows; i++) |
| 12 |
for (j = 0; j < cols; j++) |
| 13 |
Q[i][j] = (i + 1) * 100 + (j + 1); |
| 14 |
} |
| 15 |
|
| 16 |
void check (int a[][COLS], int b[][COLS], const int rows, const int cols) |
| 17 |
{ |
| 18 |
int i, j; |
| 19 |
for (i = 0; i < rows; i++) |
| 20 |
for (j = 0; j < cols; j++) |
| 21 |
if (a[i][j] != b[i][j]) |
| 22 |
abort (); |
| 23 |
} |
| 24 |
|
| 25 |
void gramSchmidt_ref (int Q[][COLS], const int rows, const int cols) |
| 26 |
{ |
| 27 |
int i, k; |
| 28 |
|
| 29 |
for (k = 0; k < cols; k++) |
| 30 |
{ |
| 31 |
int tmp = 0; |
| 32 |
|
| 33 |
for (i = 0; i < rows; i++) |
| 34 |
tmp += (Q[i][k] * Q[i][k]); |
| 35 |
|
| 36 |
for (i = 0; i < rows; i++) |
| 37 |
Q[i][k] *= tmp; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void gramSchmidt (int Q[][COLS], const int rows, const int cols) |
| 42 |
{ |
| 43 |
int i, k; |
| 44 |
|
| 45 |
#pragma omp target data map(Q[0:rows][0:cols]) map(to:COLS) |
| 46 |
for (k = 0; k < cols; k++) |
| 47 |
{ |
| 48 |
int tmp = 0; |
| 49 |
|
| 50 |
#pragma omp target map(tofrom:tmp) |
| 51 |
#pragma omp parallel for reduction(+:tmp) |
| 52 |
for (i = 0; i < rows; i++) |
| 53 |
tmp += (Q[i][k] * Q[i][k]); |
| 54 |
|
| 55 |
#pragma omp target |
| 56 |
#pragma omp parallel for |
| 57 |
for (i = 0; i < rows; i++) |
| 58 |
Q[i][k] *= tmp; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
int main () |
| 63 |
{ |
| 64 |
int (*Q1)[COLS] = (int(*)[COLS]) malloc (ROWS * COLS * sizeof (int)); |
| 65 |
int (*Q2)[COLS] = (int(*)[COLS]) malloc (ROWS * COLS * sizeof (int)); |
| 66 |
|
| 67 |
init (Q1, ROWS, COLS); |
| 68 |
init (Q2, ROWS, COLS); |
| 69 |
|
| 70 |
gramSchmidt_ref (Q1, ROWS, COLS); |
| 71 |
gramSchmidt (Q2, ROWS, COLS); |
| 72 |
|
| 73 |
check (Q1, Q2, ROWS, COLS); |
| 74 |
|
| 75 |
free (Q1); |
| 76 |
free (Q2); |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |