xref: /llvm-project/openmp/runtime/test/tasking/issue-94260-1.cpp (revision 2e57e6366677390110f5382894c8afeba8da7419)
1 // RUN: %libomp-cxx-compile-and-run
2 
3 #include <assert.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <omp.h>
7 
8 // The number of times to run each test
9 #define NTIMES 2
10 
11 // Every thread creates a single "increment" task
12 void test_tasks() {
13   for (int i = 0; i < 100; ++i)
14 #pragma omp task
15   {
16     int tid = omp_get_thread_num();
17   }
18 }
19 
20 // Testing single level of parallelism with increment tasks
21 void test_base(int nthreads) {
22 #ifdef VERBOSE
23 #pragma omp master
24   printf("    test_base(%d)\n", nthreads);
25 #endif
26 #pragma omp parallel num_threads(nthreads)
27   { test_tasks(); }
28 }
29 
30 // Testing nested parallel with increment tasks
31 // first = nthreads of outer parallel
32 // second = nthreads of nested parallel
33 void test_nest(int first, int second) {
34 #ifdef VERBOSE
35 #pragma omp master
36   printf("   test_nest(%d, %d)\n", first, second);
37 #endif
38 #pragma omp parallel num_threads(first)
39   {
40     for (int i = 0; i < 100; ++i)
41 #pragma omp task
42     {
43       int tid = omp_get_thread_num();
44     }
45     test_base(second);
46   }
47 }
48 
49 template <typename... Args>
50 void run_ntimes(int n, void (*func)(Args...), Args... args) {
51   for (int i = 0; i < n; ++i) {
52     func(args...);
53   }
54 }
55 
56 int main() {
57   omp_set_max_active_levels(5);
58 
59   for (int i = 0; i < 100; ++i) {
60     run_ntimes(NTIMES, test_nest, 4, 3);
61     run_ntimes(NTIMES, test_nest, 2, 1);
62   }
63 
64   printf("PASS\n");
65   return EXIT_SUCCESS;
66 }
67