xref: /llvm-project/openmp/runtime/test/tasking/omp_taskwait.c (revision a815cbb0105276a2258d592970c9322748d3ad49)
1614c7ef8SJonathan Peyton // RUN: %libomp-compile-and-run
2*a815cbb0SMichal Gorny 
3*a815cbb0SMichal Gorny // This test is known to be fragile on NetBSD kernel at the moment,
4*a815cbb0SMichal Gorny // https://bugs.llvm.org/show_bug.cgi?id=42020.
5*a815cbb0SMichal Gorny // UNSUPPORTED: netbsd
6614c7ef8SJonathan Peyton #include <stdio.h>
7614c7ef8SJonathan Peyton #include <math.h>
8614c7ef8SJonathan Peyton #include "omp_testsuite.h"
9614c7ef8SJonathan Peyton #include "omp_my_sleep.h"
10614c7ef8SJonathan Peyton 
test_omp_taskwait()11614c7ef8SJonathan Peyton int test_omp_taskwait()
12614c7ef8SJonathan Peyton {
13614c7ef8SJonathan Peyton   int result1 = 0;   /* Stores number of not finished tasks after the taskwait */
14614c7ef8SJonathan Peyton   int result2 = 0;   /* Stores number of wrong array elements at the end */
15614c7ef8SJonathan Peyton   int array[NUM_TASKS];
16614c7ef8SJonathan Peyton   int i;
17614c7ef8SJonathan Peyton 
18614c7ef8SJonathan Peyton   /* fill array */
19614c7ef8SJonathan Peyton   for (i = 0; i < NUM_TASKS; i++)
20614c7ef8SJonathan Peyton     array[i] = 0;
21614c7ef8SJonathan Peyton 
22614c7ef8SJonathan Peyton   #pragma omp parallel
23614c7ef8SJonathan Peyton   {
24614c7ef8SJonathan Peyton     #pragma omp single
25614c7ef8SJonathan Peyton     {
26614c7ef8SJonathan Peyton       for (i = 0; i < NUM_TASKS; i++) {
27614c7ef8SJonathan Peyton         /* First we have to store the value of the loop index in a new variable
28614c7ef8SJonathan Peyton          * which will be private for each task because otherwise it will be overwritten
29614c7ef8SJonathan Peyton          * if the execution of the task takes longer than the time which is needed to
30614c7ef8SJonathan Peyton          * enter the next step of the loop!
31614c7ef8SJonathan Peyton          */
32614c7ef8SJonathan Peyton         int myi;
33614c7ef8SJonathan Peyton         myi = i;
34614c7ef8SJonathan Peyton         #pragma omp task
35614c7ef8SJonathan Peyton         {
36614c7ef8SJonathan Peyton           my_sleep (SLEEPTIME);
37614c7ef8SJonathan Peyton           array[myi] = 1;
38614c7ef8SJonathan Peyton         } /* end of omp task */
39614c7ef8SJonathan Peyton       } /* end of for */
40614c7ef8SJonathan Peyton       #pragma omp taskwait
41614c7ef8SJonathan Peyton       /* check if all tasks were finished */
42614c7ef8SJonathan Peyton       for (i = 0; i < NUM_TASKS; i++)
43614c7ef8SJonathan Peyton         if (array[i] != 1)
44614c7ef8SJonathan Peyton           result1++;
45614c7ef8SJonathan Peyton 
46614c7ef8SJonathan Peyton       /* generate some more tasks which now shall overwrite
47614c7ef8SJonathan Peyton        * the values in the tids array */
48614c7ef8SJonathan Peyton       for (i = 0; i < NUM_TASKS; i++) {
49614c7ef8SJonathan Peyton         int myi;
50614c7ef8SJonathan Peyton         myi = i;
51614c7ef8SJonathan Peyton         #pragma omp task
52614c7ef8SJonathan Peyton         {
53614c7ef8SJonathan Peyton           array[myi] = 2;
54614c7ef8SJonathan Peyton         } /* end of omp task */
55614c7ef8SJonathan Peyton       } /* end of for */
56614c7ef8SJonathan Peyton     } /* end of single */
57614c7ef8SJonathan Peyton   } /*end of parallel */
58614c7ef8SJonathan Peyton 
59614c7ef8SJonathan Peyton   /* final check, if all array elements contain the right values: */
60614c7ef8SJonathan Peyton   for (i = 0; i < NUM_TASKS; i++) {
61614c7ef8SJonathan Peyton     if (array[i] != 2)
62614c7ef8SJonathan Peyton       result2++;
63614c7ef8SJonathan Peyton   }
64614c7ef8SJonathan Peyton   return ((result1 == 0) && (result2 == 0));
65614c7ef8SJonathan Peyton }
66614c7ef8SJonathan Peyton 
main()67614c7ef8SJonathan Peyton int main()
68614c7ef8SJonathan Peyton {
69614c7ef8SJonathan Peyton   int i;
70614c7ef8SJonathan Peyton   int num_failed=0;
71614c7ef8SJonathan Peyton 
72614c7ef8SJonathan Peyton   for(i = 0; i < REPETITIONS; i++) {
73614c7ef8SJonathan Peyton     if(!test_omp_taskwait()) {
74614c7ef8SJonathan Peyton       num_failed++;
75614c7ef8SJonathan Peyton     }
76614c7ef8SJonathan Peyton   }
77614c7ef8SJonathan Peyton   return num_failed;
78614c7ef8SJonathan Peyton }
79