|
| 1 | +#ifndef AMREX_SIMD_H_ |
| 2 | +#define AMREX_SIMD_H_ |
| 3 | + |
| 4 | +#include <AMReX_Config.H> |
| 5 | + |
| 6 | +#include <AMReX_REAL.H> |
| 7 | + |
| 8 | +#ifdef AMREX_USE_SIMD |
| 9 | +// TODO make SIMD provider configurable: VIR (C++17 TS2) or C++26 (later) |
| 10 | +# include <vir/simd.h> // includes SIMD TS2 header <experimental/simd> |
| 11 | +#endif |
| 12 | + |
| 13 | +#include <cstdint> |
| 14 | +#include <type_traits> |
| 15 | + |
| 16 | + |
| 17 | +namespace amrex::simd |
| 18 | +{ |
| 19 | + // TODO make SIMD provider configurable: VIR (C++17 TS2) or C++26 (later) |
| 20 | + //namespace stdx = std::experimental; |
| 21 | + // for https://en.cppreference.com/w/cpp/experimental/simd/simd_cast.html |
| 22 | + namespace stdx { |
| 23 | +#ifdef AMREX_USE_SIMD |
| 24 | + using namespace std::experimental; |
| 25 | + using namespace std::experimental::__proposed; |
| 26 | + using namespace vir::stdx; |
| 27 | +#else |
| 28 | + // fallback implementations for functions that are commonly used in portable code paths |
| 29 | + |
| 30 | + AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE |
| 31 | + bool any_of (bool const v) { return v; } |
| 32 | +#endif |
| 33 | + } |
| 34 | + |
| 35 | + // TODO: move to AMReX_REAL.H? |
| 36 | + |
| 37 | +#ifdef AMREX_USE_SIMD |
| 38 | + // TODO: not sure why std::experimental::simd_abi::native<T> does not work, so we use this long version |
| 39 | + constexpr auto native_simd_size_real = std::experimental::native_simd<amrex::Real>::size(); |
| 40 | + constexpr auto native_simd_size_particlereal = std::experimental::native_simd<amrex::ParticleReal>::size(); |
| 41 | + |
| 42 | + // Note: to make use of not only vector registers but also ILP, user might want to use * 2 or more of the native size |
| 43 | + // for selected compute kernels. |
| 44 | + // TODO Check if a default with * 2 or similar is sensible. |
| 45 | + template<int SIMD_WIDTH = native_simd_size_real> |
| 46 | + using SIMDReal = std::experimental::fixed_size_simd<amrex::Real, SIMD_WIDTH>; |
| 47 | + |
| 48 | + template<int SIMD_WIDTH = native_simd_size_particlereal> |
| 49 | + using SIMDParticleReal = std::experimental::fixed_size_simd<amrex::ParticleReal, SIMD_WIDTH>; |
| 50 | + |
| 51 | + // Type that has the same amount of IdCpu SIMD elements as the SIMDParticleReal type |
| 52 | + template<typename T_ParticleReal = SIMDParticleReal<>> |
| 53 | + using SIMDIdCpu = std::experimental::rebind_simd_t<std::uint64_t, T_ParticleReal>; |
| 54 | +#else |
| 55 | + constexpr auto native_simd_size_real = 1; |
| 56 | + constexpr auto native_simd_size_particlereal = 1; |
| 57 | + |
| 58 | + template<int SIMD_WIDTH = native_simd_size_real> |
| 59 | + using SIMDReal = amrex::Real; |
| 60 | + |
| 61 | + template<int SIMD_WIDTH = native_simd_size_particlereal> |
| 62 | + using SIMDParticleReal = amrex::ParticleReal; |
| 63 | + |
| 64 | + // Type that has the same amount of IdCpu SIMD elements as the SIMDParticleReal type |
| 65 | + template<typename T_ParticleReal = SIMDParticleReal<>> |
| 66 | + using SIMDIdCpu = std::uint64_t; |
| 67 | +#endif |
| 68 | + |
| 69 | + namespace detail { |
| 70 | + struct InternalVectorized {}; |
| 71 | + } |
| 72 | + |
| 73 | + /** Mixin Helper Class |
| 74 | + * |
| 75 | + * Use this class as a mixin (base) class to simplify writing functors that support/do not support |
| 76 | + * ParallelForSIMD. |
| 77 | + * |
| 78 | + * Example: |
| 79 | + * ```c++ |
| 80 | + * struct Compute : public Vectorized<> |
| 81 | + * { |
| 82 | + * template<typename T_Real> |
| 83 | + * AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE |
| 84 | + * void operator() (T_Real & AMREX_RESTRICT x) { ... } |
| 85 | + * }; |
| 86 | + * |
| 87 | + * // ... call site below |
| 88 | + * { |
| 89 | + * Compute c; |
| 90 | + * |
| 91 | + * if constexpr (amrex::simd::is_vectorized<Compute>) { |
| 92 | + * ParallelForSIMD<...>(np, c). |
| 93 | + * } else { |
| 94 | + * ParallelFor(np, c); |
| 95 | + * } |
| 96 | + * } |
| 97 | + * ``` |
| 98 | + */ |
| 99 | + template<int SIMD_WIDTH = native_simd_size_real> |
| 100 | + struct |
| 101 | + Vectorized : detail::InternalVectorized |
| 102 | + { |
| 103 | + //! SIMD width in elements |
| 104 | + static constexpr int simd_width = SIMD_WIDTH; |
| 105 | + }; |
| 106 | + |
| 107 | + /** Check if a Functor Class works with amrex::ParallelForSIMD |
| 108 | + * |
| 109 | + * @see amrex::simd::Vectorized |
| 110 | + */ |
| 111 | + template<typename T> |
| 112 | + constexpr bool is_vectorized = std::is_base_of_v<detail::InternalVectorized, T>; |
| 113 | + |
| 114 | + /** Check if a function argument is declared as non-const |
| 115 | + * |
| 116 | + * Use in conjunction with conditional write-back logic from vector registers, e.g., |
| 117 | + * |
| 118 | + * ```c++ |
| 119 | + * template<typename T_Real> |
| 120 | + * void compute (T_Real & AMREX_RESTRICT x, |
| 121 | + * T_Real const & AMREX_RESTRICT y) { ... } |
| 122 | + * |
| 123 | + * part_x.copy_from(&m_part_x[i], stdx::element_aligned); |
| 124 | + * part_y.copy_from(&m_part_y[i], stdx::element_aligned); |
| 125 | + * |
| 126 | + * compute(part_x, part_y); |
| 127 | + * |
| 128 | + * if constexpr (is_nth_arg_non_const(compute<double>, 0)) |
| 129 | + * part_x.copy_to(&m_part_x[i], stdx::element_aligned); |
| 130 | + * if constexpr (is_nth_arg_non_const(compute<double>, 1)) |
| 131 | + * part_y.copy_to(&m_part_y[i], stdx::element_aligned); |
| 132 | + */ |
| 133 | + template<typename R, typename... Args> |
| 134 | + constexpr bool is_nth_arg_non_const (R(*)(Args...), int n) |
| 135 | + { |
| 136 | + constexpr bool val_arr[sizeof...(Args)] {!std::is_const_v<std::remove_reference_t<Args>>...}; |
| 137 | + return val_arr[n]; |
| 138 | + } |
| 139 | + // same for functors (const/non-const ::operator() members) |
| 140 | + template<typename C, typename R, typename... Args> |
| 141 | + constexpr bool is_nth_arg_non_const (R(C::*)(Args...), int n) |
| 142 | + { |
| 143 | + constexpr bool val_arr[sizeof...(Args)] {!std::is_const_v<std::remove_reference_t<Args>>...}; |
| 144 | + return val_arr[n]; |
| 145 | + } |
| 146 | + template<typename C, typename R, typename... Args> |
| 147 | + constexpr bool is_nth_arg_non_const (R(C::*)(Args...) const, int n) |
| 148 | + { |
| 149 | + constexpr bool val_arr[sizeof...(Args)] {!std::is_const_v<std::remove_reference_t<Args>>...}; |
| 150 | + return val_arr[n]; |
| 151 | + } |
| 152 | + |
| 153 | +} // namespace amrex::simd |
| 154 | + |
| 155 | +#endif |
0 commit comments