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(nthreads: 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, func: test_nest, args: 4, args: 3); |
61 | run_ntimes(NTIMES, func: test_nest, args: 2, args: 1); |
62 | } |
63 | |
64 | printf(format: "PASS\n" ); |
65 | return EXIT_SUCCESS; |
66 | } |
67 | |