xref: /llvm-project/openmp/runtime/test/parallel/omp_parallel_num_threads_strict.c (revision d30b082fd4aeba0a3a99c3f17dbffe6691f859cc)
1*d30b082fSTerry Wilmarth // RUN: %libomp-compile && env OMP_NUM_THREADS=2,2,2,2,2 OMP_THREAD_LIMIT=16 \
2*d30b082fSTerry Wilmarth // RUN: %libomp-run
3*d30b082fSTerry Wilmarth #include <stdio.h>
4*d30b082fSTerry Wilmarth #include "omp_testsuite.h"
5*d30b082fSTerry Wilmarth 
6*d30b082fSTerry Wilmarth // When compiler supports num_threads clause list format and strict modifier,
7*d30b082fSTerry Wilmarth // remove the following and use num_threads clause directly
8*d30b082fSTerry Wilmarth #if defined(__cplusplus)
9*d30b082fSTerry Wilmarth extern "C" {
10*d30b082fSTerry Wilmarth #endif
11*d30b082fSTerry Wilmarth 
12*d30b082fSTerry Wilmarth int __kmpc_global_thread_num(void *loc);
13*d30b082fSTerry Wilmarth void __kmpc_push_num_threads_list(void *loc, int gtid, unsigned length,
14*d30b082fSTerry Wilmarth                                   int *list);
15*d30b082fSTerry Wilmarth void __kmpc_push_num_threads_strict(void *loc, int gtid, int nth, int sev,
16*d30b082fSTerry Wilmarth                                     const char *msg);
17*d30b082fSTerry Wilmarth void __kmpc_push_num_threads_list_strict(void *loc, int gtid, unsigned length,
18*d30b082fSTerry Wilmarth                                          int *list, int sev, const char *msg);
19*d30b082fSTerry Wilmarth 
20*d30b082fSTerry Wilmarth #if defined(__cplusplus)
21*d30b082fSTerry Wilmarth }
22*d30b082fSTerry Wilmarth #endif
23*d30b082fSTerry Wilmarth 
test_omp_parallel_num_threads_strict()24*d30b082fSTerry Wilmarth int test_omp_parallel_num_threads_strict() {
25*d30b082fSTerry Wilmarth   int num_failed = 0;
26*d30b082fSTerry Wilmarth 
27*d30b082fSTerry Wilmarth // Test regular runtime warning about exceeding thread limit.
28*d30b082fSTerry Wilmarth // Tolerate whatever value was given.
29*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed) num_threads(22)
30*d30b082fSTerry Wilmarth #pragma omp single
31*d30b082fSTerry Wilmarth   num_failed = num_failed + !(omp_get_num_threads() <= 22);
32*d30b082fSTerry Wilmarth 
33*d30b082fSTerry Wilmarth   // Test with 4 threads and strict -- no problem, no warning.
34*d30b082fSTerry Wilmarth   __kmpc_push_num_threads_strict(NULL, __kmpc_global_thread_num(NULL), 4, 1,
35*d30b082fSTerry Wilmarth                                  "This warning shouldn't happen.");
36*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed) // num_threads(strict:4)
37*d30b082fSTerry Wilmarth #pragma omp single
38*d30b082fSTerry Wilmarth   num_failed = num_failed + !(omp_get_num_threads() == 4);
39*d30b082fSTerry Wilmarth 
40*d30b082fSTerry Wilmarth   // Exceed limit, specify user warning message. Tolerate whatever was given.
41*d30b082fSTerry Wilmarth   __kmpc_push_num_threads_strict(NULL, __kmpc_global_thread_num(NULL), 20, 1,
42*d30b082fSTerry Wilmarth                                  "User-supplied warning for strict.");
43*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed)
44*d30b082fSTerry Wilmarth   // num_threads(strict:20) severity(warning)
45*d30b082fSTerry Wilmarth   // message("User-supplied warning for strict.")
46*d30b082fSTerry Wilmarth #pragma omp single
47*d30b082fSTerry Wilmarth   num_failed = num_failed + !(omp_get_num_threads() <= 20);
48*d30b082fSTerry Wilmarth 
49*d30b082fSTerry Wilmarth   // Exceed limit, no user message, use runtime default message for strict.
50*d30b082fSTerry Wilmarth   // Tolerate whatever value was given.
51*d30b082fSTerry Wilmarth   __kmpc_push_num_threads_strict(NULL, __kmpc_global_thread_num(NULL), 21, 1,
52*d30b082fSTerry Wilmarth                                  NULL);
53*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed) // num_threads(strict:21)
54*d30b082fSTerry Wilmarth #pragma omp single
55*d30b082fSTerry Wilmarth   num_failed = num_failed + !(omp_get_num_threads() <= 21);
56*d30b082fSTerry Wilmarth 
57*d30b082fSTerry Wilmarth   // Exceed limit at top level. Should see user warning message.
58*d30b082fSTerry Wilmarth   int threads3[2] = {24, 2};
59*d30b082fSTerry Wilmarth   __kmpc_push_num_threads_list_strict(NULL, __kmpc_global_thread_num(NULL), 2,
60*d30b082fSTerry Wilmarth                                       threads3, 1,
61*d30b082fSTerry Wilmarth                                       "User-supplied warning on strict list.");
62*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed)
63*d30b082fSTerry Wilmarth   // num_threads(strict:24,2)  severity(warning)
64*d30b082fSTerry Wilmarth   // message("User-supplied warning on strict. list") // 1st level
65*d30b082fSTerry Wilmarth   {
66*d30b082fSTerry Wilmarth #pragma omp single
67*d30b082fSTerry Wilmarth     num_failed = num_failed + !(omp_get_num_threads() <= 24);
68*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed) // 2nd level
69*d30b082fSTerry Wilmarth     {
70*d30b082fSTerry Wilmarth #pragma omp single
71*d30b082fSTerry Wilmarth       num_failed = num_failed + !(omp_get_num_threads() <= 2);
72*d30b082fSTerry Wilmarth     }
73*d30b082fSTerry Wilmarth   }
74*d30b082fSTerry Wilmarth 
75*d30b082fSTerry Wilmarth   // No strict limit at top level. Regular runtime limiting applies.
76*d30b082fSTerry Wilmarth   __kmpc_push_num_threads_list(NULL, __kmpc_global_thread_num(NULL), 2,
77*d30b082fSTerry Wilmarth                                threads3);
78*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed)
79*d30b082fSTerry Wilmarth   // num_threads(24,2) // 1st level
80*d30b082fSTerry Wilmarth   {
81*d30b082fSTerry Wilmarth #pragma omp single
82*d30b082fSTerry Wilmarth     num_failed = num_failed + !(omp_get_num_threads() <= 24);
83*d30b082fSTerry Wilmarth #pragma omp parallel reduction(+ : num_failed) // 2nd level
84*d30b082fSTerry Wilmarth     {
85*d30b082fSTerry Wilmarth #pragma omp single
86*d30b082fSTerry Wilmarth       num_failed = num_failed + !(omp_get_num_threads() <= 2);
87*d30b082fSTerry Wilmarth     }
88*d30b082fSTerry Wilmarth   }
89*d30b082fSTerry Wilmarth 
90*d30b082fSTerry Wilmarth   return (!num_failed);
91*d30b082fSTerry Wilmarth }
92*d30b082fSTerry Wilmarth 
main()93*d30b082fSTerry Wilmarth int main() {
94*d30b082fSTerry Wilmarth   int i;
95*d30b082fSTerry Wilmarth   int num_failed = 0;
96*d30b082fSTerry Wilmarth 
97*d30b082fSTerry Wilmarth   for (i = 0; i < REPETITIONS; i++) {
98*d30b082fSTerry Wilmarth     if (!test_omp_parallel_num_threads_strict()) {
99*d30b082fSTerry Wilmarth       num_failed++;
100*d30b082fSTerry Wilmarth     }
101*d30b082fSTerry Wilmarth   }
102*d30b082fSTerry Wilmarth   return num_failed;
103*d30b082fSTerry Wilmarth }
104