MueLu  Version of the Day
MueLu_Utilities_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 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_UTILITIES_KOKKOS_DEF_HPP
47 #define MUELU_UTILITIES_KOKKOS_DEF_HPP
48 
49 #include <Teuchos_DefaultComm.hpp>
50 #include <Teuchos_ParameterList.hpp>
51 
52 #include "MueLu_ConfigDefs.hpp"
53 
54 #ifdef HAVE_MUELU_EPETRA
55 # ifdef HAVE_MPI
56 # include "Epetra_MpiComm.h"
57 # endif
58 #endif
59 
60 #include <Kokkos_ArithTraits.hpp>
61 #include <Kokkos_Core.hpp>
62 #include <Kokkos_CrsMatrix.hpp>
63 
64 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
65 #include <EpetraExt_MatrixMatrix.h>
66 #include <EpetraExt_RowMatrixOut.h>
67 #include <EpetraExt_MultiVectorOut.h>
68 #include <EpetraExt_CrsMatrixIn.h>
69 #include <EpetraExt_MultiVectorIn.h>
70 #include <EpetraExt_BlockMapIn.h>
71 #include <Xpetra_EpetraUtils.hpp>
72 #include <Xpetra_EpetraMultiVector.hpp>
73 #include <EpetraExt_BlockMapOut.h>
74 #endif
75 
76 #ifdef HAVE_MUELU_TPETRA
77 #include <MatrixMarket_Tpetra.hpp>
78 #include <Tpetra_RowMatrixTransposer.hpp>
79 #include <TpetraExt_MatrixMatrix.hpp>
80 #include <Xpetra_TpetraMultiVector.hpp>
81 #include <Xpetra_TpetraCrsMatrix.hpp>
82 #include <Xpetra_TpetraBlockCrsMatrix.hpp>
83 #endif
84 
85 #ifdef HAVE_MUELU_EPETRA
86 #include <Xpetra_EpetraMap.hpp>
87 #endif
88 
89 #include <Xpetra_BlockedCrsMatrix.hpp>
90 #include <Xpetra_DefaultPlatform.hpp>
91 #include <Xpetra_Import.hpp>
92 #include <Xpetra_ImportFactory.hpp>
93 #include <Xpetra_Map.hpp>
94 #include <Xpetra_MapFactory.hpp>
95 #include <Xpetra_Matrix.hpp>
96 #include <Xpetra_MatrixMatrix.hpp>
97 #include <Xpetra_MatrixFactory.hpp>
98 #include <Xpetra_MultiVector.hpp>
99 #include <Xpetra_MultiVectorFactory.hpp>
100 #include <Xpetra_Operator.hpp>
101 #include <Xpetra_Vector.hpp>
102 #include <Xpetra_VectorFactory.hpp>
103 
105 
106 namespace MueLu {
107 
108  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
109  Teuchos::ArrayRCP<Scalar> Utilities_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetMatrixDiagonal(const Matrix& A) {
110  // FIXME Kokkos
111 
112  size_t numRows = A.getRowMap()->getNodeNumElements();
113  Teuchos::ArrayRCP<SC> diag(numRows);
114 
115  Teuchos::ArrayView<const LO> cols;
116  Teuchos::ArrayView<const SC> vals;
117  for (size_t i = 0; i < numRows; ++i) {
118  A.getLocalRowView(i, cols, vals);
119 
120  LO j = 0;
121  for (; j < cols.size(); ++j) {
122  if (Teuchos::as<size_t>(cols[j]) == i) {
123  diag[i] = vals[j];
124  break;
125  }
126  }
127  if (j == cols.size()) {
128  // Diagonal entry is absent
129  diag[i] = Teuchos::ScalarTraits<SC>::zero();
130  }
131  }
132 
133  return diag;
134  } //GetMatrixDiagonal
135 
136  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
137  Teuchos::RCP<Xpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > Utilities_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetMatrixDiagonalInverse(const Matrix& A, Magnitude tol) {
138  // FIXME Kokkos
139  RCP<const Map> rowMap = A.getRowMap();
140  RCP<Vector> diag = VectorFactory::Build(rowMap);
141  ArrayRCP<SC> diagVals = diag->getDataNonConst(0);
142 
143  size_t numRows = rowMap->getNodeNumElements();
144 
145  Teuchos::ArrayView<const LO> cols;
146  Teuchos::ArrayView<const SC> vals;
147  for (size_t i = 0; i < numRows; ++i) {
148  A.getLocalRowView(i, cols, vals);
149 
150  LO j = 0;
151  for (; j < cols.size(); ++j) {
152  if (Teuchos::as<size_t>(cols[j]) == i) {
153  if(Teuchos::ScalarTraits<SC>::magnitude(vals[j]) > tol)
154  diagVals[i] = Teuchos::ScalarTraits<SC>::one() / vals[j];
155  else
156  diagVals[i]=Teuchos::ScalarTraits<SC>::zero();
157  break;
158  }
159  }
160  if (j == cols.size()) {
161  // Diagonal entry is absent
162  diagVals[i]=Teuchos::ScalarTraits<SC>::zero();
163  }
164  }
165  diagVals=null;
166 
167  return diag;
168  } //GetMatrixDiagonalInverse
169 
170  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
171  Teuchos::ArrayRCP<Scalar> Utilities_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetLumpedMatrixDiagonal(const Matrix &A) {
172  // FIXME: Kokkos
173  size_t numRows = A.getRowMap()->getNodeNumElements();
174  Teuchos::ArrayRCP<SC> diag(numRows);
175 
176  Teuchos::ArrayView<const LO> cols;
177  Teuchos::ArrayView<const SC> vals;
178  for (size_t i = 0; i < numRows; ++i) {
179  A.getLocalRowView(i, cols, vals);
180 
181  diag[i] = Teuchos::ScalarTraits<Scalar>::zero();
182  for (LO j = 0; j < cols.size(); ++j) {
183  diag[i] += Teuchos::ScalarTraits<Scalar>::magnitude(vals[j]);
184  }
185  }
186 
187  return diag;
188  } //GetMatrixDiagonal
189 
190  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
191  RCP<Xpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > Utilities_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetMatrixOverlappedDiagonal(const Matrix& A) {
192  // FIXME: Kokkos
193  RCP<const Map> rowMap = A.getRowMap(), colMap = A.getColMap();
194  RCP<Vector> localDiag = VectorFactory::Build(rowMap);
195 
196  try {
197  const CrsMatrixWrap* crsOp = dynamic_cast<const CrsMatrixWrap*>(&A);
198  if (crsOp == NULL) {
199  throw Exceptions::RuntimeError("cast to CrsMatrixWrap failed");
200  }
201  Teuchos::ArrayRCP<size_t> offsets;
202  crsOp->getLocalDiagOffsets(offsets);
203  crsOp->getLocalDiagCopy(*localDiag,offsets());
204  }
205  catch (...) {
206  ArrayRCP<SC> localDiagVals = localDiag->getDataNonConst(0);
207  Teuchos::ArrayRCP<SC> diagVals = GetMatrixDiagonal(A);
208  for (LO i = 0; i < localDiagVals.size(); i++)
209  localDiagVals[i] = diagVals[i];
210  localDiagVals = diagVals = null;
211  }
212 
213  RCP<Vector> diagonal = VectorFactory::Build(colMap);
214  RCP< const Import> importer;
215  importer = A.getCrsGraph()->getImporter();
216  if (importer == Teuchos::null) {
217  importer = ImportFactory::Build(rowMap, colMap);
218  }
219  diagonal->doImport(*localDiag, *(importer), Xpetra::INSERT);
220 
221  return diagonal;
222  } //GetMatrixOverlappedDiagonal
223 
224  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
225  void Utilities_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MyOldScaleMatrix(Matrix& Op, const Teuchos::ArrayRCP<const SC>& scalingVector, bool doInverse,
226  bool doFillComplete,
227  bool doOptimizeStorage)
228  {
229  SC one = Teuchos::ScalarTraits<SC>::one();
230  Teuchos::ArrayRCP<SC> sv(scalingVector.size());
231  if (doInverse) {
232  for (int i = 0; i < scalingVector.size(); ++i)
233  sv[i] = one / scalingVector[i];
234  } else {
235  for (int i = 0; i < scalingVector.size(); ++i)
236  sv[i] = scalingVector[i];
237  }
238 
239  switch (Op.getRowMap()->lib()) {
240  case Xpetra::UseTpetra:
241  MyOldScaleMatrix_Tpetra(Op, sv, doFillComplete, doOptimizeStorage);
242  break;
243 
244  case Xpetra::UseEpetra:
245  // FIXME?
246  // Utils2_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MyOldScaleMatrix_Epetra(Op, sv, doFillComplete, doOptimizeStorage);
247  throw std::runtime_error("FIXME");
248  break;
249 
250  default:
251  throw Exceptions::RuntimeError("Only Epetra and Tpetra matrices can be scaled.");
252  break;
253  }
254  }
255 
256  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
257  void Utilities_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MyOldScaleMatrix_Epetra(Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Op, const Teuchos::ArrayRCP<Scalar>& scalingVector, bool doFillComplete, bool doOptimizeStorage) {
258  throw Exceptions::RuntimeError("MyOldScaleMatrix_Epetra: Epetra needs SC=double and LO=GO=int.");
259  }
260 
261  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
262  void Utilities_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MyOldScaleMatrix_Tpetra(Matrix& Op, const Teuchos::ArrayRCP<SC>& scalingVector,
263  bool doFillComplete,
264  bool doOptimizeStorage)
265  {
266 #ifdef HAVE_MUELU_TPETRA
267  try {
268  Tpetra::CrsMatrix<SC,LO,GO,NO>& tpOp = Op2NonConstTpetraCrs(Op);
269 
270  const RCP<const Tpetra::Map<LO,GO,NO> > rowMap = tpOp.getRowMap();
271  const RCP<const Tpetra::Map<LO,GO,NO> > domainMap = tpOp.getDomainMap();
272  const RCP<const Tpetra::Map<LO,GO,NO> > rangeMap = tpOp.getRangeMap();
273 
274  size_t maxRowSize = tpOp.getNodeMaxNumRowEntries();
275  if (maxRowSize == Teuchos::as<size_t>(-1)) // hasn't been determined yet
276  maxRowSize = 20;
277 
278  std::vector<SC> scaledVals(maxRowSize);
279  if (tpOp.isFillComplete())
280  tpOp.resumeFill();
281 
282  if (Op.isLocallyIndexed() == true) {
283  Teuchos::ArrayView<const LO> cols;
284  Teuchos::ArrayView<const SC> vals;
285 
286  for (size_t i = 0; i < rowMap->getNodeNumElements(); ++i) {
287  tpOp.getLocalRowView(i, cols, vals);
288  size_t nnz = tpOp.getNumEntriesInLocalRow(i);
289  if (nnz > maxRowSize) {
290  maxRowSize = nnz;
291  scaledVals.resize(maxRowSize);
292  }
293  for (size_t j = 0; j < nnz; ++j)
294  scaledVals[j] = vals[j]*scalingVector[i];
295 
296  if (nnz > 0) {
297  Teuchos::ArrayView<const SC> valview(&scaledVals[0], nnz);
298  tpOp.replaceLocalValues(i, cols, valview);
299  }
300  } //for (size_t i=0; ...
301 
302  } else {
303  Teuchos::ArrayView<const GO> cols;
304  Teuchos::ArrayView<const SC> vals;
305 
306  for (size_t i = 0; i < rowMap->getNodeNumElements(); ++i) {
307  GO gid = rowMap->getGlobalElement(i);
308  tpOp.getGlobalRowView(gid, cols, vals);
309  size_t nnz = tpOp.getNumEntriesInGlobalRow(gid);
310  if (nnz > maxRowSize) {
311  maxRowSize = nnz;
312  scaledVals.resize(maxRowSize);
313  }
314  // FIXME FIXME FIXME FIXME FIXME FIXME
315  for (size_t j = 0; j < nnz; ++j)
316  scaledVals[j] = vals[j]*scalingVector[i]; //FIXME i or gid?
317 
318  if (nnz > 0) {
319  Teuchos::ArrayView<const SC> valview(&scaledVals[0], nnz);
320  tpOp.replaceGlobalValues(gid, cols, valview);
321  }
322  } //for (size_t i=0; ...
323  }
324 
325  if (doFillComplete) {
326  if (domainMap == Teuchos::null || rangeMap == Teuchos::null)
327  throw Exceptions::RuntimeError("In Utils_kokkos::Scaling: cannot fillComplete because the domain and/or range map hasn't been defined");
328 
329  RCP<Teuchos::ParameterList> params = rcp(new Teuchos::ParameterList());
330  params->set("Optimize Storage", doOptimizeStorage);
331  params->set("No Nonlocal Changes", true);
332  Op.fillComplete(Op.getDomainMap(), Op.getRangeMap(), params);
333  }
334  } catch(...) {
335  throw Exceptions::RuntimeError("Only Tpetra::CrsMatrix types can be scaled (Err.1)");
336  }
337 #else
338  throw Exceptions::RuntimeError("Matrix scaling is not possible because Tpetra has not been enabled.");
339 #endif
340  } //MyOldScaleMatrix_Tpetra()
341 
342  template<class MatrixType, class BNodesType>
344  private:
345  typedef typename MatrixType::ordinal_type LO;
346  typedef typename MatrixType::value_type SC;
347  typedef Kokkos::ArithTraits<SC> ATS;
348 
349  MatrixType localMatrix;
350  BNodesType boundaryNodes;
351  typename ATS::mag_type tol;
352 
353  public:
354  DetectDirichletFunctor(MatrixType localMatrix_, BNodesType boundaryNodes_, SC tol_) :
355  localMatrix(localMatrix_),
356  boundaryNodes(boundaryNodes_)
357  {
358  tol = ATS::magnitude(tol_);
359  }
360 
361  KOKKOS_INLINE_FUNCTION
362  void operator()(const LO row) const {
363  auto rowView = localMatrix.row(row);
364  auto length = rowView.length;
365 
366  boundaryNodes(row) = true;
367  for (decltype(length) colID = 0; colID < length; colID++)
368  if ((rowView.colidx(colID) != row) && (ATS::magnitude(rowView.value(colID)) > tol)) {
369  boundaryNodes(row) = false;
370  break;
371  }
372  }
373  };
374 
375  template <class SC, class LO, class GO, class NO>
376  Kokkos::View<const bool*, typename NO::device_type> DetectDirichletRows(const Xpetra::Matrix<SC,LO,GO,NO>& A, const typename Teuchos::ScalarTraits<SC>::magnitudeType& tol) {
377  auto localMatrix = A.getLocalMatrix();
378  LO numRows = A.getNodeNumRows();
379 
380  Kokkos::View<bool*, typename NO::device_type> boundaryNodes("boundaryNodes", numRows);
381 
382  DetectDirichletFunctor<decltype(localMatrix), decltype(boundaryNodes)> functor(localMatrix, boundaryNodes, tol);
383  Kokkos::parallel_for("MueLu:Utils::DetectDirichletRows", numRows, functor);
384 
385  return boundaryNodes;
386  }
387 
388  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
389  Kokkos::View<const bool*, typename Node::device_type>
391  DetectDirichletRows(const Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>& A, const typename Teuchos::ScalarTraits<Scalar>::magnitudeType& tol) {
392  return MueLu::DetectDirichletRows<Scalar,LocalOrdinal,GlobalOrdinal,Node>(A, tol);
393  }
394 
395  template <class Node>
396  Kokkos::View<const bool*, typename Node::device_type>
398  DetectDirichletRows(const Xpetra::Matrix<double,int,int,Node>& A, const typename Teuchos::ScalarTraits<double>::magnitudeType& tol) {
399  return MueLu::DetectDirichletRows<double,int,int,Node>(A, tol);
400  }
401 
402 } //namespace MueLu
403 
404 #define MUELU_UTILITIES_KOKKOS_SHORT
405 #endif // MUELU_UTILITIES_KOKKOS_DEF_HPP
LocalOrdinal replaceGlobalValues(const GlobalOrdinal globalRow, const typename UnmanagedView< GlobalIndicesViewType >::type &inputInds, const typename UnmanagedView< ImplScalarViewType >::type &inputVals) const
void getGlobalRowView(GlobalOrdinal GlobalRow, Teuchos::ArrayView< const GlobalOrdinal > &indices, Teuchos::ArrayView< const Scalar > &values) const
size_t getNodeMaxNumRowEntries() const
DetectDirichletFunctor(MatrixType localMatrix_, BNodesType boundaryNodes_, SC tol_)
LocalOrdinal replaceLocalValues(const LocalOrdinal localRow, const typename UnmanagedView< LocalIndicesViewType >::type &inputInds, const typename UnmanagedView< ImplScalarViewType >::type &inputVals) const
void resumeFill(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Teuchos::RCP< const map_type > getRowMap() const
size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
Namespace for MueLu classes and methods.
KOKKOS_INLINE_FUNCTION void operator()(const LO row) const
Kokkos::View< const bool *, typename NO::device_type > DetectDirichletRows(const Xpetra::Matrix< SC, LO, GO, NO > &A, const typename Teuchos::ScalarTraits< SC >::magnitudeType &tol)
bool isFillComplete() const
void getLocalRowView(LocalOrdinal LocalRow, Teuchos::ArrayView< const LocalOrdinal > &indices, Teuchos::ArrayView< const Scalar > &values) const
size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Teuchos::RCP< const map_type > getDomainMap() const
Teuchos::RCP< const map_type > getRangeMap() const