|
| 1 | +// Copyright (c) 2022 Hartmut Kaiser |
| 2 | + |
| 3 | +#include <hpx/execution.hpp> |
| 4 | +#include <hpx/hpx_main.hpp> |
| 5 | + |
| 6 | +#include <experimental/linalg> |
| 7 | +#include <experimental/mdspan> |
| 8 | + |
| 9 | +#include <cstddef> |
| 10 | +#include <execution> |
| 11 | +#include <iostream> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +template <class T1, class ScalarType> |
| 15 | +void print_elements( |
| 16 | + const T1& v, const std::vector<ScalarType>& gold, char const* policy_str) |
| 17 | +{ |
| 18 | + std::cout << "Using policy: " << policy_str << "\n"; |
| 19 | + for (std::size_t i = 0; i < v.size(); i++) |
| 20 | + { |
| 21 | + std::cout << "computed = " << v(i) << " , gold = " << gold[i] << "\n"; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +void reset(auto z) |
| 26 | +{ |
| 27 | + for (std::size_t i = 0; i < z.extent(0); i++) |
| 28 | + { |
| 29 | + z(i) = 0; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +int main(int argc, char* argv[]) |
| 34 | +{ |
| 35 | + std::cout << "add example: calling HPX-kernels" << std::endl; |
| 36 | + |
| 37 | + std::size_t N = 50; |
| 38 | + |
| 39 | + using value_type = double; |
| 40 | + |
| 41 | + std::vector<value_type> x_data(N); |
| 42 | + std::vector<value_type> y_data(N); |
| 43 | + std::vector<value_type> z_data(N); |
| 44 | + |
| 45 | + value_type* x_ptr = x_data.data(); |
| 46 | + value_type* y_ptr = y_data.data(); |
| 47 | + value_type* z_ptr = z_data.data(); |
| 48 | + |
| 49 | + using dyn_1d_ext_type = |
| 50 | + std::experimental::extents<std::experimental::dynamic_extent>; |
| 51 | + using mdspan_type = std::experimental::mdspan<value_type, dyn_1d_ext_type>; |
| 52 | + mdspan_type x(x_ptr, N); |
| 53 | + mdspan_type y(y_ptr, N); |
| 54 | + mdspan_type z(z_ptr, N); |
| 55 | + |
| 56 | + std::vector<value_type> gold(N); |
| 57 | + for (std::size_t i = 0; i < x.extent(0); i++) |
| 58 | + { |
| 59 | + x(i) = static_cast<value_type>(i); |
| 60 | + y(i) = i + static_cast<value_type>(10); |
| 61 | + z(i) = 0; |
| 62 | + gold[i] = x(i) + y(i); |
| 63 | + } |
| 64 | + |
| 65 | + namespace stdla = std::experimental::linalg; |
| 66 | + const value_type init_value = 2.0; |
| 67 | + |
| 68 | + // This goes to the base implementation |
| 69 | + { |
| 70 | + stdla::add(std::execution::seq, x, y, z); |
| 71 | + print_elements(z, gold, "std::execution::seq"); |
| 72 | + } |
| 73 | + |
| 74 | + // This also goes to the base implementation |
| 75 | + { |
| 76 | + reset(z); // reset z since it is modified above |
| 77 | + stdla::add(hpx::execution::seq, x, y, z); |
| 78 | + print_elements(z, gold, "hpx::execution::seq"); |
| 79 | + } |
| 80 | + |
| 81 | + // This forwards to HPXKernels |
| 82 | + { |
| 83 | + reset(z); // reset z since it is modified above |
| 84 | + stdla::add(HPXKernelsSTD::hpx_exec<>(), x, y, z); |
| 85 | + print_elements(z, gold, "HPXKernelsSTD::hpx_exec<>()"); |
| 86 | + } |
| 87 | + |
| 88 | + // This forwards to HPXKernels if LINALG_ENABLE_HPX_DEFAULT is ON |
| 89 | + { |
| 90 | + reset(z); // reset z since it is modified above |
| 91 | + stdla::add(std::execution::par, x, y, z); |
| 92 | + print_elements(z, gold, "std::execution::par"); |
| 93 | + } |
| 94 | + |
| 95 | + // This forwards to HPXKernels |
| 96 | + { |
| 97 | + reset(z); // reset z since it is modified above |
| 98 | + stdla::add(hpx::execution::par, x, y, z); |
| 99 | + print_elements(z, gold, "hpx::execution::par"); |
| 100 | + } |
| 101 | + |
| 102 | +#if defined(HPX_HAVE_DATAPAR) |
| 103 | + // this invokes a explicitly vectorized HPX versions |
| 104 | + { |
| 105 | + reset(z); // reset z since it is modified above |
| 106 | + stdla::add(hpx::execution::simd, x, y, z); |
| 107 | + print_elements(z, gold, "hpx::execution::simd"); |
| 108 | + } |
| 109 | + |
| 110 | + { |
| 111 | + reset(z); // reset z since it is modified above |
| 112 | + stdla::add(hpx::execution::par_simd, x, y, z); |
| 113 | + print_elements(z, gold, "hpx::execution::par_simd"); |
| 114 | + } |
| 115 | +#endif |
| 116 | + |
| 117 | + return 0; |
| 118 | +} |
0 commit comments