xref: /llvm-project/openmp/runtime/test/tasking/omp_task.c (revision 74f98554f92dabb4c1be7db9e1712f060f3cdbca)
1614c7ef8SJonathan Peyton // RUN: %libomp-compile-and-run
2614c7ef8SJonathan Peyton #include <stdio.h>
3614c7ef8SJonathan Peyton #include <math.h>
4614c7ef8SJonathan Peyton #include "omp_testsuite.h"
5614c7ef8SJonathan Peyton #include "omp_my_sleep.h"
6614c7ef8SJonathan Peyton 
test_omp_task()7614c7ef8SJonathan Peyton int test_omp_task()
8614c7ef8SJonathan Peyton {
9614c7ef8SJonathan Peyton   int tids[NUM_TASKS];
10614c7ef8SJonathan Peyton   int i;
11614c7ef8SJonathan Peyton 
12614c7ef8SJonathan Peyton   #pragma omp parallel
13614c7ef8SJonathan Peyton   {
14614c7ef8SJonathan Peyton     #pragma omp single
15614c7ef8SJonathan Peyton     {
16614c7ef8SJonathan Peyton       for (i = 0; i < NUM_TASKS; i++) {
17614c7ef8SJonathan Peyton         /* First we have to store the value of the loop index in a new variable
18614c7ef8SJonathan Peyton          * which will be private for each task because otherwise it will be overwritten
19614c7ef8SJonathan Peyton          * if the execution of the task takes longer than the time which is needed to
20614c7ef8SJonathan Peyton          * enter the next step of the loop!
21614c7ef8SJonathan Peyton          */
22614c7ef8SJonathan Peyton         int myi;
23614c7ef8SJonathan Peyton         myi = i;
24614c7ef8SJonathan Peyton         #pragma omp task
25614c7ef8SJonathan Peyton         {
26614c7ef8SJonathan Peyton           my_sleep (SLEEPTIME);
27614c7ef8SJonathan Peyton           tids[myi] = omp_get_thread_num();
28614c7ef8SJonathan Peyton         } /* end of omp task */
29614c7ef8SJonathan Peyton       } /* end of for */
30614c7ef8SJonathan Peyton     } /* end of single */
31614c7ef8SJonathan Peyton   } /*end of parallel */
32614c7ef8SJonathan Peyton 
33614c7ef8SJonathan Peyton   /* Now we ckeck if more than one thread executed the tasks. */
34614c7ef8SJonathan Peyton   for (i = 1; i < NUM_TASKS; i++) {
35614c7ef8SJonathan Peyton     if (tids[0] != tids[i])
36614c7ef8SJonathan Peyton       return 1;
37614c7ef8SJonathan Peyton   }
38614c7ef8SJonathan Peyton   return 0;
39614c7ef8SJonathan Peyton } /* end of check_parallel_for_private */
40614c7ef8SJonathan Peyton 
main()41614c7ef8SJonathan Peyton int main()
42614c7ef8SJonathan Peyton {
43614c7ef8SJonathan Peyton   int i;
44614c7ef8SJonathan Peyton   int num_failed=0;
45614c7ef8SJonathan Peyton 
46*74f98554SAndrey Churbanov   if (omp_get_max_threads() < 2)
47*74f98554SAndrey Churbanov     omp_set_num_threads(8);
48*74f98554SAndrey Churbanov 
49614c7ef8SJonathan Peyton   for(i = 0; i < REPETITIONS; i++) {
50614c7ef8SJonathan Peyton     if(!test_omp_task()) {
51614c7ef8SJonathan Peyton       num_failed++;
52614c7ef8SJonathan Peyton     }
53614c7ef8SJonathan Peyton   }
54614c7ef8SJonathan Peyton   return num_failed;
55614c7ef8SJonathan Peyton }
56