Skip to content

Commit 446e11a

Browse files
authored
[OpenMP ]Adding more libomptarget reduction tests (#71616)
Based on #70766 I think it would be good to have a few more offloading reduction tests, so we do not accidentally break minimum and maximum reductions another time.
1 parent a070053 commit 446e11a

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// RUN: %libomptarget-compilexx-and-run-generic
2+
// RUN: %libomptarget-compileoptxx-and-run-generic
3+
4+
// FIXME: This is a bug in host offload, this should run fine.
5+
// UNSUPPORTED: aarch64-unknown-linux-gnu
6+
// UNSUPPORTED: aarch64-unknown-linux-gnu-LTO
7+
// UNSUPPORTED: x86_64-pc-linux-gnu
8+
// UNSUPPORTED: x86_64-pc-linux-gnu-LTO
9+
10+
// This test validates that the OpenMP target reductions to find a maximum work
11+
// as indended for a few common data types.
12+
13+
#include <algorithm>
14+
#include <cassert>
15+
#include <limits>
16+
#include <vector>
17+
18+
template <class Tp> void test_max_idx_reduction() {
19+
const Tp length = 1000;
20+
const Tp nmaximas = 8;
21+
std::vector<float> a(length, 3.0f);
22+
const Tp step = length / nmaximas;
23+
for (Tp i = 0; i < nmaximas; i++) {
24+
a[i * step] += 1.0f;
25+
}
26+
for (Tp i = nmaximas; i > 0; i--) {
27+
Tp idx = 0;
28+
float *b = a.data();
29+
#pragma omp target teams distribute parallel for reduction(max : idx) \
30+
map(always, to : b[0 : length])
31+
for (Tp j = 0; j < length - 1; j++) {
32+
if (b[j] > b[j + 1]) {
33+
idx = std::max(idx, j);
34+
}
35+
}
36+
assert(idx == (i - 1) * step &&
37+
"#pragma omp target teams distribute parallel for "
38+
"reduction(max:<identifier list>) does not work as intended.");
39+
a[idx] -= 1.0f;
40+
}
41+
}
42+
43+
template <class Tp> void test_max_val_reduction() {
44+
const int length = 1000;
45+
const int half = length / 2;
46+
std::vector<Tp> a(length, (Tp)3);
47+
a[half] += (Tp)1;
48+
Tp max_val = std::numeric_limits<Tp>::lowest();
49+
Tp *b = a.data();
50+
#pragma omp target teams distribute parallel for reduction(max : max_val) \
51+
map(always, to : b[0 : length])
52+
for (int i = 0; i < length; i++) {
53+
max_val = std::max(max_val, b[i]);
54+
}
55+
assert(std::abs(((double)a[half + 1]) - ((double)max_val) + 1.0) < 1e-6 &&
56+
"#pragma omp target teams distribute parallel for "
57+
"reduction(max:<identifier list>) does not work as intended.");
58+
}
59+
60+
int main() {
61+
// Reducing over indices
62+
test_max_idx_reduction<int>();
63+
test_max_idx_reduction<unsigned int>();
64+
test_max_idx_reduction<long>();
65+
66+
// Reducing over values
67+
test_max_val_reduction<int>();
68+
test_max_val_reduction<unsigned int>();
69+
test_max_val_reduction<long>();
70+
test_max_val_reduction<float>();
71+
test_max_val_reduction<double>();
72+
return 0;
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// RUN: %libomptarget-compilexx-and-run-generic
2+
// RUN: %libomptarget-compileoptxx-and-run-generic
3+
4+
// FIXME: This is a bug in host offload, this should run fine.
5+
// UNSUPPORTED: aarch64-unknown-linux-gnu
6+
// UNSUPPORTED: aarch64-unknown-linux-gnu-LTO
7+
// UNSUPPORTED: x86_64-pc-linux-gnu
8+
// UNSUPPORTED: x86_64-pc-linux-gnu-LTO
9+
10+
// This test validates that the OpenMP target reductions to find a minimum work
11+
// as indended for a few common data types.
12+
13+
#include <algorithm>
14+
#include <cassert>
15+
#include <limits>
16+
#include <vector>
17+
18+
template <class Tp> void test_min_idx_reduction() {
19+
const Tp length = 1000;
20+
const Tp nminimas = 8;
21+
std::vector<float> a(length, 3.0f);
22+
const Tp step = length / nminimas;
23+
for (Tp i = 0; i < nminimas; i++) {
24+
a[i * step] -= 1.0f;
25+
}
26+
for (Tp i = 0; i < nminimas; i++) {
27+
Tp idx = a.size();
28+
float *b = a.data();
29+
#pragma omp target teams distribute parallel for reduction(min : idx) \
30+
map(always, to : b[0 : length])
31+
for (Tp j = 0; j < length - 1; j++) {
32+
if (b[j] < b[j + 1]) {
33+
idx = std::min(idx, j);
34+
}
35+
}
36+
assert(idx == i * step &&
37+
"#pragma omp target teams distribute parallel for "
38+
"reduction(min:<identifier list>) does not work as intended.");
39+
a[idx] += 1.0f;
40+
}
41+
}
42+
43+
template <class Tp> void test_min_val_reduction() {
44+
const int length = 1000;
45+
const int half = length / 2;
46+
std::vector<Tp> a(length, (Tp)3);
47+
a[half] -= (Tp)1;
48+
Tp min_val = std::numeric_limits<Tp>::max();
49+
Tp *b = a.data();
50+
#pragma omp target teams distribute parallel for reduction(min : min_val) \
51+
map(always, to : b[0 : length])
52+
for (int i = 0; i < length; i++) {
53+
min_val = std::min(min_val, b[i]);
54+
}
55+
assert(std::abs(((double)a[half + 1]) - ((double)min_val) - 1.0) < 1e-6 &&
56+
"#pragma omp target teams distribute parallel for "
57+
"reduction(min:<identifier list>) does not work as intended.");
58+
}
59+
60+
int main() {
61+
// Reducing over indices
62+
test_min_idx_reduction<int>();
63+
test_min_idx_reduction<unsigned int>();
64+
test_min_idx_reduction<long>();
65+
66+
// Reducing over values
67+
test_min_val_reduction<int>();
68+
test_min_val_reduction<unsigned int>();
69+
test_min_val_reduction<long>();
70+
test_min_val_reduction<float>();
71+
test_min_val_reduction<double>();
72+
return 0;
73+
}

0 commit comments

Comments
 (0)