1 /* { dg-do run } */
2
3 #include <stdlib.h>
4
5 #define EPS 0.0001
6 #define N 1024*1024
7
init(float B[],float C[],int n)8 void init (float B[], float C[], int n)
9 {
10 int i;
11 for (i = 0; i < n; i++)
12 {
13 B[i] = 0.1 * i;
14 C[i] = 0.01 * i * i;
15 }
16 }
17
dotprod_ref(float B[],float C[],int n)18 float dotprod_ref (float B[], float C[], int n)
19 {
20 int i;
21 float sum = 0.0;
22
23 for (i = 0; i < n; i++)
24 sum += B[i] * C[i];
25
26 return sum;
27 }
28
dotprod(float B[],float C[],int n)29 float dotprod (float B[], float C[], int n)
30 {
31 int i;
32 float sum = 0;
33
34 #pragma omp target teams map(to: B[0:n], C[0:n]) \
35 map(tofrom: sum) reduction(+:sum)
36 #pragma omp distribute parallel for reduction(+:sum)
37 for (i = 0; i < n; i++)
38 sum += B[i] * C[i];
39
40 return sum;
41 }
42
check(float a,float b)43 void check (float a, float b)
44 {
45 float err = (b == 0.0) ? a : (a - b) / b;
46 if (((err > 0) ? err : -err) > EPS)
47 abort ();
48 }
49
main()50 int main ()
51 {
52 float *v1 = (float *) malloc (N * sizeof (float));
53 float *v2 = (float *) malloc (N * sizeof (float));
54
55 float p1, p2;
56
57 init (v1, v2, N);
58
59 p1 = dotprod_ref (v1, v2, N);
60 p2 = dotprod (v1, v2, N);
61
62 check (p1, p2);
63
64 free (v1);
65 free (v2);
66
67 return 0;
68 }
69