Branch data 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 : 902 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const Args&... args)
19 : 902 : : Vector({static_cast<T>(args)...}) {}
20 : :
21 : : template <typename T, unsigned Dim>
22 : : template <typename E, size_t N>
23 : 6995408 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const detail::Expression<E, N>& expr) {
24 [ + + ]: 45945408 : for (unsigned int i = 0; i < Dim; ++i) {
25 : 38950000 : data_m[i] = expr[i];
26 : : }
27 : 6995408 : }
28 : :
29 : : template <typename T, unsigned Dim>
30 [ + + + + : 43055600 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const T& val) {
- - - - ]
[ # # ]
31 [ + + ]: 213431704 : for (unsigned int i = 0; i < Dim; ++i)
32 : 180501474 : data_m[i] = val;
33 : 32930230 : }
34 : :
35 : : template <typename T, unsigned Dim>
36 [ + + ][ + - : 73905986 : KOKKOS_FUNCTION Vector<T, Dim>::Vector(const std::initializer_list<T>& list) {
+ + - - -
- ]
37 : : // PAssert(list.size() == Dim);
38 : 71831982 : unsigned int i = 0;
39 [ + + ]: 471563010 : for (auto& l : list) {
40 : 399731028 : data_m[i] = l;
41 : 399731028 : ++i;
42 : : }
43 : 71831982 : }
44 : :
45 : : /*
46 : : *
47 : : * Element access operators
48 : : *
49 : : */
50 : : template <typename T, unsigned Dim>
51 : 49026508 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type& Vector<T, Dim>::operator[](
52 : : unsigned int i) {
53 : : // PAssert(i < Dim);
54 : 49026508 : return data_m[i];
55 : : }
56 : :
57 : : template <typename T, unsigned Dim>
58 : 677774476 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type Vector<T, Dim>::operator[](
59 : : unsigned int i) const {
60 : : // PAssert(i < Dim);
61 : 677774476 : return data_m[i];
62 : : }
63 : :
64 : : template <typename T, unsigned Dim>
65 : 2352 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type& Vector<T, Dim>::operator()(
66 : : unsigned int i) {
67 : : // PAssert(i < Dim);
68 : 2352 : return data_m[i];
69 : : }
70 : :
71 : : template <typename T, unsigned Dim>
72 : 1176 : KOKKOS_INLINE_FUNCTION typename Vector<T, Dim>::value_type Vector<T, Dim>::operator()(
73 : : unsigned int i) const {
74 : : // PAssert(i < Dim);
75 : 1176 : 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 : 738 : KOKKOS_INLINE_FUNCTION Vector<T, Dim>& Vector<T, Dim>::operator*=(const T& val) {
148 [ + + ]: 2706 : for (unsigned int i = 0; i < Dim; ++i) {
149 : 1968 : data_m[i] *= val;
150 : : }
151 : 738 : 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 : 1795692 : KOKKOS_INLINE_FUNCTION constexpr typename Vector<T, Dim>::iterator Vector<T, Dim>::begin() {
161 : 1795692 : return data_m;
162 : : }
163 : :
164 : : template <typename T, unsigned Dim>
165 : 1795692 : KOKKOS_INLINE_FUNCTION constexpr typename Vector<T, Dim>::iterator Vector<T, Dim>::end() {
166 : 1795692 : 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 : 898 : KOKKOS_INLINE_FUNCTION T Vector<T, Dim>::dot(const Vector<T, Dim>& rhs) const {
183 : 898 : T res = 0.0;
184 [ + + ]: 3314 : for (unsigned i = 0; i < Dim; ++i) {
185 : 2416 : res += data_m[i] * rhs[i];
186 : : }
187 : 898 : return res;
188 : : }
189 : :
190 : : template <typename T, unsigned Dim>
191 : 0 : inline std::ostream& operator<<(std::ostream& out, const Vector<T, Dim>& v) {
192 : 0 : std::streamsize sw = out.width();
193 : 0 : out << std::setw(1);
194 : : if constexpr (Dim > 1) {
195 : 0 : out << "( ";
196 [ # # ]: 0 : for (unsigned int i = 0; i < Dim - 1; i++) {
197 : 0 : out << std::setw(sw) << v[i] << " , ";
198 : : }
199 : 0 : out << std::setw(sw) << v[Dim - 1] << " )";
200 : : } else {
201 : : out << "( " << std::setw(sw) << v[0] << " )";
202 : : }
203 : 0 : return out;
204 : : }
205 : : template <typename T, unsigned Dim>
206 : 238656 : KOKKOS_INLINE_FUNCTION Vector<T, Dim> min(const Vector<T, Dim>& a, const Vector<T, Dim>& b) {
207 : : using Kokkos::min;
208 : 238656 : Vector<T, Dim> ret;
209 [ + + ]: 1510528 : for (unsigned d = 0; d < Dim; d++) {
210 : 1271872 : ret[d] = min(a[d], b[d]);
211 : : }
212 : 238656 : return ret;
213 : : }
214 : : template <typename T, unsigned Dim>
215 : 238656 : KOKKOS_INLINE_FUNCTION Vector<T, Dim> max(const Vector<T, Dim>& a, const Vector<T, Dim>& b) {
216 : : using Kokkos::max;
217 : 238656 : Vector<T, Dim> ret;
218 [ + + ]: 1510528 : for (unsigned d = 0; d < Dim; d++) {
219 : 1271872 : ret[d] = max(a[d], b[d]);
220 : : }
221 : 238656 : return ret;
222 : : }
223 : : } // namespace ippl
224 : :
225 : : // vi: set et ts=4 sw=4 sts=4:
226 : : // Local Variables:
227 : : // mode:c
228 : : // c-basic-offset: 4
229 : : // indent-tabs-mode: nil
230 : : // require-final-newline: nil
231 : : // End:
|