Skip to content

Commit d3ebce9

Browse files
committed
[OpenMP] add offload tests with reduction on complex data types
Differential Revision: https://reviews.llvm.org/D139856
1 parent aea5980 commit d3ebce9

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

openmp/libomptarget/test/offloading/bug49021.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// RUN: %libomptarget-compilexx-generic -O3 && %libomptarget-run-generic
2-
3-
// Hangs
4-
// UNSUPPORTED: amdgcn-amd-amdhsa
5-
// UNSUPPORTED: amdgcn-amd-amdhsa-LTO
2+
// RUN: %libomptarget-compilexx-generic -O3 -ffast-math && %libomptarget-run-generic
63

74
#include <iostream>
85

96
template <typename T> int test_map() {
10-
std::cout << "map(complex<>)" << std::endl;
7+
std::cout << "map(T)" << std::endl;
118
T a(0.2), a_check;
129
#pragma omp target map(from : a_check)
1310
{ a_check = a; }
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// RUN: %libomptarget-compilexx-generic -O3 && %libomptarget-run-generic
2+
// RUN: %libomptarget-compilexx-generic -O3 -ffast-math && %libomptarget-run-generic
3+
4+
#include <complex>
5+
#include <iostream>
6+
7+
bool failed = false;
8+
9+
template <typename T> void test_map() {
10+
std::cout << "map(complex<T>)" << std::endl;
11+
std::complex<T> a(0.2, 1), a_check;
12+
#pragma omp target map(from : a_check)
13+
{ a_check = a; }
14+
15+
if (std::abs(a - a_check) > 1e-6) {
16+
std::cout << "wrong map value check" << a_check << " correct value " << a
17+
<< std::endl;
18+
failed = true;
19+
}
20+
}
21+
22+
#if !defined(__NO_UDR)
23+
#pragma omp declare reduction(+ : std::complex <float> : omp_out += omp_in)
24+
#pragma omp declare reduction(+ : std::complex <double> : omp_out += omp_in)
25+
#endif
26+
27+
template <typename T> class initiator {
28+
public:
29+
static T value(int i) { return T(i); }
30+
};
31+
32+
template <typename T> class initiator<std::complex<T>> {
33+
public:
34+
static std::complex<T> value(int i) { return {T(i), T(-i)}; }
35+
};
36+
37+
template <typename T> void test_reduction() {
38+
T sum(0), sum_host(0);
39+
const int size = 100;
40+
T array[size];
41+
for (int i = 0; i < size; i++) {
42+
array[i] = initiator<T>::value(i);
43+
sum_host += array[i];
44+
}
45+
46+
#pragma omp target teams distribute parallel for map(to : array[:size]) reduction(+ : sum)
47+
for (int i = 0; i < size; i++)
48+
sum += array[i];
49+
50+
if (std::abs(sum - sum_host) > 1e-6) {
51+
std::cout << "wrong reduction value check" << sum << " correct value "
52+
<< sum_host << std::endl;
53+
failed = true;
54+
}
55+
56+
const int nblock(10), block_size(10);
57+
T block_sum[nblock];
58+
#pragma omp target teams distribute map(to \
59+
: array[:size]) \
60+
map(from \
61+
: block_sum[:nblock])
62+
for (int ib = 0; ib < nblock; ib++) {
63+
T partial_sum(0);
64+
const int istart = ib * block_size;
65+
const int iend = (ib + 1) * block_size;
66+
#pragma omp parallel for reduction(+ : partial_sum)
67+
for (int i = istart; i < iend; i++)
68+
partial_sum += array[i];
69+
block_sum[ib] = partial_sum;
70+
}
71+
72+
sum = 0;
73+
for (int ib = 0; ib < nblock; ib++)
74+
sum += block_sum[ib];
75+
if (std::abs(sum - sum_host) > 1e-6) {
76+
std::cout << "hierarchical parallelism wrong reduction value check" << sum
77+
<< " correct value " << sum_host << std::endl;
78+
failed = true;
79+
}
80+
}
81+
82+
template <typename T> void test_complex() {
83+
test_map<T>();
84+
test_reduction<std::complex<T>>();
85+
}
86+
87+
int main() {
88+
std::cout << "Testing complex" << std::endl;
89+
std::cout << "Testing float" << std::endl;
90+
test_complex<float>();
91+
std::cout << "Testing double" << std::endl;
92+
test_complex<double>();
93+
return failed;
94+
}

0 commit comments

Comments
 (0)