1// RUN: %libomp-cxx-compile -fopenmp-version=51
2// RUN: %libomp-run | FileCheck %s --check-prefix OMP51
3
4#include <stdio.h>
5#include <omp.h>
6
7void foo() {
8#pragma omp parallel num_threads(10)
9 { printf(format: "\ntarget: foo(): parallel num_threads(10)"); }
10}
11
12int main(void) {
13
14 int tl = 4;
15 printf("\nmain: thread_limit = %d", omp_get_thread_limit());
16 // OMP51: main: thread_limit = {{[0-9]+}}
17
18#pragma omp target thread_limit(tl)
19 {
20 printf("\ntarget: thread_limit = %d", omp_get_thread_limit());
21 int count = 0;
22// OMP51: target: thread_limit = 4
23// check whether thread_limit is honoured
24#pragma omp parallel reduction(+:count)
25 { count++; }
26 printf(format: "\ntarget: parallel: count = %d", count);
27// OMP51: target: parallel: count = {{(1|2|3|4)$}}
28
29// check whether num_threads is honoured
30#pragma omp parallel num_threads(2)
31 { printf(format: "\ntarget: parallel num_threads(2)"); }
32// OMP51: target: parallel num_threads(2)
33// OMP51: target: parallel num_threads(2)
34// OMP51-NOT: target: parallel num_threads(2)
35
36// check whether thread_limit is honoured when there is a conflicting
37// num_threads
38#pragma omp parallel num_threads(10)
39 { printf(format: "\ntarget: parallel num_threads(10)"); }
40 // OMP51: target: parallel num_threads(10)
41 // OMP51: target: parallel num_threads(10)
42 // OMP51: target: parallel num_threads(10)
43 // OMP51: target: parallel num_threads(10)
44 // OMP51-NOT: target: parallel num_threads(10)
45
46 // check whether threads are limited across functions
47 foo();
48 // OMP51: target: foo(): parallel num_threads(10)
49 // OMP51: target: foo(): parallel num_threads(10)
50 // OMP51: target: foo(): parallel num_threads(10)
51 // OMP51: target: foo(): parallel num_threads(10)
52 // OMP51-NOT: target: foo(): parallel num_threads(10)
53
54 // check if user can set num_threads at runtime
55 omp_set_num_threads(2);
56#pragma omp parallel
57 { printf(format: "\ntarget: parallel with omp_set_num_thread(2)"); }
58 // OMP51: target: parallel with omp_set_num_thread(2)
59 // OMP51: target: parallel with omp_set_num_thread(2)
60 // OMP51-NOT: target: parallel with omp_set_num_thread(2)
61
62 // make sure thread_limit is unaffected by omp_set_num_threads
63 printf("\ntarget: thread_limit = %d", omp_get_thread_limit());
64 // OMP51: target: thread_limit = 4
65 }
66
67// checking consecutive target regions with different thread_limits
68#pragma omp target thread_limit(3)
69 {
70 printf("\nsecond target: thread_limit = %d", omp_get_thread_limit());
71 int count = 0;
72// OMP51: second target: thread_limit = 3
73#pragma omp parallel reduction(+:count)
74 { count++; }
75 printf(format: "\nsecond target: parallel: count = %d", count);
76 // OMP51: second target: parallel: count = {{(1|2|3)$}}
77 }
78
79 // confirm that thread_limit's effects are limited to target region
80 printf("\nmain: thread_limit = %d", omp_get_thread_limit());
81 // OMP51: main: thread_limit = {{[0-9]+}}
82#pragma omp parallel num_threads(10)
83 { printf(format: "\nmain: parallel num_threads(10)"); }
84 // OMP51: main: parallel num_threads(10)
85 // OMP51: main: parallel num_threads(10)
86 // OMP51: main: parallel num_threads(10)
87 // OMP51: main: parallel num_threads(10)
88 // OMP51: main: parallel num_threads(10)
89 // OMP51: main: parallel num_threads(10)
90 // OMP51: main: parallel num_threads(10)
91 // OMP51: main: parallel num_threads(10)
92 // OMP51: main: parallel num_threads(10)
93 // OMP51: main: parallel num_threads(10)
94 // OMP51-NOT: main: parallel num_threads(10)
95
96// check combined target directives which support thread_limit
97// target parallel
98#pragma omp target parallel thread_limit(2)
99 printf(format: "\ntarget parallel thread_limit(2)");
100 // OMP51: target parallel thread_limit(2)
101 // OMP51: target parallel thread_limit(2)
102 // OMP51-NOT: target parallel thread_limit(2)
103
104#pragma omp target parallel num_threads(2) thread_limit(3)
105 printf(format: "\ntarget parallel num_threads(2) thread_limit(3)");
106 // OMP51: target parallel num_threads(2) thread_limit(3)
107 // OMP51: target parallel num_threads(2) thread_limit(3)
108 // OMP51-NOT: target parallel num_threads(2) thread_limit(3)
109
110#pragma omp target parallel num_threads(3) thread_limit(2)
111 printf(format: "\ntarget parallel num_threads(3) thread_limit(2)");
112 // OMP51: target parallel num_threads(3) thread_limit(2)
113 // OMP51: target parallel num_threads(3) thread_limit(2)
114 // OMP51-NOT: target parallel num_threads(3) thread_limit(2)
115
116// target parallel for
117#pragma omp target parallel for thread_limit(2)
118 for (int i = 0; i < 5; ++i)
119 printf("\ntarget parallel for thread_limit(2) : thread num = %d",
120 omp_get_thread_num());
121 // OMP51: target parallel for thread_limit(2) : thread num = {{0|1}}
122 // OMP51: target parallel for thread_limit(2) : thread num = {{0|1}}
123 // OMP51: target parallel for thread_limit(2) : thread num = {{0|1}}
124 // OMP51: target parallel for thread_limit(2) : thread num = {{0|1}}
125 // OMP51: target parallel for thread_limit(2) : thread num = {{0|1}}
126 // OMP51-NOT: target parallel for thread_limit(3) : thread num = {{0|1}}
127
128// target parallel for simd
129#pragma omp target parallel for simd thread_limit(2)
130 for (int i = 0; i < 5; ++i)
131 printf("\ntarget parallel for simd thread_limit(2) : thread num = %d",
132 omp_get_thread_num());
133 // OMP51: target parallel for simd thread_limit(2) : thread num = {{0|1}}
134 // OMP51: target parallel for simd thread_limit(2) : thread num = {{0|1}}
135 // OMP51: target parallel for simd thread_limit(2) : thread num = {{0|1}}
136 // OMP51: target parallel for simd thread_limit(2) : thread num = {{0|1}}
137 // OMP51: target parallel for simd thread_limit(2) : thread num = {{0|1}}
138 // OMP51-NOT: target parallel for simd thread_limit(2) : thread num =
139 // {{0|1}}
140
141// target simd
142#pragma omp target simd thread_limit(2)
143 for (int i = 0; i < 5; ++i)
144 printf("\ntarget simd thread_limit(2) : thread num = %d",
145 omp_get_thread_num());
146 // OMP51: target simd thread_limit(2) : thread num = {{0|1}}
147 // OMP51: target simd thread_limit(2) : thread num = {{0|1}}
148 // OMP51: target simd thread_limit(2) : thread num = {{0|1}}
149 // OMP51: target simd thread_limit(2) : thread num = {{0|1}}
150 // OMP51: target simd thread_limit(2) : thread num = {{0|1}}
151 // OMP51-NOT: target simd thread_limit(2) : thread num = {{0|1}}
152
153// target parallel loop
154#pragma omp target parallel loop thread_limit(2)
155 for (int i = 0; i < 5; ++i)
156 printf("\ntarget parallel loop thread_limit(2) : thread num = %d",
157 omp_get_thread_num());
158 // # OMP51: target parallel loop thread_limit(2) : thread num = {{0|1}}
159 // # OMP51: target parallel loop thread_limit(2) : thread num = {{0|1}}
160 // # OMP51: target parallel loop thread_limit(2) : thread num = {{0|1}}
161 // # OMP51: target parallel loop thread_limit(2) : thread num = {{0|1}}
162 // # OMP51: target parallel loop thread_limit(2) : thread num = {{0|1}}
163 // # OMP51-NOT: target parallel loop thread_limit(2) : thread num = {{0|1}}
164 return 0;
165}
166

source code of openmp/runtime/test/target/target_thread_limit.cpp