xref: /llvm-project/openmp/runtime/test/worksharing/for/kmp_set_dispatch_buf.c (revision 5dd4d0d46fb892975bbb3a086da5a3a9996ced4d)
1 // RUN: %libomp-compile && %libomp-run 7
2 // RUN: %libomp-run 0 && %libomp-run -1
3 // RUN: %libomp-run 1 && %libomp-run 2 && %libomp-run 5
4 // RUN: %libomp-compile -DMY_SCHEDULE=guided && %libomp-run 7
5 // RUN: %libomp-run 1 && %libomp-run 2 && %libomp-run 5
6 // UNSUPPORTED: clang-11, clang-12
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 #ifndef MY_SCHEDULE
17 # define MY_SCHEDULE dynamic
18 #endif
19 
20 int num_disp_buffers, num_loops;
21 int a, b, a_known_value, b_known_value;
22 
test_kmp_set_disp_num_buffers()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 
main(int argc,char ** argv)54 int main(int argc, char** argv)
55 {
56   int i,j;
57   int num_failed=0;
58 
59   if (argc != 2) {
60     fprintf(stderr, "usage: %s num_disp_buffers\n", argv[0]);
61     exit(1);
62   }
63 
64   // set the number of dispatch buffers
65   num_disp_buffers = atoi(argv[1]);
66   kmp_set_disp_num_buffers(num_disp_buffers);
67 
68   // figure out the known values to compare with calculated result
69   a_known_value = 0;
70   b_known_value = 0;
71 
72   // if specified to use bad num_disp_buffers set num_loops
73   // to something reasonable
74   if (num_disp_buffers <= 0)
75     num_loops = 10;
76   else
77     num_loops = num_disp_buffers*10;
78 
79   for (j = 0; j < num_loops; j++) {
80     for (i = MY_MIN; i < MY_MAX; i+=INCR)
81       a_known_value++;
82     for (i = MY_MAX; i >= MY_MIN; i-=INCR)
83       b_known_value++;
84   }
85 
86   for(i = 0; i < REPETITIONS; i++) {
87     if(!test_kmp_set_disp_num_buffers()) {
88       num_failed++;
89     }
90   }
91   if (num_failed == 0)
92     printf("passed\n");
93   else
94     printf("failed %d\n", num_failed);
95   return num_failed;
96 }
97