Line data Source code
1 : //
2 : // Class Vector
3 : // Vector class used for vector fields and particle attributes like the coordinate.
4 : //
5 : // #include "Utility/PAssert.h"
6 :
7 : #include <iomanip>
8 : #include <iostream>
9 :
10 : namespace ippl {
11 : namespace detail {
12 : template <typename T, unsigned Dim>
13 : struct isExpression<Vector<T, Dim>> : std::true_type {};
14 : } // namespace detail
15 :
16 : template <typename T, unsigned Dim>
17 : template <typename... Args, typename std::enable_if<sizeof...(Args) == Dim, bool>::type>
18 798 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const Args&... args)
19 798 : : Vector({static_cast<T>(args)...}) {}
20 :
21 : template <typename T, unsigned Dim>
22 : template <typename E, size_t N>
23 6974831 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const detail::Expression<E, N>& expr) {
24 45852527 : for (unsigned int i = 0; i < Dim; ++i) {
25 38877696 : data_m[i] = expr[i];
26 : }
27 6974831 : }
28 :
29 : template <typename T, unsigned Dim>
30 43517270 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const T& val) {
31 215266826 : for (unsigned int i = 0; i < Dim; ++i)
32 181877898 : data_m[i] = val;
33 33388928 : }
34 :
35 : template <typename T, unsigned Dim>
36 66642650 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const std::initializer_list<T>& list) {
37 : // PAssert(list.size() == Dim);
38 64551526 : unsigned int i = 0;
39 423973246 : for (auto& l : list) {
40 359421720 : data_m[i] = l;
41 359421720 : ++i;
42 : }
43 64551526 : }
44 :
45 : /*
46 : *
47 : * Element access operators
48 : *
49 : */
50 : template <typename T, unsigned Dim>
51 43532192 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type& Vector<T, Dim>::operator[](
52 : unsigned int i) {
53 : // PAssert(i < Dim);
54 43532192 : return data_m[i];
55 : }
56 :
57 : template <typename T, unsigned Dim>
58 603089896 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type Vector<T, Dim>::operator[](
59 : unsigned int i) const {
60 : // PAssert(i < Dim);
61 603089896 : return data_m[i];
62 : }
63 :
64 : template <typename T, unsigned Dim>
65 480 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type& Vector<T, Dim>::operator()(
66 : unsigned int i) {
67 : // PAssert(i < Dim);
68 480 : return data_m[i];
69 : }
70 :
71 : template <typename T, unsigned Dim>
72 240 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type Vector<T, Dim>::operator()(
73 : unsigned int i) const {
74 : // PAssert(i < Dim);
75 240 : return data_m[i];
76 : }
77 :
78 : /*
79 : *
80 : * Vector Expression assignment operators
81 : *
82 : */
83 : template <typename T, unsigned Dim>
84 : template <typename E, size_t N>
85 4630596 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator=(
86 : const detail::Expression<E, N>& expr) {
87 30591048 : for (unsigned int i = 0; i < Dim; ++i) {
88 25960452 : data_m[i] = expr[i];
89 : }
90 4630596 : return *this;
91 : }
92 :
93 : template <typename T, unsigned Dim>
94 : template <typename E, size_t N>
95 1510528 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator+=(
96 : const detail::Expression<E, N>& expr) {
97 9741568 : for (unsigned int i = 0; i < Dim; ++i) {
98 8231040 : data_m[i] += expr[i];
99 : }
100 1510528 : return *this;
101 : }
102 :
103 : template <typename T, unsigned Dim>
104 : template <typename E, size_t N>
105 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator-=(
106 : const detail::Expression<E, N>& expr) {
107 : for (unsigned int i = 0; i < Dim; ++i) {
108 : data_m[i] -= expr[i];
109 : }
110 : return *this;
111 : }
112 :
113 : template <typename T, unsigned Dim>
114 : template <typename E, size_t N>
115 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator*=(
116 : const detail::Expression<E, N>& expr) {
117 : for (unsigned int i = 0; i < Dim; ++i) {
118 : data_m[i] *= expr[i];
119 : }
120 : return *this;
121 : }
122 :
123 : template <typename T, unsigned Dim>
124 : template <typename E, size_t N>
125 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator/=(
126 : const detail::Expression<E, N>& expr) {
127 : for (unsigned int i = 0; i < Dim; ++i) {
128 : data_m[i] /= expr[i];
129 : }
130 : return *this;
131 : }
132 :
133 : template <typename T, unsigned Dim>
134 0 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator+=(const T& val) {
135 0 : for (unsigned int i = 0; i < Dim; ++i) {
136 0 : data_m[i] += val;
137 : }
138 0 : return *this;
139 : }
140 :
141 : template <typename T, unsigned Dim>
142 0 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator-=(const T& val) {
143 0 : return this->operator+=(-val);
144 : }
145 :
146 : template <typename T, unsigned Dim>
147 1194 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator*=(const T& val) {
148 4482 : for (unsigned int i = 0; i < Dim; ++i) {
149 3288 : data_m[i] *= val;
150 : }
151 1194 : return *this;
152 : }
153 :
154 : template <typename T, unsigned Dim>
155 0 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator/=(const T& val) {
156 0 : return this->operator*=(T(1.0) / val);
157 : }
158 :
159 : template <typename T, unsigned Dim>
160 2242470 : KOKKOS_INLINE_FUNCTION constexpr typename Vector<T, Dim>::iterator Vector<T, Dim>::begin() {
161 2242470 : return data_m;
162 : }
163 :
164 : template <typename T, unsigned Dim>
165 2242470 : KOKKOS_INLINE_FUNCTION constexpr typename Vector<T, Dim>::iterator Vector<T, Dim>::end() {
166 2242470 : return data_m + Dim;
167 : }
168 :
169 : template <typename T, unsigned Dim>
170 : KOKKOS_INLINE_FUNCTION constexpr typename Vector<T, Dim>::const_iterator Vector<T, Dim>::begin()
171 : const {
172 : return data_m;
173 : }
174 :
175 : template <typename T, unsigned Dim>
176 : KOKKOS_INLINE_FUNCTION constexpr typename Vector<T, Dim>::const_iterator Vector<T, Dim>::end()
177 : const {
178 : return data_m + Dim;
179 : }
180 :
181 : template <typename T, unsigned Dim>
182 1354 : KOKKOS_INLINE_FUNCTION T Vector<T, Dim>::dot(const Vector<T, Dim>& rhs) const {
183 1354 : T res = 0.0;
184 5090 : for (unsigned i = 0; i < Dim; ++i) {
185 3736 : res += data_m[i] * rhs[i];
186 : }
187 1354 : return res;
188 : }
189 :
190 : template <typename T, unsigned Dim>
191 0 : KOKKOS_INLINE_FUNCTION T Vector<T, Dim>::Pnorm(const int p) const {
192 :
193 0 : T val = 0.0;
194 0 : for(unsigned i = 0; i < Dim; ++i) {
195 0 : val += Kokkos::pow(Kokkos::abs(data_m[i]), p);
196 : }
197 :
198 0 : return Kokkos::pow(val, T(1.0) / T(p));
199 : }
200 :
201 : template <typename T, unsigned Dim>
202 0 : inline std::ostream& operator<<(std::ostream& out, const Vector<T, Dim>& v) {
203 0 : std::streamsize sw = out.width();
204 0 : out << std::setw(1);
205 : if constexpr (Dim > 1) {
206 0 : out << "( ";
207 0 : for (unsigned int i = 0; i < Dim - 1; i++) {
208 0 : out << std::setw(sw) << v[i] << " , ";
209 : }
210 0 : out << std::setw(sw) << v[Dim - 1] << " )";
211 : } else {
212 : out << "( " << std::setw(sw) << v[0] << " )";
213 : }
214 0 : return out;
215 : }
216 : template <typename T, unsigned Dim>
217 238656 : KOKKOS_INLINE_FUNCTION Vector<T, Dim> min(const Vector<T, Dim>& a, const Vector<T, Dim>& b) {
218 : using Kokkos::min;
219 238656 : Vector<T, Dim> ret;
220 1510528 : for (unsigned d = 0; d < Dim; d++) {
221 1271872 : ret[d] = min(a[d], b[d]);
222 : }
223 238656 : return ret;
224 : }
225 : template <typename T, unsigned Dim>
226 238656 : KOKKOS_INLINE_FUNCTION Vector<T, Dim> max(const Vector<T, Dim>& a, const Vector<T, Dim>& b) {
227 : using Kokkos::max;
228 238656 : Vector<T, Dim> ret;
229 1510528 : for (unsigned d = 0; d < Dim; d++) {
230 1271872 : ret[d] = max(a[d], b[d]);
231 : }
232 238656 : return ret;
233 : }
234 : } // namespace ippl
235 :
236 : // vi: set et ts=4 sw=4 sts=4:
237 : // Local Variables:
238 : // mode:c
239 : // c-basic-offset: 4
240 : // indent-tabs-mode: nil
241 : // require-final-newline: nil
242 : // End:
|