LCOV - code coverage report
Current view: top level - src/Field - BareField.hpp (source / functions) Coverage Total Hit
Test: report.info Lines: 85.5 % 83 71
Test Date: 2025-05-21 10:57:37 Functions: 80.3 % 1116 896
Branches: 40.6 % 278 113

             Branch data     Line data    Source code
       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                 :             : 
      13                 :             : #include "Communicate/DataTypes.h"
      14                 :             : 
      15                 :             : #include "Utility/Inform.h"
      16                 :             : #include "Utility/IpplInfo.h"
      17                 :             : namespace 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   [ +  -  +  -  :          28 :             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                 :          24 :             return ippl::Vector<T, Dim>(std::numeric_limits<T>::infinity());
      28                 :             :         }
      29                 :             :         KOKKOS_FORCEINLINE_FUNCTION static ippl::Vector<T, Dim> max() {
      30                 :          24 :             return ippl::Vector<T, Dim>(-std::numeric_limits<T>::infinity());
      31                 :             :         }
      32                 :             :     };
      33                 :             : }  // namespace Kokkos
      34                 :             : 
      35                 :             : namespace KokkosCorrection {
      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                 :          24 :         KOKKOS_INLINE_FUNCTION Max(value_type& vref)
      41                 :          24 :             : 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                 :          24 :         KOKKOS_INLINE_FUNCTION Min(value_type& vref)
      53                 :          24 :             : 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                 :          84 :         KOKKOS_INLINE_FUNCTION Sum(value_type& vref)
      65                 :          84 :             : 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                 :          12 :         KOKKOS_INLINE_FUNCTION Prod(value_type& vref)
      75                 :          12 :             : 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                 :             : 
      82                 :             : namespace 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>
      89                 :          24 :     BareField<T, Dim, ViewArgs...>::BareField()
      90                 :          24 :         : nghost_m(1)
      91         [ +  - ]:          24 :         , layout_m(nullptr) {}
      92                 :             : 
      93                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
      94                 :          12 :     BareField<T, Dim, ViewArgs...> BareField<T, Dim, ViewArgs...>::deepCopy() const {
      95                 :          12 :         BareField<T, Dim, ViewArgs...> copy(*layout_m, nghost_m);
      96         [ +  - ]:          12 :         Kokkos::deep_copy(copy.dview_m, dview_m);
      97                 :          12 :         return copy;
      98                 :           0 :     }
      99                 :             : 
     100                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     101                 :         732 :     BareField<T, Dim, ViewArgs...>::BareField(Layout_t& l, int nghost)
     102                 :         732 :         : nghost_m(nghost)
     103                 :             :         //     , owned_m(0)
     104         [ +  - ]:         732 :         , layout_m(&l) {
     105         [ +  - ]:         732 :         setup();
     106                 :         732 :     }
     107                 :             : 
     108                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     109                 :          24 :     void BareField<T, Dim, ViewArgs...>::initialize(Layout_t& l, int nghost) {
     110         [ +  - ]:          24 :         if (layout_m == 0) {
     111                 :          24 :             layout_m = &l;
     112                 :          24 :             nghost_m = nghost;
     113                 :          24 :             setup();
     114                 :             :         }
     115                 :          24 :     }
     116                 :             : 
     117                 :             :     // ML
     118                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     119                 :          48 :     void BareField<T, Dim, ViewArgs...>::updateLayout(Layout_t& l, int nghost) {
     120                 :             :         // std::cout << "Got in BareField::updateLayout()" << std::endl;
     121                 :          48 :         layout_m = &l;
     122                 :          48 :         nghost_m = nghost;
     123                 :          48 :         setup();
     124                 :          48 :     }
     125                 :             : 
     126                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     127                 :         804 :     void BareField<T, Dim, ViewArgs...>::setup() {
     128         [ +  - ]:         804 :         owned_m = layout_m->getLocalNDIndex();
     129                 :             : 
     130                 :        2412 :         auto resize = [&]<size_t... Idx>(const std::index_sequence<Idx...>&) {
     131                 :         784 :             this->resize((owned_m[Idx].length() + 2 * nghost_m)...);
     132                 :             :         };
     133         [ +  - ]:         804 :         resize(std::make_index_sequence<Dim>{});
     134                 :         804 :     }
     135                 :             : 
     136                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     137                 :             :     template <typename... Args>
     138                 :         804 :     void BareField<T, Dim, ViewArgs...>::resize(Args... args) {
     139                 :         804 :         Kokkos::resize(dview_m, args...);
     140                 :         804 :     }
     141                 :             : 
     142                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     143                 :         134 :     void BareField<T, Dim, ViewArgs...>::fillHalo() {
     144         [ -  + ]:         134 :         if (layout_m->comm.size() > 1) {
     145                 :           0 :             halo_m.fillHalo(dview_m, layout_m);
     146                 :             :         }
     147         [ -  + ]:         134 :         if (layout_m->isAllPeriodic_m) {
     148                 :             :             using Op = typename detail::HaloCells<T, Dim, ViewArgs...>::assign;
     149                 :           0 :             halo_m.template applyPeriodicSerialDim<Op>(dview_m, layout_m, nghost_m);
     150                 :             :         }
     151                 :         134 :     }
     152                 :             : 
     153                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     154                 :          74 :     void BareField<T, Dim, ViewArgs...>::accumulateHalo() {
     155         [ -  + ]:          74 :         if (layout_m->comm.size() > 1) {
     156                 :           0 :             halo_m.accumulateHalo(dview_m, layout_m);
     157                 :             :         }
     158         [ +  + ]:          74 :         if (layout_m->isAllPeriodic_m) {
     159                 :             :             using Op = typename detail::HaloCells<T, Dim, ViewArgs...>::rhs_plus_assign;
     160                 :          36 :             halo_m.template applyPeriodicSerialDim<Op>(dview_m, layout_m, nghost_m);
     161                 :             :         }
     162                 :          74 :     }
     163                 :             : 
     164                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     165                 :          10 :     void BareField<T, Dim, ViewArgs...>::accumulateHalo_noghost(int nghost) {
     166         [ -  + ]:          10 :         if (layout_m->comm.size() > 1) {
     167                 :           0 :             halo_m.accumulateHalo_noghost(dview_m, layout_m, nghost);
     168                 :             :         }
     169                 :          10 :     }
     170                 :             : 
     171                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     172                 :         338 :     BareField<T, Dim, ViewArgs...>& BareField<T, Dim, ViewArgs...>::operator=(T x) {
     173                 :             :         using index_array_type = typename RangePolicy<Dim, execution_space>::index_array_type;
     174   [ +  -  +  - ]:         338 :         ippl::parallel_for(
     175                 :         338 :             "BareField::operator=(T)", getRangePolicy(dview_m),
     176   [ +  -  +  -  :    43562306 :             KOKKOS_CLASS_LAMBDA(const index_array_type& args) { apply(dview_m, args) = x; });
                   -  - ]
     177                 :         338 :         return *this;
     178                 :             :     }
     179                 :             : 
     180                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     181                 :             :     template <typename E, size_t N>
     182                 :         256 :     BareField<T, Dim, ViewArgs...>& BareField<T, Dim, ViewArgs...>::operator=(
     183                 :             :         const detail::Expression<E, N>& expr) {
     184                 :             :         using capture_type     = detail::CapturedExpression<E, N>;
     185                 :         256 :         capture_type expr_     = reinterpret_cast<const capture_type&>(expr);
     186                 :             :         using index_array_type = typename RangePolicy<Dim, execution_space>::index_array_type;
     187   [ +  -  +  - ]:         256 :         ippl::parallel_for(
     188                 :         256 :             "BareField::operator=(const Expression&)", getRangePolicy(dview_m, nghost_m),
     189   [ +  -  +  -  :    10040468 :             KOKKOS_CLASS_LAMBDA(const index_array_type& args) {
                   -  - ]
     190   [ +  -  +  -  :     5019968 :                 apply(dview_m, args) = apply(expr_, args);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
           [ #  #  #  #  
          #  #  #  #  #  
                      # ]
           [ #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     191                 :             :             });
     192                 :         256 :         return *this;
     193                 :             :     }
     194                 :             : 
     195                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     196                 :           0 :     void BareField<T, Dim, ViewArgs...>::write(std::ostream& out) const {
     197   [ #  #  #  # ]:           0 :         Kokkos::fence();
     198                 :           0 :         detail::write<T, Dim>(dview_m, out);
     199                 :           0 :     }
     200                 :             : 
     201                 :             :     template <typename T, unsigned Dim, class... ViewArgs>
     202                 :           0 :     void BareField<T, Dim, ViewArgs...>::write(Inform& inf) const {
     203                 :           0 :         write(inf.getDestination());
     204                 :           0 :     }
     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   [ +  +  +  -  :     1670940 :     DefineReduction(Sum, sum, valL += myVal, std::plus)
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     225   [ +  +  +  -  :      477420 :     DefineReduction(Max, max, using Kokkos::max; valL = max(valL, myVal), std::greater)
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     226   [ +  +  +  -  :      477420 :     DefineReduction(Min, min, using Kokkos::min; valL = min(valL, myVal), std::less)
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     227   [ -  +  -  -  :      238704 :     DefineReduction(Prod, prod, valL *= myVal, std::multiplies)
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     228                 :             : 
     229                 :             : }  // namespace ippl
        

Generated by: LCOV version 2.0-1