IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
BareField.hpp
Go to the documentation of this file.
1//
2// Class BareField
3// A BareField consists of multple LFields and represents a field.
4//
5#include "Ippl.h"
6
7#include <Kokkos_ReductionIdentity.hpp>
8#include <cstdlib>
9#include <limits>
10#include <map>
11#include <utility>
12
14
15#include "Utility/Inform.h"
16#include "Utility/IpplInfo.h"
17namespace Kokkos {
18 template <typename T, unsigned Dim>
19 struct reduction_identity<ippl::Vector<T, Dim>> {
20 KOKKOS_FORCEINLINE_FUNCTION static ippl::Vector<T, Dim> sum() {
21 return ippl::Vector<T, Dim>(0);
22 }
23 KOKKOS_FORCEINLINE_FUNCTION static ippl::Vector<T, Dim> prod() {
24 return ippl::Vector<T, Dim>(1);
25 }
26 KOKKOS_FORCEINLINE_FUNCTION static ippl::Vector<T, Dim> min() {
27 return ippl::Vector<T, Dim>(std::numeric_limits<T>::infinity());
28 }
29 KOKKOS_FORCEINLINE_FUNCTION static ippl::Vector<T, Dim> max() {
30 return ippl::Vector<T, Dim>(-std::numeric_limits<T>::infinity());
31 }
32 };
33} // namespace Kokkos
34
36 template <typename Scalar, class Space = Kokkos::HostSpace>
37 struct Max : Kokkos::Max<Scalar, Space> {
38 using Super = Kokkos::Max<Scalar, Space>;
39 using value_type = typename Super::value_type;
40 KOKKOS_INLINE_FUNCTION Max(value_type& vref)
41 : Super(vref) {}
42 KOKKOS_INLINE_FUNCTION void join(value_type& dest, const value_type& src) const {
43 using ippl::max;
44 using Kokkos::max;
45 dest = max(dest, src);
46 }
47 };
48 template <typename Scalar, class Space = Kokkos::HostSpace>
49 struct Min : Kokkos::Min<Scalar, Space> {
50 using Super = Kokkos::Min<Scalar, Space>;
51 using value_type = typename Super::value_type;
52 KOKKOS_INLINE_FUNCTION Min(value_type& vref)
53 : Super(vref) {}
54 KOKKOS_INLINE_FUNCTION void join(value_type& dest, const value_type& src) const {
55 using ippl::min;
56 using Kokkos::min;
57 dest = min(dest, src);
58 }
59 };
60 template <typename Scalar, class Space = Kokkos::HostSpace>
61 struct Sum : Kokkos::Sum<Scalar, Space> {
62 using Super = Kokkos::Sum<Scalar, Space>;
63 using value_type = typename Super::value_type;
64 KOKKOS_INLINE_FUNCTION Sum(value_type& vref)
65 : Super(vref) {}
66 KOKKOS_INLINE_FUNCTION void join(value_type& dest, const value_type& src) const {
67 dest += src;
68 }
69 };
70 template <typename Scalar, class Space = Kokkos::HostSpace>
71 struct Prod : Kokkos::Prod<Scalar, Space> {
72 using Super = Kokkos::Prod<Scalar, Space>;
73 using value_type = typename Super::value_type;
74 KOKKOS_INLINE_FUNCTION Prod(value_type& vref)
75 : Super(vref) {}
76 KOKKOS_INLINE_FUNCTION void join(value_type& dest, const value_type& src) const {
77 dest *= src;
78 }
79 };
80} // namespace KokkosCorrection
81
82namespace ippl {
83 namespace detail {
84 template <typename T, unsigned Dim, class... ViewArgs>
85 struct isExpression<BareField<T, Dim, ViewArgs...>> : std::true_type {};
86 } // namespace detail
87
88 template <typename T, unsigned Dim, class... ViewArgs>
92
93 template <typename T, unsigned Dim, class... ViewArgs>
95 BareField<T, Dim, ViewArgs...> copy(*layout_m, nghost_m);
96 Kokkos::deep_copy(copy.dview_m, dview_m);
97 return copy;
98 }
99
100 template <typename T, unsigned Dim, class... ViewArgs>
102 : nghost_m(nghost)
103 // , owned_m(0)
104 , layout_m(&l) {
105 setup();
106 }
107
108 template <typename T, unsigned Dim, class... ViewArgs>
110 if (layout_m == 0) {
111 layout_m = &l;
112 nghost_m = nghost;
113 setup();
114 }
115 }
116
117 // ML
118 template <typename T, unsigned Dim, class... ViewArgs>
120 // std::cout << "Got in BareField::updateLayout()" << std::endl;
121 layout_m = &l;
122 nghost_m = nghost;
123 setup();
124 }
125
126 template <typename T, unsigned Dim, class... ViewArgs>
128 owned_m = layout_m->getLocalNDIndex();
129
130 auto resize = [&]<size_t... Idx>(const std::index_sequence<Idx...>&) {
131 this->resize((owned_m[Idx].length() + 2 * nghost_m)...);
132 };
133 resize(std::make_index_sequence<Dim>{});
134 }
135
136 template <typename T, unsigned Dim, class... ViewArgs>
137 template <typename... Args>
139 Kokkos::resize(dview_m, args...);
140 }
141
142 template <typename T, unsigned Dim, class... ViewArgs>
144 if (layout_m->comm.size() > 1) {
145 halo_m.fillHalo(dview_m, layout_m);
146 }
147 if (layout_m->isAllPeriodic_m) {
148 using Op = typename detail::HaloCells<T, Dim, ViewArgs...>::assign;
149 halo_m.template applyPeriodicSerialDim<Op>(dview_m, layout_m, nghost_m);
150 }
151 }
152
153 template <typename T, unsigned Dim, class... ViewArgs>
155 if (layout_m->comm.size() > 1) {
156 halo_m.accumulateHalo(dview_m, layout_m);
157 }
158 if (layout_m->isAllPeriodic_m) {
159 using Op = typename detail::HaloCells<T, Dim, ViewArgs...>::rhs_plus_assign;
160 halo_m.template applyPeriodicSerialDim<Op>(dview_m, layout_m, nghost_m);
161 }
162 }
163
164 template <typename T, unsigned Dim, class... ViewArgs>
166 if (layout_m->comm.size() > 1) {
167 halo_m.accumulateHalo_noghost(dview_m, layout_m, nghost);
168 }
169 }
170
171 template <typename T, unsigned Dim, class... ViewArgs>
173 using index_array_type = typename RangePolicy<Dim, execution_space>::index_array_type;
175 "BareField::operator=(T)", getRangePolicy(dview_m),
176 KOKKOS_CLASS_LAMBDA(const index_array_type& args) { apply(dview_m, args) = x; });
177 return *this;
178 }
179
180 template <typename T, unsigned Dim, class... ViewArgs>
181 template <typename E, size_t N>
183 const detail::Expression<E, N>& expr) {
184 using capture_type = detail::CapturedExpression<E, N>;
185 capture_type expr_ = reinterpret_cast<const capture_type&>(expr);
186 using index_array_type = typename RangePolicy<Dim, execution_space>::index_array_type;
188 "BareField::operator=(const Expression&)", getRangePolicy(dview_m, nghost_m),
189 KOKKOS_CLASS_LAMBDA(const index_array_type& args) {
190 apply(dview_m, args) = apply(expr_, args);
191 });
192 return *this;
193 }
194
195 template <typename T, unsigned Dim, class... ViewArgs>
196 void BareField<T, Dim, ViewArgs...>::write(std::ostream& out) const {
197 Kokkos::fence();
199 }
200
201 template <typename T, unsigned Dim, class... ViewArgs>
205
206#define DefineReduction(fun, name, op, MPI_Op) \
207 template <typename T, unsigned Dim, class... ViewArgs> \
208 T BareField<T, Dim, ViewArgs...>::name(int nghost) const { \
209 PAssert_LE(nghost, nghost_m); \
210 T temp = Kokkos::reduction_identity<T>::name(); \
211 using index_array_type = typename RangePolicy<Dim, execution_space>::index_array_type; \
212 ippl::parallel_reduce( \
213 "fun", getRangePolicy(dview_m, nghost_m - nghost), \
214 KOKKOS_CLASS_LAMBDA(const index_array_type& args, T& valL) { \
215 T myVal = apply(dview_m, args); \
216 op; \
217 }, \
218 KokkosCorrection::fun<T>(temp)); \
219 T globaltemp = 0.0; \
220 layout_m->comm.allreduce(temp, globaltemp, 1, MPI_Op<T>()); \
221 return globaltemp; \
222 }
223
224 DefineReduction(Sum, sum, valL += myVal, std::plus)
225 DefineReduction(Max, max, using Kokkos::max; valL = max(valL, myVal), std::greater)
226 DefineReduction(Min, min, using Kokkos::min; valL = min(valL, myVal), std::less)
227 DefineReduction(Prod, prod, valL *= myVal, std::multiplies)
228
229} // namespace ippl
constexpr unsigned Dim
ippl::Vector< T, Dim > Vector
Definition datatypes.h:26
#define DefineReduction(fun, name, op, MPI_Op)
STL namespace.
Definition Archive.h:20
DefineReduction(Sum, sum, valL+=myVal, std::plus) DefineReduction(Max
KOKKOS_INLINE_FUNCTION constexpr decltype(auto) apply(const View &view, const Coords &coords)
RangePolicy< View::rank, typenameView::execution_space, PolicyArgs... >::policy_type getRangePolicy(const View &view, int shift=0)
std::greater prod
KOKKOS_INLINE_FUNCTION Vector< T, Dim > min(const Vector< T, Dim > &a, const Vector< T, Dim > &b)
Definition Vector.hpp:217
void parallel_for(const std::string &name, const ExecPolicy &policy, const FunctorType &functor)
void write(const typename ViewType< T, Dim, Properties... >::view_type &view, std::ostream &out=std::cout)
Definition ViewUtils.h:84
BareField & operator=(T x)
Layout_t * layout_m
How the arrays are laid out.
Definition BareField.h:225
void accumulateHalo()
FieldLayout< Dim > Layout_t
Definition BareField.h:44
halo_type halo_m
Definition BareField.h:217
void initialize(Layout_t &l, int nghost=1)
void write(std::ostream &out=std::cout) const
BareField deepCopy() const
Definition BareField.hpp:94
void updateLayout(Layout_t &, int nghost=1)
Domain_t owned_m
Domain of the data.
Definition BareField.h:215
view_type dview_m
Actual field data.
Definition BareField.h:212
void accumulateHalo_noghost(int nghost=1)
int nghost_m
Number of ghost layers on each field boundary.
Definition BareField.h:209
void resize(Args... args)
static KOKKOS_FORCEINLINE_FUNCTION ippl::Vector< T, Dim > min()
Definition BareField.hpp:26
static KOKKOS_FORCEINLINE_FUNCTION ippl::Vector< T, Dim > max()
Definition BareField.hpp:29
static KOKKOS_FORCEINLINE_FUNCTION ippl::Vector< T, Dim > prod()
Definition BareField.hpp:23
static KOKKOS_FORCEINLINE_FUNCTION ippl::Vector< T, Dim > sum()
Definition BareField.hpp:20
KOKKOS_INLINE_FUNCTION Max(value_type &vref)
Definition BareField.hpp:40
typename Super::value_type value_type
Definition BareField.hpp:39
KOKKOS_INLINE_FUNCTION void join(value_type &dest, const value_type &src) const
Definition BareField.hpp:42
Kokkos::Max< Scalar, Space > Super
Definition BareField.hpp:38
KOKKOS_INLINE_FUNCTION Min(value_type &vref)
Definition BareField.hpp:52
KOKKOS_INLINE_FUNCTION void join(value_type &dest, const value_type &src) const
Definition BareField.hpp:54
Kokkos::Min< Scalar, Space > Super
Definition BareField.hpp:50
typename Super::value_type value_type
Definition BareField.hpp:51
Kokkos::Sum< Scalar, Space > Super
Definition BareField.hpp:62
KOKKOS_INLINE_FUNCTION void join(value_type &dest, const value_type &src) const
Definition BareField.hpp:66
typename Super::value_type value_type
Definition BareField.hpp:63
KOKKOS_INLINE_FUNCTION Sum(value_type &vref)
Definition BareField.hpp:64
typename Super::value_type value_type
Definition BareField.hpp:73
KOKKOS_INLINE_FUNCTION Prod(value_type &vref)
Definition BareField.hpp:74
KOKKOS_INLINE_FUNCTION void join(value_type &dest, const value_type &src) const
Definition BareField.hpp:76
Kokkos::Prod< Scalar, Space > Super
Definition BareField.hpp:72
std::ostream & getDestination()
Definition Inform.h:74
::ippl::Vector< index_type, Dim > index_array_type