1 /* { dg-do run } */
2
3 #include <stdlib.h>
4
5 const int MAX = 1800;
6
check(int * a,int * b,int N)7 void check (int *a, int *b, int N)
8 {
9 int i;
10 for (i = 0; i < N; i++)
11 if (a[i] != b[i])
12 abort ();
13 }
14
init(int * a1,int * a2,int N)15 void init (int *a1, int *a2, int N)
16 {
17 int i, s = -1;
18 for (i = 0; i < N; i++)
19 {
20 a1[i] = s;
21 a2[i] = i;
22 s = -s;
23 }
24 }
25
init_again(int * a1,int * a2,int N)26 void init_again (int *a1, int *a2, int N)
27 {
28 int i, s = -1;
29 for (i = 0; i < N; i++)
30 {
31 a1[i] = s * 10;
32 a2[i] = i;
33 s = -s;
34 }
35 }
36
vec_mult_ref(int * p,int * v1,int * v2,int N)37 void vec_mult_ref (int *p, int *v1, int *v2, int N)
38 {
39 int i;
40
41 init (v1, v2, MAX);
42
43 for (i = 0; i < N; i++)
44 p[i] = v1[i] * v2[i];
45
46 init_again (v1, v2, N);
47
48 for (i = 0; i < N; i++)
49 p[i] = p[i] + (v1[i] * v2[i]);
50 }
51
vec_mult(int * p,int * v1,int * v2,int N)52 void vec_mult (int *p, int *v1, int *v2, int N)
53 {
54 int i;
55
56 init (v1, v2, MAX);
57
58 #pragma omp target data map(to: v1[:N], v2[:N]) map(from: p[0:N])
59 {
60 #pragma omp target
61 #pragma omp parallel for
62 for (i = 0; i < N; i++)
63 p[i] = v1[i] * v2[i];
64
65 init_again (v1, v2, N);
66
67 #pragma omp target update to(v1[:N], v2[:N])
68
69 #pragma omp target
70 #pragma omp parallel for
71 for (i = 0; i < N; i++)
72 p[i] = p[i] + (v1[i] * v2[i]);
73 }
74 }
75
main()76 int main ()
77 {
78 int *p1 = (int *) malloc (MAX * sizeof (int));
79 int *p2 = (int *) malloc (MAX * sizeof (int));
80 int *v1 = (int *) malloc (MAX * sizeof (int));
81 int *v2 = (int *) malloc (MAX * sizeof (int));
82
83 vec_mult_ref (p1, v1, v2, MAX);
84 vec_mult (p2, v1, v2, MAX);
85
86 check (p1, p2, MAX);
87
88 free (p1);
89 free (p2);
90 free (v1);
91 free (v2);
92
93 return 0;
94 }
95