xref: /llvm-project/openmp/runtime/test/worksharing/for/omp_for_schedule_dynamic.c (revision 5cc603cb2244bb46683e4ce801fdf224aa2d1636)
1 // RUN: %libomp-compile-and-run
2 // RUN: env KMP_AFFINITY=compact,0 %libomp-run
3 /*
4  * Test for dynamic scheduling with chunk size
5  * Method: calculate how many times the iteration space is dispatched
6  *     and judge if each dispatch has the requested chunk size
7  *     unless it is the last one.
8  * It is possible for two adjacent chunks are assigned to the same thread
9  * Modified by Chunhua Liao
10  */
11 #include <stdio.h>
12 #include <omp.h>
13 #include <stdlib.h>
14 #include "omp_testsuite.h"
15 
16 #define CFDMAX_SIZE 100
17 const int chunk_size = 7;
18 
test_omp_for_schedule_dynamic()19 int test_omp_for_schedule_dynamic()
20 {
21   int tid;
22   int *tids;
23   int i;
24   int tidsArray[CFDMAX_SIZE];
25   int count = 0;
26   int tmp_count = 0; /*dispatch times*/
27   int *tmp;  /*store chunk size for each dispatch*/
28   int result = 0;
29 
30   tids = tidsArray;
31 
32   #pragma omp parallel private(tid) shared(tids)
33   {        /* begin of parallel */
34     int tid;
35     tid = omp_get_thread_num ();
36     #pragma omp for schedule(dynamic,chunk_size)
37     for (i = 0; i < CFDMAX_SIZE; i++) {
38       tids[i] = tid;
39     }
40   }
41 
42   for (i = 0; i < CFDMAX_SIZE - 1; ++i) {
43     if (tids[i] != tids[i + 1]) {
44       count++;
45     }
46   }
47 
48   tmp = (int *) malloc (sizeof (int) * (count + 1));
49   tmp[0] = 1;
50 
51   for (i = 0; i < CFDMAX_SIZE - 1; ++i) {
52     if (tmp_count > count) {
53       printf ("--------------------\nTestinternal Error: List too small!!!\n--------------------\n");  /* Error handling */
54       break;
55     }
56     if (tids[i] != tids[i + 1]) {
57       tmp_count++;
58       tmp[tmp_count] = 1;
59     } else {
60       tmp[tmp_count]++;
61     }
62   }
63   /* is dynamic statement working? */
64   for (i = 0; i < count; i++) {
65     if ((tmp[i]%chunk_size)!=0) {
66       /* it is possible for 2 adjacent chunks assigned to a same thread */
67       result++;
68       fprintf(stderr,"The intermediate dispatch has wrong chunksize.\n");
69       /* result += ((tmp[i] / chunk_size) - 1); */
70     }
71   }
72   if ((tmp[count]%chunk_size)!=(CFDMAX_SIZE%chunk_size)) {
73     result++;
74     fprintf(stderr,"the last dispatch has wrong chunksize.\n");
75   }
76   /* for (int i=0;i<count+1;++i) printf("%d\t:=\t%d\n",i+1,tmp[i]); */
77   return (result==0);
78 }
main()79 int main()
80 {
81   int i;
82   int num_failed=0;
83 
84   for(i = 0; i < REPETITIONS; i++) {
85     if(!test_omp_for_schedule_dynamic()) {
86       num_failed++;
87     }
88   }
89   return num_failed;
90 }
91