• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %compile-run-and-check
2 
3 #include <omp.h>
4 #include <stdio.h>
5 
6 #pragma omp declare target
putValueInParallel(int * ptr,int value)7 static void putValueInParallel(int *ptr, int value) {
8   #pragma omp parallel
9   {
10     *ptr = value;
11   }
12 }
13 
getId()14 static int getId() {
15   int id;
16   putValueInParallel(&id, omp_get_thread_num());
17   return id;
18 }
19 #pragma omp end declare target
20 
21 const int MaxThreads = 1024;
22 const int Threads = 64;
23 
main(int argc,char * argv[])24 int main(int argc, char *argv[]) {
25   int master;
26   int check[MaxThreads];
27   for (int i = 0; i < MaxThreads; i++) {
28     check[i] = 0;
29   }
30 
31   #pragma omp target map(master, check[:])
32   {
33     master = getId();
34 
35     #pragma omp parallel num_threads(Threads)
36     {
37       check[omp_get_thread_num()] = getId();
38     }
39   }
40 
41   // CHECK: master = 0.
42   printf("master = %d.\n", master);
43   // CHECK-NOT: invalid
44   for (int i = 0; i < MaxThreads; i++) {
45     if (i < Threads) {
46       if (check[i] != i) {
47         printf("invalid: check[%d] should be %d, is %d\n", i, i, check[i]);
48       }
49     } else if (check[i] != 0) {
50       printf("invalid: check[%d] should be 0, is %d\n", i, check[i]);
51     }
52   }
53 
54   return 0;
55 }
56