1 // RUN: %libomp-compile-and-run
2 #include <stdio.h>
3 #include <math.h>
4 #include "omp_testsuite.h"
5 #include "omp_my_sleep.h"
6 
7 int test_omp_taskwait()
8 {
9   int result1 = 0;   /* Stores number of not finished tasks after the taskwait */
10   int result2 = 0;   /* Stores number of wrong array elements at the end */
11   int array[NUM_TASKS];
12   int i;
13 
14   /* fill array */
15   for (i = 0; i < NUM_TASKS; i++)
16     array[i] = 0;
17 
18   #pragma omp parallel
19   {
20     #pragma omp single
21     {
22       for (i = 0; i < NUM_TASKS; i++) {
23         /* First we have to store the value of the loop index in a new variable
24          * which will be private for each task because otherwise it will be overwritten
25          * if the execution of the task takes longer than the time which is needed to
26          * enter the next step of the loop!
27          */
28         int myi;
29         myi = i;
30         #pragma omp task
31         {
32           my_sleep (SLEEPTIME);
33           array[myi] = 1;
34         } /* end of omp task */
35       } /* end of for */
36       #pragma omp taskwait
37       /* check if all tasks were finished */
38       for (i = 0; i < NUM_TASKS; i++)
39         if (array[i] != 1)
40           result1++;
41 
42       /* generate some more tasks which now shall overwrite
43        * the values in the tids array */
44       for (i = 0; i < NUM_TASKS; i++) {
45         int myi;
46         myi = i;
47         #pragma omp task
48         {
49           array[myi] = 2;
50         } /* end of omp task */
51       } /* end of for */
52     } /* end of single */
53   } /*end of parallel */
54 
55   /* final check, if all array elements contain the right values: */
56   for (i = 0; i < NUM_TASKS; i++) {
57     if (array[i] != 2)
58       result2++;
59   }
60   return ((result1 == 0) && (result2 == 0));
61 }
62 
63 int main()
64 {
65   int i;
66   int num_failed=0;
67 
68   for(i = 0; i < REPETITIONS; i++) {
69     if(!test_omp_taskwait()) {
70       num_failed++;
71     }
72   }
73   return num_failed;
74 }
75