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