1 // RUN: %libomp-compile-and-run
2 // RUN: %libomp-compile && env KMP_TASKLOOP_MIN_TASKS=1 %libomp-run
3 // REQUIRES: openmp-4.5
4 
5 // These compilers don't support the taskloop construct
6 // UNSUPPORTED: gcc-4, gcc-5, icc-16
7 // GCC 6 has support for taskloops, but at least 6.3.0 is crashing on this test
8 // UNSUPPORTED: gcc-6
9 
10 /*
11  * Test for taskloop
12  * Method: caculate how many times the iteration space is dispatched
13  *     and judge if each dispatch has the requested grainsize
14  * It is possible for two adjacent chunks are executed by the same thread
15  */
16 #include <stdio.h>
17 #include <omp.h>
18 #include <stdlib.h>
19 #include "omp_testsuite.h"
20 
21 #define CFDMAX_SIZE 1120
22 
23 int test_omp_taskloop_grainsize()
24 {
25   int result = 0;
26   int i, grainsize, count, tmp_count, num_off;
27   int *tmp, *tids, *tidsArray;
28 
29   tidsArray = (int *)malloc(sizeof(int) * CFDMAX_SIZE);
30   tids = tidsArray;
31 
32   for (grainsize = 1; grainsize < 48; ++grainsize) {
33     fprintf(stderr, "Grainsize %d\n", grainsize);
34     count = tmp_count = num_off = 0;
35 
36     for (i = 0; i < CFDMAX_SIZE; ++i) {
37       tids[i] = -1;
38     }
39 
40     #pragma omp parallel shared(tids)
41     {
42       #pragma omp master
43       #pragma omp taskloop grainsize(grainsize)
44       for (i = 0; i < CFDMAX_SIZE; i++) {
45         tids[i] = omp_get_thread_num();
46       }
47     }
48 
49     for (i = 0; i < CFDMAX_SIZE; ++i) {
50       if (tids[i] == -1) {
51         fprintf(stderr, "  Iteration %d not touched!\n", i);
52         result++;
53       }
54     }
55 
56     for (i = 0; i < CFDMAX_SIZE - 1; ++i) {
57       if (tids[i] != tids[i + 1]) {
58         count++;
59       }
60     }
61 
62     tmp = (int *)malloc(sizeof(int) * (count + 1));
63     tmp[0] = 1;
64 
65     for (i = 0; i < CFDMAX_SIZE - 1; ++i) {
66       if (tmp_count > count) {
67         printf("--------------------\nTestinternal Error: List too "
68                "small!!!\n--------------------\n");
69         break;
70       }
71       if (tids[i] != tids[i + 1]) {
72         tmp_count++;
73         tmp[tmp_count] = 1;
74       } else {
75         tmp[tmp_count]++;
76       }
77     }
78 
79     // is grainsize statement working?
80     int num_tasks = CFDMAX_SIZE / grainsize;
81     int multiple1 = CFDMAX_SIZE / num_tasks;
82     int multiple2 = CFDMAX_SIZE / num_tasks + 1;
83     for (i = 0; i < count; i++) {
84       // it is possible for 2 adjacent chunks assigned to a same thread
85       if (tmp[i] % multiple1 != 0 && tmp[i] % multiple2 != 0) {
86         num_off++;
87       }
88     }
89 
90     if (num_off > 1) {
91       fprintf(stderr, "  The number of bad chunks is %d\n", num_off);
92       result++;
93     } else {
94       fprintf(stderr, "  Everything ok\n");
95     }
96 
97     free(tmp);
98   }
99   free(tidsArray);
100   return (result==0);
101 }
102 
103 int main()
104 {
105   int i;
106   int num_failed=0;
107 
108   for (i = 0; i < REPETITIONS; i++) {
109     if (!test_omp_taskloop_grainsize()) {
110       num_failed++;
111     }
112   }
113   return num_failed;
114 }
115