XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
simd.hpp
Go to the documentation of this file.
1 #ifndef SIMD_HPP
2 #define SIMD_HPP
3 #include <Kokkos_Core.hpp>
4 
5 /*** SIMD structures ****/
6 
7 // Use simd pragma on CPU
8 #ifdef USE_GPU
9 #define FORCE_SIMD
10 #else
11 #define FORCE_SIMD _Pragma("simd")
12 #endif
13 
14 
15 
16 // A 1-dimension array of size SIMD_SIZE and type T
17 template<typename T>
18 struct Simd{
19  T x[SIMD_SIZE];
20 
21  // Constructors
22  KOKKOS_INLINE_FUNCTION Simd<T>(){}
23  KOKKOS_INLINE_FUNCTION Simd<T>(T initval){
24  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) x[i_simd] = initval;
25  }
26 
27  // Allows array-like access pattern
28  KOKKOS_INLINE_FUNCTION T operator [](int i) const {return x[i];}
29  KOKKOS_INLINE_FUNCTION T& operator [](int i) {return x[i];}
30 
31  // Basic operations
32  // Assignment from scalar
33  template<typename Trhs>
34  KOKKOS_INLINE_FUNCTION void operator = (const Trhs rhs) {
35  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) x[i_simd] = rhs;
36  // Void return, chaining not enabled
37  }
38 
39  KOKKOS_INLINE_FUNCTION Simd<T> operator - () const {
40  Simd<T> out;
41  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = -x[i_simd];
42  return out;
43  }
44 
45  template<typename Trhs>
46  KOKKOS_INLINE_FUNCTION Simd<T> operator - (const Simd<Trhs>& rhs) const {
47  Simd<T> out;
48  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = x[i_simd] - rhs[i_simd];
49  return out;
50  }
51 
52  template<typename Trhs>
53  KOKKOS_INLINE_FUNCTION Simd<T> operator + (const Simd<Trhs>& rhs) const {
54  Simd<T> out;
55  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = x[i_simd] + rhs[i_simd];
56  return out;
57  }
58 
59  template<typename Trhs>
60  KOKKOS_INLINE_FUNCTION Simd<T> operator * (const Simd<Trhs>& rhs) const {
61  Simd<T> out;
62  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = x[i_simd] * rhs[i_simd];
63  return out;
64  }
65 
66  template<typename Trhs>
67  KOKKOS_INLINE_FUNCTION Simd<T> operator / (const Simd<Trhs>& rhs) const {
68  Simd<T> out;
69  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = x[i_simd] / rhs[i_simd];
70  return out;
71  }
72 
73  // Other operations
74 
75  // Sets values to 0.0
76  KOKKOS_INLINE_FUNCTION void zero(){
77  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) x[i_simd] = 0.0;
78  }
79 };
80 
81 // Operators with non-Simd argument
82 template<typename T>
83 KOKKOS_INLINE_FUNCTION Simd<T> operator - (const Simd<T>& lhs, const T rhs) {
84  Simd<T> out;
85  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs.x[i_simd] - rhs;
86  return out;
87 }
88 
89 template<typename T>
90 KOKKOS_INLINE_FUNCTION Simd<T> operator - (const T lhs, const Simd<T>& rhs) {
91  Simd<T> out;
92  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs - rhs.x[i_simd];
93  return out;
94 }
95 
96 template<typename T>
97 KOKKOS_INLINE_FUNCTION Simd<T> operator + (const Simd<T>& lhs, const T rhs) {
98  Simd<T> out;
99  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs.x[i_simd] + rhs;
100  return out;
101 }
102 
103 template<typename T>
104 KOKKOS_INLINE_FUNCTION Simd<T> operator + (const T lhs, const Simd<T>& rhs) {
105  Simd<T> out;
106  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs + rhs.x[i_simd];
107  return out;
108 }
109 
110 template<typename T>
111 KOKKOS_INLINE_FUNCTION Simd<T> operator * (const Simd<T>& lhs, const T rhs) {
112  Simd<T> out;
113  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs.x[i_simd] * rhs;
114  return out;
115 }
116 
117 template<typename T>
118 KOKKOS_INLINE_FUNCTION Simd<T> operator * (const T lhs, const Simd<T>& rhs) {
119  Simd<T> out;
120  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs * rhs.x[i_simd];
121  return out;
122 }
123 
124 template<typename T>
125 KOKKOS_INLINE_FUNCTION Simd<T> operator / (const Simd<T>& lhs, const T rhs) {
126  Simd<T> out;
127  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs.x[i_simd] / rhs;
128  return out;
129 }
130 
131 template<typename T>
132 KOKKOS_INLINE_FUNCTION Simd<T> operator / (const T lhs, const Simd<T>& rhs) {
133  Simd<T> out;
134  for (int i_simd = 0; i_simd<SIMD_SIZE; i_simd++) out.x[i_simd] = lhs / rhs.x[i_simd];
135  return out;
136 }
137 
138 
139 #endif
KOKKOS_INLINE_FUNCTION void operator=(const Trhs rhs)
Definition: simd.hpp:34
KOKKOS_INLINE_FUNCTION T operator[](int i) const
Definition: simd.hpp:28
KOKKOS_INLINE_FUNCTION Simd< T > operator/(const Simd< T > &lhs, const T rhs)
Definition: simd.hpp:125
KOKKOS_INLINE_FUNCTION Simd< T > operator/(const Simd< Trhs > &rhs) const
Definition: simd.hpp:67
KOKKOS_INLINE_FUNCTION Simd< T > operator*(const Simd< T > &lhs, const T rhs)
Definition: simd.hpp:111
T x[SIMD_SIZE]
Definition: simd.hpp:19
KOKKOS_INLINE_FUNCTION Simd< T > operator-() const
Definition: simd.hpp:39
KOKKOS_INLINE_FUNCTION void zero()
Definition: simd.hpp:76
KOKKOS_INLINE_FUNCTION Simd< T > operator-(const Simd< T > &lhs, const T rhs)
Definition: simd.hpp:83
Definition: simd.hpp:18
KOKKOS_INLINE_FUNCTION Simd< T > operator+(const Simd< Trhs > &rhs) const
Definition: simd.hpp:53
KOKKOS_INLINE_FUNCTION Simd< T > operator*(const Simd< Trhs > &rhs) const
Definition: simd.hpp:60
KOKKOS_INLINE_FUNCTION Simd< T > operator+(const Simd< T > &lhs, const T rhs)
Definition: simd.hpp:97