MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_Utilities_kokkos_decl.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_DECL_HPP
47 #define MUELU_UTILITIES_KOKKOS_DECL_HPP
48 
49 #include "MueLu_ConfigDefs.hpp"
50 #if defined(HAVE_MUELU_KOKKOS_REFACTOR)
51 
52 #include <string>
53 
54 #include <Teuchos_ScalarTraits.hpp>
56 
58 #include <Xpetra_CrsMatrix_fwd.hpp>
60 #include <Xpetra_ExportFactory.hpp>
63 #include <Xpetra_Map_fwd.hpp>
65 #include <Xpetra_Matrix_fwd.hpp>
68 #include <Xpetra_Operator_fwd.hpp>
70 #include <Xpetra_Vector_fwd.hpp>
71 
72 #include <Xpetra_IO.hpp>
73 
74 #ifdef HAVE_MUELU_EPETRA
75 #include <Epetra_MultiVector.h>
76 #include <Epetra_CrsMatrix.h>
79 #endif
80 
81 #include "MueLu_Exceptions.hpp"
82 #include "MueLu_Utilities.hpp"
83 #include "MueLu_UtilitiesBase.hpp"
84 
85 #ifdef HAVE_MUELU_TPETRA
86 #include <Tpetra_CrsMatrix.hpp>
87 #include <Tpetra_Map.hpp>
88 #include <Tpetra_MultiVector.hpp>
91 #endif
92 
93 
94 namespace MueLu {
95 
103  template <class Scalar,
104  class LocalOrdinal = int,
105  class GlobalOrdinal = LocalOrdinal,
107  class Utilities_kokkos : public MueLu::UtilitiesBase<Scalar,LocalOrdinal,GlobalOrdinal,Node> {
108 #undef MUELU_UTILITIES_KOKKOS_SHORT
109 #include "MueLu_UseShortNames.hpp"
110 
111  public:
112  typedef typename Teuchos::ScalarTraits<SC>::magnitudeType Magnitude;
113  typedef Xpetra::MultiVector<Magnitude,LO,GO,NO> RealValuedMultiVector;
114 
115 #ifdef HAVE_MUELU_EPETRA
116  // @{
118  static RCP<const Epetra_MultiVector> MV2EpetraMV(RCP<MultiVector> const vec) { return Utilities::MV2EpetraMV(vec); }
119  static RCP< Epetra_MultiVector> MV2NonConstEpetraMV(RCP<MultiVector> vec) { return Utilities::MV2NonConstEpetraMV(vec); }
120 
121  static const Epetra_MultiVector& MV2EpetraMV(const MultiVector& vec) { return Utilities::MV2EpetraMV(vec); }
122  static Epetra_MultiVector& MV2NonConstEpetraMV(MultiVector& vec) { return Utilities::MV2NonConstEpetraMV(vec); }
123 
124  static RCP<const Epetra_CrsMatrix> Op2EpetraCrs(RCP<const Matrix> Op) { return Utilities::Op2EpetraCrs(Op); }
125  static RCP< Epetra_CrsMatrix> Op2NonConstEpetraCrs(RCP<Matrix> Op) { return Utilities::Op2NonConstEpetraCrs(Op); }
126 
127  static const Epetra_CrsMatrix& Op2EpetraCrs(const Matrix& Op) { return Utilities::Op2EpetraCrs(Op); }
128  static Epetra_CrsMatrix& Op2NonConstEpetraCrs(Matrix& Op) { return Utilities::Op2NonConstEpetraCrs(Op); }
129 
130  static const Epetra_Map& Map2EpetraMap(const Map& map) { return Utilities::Map2EpetraMap(map); }
131  // @}
132 #endif
133 
134 #ifdef HAVE_MUELU_TPETRA
135  // @{
137  static RCP<const Tpetra::MultiVector<SC,LO,GO,NO> > MV2TpetraMV(RCP<MultiVector> const vec) { return Utilities::MV2TpetraMV(vec); }
138  static RCP< Tpetra::MultiVector<SC,LO,GO,NO> > MV2NonConstTpetraMV(RCP<MultiVector> vec) { return Utilities::MV2NonConstTpetraMV(vec); }
139  static RCP< Tpetra::MultiVector<SC,LO,GO,NO> > MV2NonConstTpetraMV2(MultiVector& vec) { return Utilities::MV2NonConstTpetraMV2(vec); }
140 
141  static const Tpetra::MultiVector<SC,LO,GO,NO>& MV2TpetraMV(const MultiVector& vec) { return Utilities::MV2TpetraMV(vec); }
142  static Tpetra::MultiVector<SC,LO,GO,NO>& MV2NonConstTpetraMV(MultiVector& vec) { return Utilities::MV2NonConstTpetraMV(vec); }
143 
144  static RCP<const Tpetra::CrsMatrix<SC,LO,GO,NO> > Op2TpetraCrs(RCP<const Matrix> Op) { return Utilities::Op2TpetraCrs(Op); }
145  static RCP< Tpetra::CrsMatrix<SC,LO,GO,NO> > Op2NonConstTpetraCrs(RCP<Matrix> Op) { return Utilities::Op2NonConstTpetraCrs(Op); }
146 
147  static const Tpetra::CrsMatrix<SC,LO,GO,NO>& Op2TpetraCrs(const Matrix& Op) { return Utilities::Op2TpetraCrs(Op); }
148  static Tpetra::CrsMatrix<SC,LO,GO,NO>& Op2NonConstTpetraCrs(Matrix& Op) { return Utilities::Op2NonConstTpetraCrs(Op); }
149 
150  static RCP<const Tpetra::RowMatrix<SC,LO,GO,NO> > Op2TpetraRow(RCP<const Matrix> Op) { return Utilities::Op2TpetraRow(Op); }
151  static RCP< Tpetra::RowMatrix<SC,LO,GO,NO> > Op2NonConstTpetraRow(RCP<Matrix> Op) { return Utilities::Op2NonConstTpetraRow(Op); }
152 
153  static const RCP<const Tpetra::Map<LO, GO, NO> > Map2TpetraMap(const Map& map) { return Utilities::Map2TpetraMap(map); }
154 #endif
155 
156  static RCP<Xpetra::Matrix<SC,LO,GO,NO> > Crs2Op(RCP<CrsMatrix> Op) { return Utilities::Crs2Op(Op); }
157 
164  static Teuchos::ArrayRCP<SC> GetMatrixDiagonal(const Matrix& A); // FIXME
165 
172  static RCP<Vector> GetMatrixDiagonalInverse(const Matrix& A, Magnitude tol = Teuchos::ScalarTraits<SC>::eps()*100); // FIXME
173 
174 
175 
182  static Teuchos::ArrayRCP<SC> GetLumpedMatrixDiagonal(const Matrix& A); // FIXME
183 
185 
193  static RCP<Vector> GetMatrixOverlappedDiagonal(const Matrix& A); // FIXME
194 
203 
204  // TODO: should NOT return an Array. Definition must be changed to:
205  // - ArrayRCP<> ResidualNorm(Matrix const &Op, MultiVector const &X, MultiVector const &RHS)
206  // or
207  // - void ResidualNorm(Matrix const &Op, MultiVector const &X, MultiVector const &RHS, Array &)
208  static Teuchos::Array<Magnitude> ResidualNorm(const Operator& Op, const MultiVector& X, const MultiVector& RHS) {
209  return Utilities::ResidualNorm(Op, X, RHS);
210  }
211 
212  static RCP<MultiVector> Residual(const Operator& Op, const MultiVector& X, const MultiVector& RHS) {
213  return Utilities::Residual(Op, X, RHS);
214  }
215 
216  static void PauseForDebugger();
217 
233  static SC PowerMethod(const Matrix& A, bool scaleByDiag = true,
234  LO niters = 10, Magnitude tolerance = 1e-2, bool verbose = false, unsigned int seed = 123) {
235  return Utilities::PowerMethod(A, scaleByDiag, niters, tolerance, verbose, seed);
236  }
237 
238  static void MyOldScaleMatrix(Matrix& Op, const Teuchos::ArrayRCP<const SC>& scalingVector, bool doInverse = true,
239  bool doFillComplete = true, bool doOptimizeStorage = true); // FIXME
240 
241  static void MyOldScaleMatrix_Tpetra(Matrix& Op, const Teuchos::ArrayRCP<SC>& scalingVector,
242  bool doFillComplete, bool doOptimizeStorage); // FIXME
243 
244  static void MyOldScaleMatrix_Epetra(Matrix& Op, const Teuchos::ArrayRCP<SC>& scalingVector,
245  bool doFillComplete, bool doOptimizeStorage); // FIXME
246 
247  static RCP<Teuchos::FancyOStream> MakeFancy(std::ostream& os) { return Utilities::MakeFancy(os); }
248 
256  static Kokkos::View<const bool*, typename NO::device_type> DetectDirichletRows(const Matrix& A, const Magnitude& tol = Teuchos::ScalarTraits<SC>::zero(), const bool count_twos_as_dirichlet=false);
257 
269 
270 
271  static void ZeroDirichletRows(RCP<Matrix>& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows, SC replaceWith=Teuchos::ScalarTraits<SC>::zero());
272 
273  static void ZeroDirichletRows(RCP<MultiVector>& X, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows, SC replaceWith=Teuchos::ScalarTraits<SC>::zero());
274 
275  static void ZeroDirichletCols(RCP<Matrix>& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletCols, SC replaceWith=Teuchos::ScalarTraits<SC>::zero());
276 
277  static RCP<MultiVector> RealValuedToScalarMultiVector(RCP<RealValuedMultiVector> X);
278 
288  static void SetRandomSeed(const Teuchos::Comm<int> &comm) { return Utilities::SetRandomSeed(comm); }
289 
295  static RCP<Matrix> Transpose(Matrix& Op, bool optimizeTranspose = false, const std::string & label = std::string()) {
296  return Utilities::Transpose(Op, optimizeTranspose, label);
297  }
298 
299  static RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LocalOrdinal,GlobalOrdinal,Node> > ExtractCoordinatesFromParameterList(ParameterList& paramList) {
301  }
302 
303  }; // class Utils
304 
305 
315  template <class Node>
316  class Utilities_kokkos<double,int,int,Node> : public UtilitiesBase<double,int,int,Node> {
317  public:
318  typedef double Scalar;
319  typedef int LocalOrdinal;
320  typedef int GlobalOrdinal;
321  typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType Magnitude;
323 
324  private:
325 #undef MUELU_UTILITIES_KOKKOS_SHORT
326 #include "MueLu_UseShortNames.hpp"
327 
328  public:
329 
330 #ifdef HAVE_MUELU_EPETRA
331  // @{
333  static RCP<const Epetra_MultiVector> MV2EpetraMV(RCP<MultiVector> const vec) { return Utilities::MV2EpetraMV(vec); }
334  static RCP< Epetra_MultiVector> MV2NonConstEpetraMV(RCP<MultiVector> vec) { return Utilities::MV2NonConstEpetraMV(vec); }
335 
336  static const Epetra_MultiVector& MV2EpetraMV(const MultiVector& vec) { return Utilities::MV2EpetraMV(vec); }
337  static Epetra_MultiVector& MV2NonConstEpetraMV(MultiVector& vec) { return Utilities::MV2NonConstEpetraMV(vec); }
338 
339  static RCP<const Epetra_CrsMatrix> Op2EpetraCrs(RCP<const Matrix> Op) { return Utilities::Op2EpetraCrs(Op); }
340  static RCP< Epetra_CrsMatrix> Op2NonConstEpetraCrs(RCP<Matrix> Op) { return Utilities::Op2NonConstEpetraCrs(Op); }
341 
342  static const Epetra_CrsMatrix& Op2EpetraCrs(const Matrix& Op) { return Utilities::Op2EpetraCrs(Op); }
343  static Epetra_CrsMatrix& Op2NonConstEpetraCrs(Matrix& Op) { return Utilities::Op2NonConstEpetraCrs(Op); }
344 
345  static const Epetra_Map& Map2EpetraMap(const Map& map) { return Utilities::Map2EpetraMap(map); }
346  // @}
347 #endif
348 
349 #ifdef HAVE_MUELU_TPETRA
350  // @{
352  static RCP<const Tpetra::MultiVector<SC,LO,GO,NO> > MV2TpetraMV(RCP<MultiVector> const vec) { return Utilities::MV2TpetraMV(vec); }
353  static RCP< Tpetra::MultiVector<SC,LO,GO,NO> > MV2NonConstTpetraMV(RCP<MultiVector> vec) { return Utilities::MV2NonConstTpetraMV(vec); }
354  static RCP< Tpetra::MultiVector<SC,LO,GO,NO> > MV2NonConstTpetraMV2(MultiVector& vec) { return Utilities::MV2NonConstTpetraMV2(vec); }
355 
356  static const Tpetra::MultiVector<SC,LO,GO,NO>& MV2TpetraMV(const MultiVector& vec) { return Utilities::MV2TpetraMV(vec); }
357  static Tpetra::MultiVector<SC,LO,GO,NO>& MV2NonConstTpetraMV(MultiVector& vec) { return Utilities::MV2NonConstTpetraMV(vec); }
358 
359  static RCP<const Tpetra::CrsMatrix<SC,LO,GO,NO> > Op2TpetraCrs(RCP<const Matrix> Op) { return Utilities::Op2TpetraCrs(Op); }
360  static RCP< Tpetra::CrsMatrix<SC,LO,GO,NO> > Op2NonConstTpetraCrs(RCP<Matrix> Op) { return Utilities::Op2NonConstTpetraCrs(Op); }
361 
362  static const Tpetra::CrsMatrix<SC,LO,GO,NO>& Op2TpetraCrs(const Matrix& Op) { return Utilities::Op2TpetraCrs(Op); }
363  static Tpetra::CrsMatrix<SC,LO,GO,NO>& Op2NonConstTpetraCrs(Matrix& Op) { return Utilities::Op2NonConstTpetraCrs(Op); }
364 
365  static RCP<const Tpetra::RowMatrix<SC,LO,GO,NO> > Op2TpetraRow(RCP<const Matrix> Op) { return Utilities::Op2TpetraRow(Op); }
366  static RCP< Tpetra::RowMatrix<SC,LO,GO,NO> > Op2NonConstTpetraRow(RCP<Matrix> Op) { return Utilities::Op2NonConstTpetraRow(Op); }
367 
368  static const RCP<const Tpetra::Map<LO, GO, NO> > Map2TpetraMap(const Map& map) { return Utilities::Map2TpetraMap(map); }
369 #endif
370  static RCP<Xpetra::Matrix<SC,LO,GO,NO> > Crs2Op(RCP<CrsMatrix> Op) { return Utilities::Crs2Op(Op); }
371 
372  static ArrayRCP<SC> GetMatrixDiagonal(const Matrix& A) {
374  }
375  static RCP<Vector> GetMatrixDiagonalInverse(const Matrix& A, Magnitude tol = Teuchos::ScalarTraits<SC>::eps()*100) {
377  }
378  static ArrayRCP<SC> GetLumpedMatrixDiagonal(const Matrix& A) {
380  }
381  static RCP<Vector> GetLumpedMatrixDiagonal(RCP<const Matrix > A) {
383  }
384  static RCP<Vector> GetMatrixOverlappedDiagonal(const Matrix& A) {
386  }
387  static RCP<Vector> GetInverse(RCP<const Vector> v, Magnitude tol = Teuchos::ScalarTraits<SC>::eps()*100, SC tolReplacement = Teuchos::ScalarTraits<SC>::zero()) {
388  return UtilitiesBase::GetInverse(v,tol,tolReplacement);
389  }
390  static Teuchos::Array<Magnitude> ResidualNorm(const Operator& Op, const MultiVector& X, const MultiVector& RHS) {
391  return UtilitiesBase::ResidualNorm(Op,X,RHS);
392  }
393  static RCP<MultiVector> Residual(const Operator& Op, const MultiVector& X, const MultiVector& RHS) {
394  return UtilitiesBase::Residual(Op,X,RHS);
395  }
396  static void PauseForDebugger() {
398  }
399  static RCP<Teuchos::FancyOStream> MakeFancy(std::ostream& os) {
400  return UtilitiesBase::MakeFancy(os);
401  }
402  static void SetRandomSeed(const Teuchos::Comm<int> &comm) {
404  }
405 
406  // todo: move this to UtilitiesBase::kokkos
407  static Kokkos::View<const bool*, typename Node::device_type> DetectDirichletRows(const Matrix& A, const Magnitude& tol = Teuchos::ScalarTraits<SC>::zero(), const bool count_twos_as_dirichlet=false);
408 
410 
411  static void ZeroDirichletRows(RCP<Matrix>& A, const Kokkos::View<const bool*, typename Node::device_type>& dirichletRows, SC replaceWith=Teuchos::ScalarTraits<SC>::zero());
412 
413  static void ZeroDirichletRows(RCP<MultiVector>& X, const Kokkos::View<const bool*, typename Node::device_type>& dirichletRows, SC replaceWith=Teuchos::ScalarTraits<SC>::zero());
414 
415  static void ZeroDirichletCols(RCP<Matrix>& A, const Kokkos::View<const bool*, typename Node::device_type>& dirichletCols, SC replaceWith=Teuchos::ScalarTraits<SC>::zero());
416 
417  static RCP<MultiVector> RealValuedToScalarMultiVector(RCP<RealValuedMultiVector> X);
418 
419  static Scalar PowerMethod(const Matrix& A, bool scaleByDiag = true, LO niters = 10, Magnitude tolerance = 1e-2, bool verbose = false, unsigned int seed = 123) {
420  return UtilitiesBase::PowerMethod(A,scaleByDiag,niters,tolerance,verbose,seed);
421  }
422 
423  static void MyOldScaleMatrix(Matrix& Op, const Teuchos::ArrayRCP<const SC>& scalingVector, bool doInverse = true,
424  bool doFillComplete = true, bool doOptimizeStorage = true) {
426  Teuchos::ArrayRCP<SC> sv(scalingVector.size());
427  if (doInverse) {
428  for (int i = 0; i < scalingVector.size(); ++i)
429  sv[i] = one / scalingVector[i];
430  } else {
431  for (int i = 0; i < scalingVector.size(); ++i)
432  sv[i] = scalingVector[i];
433  }
434 
435  switch (Op.getRowMap()->lib()) {
436  case Xpetra::UseTpetra:
437  MyOldScaleMatrix_Tpetra(Op, sv, doFillComplete, doOptimizeStorage);
438  break;
439 
440  case Xpetra::UseEpetra:
441  MyOldScaleMatrix_Epetra(Op, sv, doFillComplete, doOptimizeStorage);
442  break;
443 
444  default:
445  throw Exceptions::RuntimeError("Only Epetra and Tpetra matrices can be scaled.");
446 #ifndef __NVCC__ //prevent nvcc warning
447  break;
448 #endif
449  }
450  }
451 
452  // TODO This is the <double,int,int> specialization
453  static void MyOldScaleMatrix_Tpetra(Matrix& Op, const Teuchos::ArrayRCP<Scalar>& scalingVector,
454  bool doFillComplete, bool doOptimizeStorage) {
455  #ifdef HAVE_MUELU_TPETRA
456  #ifdef HAVE_MUELU_TPETRA_INST_INT_INT
457  try {
458  Tpetra::CrsMatrix<SC,LO,GO,NO>& tpOp = Op2NonConstTpetraCrs(Op);
459 
460  const RCP<const Tpetra::Map<LO,GO,NO> > rowMap = tpOp.getRowMap();
461  const RCP<const Tpetra::Map<LO,GO,NO> > domainMap = tpOp.getDomainMap();
462  const RCP<const Tpetra::Map<LO,GO,NO> > rangeMap = tpOp.getRangeMap();
463 
464  size_t maxRowSize = tpOp.getNodeMaxNumRowEntries();
465  if (maxRowSize == Teuchos::as<size_t>(-1)) // hasn't been determined yet
466  maxRowSize = 20;
467 
468  std::vector<SC> scaledVals(maxRowSize);
469  if (tpOp.isFillComplete())
470  tpOp.resumeFill();
471 
472  if (Op.isLocallyIndexed() == true) {
475 
476  for (size_t i = 0; i < rowMap->getNodeNumElements(); ++i) {
477  tpOp.getLocalRowView(i, cols, vals);
478  size_t nnz = tpOp.getNumEntriesInLocalRow(i);
479  if (nnz > maxRowSize) {
480  maxRowSize = nnz;
481  scaledVals.resize(maxRowSize);
482  }
483  for (size_t j = 0; j < nnz; ++j)
484  scaledVals[j] = vals[j]*scalingVector[i];
485 
486  if (nnz > 0) {
487  Teuchos::ArrayView<const SC> valview(&scaledVals[0], nnz);
488  tpOp.replaceLocalValues(i, cols, valview);
489  }
490  } //for (size_t i=0; ...
491 
492  } else {
495 
496  for (size_t i = 0; i < rowMap->getNodeNumElements(); ++i) {
497  GO gid = rowMap->getGlobalElement(i);
498  tpOp.getGlobalRowView(gid, cols, vals);
499  size_t nnz = tpOp.getNumEntriesInGlobalRow(gid);
500  if (nnz > maxRowSize) {
501  maxRowSize = nnz;
502  scaledVals.resize(maxRowSize);
503  }
504  // FIXME FIXME FIXME FIXME FIXME FIXME
505  for (size_t j = 0; j < nnz; ++j)
506  scaledVals[j] = vals[j]*scalingVector[i]; //FIXME i or gid?
507 
508  if (nnz > 0) {
509  Teuchos::ArrayView<const SC> valview(&scaledVals[0], nnz);
510  tpOp.replaceGlobalValues(gid, cols, valview);
511  }
512  } //for (size_t i=0; ...
513  }
514 
515  if (doFillComplete) {
516  if (domainMap == Teuchos::null || rangeMap == Teuchos::null)
517  throw Exceptions::RuntimeError("In Utilities::Scaling: cannot fillComplete because the domain and/or range map hasn't been defined");
518 
519  RCP<Teuchos::ParameterList> params = rcp(new Teuchos::ParameterList());
520  params->set("Optimize Storage", doOptimizeStorage);
521  params->set("No Nonlocal Changes", true);
522  Op.fillComplete(Op.getDomainMap(), Op.getRangeMap(), params);
523  }
524  } catch(...) {
525  throw Exceptions::RuntimeError("Only Tpetra::CrsMatrix types can be scaled (Err.1)");
526  }
527 #else
528  throw Exceptions::RuntimeError("Matrix scaling is not possible because Tpetra has not been compiled with support for LO=GO=int.");
529 #endif
530 #else
531  throw Exceptions::RuntimeError("Matrix scaling is not possible because Tpetra has not been enabled.");
532 #endif
533  }
534 
535  static void MyOldScaleMatrix_Epetra(Matrix& Op, const Teuchos::ArrayRCP<Scalar>& scalingVector, bool doFillComplete, bool doOptimizeStorage) {
536 #ifdef HAVE_MUELU_EPETRA
537  try {
538  //const Epetra_CrsMatrix& epOp = Utilities<double,int,int>::Op2NonConstEpetraCrs(Op);
539  const Epetra_CrsMatrix& epOp = Op2NonConstEpetraCrs(Op);
540 
541  Epetra_Map const &rowMap = epOp.RowMap();
542  int nnz;
543  double *vals;
544  int *cols;
545 
546  for (int i = 0; i < rowMap.NumMyElements(); ++i) {
547  epOp.ExtractMyRowView(i, nnz, vals, cols);
548  for (int j = 0; j < nnz; ++j)
549  vals[j] *= scalingVector[i];
550  }
551 
552  } catch (...){
553  throw Exceptions::RuntimeError("Only Epetra_CrsMatrix types can be scaled");
554  }
555 #else
556  throw Exceptions::RuntimeError("Matrix scaling is not possible because Epetra has not been enabled.");
557 #endif // HAVE_MUELU_EPETRA
558  }
559 
565  static RCP<Matrix> Transpose(Matrix& Op, bool /* optimizeTranspose */ = false, const std::string & label = std::string(),const Teuchos::RCP<Teuchos::ParameterList> &params=Teuchos::null) {
566  switch (Op.getRowMap()->lib()) {
567  case Xpetra::UseTpetra:
568  {
569 #ifdef HAVE_MUELU_TPETRA
570 #ifdef HAVE_MUELU_TPETRA_INST_INT_INT
571  try {
572  const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& tpetraOp = Utilities::Op2TpetraCrs(Op);
573 
574  // Compute the transpose A of the Tpetra matrix tpetraOp.
575  RCP<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A;
576  Tpetra::RowMatrixTransposer<SC,LO,GO,NO> transposer(rcpFromRef(tpetraOp),label);
577  A = transposer.createTranspose(params);
578  RCP<Xpetra::TpetraCrsMatrix<SC,LO,GO,NO> > AA = rcp(new Xpetra::TpetraCrsMatrix<SC,LO,GO,NO>(A));
579  RCP<CrsMatrix> AAA = rcp_implicit_cast<CrsMatrix>(AA);
580  RCP<CrsMatrixWrap> AAAA = rcp(new CrsMatrixWrap(AAA));
581 
582  return AAAA;
583  }
584  catch (std::exception& e) {
585  std::cout << "threw exception '" << e.what() << "'" << std::endl;
586  throw Exceptions::RuntimeError("Utilities::Transpose failed, perhaps because matrix is not a Crs matrix");
587  }
588 #else
589  throw Exceptions::RuntimeError("Utilities::Transpose: Tpetra is not compiled with LO=GO=int. Add TPETRA_INST_INT_INT:BOOL=ON to your configuration!");
590 #endif
591 #else
592  throw Exceptions::RuntimeError("Utilities::Transpose: Tpetra is not compiled!");
593 #endif
594 #ifndef __NVCC__ //prevent nvcc warning
595  break;
596 #endif
597  }
598  case Xpetra::UseEpetra:
599  {
600 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
601  Teuchos::TimeMonitor tm(*Teuchos::TimeMonitor::getNewTimer("ZZ Entire Transpose"));
602  // Epetra case
605  Epetra_CrsMatrix * A = dynamic_cast<Epetra_CrsMatrix*>(&transposer(epetraOp));
606  transposer.ReleaseTranspose(); // So we can keep A in Muelu...
607 
608  RCP<Epetra_CrsMatrix> rcpA(A);
609  RCP<Xpetra::EpetraCrsMatrixT<GO,NO> > AA = rcp(new Xpetra::EpetraCrsMatrixT<GO,NO> (rcpA));
610  RCP<CrsMatrix> AAA = rcp_implicit_cast<CrsMatrix>(AA);
611  RCP<CrsMatrixWrap> AAAA = rcp( new CrsMatrixWrap(AAA));
612  AAAA->fillComplete(Op.getRangeMap(), Op.getDomainMap());
613 
614  return AAAA;
615 #else
616  throw Exceptions::RuntimeError("Epetra (Err. 2)");
617 #endif
618 #ifndef __NVCC__ //prevent nvcc warning
619  break;
620 #endif
621  }
622  default:
623  throw Exceptions::RuntimeError("Only Epetra and Tpetra matrices can be transposed.");
624 #ifndef __NVCC__ //prevent nvcc warning
625  break;
626 #endif
627  }
628 
629 #ifndef __NVCC__ //prevent nvcc warning
630  return Teuchos::null;
631 #endif
632  }
633 
636  static RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO> > ExtractCoordinatesFromParameterList(ParameterList& paramList) {
637  RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO> > coordinates = Teuchos::null;
638 
639  // check whether coordinates are contained in parameter list
640  if(paramList.isParameter ("Coordinates") == false)
641  return coordinates;
642 
643 #if defined(HAVE_MUELU_TPETRA)
644 #if ( defined(EPETRA_HAVE_OMP) && defined(HAVE_TPETRA_INST_OPENMP) && defined(HAVE_TPETRA_INST_INT_INT)) || \
645  (!defined(EPETRA_HAVE_OMP) && defined(HAVE_TPETRA_INST_SERIAL) && defined(HAVE_TPETRA_INST_INT_INT))
646 
647  // define Tpetra::MultiVector type with Scalar=float only if
648  // * ETI is turned off, since then the compiler will instantiate it automatically OR
649  // * Tpetra is instantiated on Scalar=float
650 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
651  typedef Tpetra::MultiVector<float, LO,GO,NO> tfMV;
652  RCP<tfMV> floatCoords = Teuchos::null;
653 #endif
654 
655  // define Tpetra::MultiVector type with Scalar=double only if
656  // * ETI is turned off, since then the compiler will instantiate it automatically OR
657  // * Tpetra is instantiated on Scalar=double
658  typedef Tpetra::MultiVector<SC,LO,GO,NO> tdMV;
659  RCP<tdMV> doubleCoords = Teuchos::null;
660  if (paramList.isType<RCP<tdMV> >("Coordinates")) {
661  // Coordinates are stored as a double vector
662  doubleCoords = paramList.get<RCP<tdMV> >("Coordinates");
663  paramList.remove("Coordinates");
664  }
665 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
666  else if (paramList.isType<RCP<tfMV> >("Coordinates")) {
667  // check if coordinates are stored as a float vector
668  floatCoords = paramList.get<RCP<tfMV> >("Coordinates");
669  paramList.remove("Coordinates");
670  doubleCoords = rcp(new tdMV(floatCoords->getMap(), floatCoords->getNumVectors()));
671  deep_copy(*doubleCoords, *floatCoords);
672  }
673 #endif
674  // We have the coordinates in a Tpetra double vector
675  if(doubleCoords != Teuchos::null) {
676  coordinates = Teuchos::rcp(new Xpetra::TpetraMultiVector<SC,LO,GO,NO>(doubleCoords));
677  TEUCHOS_TEST_FOR_EXCEPT(doubleCoords->getNumVectors() != coordinates->getNumVectors());
678  }
679 #endif // Tpetra instantiated on GO=int and EpetraNode
680 #endif // endif HAVE_TPETRA
681 
682 #if defined(HAVE_MUELU_EPETRA)
683  RCP<Epetra_MultiVector> doubleEpCoords;
684  if (paramList.isType<RCP<Epetra_MultiVector> >("Coordinates")) {
685  doubleEpCoords = paramList.get<RCP<Epetra_MultiVector> >("Coordinates");
686  paramList.remove("Coordinates");
687  RCP<Xpetra::EpetraMultiVectorT<GO,NO> > epCoordinates = Teuchos::rcp(new Xpetra::EpetraMultiVectorT<GO,NO>(doubleEpCoords));
688  coordinates = rcp_dynamic_cast<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO> >(epCoordinates);
689  TEUCHOS_TEST_FOR_EXCEPT(doubleEpCoords->NumVectors() != Teuchos::as<int>(coordinates->getNumVectors()));
690  }
691 #endif
693  return coordinates;
694  }
695  }; // class Utilities (specialization SC=double LO=GO=int)
696 
697 
698 
699  // Useful Kokkos conversions
700  template < class View, unsigned AppendValue >
701  struct AppendTrait {
702  // static_assert(false, "Error: NOT a Kokkos::View");
703  };
704 
705  template < class MT, unsigned T >
706  struct CombineMemoryTraits {
707  // empty
708  };
709 
710  template < unsigned U, unsigned T>
711  struct CombineMemoryTraits<Kokkos::MemoryTraits<U>, T> {
712  typedef Kokkos::MemoryTraits<U|T> type;
713  };
714 
715  template < class DataType, unsigned T, class... Pack >
716  struct AppendTrait< Kokkos::View< DataType, Pack... >, T> {
717  typedef Kokkos::View< DataType, Pack... > view_type;
719  };
720 
721 } //namespace MueLu
722 
723 #define MUELU_UTILITIES_KOKKOS_SHORT
724 
725 #endif // #if defined(HAVE_MUELU_KOKKOS_REFACTOR)
726 
727 #endif // MUELU_UTILITIES_KOKKOS_DECL_HPP
static void SetRandomSeed(const Teuchos::Comm< int > &comm)
static const RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > Map2TpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
static RCP< const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2TpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > const vec)
Helper utility to pull out the underlying Tpetra objects from an Xpetra object.
static RCP< const Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2TpetraCrs(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static const Epetra_Map & Map2EpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
static RCP< Epetra_CrsMatrix > Op2NonConstEpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static Scalar PowerMethod(const Matrix &A, bool scaleByDiag=true, LocalOrdinal niters=10, Magnitude tolerance=1e-2, bool verbose=false, unsigned int seed=123)
Simple transpose for Tpetra::CrsMatrix types.
GlobalOrdinal GO
static Teuchos::RCP< Vector > GetInverse(Teuchos::RCP< const Vector > v, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps()*100, Scalar tolReplacement=Teuchos::ScalarTraits< Scalar >::zero())
Return vector containing inverse of input vector.
static RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType, LocalOrdinal, GlobalOrdinal, Node > > ExtractCoordinatesFromParameterList(ParameterList &paramList)
bool is_null(const std::shared_ptr< T > &p)
static RCP< const Epetra_MultiVector > MV2EpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > const vec)
Helper utility to pull out the underlying Epetra objects from an Xpetra object.
void ZeroDirichletCols(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, const Kokkos::View< const bool *, typename Node::device_type > &dirichletCols, Scalar replaceWith)
void ZeroDirichletRows(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, const Kokkos::View< const bool *, typename Node::device_type > &dirichletRows, Scalar replaceWith)
LocalOrdinal LO
size_type size() const
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > vec)
static RCP< Time > getNewTimer(const std::string &name)
int ExtractMyRowView(int MyRow, int &NumEntries, double *&Values, int *&Indices) const
const Epetra_Map & RowMap() const
int NumMyElements() const
static RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &RHS)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static RCP< Epetra_MultiVector > MV2NonConstEpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > vec)
void deep_copy(const DynRankView< DT, DP...> &dst, typename ViewTraits< DT, DP...>::const_value_type &value, typename std::enable_if< std::is_same< typename ViewTraits< DT, DP...>::specialize, void >::value >::type *=0)
static RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV2(Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
static RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2TpetraRow(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static Teuchos::ArrayRCP< Scalar > GetLumpedMatrixDiagonal(const Matrix &A)
Extract Matrix Diagonal of lumped matrix.
static Teuchos::Array< Magnitude > ResidualNorm(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Crs2Op(RCP< Xpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static RCP< const Epetra_CrsMatrix > Op2EpetraCrs(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static RCP< MultiVector > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
static Teuchos::Array< Magnitude > ResidualNorm(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &RHS)
Scalar SC
static RCP< Teuchos::FancyOStream > MakeFancy(std::ostream &os)
Kokkos::View< const bool *, typename NO::device_type > DetectDirichletCols(const Xpetra::Matrix< SC, LO, GO, NO > &A, const Kokkos::View< const bool *, typename NO::device_type > &dirichletRows)
Node NO
static RCP< Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraRow(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static RCP< Vector > GetMatrixOverlappedDiagonal(const Matrix &A)
Extract Overlapped Matrix Diagonal.
static RCP< Teuchos::FancyOStream > MakeFancy(std::ostream &os)
static Scalar PowerMethod(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, bool scaleByDiag=true, LocalOrdinal niters=10, Magnitude tolerance=1e-2, bool verbose=false, unsigned int seed=123)
static RCP< Vector > GetMatrixDiagonalInverse(const Matrix &A, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps()*100)
Extract Matrix Diagonal.
static void SetRandomSeed(const Teuchos::Comm< int > &comm)
Set seed for random number generator.
Kokkos::View< const bool *, typename NO::device_type > DetectDirichletRows(const Xpetra::Matrix< SC, LO, GO, NO > &A, const typename Teuchos::ScalarTraits< SC >::magnitudeType &tol, const bool count_twos_as_dirichlet)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
static Teuchos::ArrayRCP< Scalar > GetMatrixDiagonal(const Matrix &A)
Extract Matrix Diagonal.