xref: /llvm-project/offload/test/offloading/back2back_distribute.c (revision 330d8983d25d08580fc1642fea48b2473f47a9da)
1 // RUN: %libomptarget-compile-generic -O3 && %libomptarget-run-generic | %fcheck-generic
2 
3 #include <omp.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #define MAX_N 25000
8 
reset_input(double * a,double * a_h,double * b,double * c)9 void reset_input(double *a, double *a_h, double *b, double *c) {
10   for(int i = 0 ; i < MAX_N ; i++) {
11     a[i] = a_h[i] = i;
12     b[i] = i*2;
13     c[i] = i-3;
14   }
15 }
16 
main(int argc,char * argv[])17 int main(int argc, char *argv[]) {
18   double *a = (double *)calloc(MAX_N, sizeof(double));
19   double *a_h = (double *)calloc(MAX_N, sizeof(double));
20   double *d = (double *)calloc(MAX_N, sizeof(double));
21   double *d_h = (double *)calloc(MAX_N, sizeof(double));
22   double *b = (double *)calloc(MAX_N, sizeof(double));
23   double *c = (double *)calloc(MAX_N, sizeof(double));
24 
25 #pragma omp target enter data map(to:a[:MAX_N],b[:MAX_N],c[:MAX_N],d[:MAX_N])
26 
27   for (int n = 32 ; n < MAX_N ; n+=5000) {
28     reset_input(a, a_h, b, c);
29 
30 #pragma omp target update to(a[:n],b[:n],c[:n],d[:n])
31     int t = 0;
32     for (int tms = 1 ; tms <= 256 ; tms *= 2) { // 8 times
33       for (int ths = 32 ; ths <= 1024 ; ths *= 2) { // 6 times
34         t++;
35 #pragma omp target
36 #pragma omp teams num_teams(tms) thread_limit(ths)
37         {
38 #pragma omp distribute parallel for
39           for (int i = 0; i < n; ++i) {
40             a[i] += b[i] + c[i];
41           }
42 #pragma omp distribute parallel for
43           for (int i = 0; i < n; ++i) {
44            d[i] -= b[i] + c[i];
45           }
46         }
47       } // loop over 'ths'
48     } // loop over 'tms'
49 
50     // check results for each 'n'
51     for (int times = 0 ; times < t ; times++) {
52       for (int i = 0; i < n; ++i) {
53         a_h[i] += b[i] + c[i];
54       }
55       for (int i = 0; i < n; ++i)
56         d_h[i] -= b[i] + c[i];
57     }
58 #pragma omp target update from(a[:n],d[:n])
59 
60     for (int i = 0; i < n; ++i) {
61       if (a_h[i] != a[i]) {
62         printf("A Error at n = %d, i = %d: host = %f, device = %f\n", n, i, a_h[i], a[i]);
63         return 1;
64       }
65       if (d_h[i] != d[i]) {
66         printf("D Error at n = %d, i = %d: host = %lf, device = %lf\n", n, i, d_h[i], d[i]);
67         return 1;
68       }
69     }
70   } // loop over 'n'
71 
72   // CHECK: Succeeded
73   printf("Succeeded\n");
74   return 0;
75 }
76