[RISCV] Rename a lambda to have plural nouns to reflect that it contains a loop. NFC
[llvm-project.git] / offload / test / offloading / complex_reduction.cpp
blob3239f48743ccd0652a6d6fb7586c8557b71f4002
1 // RUN: %libomptarget-compilexx-generic -O3 && %libomptarget-run-generic
2 // RUN: %libomptarget-compilexx-generic -O3 -ffast-math && %libomptarget-run-generic
4 #include <complex>
5 #include <iostream>
7 bool failed = false;
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; }
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;
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
27 template <typename T> class initiator {
28 public:
29 static T value(int i) { return T(i); }
32 template <typename T> class initiator<std::complex<T>> {
33 public:
34 static std::complex<T> value(int i) { return {T(i), T(-i)}; }
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];
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];
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;
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;
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;
82 template <typename T> void test_complex() {
83 test_map<T>();
84 test_reduction<std::complex<T>>();
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;