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
7bool failed = false;
8
9template <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
27template <typename T> class initiator {
28public:
29 static T value(int i) { return T(i); }
30};
31
32template <typename T> class initiator<std::complex<T>> {
33public:
34 static std::complex<T> value(int i) { return {T(i), T(-i)}; }
35};
36
37template <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
82template <typename T> void test_complex() {
83 test_map<T>();
84 test_reduction<std::complex<T>>();
85}
86
87int 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}
95

source code of offload/test/offloading/complex_reduction.cpp