MueLu  Version of the Day
MueLu_Aggregates_kokkos_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 // Tobias Wiesner (tawiesn@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef MUELU_AGGREGATES_KOKKOS_DEF_HPP
48 #define MUELU_AGGREGATES_KOKKOS_DEF_HPP
49 
50 #include <Xpetra_Map.hpp>
51 #include <Xpetra_Vector.hpp>
52 #include <Xpetra_MultiVectorFactory.hpp>
53 #include <Xpetra_VectorFactory.hpp>
54 
55 #include "MueLu_LWGraph_kokkos.hpp"
58 
59 namespace MueLu {
60 
62  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
63  Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::Aggregates_kokkos(LWGraph_kokkos graph) {
64  nAggregates_ = 0;
65 
66  vertex2AggId_ = LOVectorFactory::Build(graph.GetImportMap());
67  vertex2AggId_->putScalar(MUELU_UNAGGREGATED);
68 
69  procWinner_ = LOVectorFactory::Build(graph.GetImportMap());
70  procWinner_->putScalar(MUELU_UNASSIGNED);
71 
72  isRoot_ = Teuchos::ArrayRCP<bool>(graph.GetImportMap()->getNodeNumElements(), false);
73 
74  // slow but safe, force TentativePFactory to build column map for P itself
75  aggregatesIncludeGhosts_ = true;
76  }
77 
79  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
80  Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::Aggregates_kokkos(const RCP<const Map>& map) {
81  nAggregates_ = 0;
82 
83  vertex2AggId_ = LOVectorFactory::Build(map);
84  vertex2AggId_->putScalar(MUELU_UNAGGREGATED);
85 
86  procWinner_ = LOVectorFactory::Build(map);
87  procWinner_->putScalar(MUELU_UNASSIGNED);
88 
89  isRoot_ = Teuchos::ArrayRCP<bool>(map->getNodeNumElements(), false);
90 
91  // slow but safe, force TentativePFactory to build column map for P itself
92  aggregatesIncludeGhosts_ = true;
93  }
94 
96  template<class ProcWinnerType, class Vertex2AggIdType, class AggregateSizesType, class LO>
98  private:
99  ProcWinnerType procWinner;
100  Vertex2AggIdType vertex2AggId;
101  int myPID;
102  AggregateSizesType aggregateSizes;
103 
104  public:
105  ComputeAggregateSizesFunctor(ProcWinnerType procWinner_, Vertex2AggIdType vertex2AggId_, int myPID_, AggregateSizesType aggregateSizes_) :
106  procWinner(procWinner_),
107  vertex2AggId(vertex2AggId_),
108  myPID(myPID_),
109  aggregateSizes(aggregateSizes_)
110  { }
111 
112  KOKKOS_INLINE_FUNCTION
113  void operator()(const LO k) const {
114  if (procWinner(k, 0) == myPID)
115  aggregateSizes(vertex2AggId(k, 0))++;
116  }
117  };
118  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
119  typename Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::aggregates_sizes_type::const_type
120  Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::ComputeAggregateSizes(bool forceRecompute, bool cacheSizes) const {
121  if (aggregateSizes_.size() && !forceRecompute) {
122  return aggregateSizes_;
123 
124  } else {
125 
126  // invalidate previous sizes
127  aggregateSizes_ = aggregates_sizes_type("aggregates", 0);
128 
129  aggregates_sizes_type aggregateSizes("aggregates", nAggregates_);
130 
131  int myPID = GetMap()->getComm()->getRank();
132 
133  auto vertex2AggId = vertex2AggId_->template getLocalView<DeviceType>();
134  auto procWinner = procWinner_ ->template getLocalView<DeviceType>();
135 
136  typename AppendTrait<decltype(aggregateSizes_), Kokkos::Atomic>::type aggregateSizesAtomic = aggregateSizes;
137 
139  computeAggSizesFunctor(procWinner, vertex2AggId, myPID, aggregateSizesAtomic);
140  Kokkos::parallel_for("MueLu:Aggregates:ComputeAggregateSizes:for", range_type(0,procWinner.size()), computeAggSizesFunctor);
141 
142  if (cacheSizes)
143  aggregateSizes_ = aggregateSizes;
144 
145  return aggregateSizes;
146  }
147 
148  }
149 
150  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
151  typename Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::local_graph_type
152  Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::GetGraph() const {
153  typedef typename local_graph_type::row_map_type row_map_type;
154  typedef typename local_graph_type::entries_type entries_type;
155 
156  int myPID = GetMap()->getComm()->getRank();
157 
158  ArrayRCP<LO> vertex2AggId = vertex2AggId_->getDataNonConst(0);
159  ArrayRCP<LO> procWinner = procWinner_->getDataNonConst(0);
160 
161  typename aggregates_sizes_type::const_type sizes = ComputeAggregateSizes();
162 
163  int numAggregates = nAggregates_;
164 
165  typename row_map_type::non_const_type rows("row_map", numAggregates+1); // rows(0) = 0 automatically
166  for (LO i = 0; i < nAggregates_; i++) // TODO: replace by parallel_scan
167  rows(i+1) = rows(i) + sizes(i);
168 
169  aggregates_sizes_type offsets("offsets", numAggregates);
170  for (LO i = 0; i < numAggregates; i++) // TODO: replace by parallel_for
171  offsets(i) = rows(i);
172 
173  typename entries_type::non_const_type cols("entries", rows(nAggregates_));
174  for (LO i = 0; i < procWinner.size(); i++)
175  if (procWinner[i] == myPID)
176  cols(offsets(vertex2AggId[i])++) = i;
177 
178  return local_graph_type(cols, rows);
179  }
180 
181  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
182  std::string Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::description() const {
183  return BaseClass::description() + "{nGlobalAggregates = " + toString(GetNumGlobalAggregates()) + "}";
184  }
185 
186  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
187  void Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::print(Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const {
189 
190  if (verbLevel & Statistics1)
191  out0 << "Global number of aggregates: " << GetNumGlobalAggregates() << std::endl;
192  }
193 
194  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
195  GlobalOrdinal Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType> >::GetNumGlobalAggregates() const {
196  LO nAggregates = GetNumAggregates();
197  GO nGlobalAggregates;
198  MueLu_sumAll(vertex2AggId_->getMap()->getComm(), (GO)nAggregates, nGlobalAggregates);
199  return nGlobalAggregates;
200  }
201 
202  template <class LocalOrdinal, class GlobalOrdinal, class DeviceType>
203  const RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType>> >
204  Aggregates_kokkos<LocalOrdinal, GlobalOrdinal, Kokkos::Compat::KokkosDeviceWrapperNode<DeviceType>>::GetMap() const {
205  return vertex2AggId_->getMap();
206  }
207 
208 } //namespace MueLu
209 
210 #endif // MUELU_AGGREGATES_KOKKOS_DEF_HPP
#define MUELU_UNASSIGNED
#define MueLu_sumAll(rcpComm, in, out)
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
ComputeAggregateSizesFunctor(ProcWinnerType procWinner_, Vertex2AggIdType vertex2AggId_, int myPID_, AggregateSizesType aggregateSizes_)
Print more statistics.
Namespace for MueLu classes and methods.
#define MUELU_UNAGGREGATED
KOKKOS_INLINE_FUNCTION void operator()(const LO k) const
void parallel_for(const ExecPolicy &policy, const FunctorType &functor, const std::string &str="", typename Impl::enable_if< ! Impl::is_integral< ExecPolicy >::value >::type *=0)
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
virtual std::string description() const
Return a simple one-line description of this object.