1 // RUN: %libomp-compile-and-run
2 // RUN: %libomp-compile && env KMP_TASKLOOP_MIN_TASKS=1 %libomp-run
3 
4 // These compilers don't support the taskloop construct
5 // UNSUPPORTED: gcc-4, gcc-5, icc-16
6 
7 /*
8  * Test for taskloop
9  * Method: caculate how many times the iteration space is dispatched
10  *     and judge if each dispatch has the requested grainsize
11  * It is possible for two adjacent chunks are executed by the same thread
12  */
13 #include <stdio.h>
14 #include <omp.h>
15 #include <stdlib.h>
16 #include "omp_testsuite.h"
17 
18 #define CFDMAX_SIZE 1120
19 
20 int test_omp_taskloop_num_tasks()
21 {
22   int i;
23   int *tids;
24   int *tidsArray;
25   int count;
26   int result = 0;
27   int num_tasks;
28 
29   for (num_tasks = 1; num_tasks < 120; ++num_tasks) {
30     count = 0;
31     tidsArray = (int *)malloc(sizeof(int) * CFDMAX_SIZE);
32     tids = tidsArray;
33 
34     #pragma omp parallel shared(tids)
35     {
36       int i;
37       #pragma omp master
38       #pragma omp taskloop num_tasks(num_tasks)
39       for (i = 0; i < CFDMAX_SIZE; i++) {
40         tids[i] = omp_get_thread_num();
41       }
42     }
43 
44     for (i = 0; i < CFDMAX_SIZE - 1; ++i) {
45       if (tids[i] != tids[i + 1]) {
46         count++;
47       }
48     }
49 
50     if (count > num_tasks) {
51       fprintf(stderr, "counted too many tasks: (wanted %d, got %d)\n",
52               num_tasks, count);
53       result++;
54     }
55   }
56 
57   return (result==0);
58 }
59 
60 int main()
61 {
62   int i;
63   int num_failed=0;
64 
65   for (i = 0; i < REPETITIONS; i++) {
66     if (!test_omp_taskloop_num_tasks()) {
67       num_failed++;
68     }
69   }
70   return num_failed;
71 }
72