xref: /llvm-project/openmp/runtime/test/tasking/issue-94260-2.c (revision 2e57e6366677390110f5382894c8afeba8da7419)
1 // RUN: %libomp-compile-and-run
2 
3 #include <stdio.h>
4 #include <omp.h>
5 
6 int test_omp_parallel_num_threads() {
7   int num_failed;
8   int threads;
9   int nthreads;
10   int max_threads = 0;
11 
12   num_failed = 0;
13 #pragma omp task
14   {}
15 
16 /* first we check how many threads are available */
17 #pragma omp parallel
18   {
19 #pragma omp task
20     {}
21 #pragma omp master
22     max_threads = omp_get_num_threads();
23   }
24 
25   /* we increase the number of threads from one to maximum:*/
26   for (threads = 1; threads <= max_threads; threads++) {
27     nthreads = 0;
28 #pragma omp parallel reduction(+ : num_failed) num_threads(threads)
29     {
30 #pragma omp task
31       {}
32       num_failed = num_failed + !(threads == omp_get_num_threads());
33 #pragma omp atomic
34       nthreads += 1;
35     }
36     num_failed = num_failed + !(nthreads == threads);
37   }
38   return (!num_failed);
39 }
40 
41 int main() {
42   int i;
43   int num_failed = 0;
44 
45   for (i = 0; i < 100; i++) {
46     if (!test_omp_parallel_num_threads()) {
47       num_failed++;
48     }
49   }
50   return num_failed;
51 }
52