blob: c3a0ea7ee600a7ecc7b0583a572dff72ebdf4d1d [file] [log] [blame]
Jonathan Peyton614c7ef2015-09-21 20:41:31 +00001// 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
7int 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 */
Jonathan Peyton37310762016-05-17 21:08:52 +000015 for (i = 0; i < NUM_TASKS; i++)
Jonathan Peyton614c7ef2015-09-21 20:41:31 +000016 array[i] = 0;
17
Jonathan Peyton37310762016-05-17 21:08:52 +000018 #pragma omp parallel
Jonathan Peyton614c7ef2015-09-21 20:41:31 +000019 {
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
Jonathan Peyton37310762016-05-17 21:08:52 +000025 * if the execution of the task takes longer than the time which is needed to
Jonathan Peyton614c7ef2015-09-21 20:41:31 +000026 * 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 */
Jonathan Peyton37310762016-05-17 21:08:52 +000038 for (i = 0; i < NUM_TASKS; i++)
Jonathan Peyton614c7ef2015-09-21 20:41:31 +000039 if (array[i] != 1)
40 result1++;
41
Jonathan Peyton37310762016-05-17 21:08:52 +000042 /* generate some more tasks which now shall overwrite
Jonathan Peyton614c7ef2015-09-21 20:41:31 +000043 * 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
63int 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}