IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
BareFieldOperations.hpp
Go to the documentation of this file.
1//
2// File BareFieldOperations
3// Norms and a scalar product for fields
4//
5
6namespace ippl {
13 template <typename BareField>
14 typename BareField::value_type innerProduct(const BareField& f1, const BareField& f2) {
15 using T = typename BareField::value_type;
16 constexpr unsigned Dim = BareField::dim;
17
18 T sum = 0;
19 auto layout = f1.getLayout();
20 auto view1 = f1.getView();
21 auto view2 = f2.getView();
22 using exec_space = typename BareField::execution_space;
23 using index_array_type = typename RangePolicy<Dim, exec_space>::index_array_type;
25 "Field::innerProduct(Field&, Field&)", f1.getFieldRangePolicy(),
26 KOKKOS_LAMBDA(const index_array_type& args, T& val) {
27 val += apply(view1, args) * apply(view2, args);
28 },
29 Kokkos::Sum<T>(sum));
30 T globalSum = 0;
31 layout.comm.allreduce(sum, globalSum, 1, std::plus<T>());
32 return globalSum;
33 }
34
41 template <typename BareField>
42 typename BareField::value_type norm(const BareField& field, int p = 2) {
43 using T = typename BareField::value_type;
44 constexpr unsigned Dim = BareField::dim;
45
46 T local = 0;
47 auto layout = field.getLayout();
48 auto view = field.getView();
49 using exec_space = typename BareField::execution_space;
50 using index_array_type = typename RangePolicy<Dim, exec_space>::index_array_type;
51 switch (p) {
52 case 0: {
54 "Field::norm(0)", field.getFieldRangePolicy(),
55 KOKKOS_LAMBDA(const index_array_type& args, T& val) {
56 T myVal = Kokkos::abs(apply(view, args));
57 if (myVal > val)
58 val = myVal;
59 },
60 Kokkos::Max<T>(local));
61 T globalMax = 0;
62 layout.comm.allreduce(local, globalMax, 1, std::greater<T>());
63 return globalMax;
64 }
65 default: {
67 "Field::norm(int) general", field.getFieldRangePolicy(),
68 KOKKOS_LAMBDA(const index_array_type& args, T& val) {
69 val += std::pow(Kokkos::abs(apply(view, args)), p);
70 },
71 Kokkos::Sum<T>(local));
72 T globalSum = 0;
73 layout.comm.allreduce(local, globalSum, 1, std::plus<T>());
74 return std::pow(globalSum, 1.0 / p);
75 }
76 }
77 }
78} // namespace ippl
constexpr unsigned Dim
Definition Archive.h:20
KOKKOS_INLINE_FUNCTION constexpr decltype(auto) apply(const View &view, const Coords &coords)
T innerProduct(const FEMVector< T > &a, const FEMVector< T > &b)
Calculate the inner product between two ippl::FEMVector(s).
Definition FEMVector.h:405
void parallel_reduce(const std::string &name, const ExecPolicy &policy, const FunctorType &functor, ReducerArgument &&... reducer)
T norm(const FEMVector< T > &v, int p=2)
Definition FEMVector.h:425
Layout_t & getLayout() const
Definition BareField.h:134
static constexpr unsigned dim
Definition BareField.h:60
view_type & getView()
Definition BareField.h:169
policy_type< execution_space, PolicyArgs... > getFieldRangePolicy(const int nghost=0) const
Definition BareField.h:183
view_type::execution_space execution_space
Definition BareField.h:52
::ippl::Vector< index_type, Dim > index_array_type