Skip to content

Commit fb1b801

Browse files
committedSep 29, 2018
[libomptarget-nvptx] Add tests for nested parallelism
Clang trunk will serialize nested parallel regions. Check that this is correctly reflected in various API methods. Differential Revision: https://reviews.llvm.org/D51786 llvm-svn: 343382
1 parent c89a14f commit fb1b801

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
 
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %compile-run-and-check
2+
3+
#include <omp.h>
4+
#include <stdio.h>
5+
6+
const int MaxThreads = 1024;
7+
const int NumThreads = 64;
8+
9+
int main(int argc, char *argv[]) {
10+
int level = -1, activeLevel = -1;
11+
int check1[MaxThreads];
12+
int check2[MaxThreads];
13+
for (int i = 0; i < MaxThreads; i++) {
14+
check1[i] = check2[i] = 0;
15+
}
16+
17+
#pragma omp target map(level, activeLevel, check1[:], check2[:])
18+
{
19+
level = omp_get_level();
20+
activeLevel = omp_get_active_level();
21+
22+
// Expecting active parallel region.
23+
#pragma omp parallel num_threads(NumThreads)
24+
{
25+
int id = omp_get_thread_num();
26+
// Multiply return value of omp_get_level by 5 to avoid that this test
27+
// passes if both API calls return wrong values.
28+
check1[id] += omp_get_level() * 5 + omp_get_active_level();
29+
30+
// Expecting serialized parallel region.
31+
#pragma omp parallel
32+
{
33+
#pragma omp atomic
34+
check2[id] += omp_get_level() * 5 + omp_get_active_level();
35+
}
36+
}
37+
}
38+
39+
// CHECK: target: level = 0, activeLevel = 0
40+
printf("target: level = %d, activeLevel = %d\n", level, activeLevel);
41+
42+
// CHECK-NOT: invalid
43+
for (int i = 0; i < MaxThreads; i++) {
44+
// Check active parallel region:
45+
// omp_get_level() = 1, omp_get_active_level() = 1
46+
const int Expected1 = 6;
47+
48+
if (i < NumThreads) {
49+
if (check1[i] != Expected1) {
50+
printf("invalid: check1[%d] should be %d, is %d\n", i, Expected1, check1[i]);
51+
}
52+
} else if (check1[i] != 0) {
53+
printf("invalid: check1[%d] should be 0, is %d\n", i, check1[i]);
54+
}
55+
56+
// Check serialized parallel region:
57+
// omp_get_level() = 2, omp_get_active_level() = 1
58+
const int Expected2 = 11;
59+
if (i < NumThreads) {
60+
if (check2[i] != Expected2) {
61+
printf("invalid: check2[%d] should be %d, is %d\n", i, Expected2, check2[i]);
62+
}
63+
} else if (check2[i] != 0) {
64+
printf("invalid: check2[%d] should be 0, is %d\n", i, check2[i]);
65+
}
66+
}
67+
68+
return 0;
69+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// RUN: %compile-run-and-check
2+
3+
#include <omp.h>
4+
#include <stdio.h>
5+
6+
const int MaxThreads = 1024;
7+
const int NumThreads = 64;
8+
9+
int main(int argc, char *argv[]) {
10+
int inParallel = -1, numThreads = -1, threadNum = -1;
11+
int check1[MaxThreads];
12+
int check2[MaxThreads];
13+
for (int i = 0; i < MaxThreads; i++) {
14+
check1[i] = check2[i] = 0;
15+
}
16+
17+
#pragma omp target map(inParallel, numThreads, threadNum, check1[:], check2[:])
18+
{
19+
inParallel = omp_in_parallel();
20+
numThreads = omp_get_num_threads();
21+
threadNum = omp_get_thread_num();
22+
23+
// Expecting active parallel region.
24+
#pragma omp parallel num_threads(NumThreads)
25+
{
26+
int id = omp_get_thread_num();
27+
check1[id] += omp_get_num_threads() + omp_in_parallel();
28+
29+
// Expecting serialized parallel region.
30+
#pragma omp parallel
31+
{
32+
// Expected to be 1.
33+
int nestedInParallel = omp_in_parallel();
34+
// Expected to be 1.
35+
int nestedNumThreads = omp_get_num_threads();
36+
// Expected to be 0.
37+
int nestedThreadNum = omp_get_thread_num();
38+
#pragma omp atomic
39+
check2[id] += nestedInParallel + nestedNumThreads + nestedThreadNum;
40+
}
41+
}
42+
}
43+
44+
// CHECK: target: inParallel = 0, numThreads = 1, threadNum = 0
45+
printf("target: inParallel = %d, numThreads = %d, threadNum = %d\n",
46+
inParallel, numThreads, threadNum);
47+
48+
// CHECK-NOT: invalid
49+
for (int i = 0; i < MaxThreads; i++) {
50+
// Check that all threads reported
51+
// omp_get_num_threads() = 64, omp_in_parallel() = 1.
52+
int Expected = NumThreads + 1;
53+
if (i < NumThreads) {
54+
if (check1[i] != Expected) {
55+
printf("invalid: check1[%d] should be %d, is %d\n", i, Expected, check1[i]);
56+
}
57+
} else if (check1[i] != 0) {
58+
printf("invalid: check1[%d] should be 0, is %d\n", i, check1[i]);
59+
}
60+
61+
// Check serialized parallel region.
62+
if (i < NumThreads) {
63+
if (check2[i] != 2) {
64+
printf("invalid: check2[%d] should be 2, is %d\n", i, check2[i]);
65+
}
66+
} else if (check2[i] != 0) {
67+
printf("invalid: check2[%d] should be 0, is %d\n", i, check2[i]);
68+
}
69+
}
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)
Please sign in to comment.