Block-Structured AMR Software Framework
 
Loading...
Searching...
No Matches
AMReX_SIMD.H
Go to the documentation of this file.
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# if __cplusplus >= 202002L
12# include <vir/simd_cvt.h>
13# endif
14#endif
15
16#include <cstdint>
17#include <type_traits>
18
19
20namespace amrex::simd
21{
22 // TODO make SIMD provider configurable: VIR (C++17 TS2) or C++26 (later)
23 // for https://en.cppreference.com/w/cpp/experimental/simd/simd_cast.html
24 namespace stdx {
25#ifdef AMREX_USE_SIMD
26 using namespace vir::stdx;
27# if __cplusplus >= 202002L
28 using vir::cvt;
29# endif
30#else
31 // fallback implementations for functions that are commonly used in portable code paths
32
34 bool any_of (bool const v) { return v; }
35#endif
36 }
37
38 // TODO: move to AMReX_REAL.H?
39
40#ifdef AMREX_USE_SIMD
41 // TODO: not sure why std::experimental::simd_abi::native<T> does not work, so we use this long version
42 constexpr auto native_simd_size_real = stdx::native_simd<amrex::Real>::size();
43 constexpr auto native_simd_size_particlereal = stdx::native_simd<amrex::ParticleReal>::size();
44
45 // Note: to make use of not only vector registers but also ILP, user might want to use * 2 or more of the native size
46 // for selected compute kernels.
47 // TODO Check if a default with * 2 or similar is sensible.
48 template<int SIMD_WIDTH = native_simd_size_real>
49 using SIMDReal = stdx::fixed_size_simd<amrex::Real, SIMD_WIDTH>;
50
51 template<int SIMD_WIDTH = native_simd_size_particlereal>
52 using SIMDParticleReal = stdx::fixed_size_simd<amrex::ParticleReal, SIMD_WIDTH>;
53
54 // Type that has the same amount of IdCpu SIMD elements as the SIMDParticleReal type
55 template<typename T_ParticleReal = SIMDParticleReal<>>
56 using SIMDIdCpu = stdx::rebind_simd_t<std::uint64_t, T_ParticleReal>;
57#else
58 constexpr auto native_simd_size_real = 1;
60
61 template<int SIMD_WIDTH = native_simd_size_real>
63
64 template<int SIMD_WIDTH = native_simd_size_particlereal>
66
67 // Type that has the same amount of IdCpu SIMD elements as the SIMDParticleReal type
68 template<typename T_ParticleReal = SIMDParticleReal<>>
69 using SIMDIdCpu = std::uint64_t;
70#endif
71
73 namespace detail {
74 struct InternalVectorized {};
75 }
77
104 template<int SIMD_WIDTH = native_simd_size_real>
105 struct
106 Vectorized : detail::InternalVectorized
107 {
109 static constexpr int simd_width = SIMD_WIDTH;
110 };
111
116 template<typename T>
117 constexpr bool is_vectorized = std::is_base_of_v<detail::InternalVectorized, T>;
118
139 template<typename R, typename... Args>
140 constexpr bool is_nth_arg_non_const (R(*)(Args...), int n)
141 {
142 constexpr bool val_arr[sizeof...(Args)] {!std::is_const_v<std::remove_reference_t<Args>>...};
143 return val_arr[n];
144 }
145 // same for functors (const/non-const ::operator() members)
146 template<typename C, typename R, typename... Args>
147 constexpr bool is_nth_arg_non_const (R(C::*)(Args...), 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 template<typename C, typename R, typename... Args>
153 constexpr bool is_nth_arg_non_const (R(C::*)(Args...) const, int n)
154 {
155 constexpr bool val_arr[sizeof...(Args)] {!std::is_const_v<std::remove_reference_t<Args>>...};
156 return val_arr[n];
157 }
158
159} // namespace amrex::simd
160
161#endif
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:79
amrex_particle_real ParticleReal
Floating Point Type for Particles.
Definition AMReX_REAL.H:90
AMREX_GPU_HOST_DEVICE bool any_of(bool const v)
Definition AMReX_SIMD.H:34
Definition AMReX_SIMD.H:21
std::uint64_t SIMDIdCpu
Definition AMReX_SIMD.H:69
constexpr auto native_simd_size_real
Definition AMReX_SIMD.H:58
constexpr bool is_vectorized
Definition AMReX_SIMD.H:117
constexpr bool is_nth_arg_non_const(R(*)(Args...), int n)
Definition AMReX_SIMD.H:140
amrex::ParticleReal SIMDParticleReal
Definition AMReX_SIMD.H:65
constexpr auto native_simd_size_particlereal
Definition AMReX_SIMD.H:59
amrex::Real SIMDReal
Definition AMReX_SIMD.H:62
Definition AMReX_SIMD.H:107