1 // RUN: %libomp-compile && env KMP_DISP_NUM_BUFFERS=0 %libomp-run
2 // RUN: env KMP_DISP_NUM_BUFFERS=1 %libomp-run && env KMP_DISP_NUM_BUFFERS=3 %libomp-run
3 // RUN: env KMP_DISP_NUM_BUFFERS=4 %libomp-run && env KMP_DISP_NUM_BUFFERS=7 %libomp-run
4 // RUN: %libomp-compile -DMY_SCHEDULE=guided && env KMP_DISP_NUM_BUFFERS=1 %libomp-run
5 // RUN: env KMP_DISP_NUM_BUFFERS=3 %libomp-run && env KMP_DISP_NUM_BUFFERS=4 %libomp-run
6 // RUN: env KMP_DISP_NUM_BUFFERS=7 %libomp-run
7 #include <stdio.h>
8 #include <omp.h>
9 #include <stdlib.h>
10 #include <limits.h>
11 #include "omp_testsuite.h"
12 
13 #define INCR 7
14 #define MY_MAX 200
15 #define MY_MIN -200
16 #define NUM_LOOPS 100
17 #ifndef MY_SCHEDULE
18 # define MY_SCHEDULE dynamic
19 #endif
20 
21 int a, b, a_known_value, b_known_value;
22 
23 int test_kmp_set_disp_num_buffers()
24 {
25   int success = 1;
26   a = 0;
27   b = 0;
28   // run many small dynamic loops to stress the dispatch buffer system
29   #pragma omp parallel
30   {
31     int i,j;
32     for (j = 0; j < NUM_LOOPS; j++) {
33       #pragma omp for schedule(MY_SCHEDULE) nowait
34       for (i = MY_MIN; i < MY_MAX; i+=INCR) {
35         #pragma omp atomic
36         a++;
37       }
38       #pragma omp for schedule(MY_SCHEDULE) nowait
39       for (i = MY_MAX; i >= MY_MIN; i-=INCR) {
40         #pragma omp atomic
41         b++;
42       }
43     }
44   }
45   // detect failure
46   if (a != a_known_value || b != b_known_value) {
47     success = 0;
48     printf("a = %d (should be %d), b = %d (should be %d)\n", a, a_known_value,
49            b, b_known_value);
50   }
51   return success;
52 }
53 
54 int main(int argc, char** argv)
55 {
56   int i,j;
57   int num_failed=0;
58 
59   // figure out the known values to compare with calculated result
60   a_known_value = 0;
61   b_known_value = 0;
62 
63   for (j = 0; j < NUM_LOOPS; j++) {
64     for (i = MY_MIN; i < MY_MAX; i+=INCR)
65       a_known_value++;
66     for (i = MY_MAX; i >= MY_MIN; i-=INCR)
67       b_known_value++;
68   }
69 
70   for(i = 0; i < REPETITIONS; i++) {
71     if(!test_kmp_set_disp_num_buffers()) {
72       num_failed++;
73     }
74   }
75   return num_failed;
76 }
77