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