MueLu  Version of the Day
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>
55 #include <Teuchos_ParameterList.hpp>
56 
57 #include <Xpetra_BlockedCrsMatrix_fwd.hpp>
58 #include <Xpetra_CrsMatrix_fwd.hpp>
59 #include <Xpetra_CrsMatrixWrap_fwd.hpp>
60 #include <Xpetra_ExportFactory.hpp>
61 #include <Xpetra_ImportFactory_fwd.hpp>
62 #include <Xpetra_MapFactory_fwd.hpp>
63 #include <Xpetra_Map_fwd.hpp>
64 #include <Xpetra_MatrixFactory_fwd.hpp>
65 #include <Xpetra_Matrix_fwd.hpp>
66 #include <Xpetra_MultiVectorFactory_fwd.hpp>
67 #include <Xpetra_MultiVector_fwd.hpp>
68 #include <Xpetra_Operator_fwd.hpp>
69 #include <Xpetra_VectorFactory_fwd.hpp>
70 #include <Xpetra_Vector_fwd.hpp>
71 
72 #ifdef HAVE_MUELU_EPETRA
73 #include <Epetra_MultiVector.h>
74 #include <Epetra_CrsMatrix.h>
75 #include <Xpetra_EpetraCrsMatrix_fwd.hpp>
76 #include <Xpetra_EpetraMultiVector_fwd.hpp>
77 #endif
78 
79 #include "MueLu_Exceptions.hpp"
80 #include "MueLu_Utilities.hpp"
81 
82 #ifdef HAVE_MUELU_TPETRA
83 #include <Tpetra_CrsMatrix.hpp>
84 #include <Tpetra_Map.hpp>
85 #include <Tpetra_MultiVector.hpp>
86 #include <Xpetra_TpetraCrsMatrix_fwd.hpp>
87 #include <Xpetra_TpetraMultiVector_fwd.hpp>
88 #endif
89 
90 
91 namespace MueLu {
92 
100  template <class Scalar,
101  class LocalOrdinal = int,
102  class GlobalOrdinal = LocalOrdinal,
103  class Node = KokkosClassic::DefaultNode::DefaultNodeType>
104  class Utils_kokkos {
105 #undef MUELU_UTILITIES_KOKKOS_SHORT
106 #include "MueLu_UseShortNames.hpp"
107 
108  public:
109  typedef typename Teuchos::ScalarTraits<SC>::magnitudeType Magnitude;
110 
111 #ifdef HAVE_MUELU_EPETRA
112  // @{
114  static RCP<const Epetra_MultiVector> MV2EpetraMV(RCP<MultiVector> const Vec) { return Utils::MV2EpetraMV(Vec); }
115  static RCP< Epetra_MultiVector> MV2NonConstEpetraMV(RCP<MultiVector> Vec) { return Utils::MV2NonConstEpetraMV(Vec); }
116 
117  static const Epetra_MultiVector& MV2EpetraMV(const MultiVector& Vec) { return Utils::MV2EpetraMV(Vec); }
118  static Epetra_MultiVector& MV2NonConstEpetraMV(MultiVector& Vec) { return Utils::MV2NonConstEpetraMV(Vec); }
119 
120  static RCP<const Epetra_CrsMatrix> Op2EpetraCrs(RCP<const Matrix> Op) { return Utils::Op2EpetraCrs(Op); }
121  static RCP< Epetra_CrsMatrix> Op2NonConstEpetraCrs(RCP<Matrix> Op) { return Utils::Op2NonConstEpetraCrs(Op); }
122 
123  static const Epetra_CrsMatrix& Op2EpetraCrs(const Matrix& Op) { return Utils::Op2EpetraCrs(Op); }
124  static Epetra_CrsMatrix& Op2NonConstEpetraCrs(Matrix& Op) { return Utils::Op2NonConstEpetraCrs(Op); }
125 
126  static const Epetra_Map& Map2EpetraMap(const Map& map) { return Utils::Map2EpetraMap(map); }
127  // @}
128 #endif
129 
130 #ifdef HAVE_MUELU_TPETRA
131  // @{
133  static RCP<const Tpetra::MultiVector<SC,LO,GO,NO> > MV2TpetraMV(RCP<MultiVector> const Vec) { return Utils::MV2TpetraMV(Vec); }
134  static RCP< Tpetra::MultiVector<SC,LO,GO,NO> > MV2NonConstTpetraMV(RCP<MultiVector> Vec) { return Utils::MV2NonConstTpetraMV(Vec); }
135  static RCP< Tpetra::MultiVector<SC,LO,GO,NO> > MV2NonConstTpetraMV2(MultiVector& Vec) { return Utils::MV2NonConstTpetraMV2(Vec); }
136 
137  static const Tpetra::MultiVector<SC,LO,GO,NO>& MV2TpetraMV(const MultiVector& Vec) { return Utils::MV2TpetraMV(Vec); }
138  static Tpetra::MultiVector<SC,LO,GO,NO>& MV2NonConstTpetraMV(MultiVector& Vec) { return Utils::MV2NonConstTpetraMV(Vec); }
139 
140  static RCP<const Tpetra::CrsMatrix<SC,LO,GO,NO> > Op2TpetraCrs(RCP<const Matrix> Op) { return Utils::Op2TpetraCrs(Op); }
141  static RCP< Tpetra::CrsMatrix<SC,LO,GO,NO> > Op2NonConstTpetraCrs(RCP<Matrix> Op) { return Utils::Op2NonConstTpetraCrs(Op); }
142 
143  static const Tpetra::CrsMatrix<SC,LO,GO,NO>& Op2TpetraCrs(const Matrix& Op) { return Utils::Op2TpetraCrs(Op); }
144  static Tpetra::CrsMatrix<SC,LO,GO,NO>& Op2NonConstTpetraCrs(Matrix& Op) { return Utils::Op2NonConstTpetraCrs(Op); }
145 
146  static RCP<const Tpetra::RowMatrix<SC,LO,GO,NO> > Op2TpetraRow(RCP<const Matrix> Op) { return Utils::Op2TpetraRow(Op); }
147  static RCP< Tpetra::RowMatrix<SC,LO,GO,NO> > Op2NonConstTpetraRow(RCP<Matrix> Op) { return Utils::Op2NonConstTpetraRow(Op); }
148 
149  static const RCP<const Tpetra::Map<LO, GO, NO> > Map2TpetraMap(const Map& map) { return Utils::Map2TpetraMap(map); }
150 #endif
151 
152  static RCP<Xpetra::Matrix<SC,LO,GO,NO> > Crs2Op(RCP<CrsMatrix> Op) { return Utils::Crs2Op(Op); }
153 
160  static Teuchos::ArrayRCP<SC> GetMatrixDiagonal(const Matrix& A); // FIXME
161 
168  static RCP<Vector> GetMatrixDiagonalInverse(const Matrix& A, Magnitude tol = Teuchos::ScalarTraits<SC>::eps()*100); // FIXME
169 
170 
171 
178  static Teuchos::ArrayRCP<SC> GetLumpedMatrixDiagonal(const Matrix& A); // FIXME
179 
187  static RCP<Vector> GetMatrixOverlappedDiagonal(const Matrix& A); // FIXME
188 
198  static void ScaleMatrix(Matrix& Op, const Teuchos::ArrayRCP<SC>& scalingVector, bool doInverse = true); // FIXME
199 
200 
201  // TODO: should NOT return an Array. Definition must be changed to:
202  // - ArrayRCP<> ResidualNorm(Matrix const &Op, MultiVector const &X, MultiVector const &RHS)
203  // or
204  // - void ResidualNorm(Matrix const &Op, MultiVector const &X, MultiVector const &RHS, Array &)
205  static Teuchos::Array<Magnitude> ResidualNorm(const Operator& Op, const MultiVector& X, const MultiVector& RHS) {
206  return Utils::ResidualNorm(Op, X, RHS);
207  }
208 
209  static RCP<MultiVector> Residual(const Operator& Op, const MultiVector& X, const MultiVector& RHS) {
210  return Utils::Residual(Op, X, RHS);
211  }
212 
213  // NOTE:
214  // A better place for the Read/Write function is probably Xpetra
215 
217 
218 
219  static void Write(const std::string& fileName, const Map& M) { Utils::Write(fileName, M); }
220 
222  static void Write(const std::string& fileName, const MultiVector& Vec) { Utils::Write(fileName, Vec); }
223 
225  static void Write(const std::string& fileName, const Matrix& Op) { Utils::Write(fileName, Op); }
226 
228  static Teuchos::RCP<Matrix> Read(const std::string& fileName, Xpetra::UnderlyingLib lib, const RCP<const Teuchos::Comm<int> >& comm, bool binary = false) {
229  return Utils::Read(fileName, lib, comm, binary);
230  }
231 
236  static Teuchos::RCP<Matrix> Read(const std::string& fileName,
237  const RCP<const Map> rowMap,
238  RCP<const Map> colMap = Teuchos::null,
239  const RCP<const Map> domainMap = Teuchos::null,
240  const RCP<const Map> rangeMap = Teuchos::null,
241  const bool callFillComplete = true,
242  const bool binary = false,
243  const bool tolerant = false,
244  const bool debug = false)
245  { return Utils::Read(fileName, rowMap, colMap, domainMap, rangeMap, callFillComplete, binary, tolerant, debug); }
247 
248  static void PauseForDebugger();
249 
265  static SC PowerMethod(const Matrix& A, bool scaleByDiag = true,
266  LO niters = 10, Magnitude tolerance = 1e-2, bool verbose = false, unsigned int seed = 123) {
267  return Utils::PowerMethod(A, scaleByDiag, niters, tolerance, verbose, seed);
268  }
269 
270  static void MyOldScaleMatrix(Matrix& Op, const Teuchos::ArrayRCP<const SC>& scalingVector, bool doInverse = true,
271  bool doFillComplete = true, bool doOptimizeStorage = true); // FIXME
272 
273  static void MyOldScaleMatrix_Tpetra(Matrix& Op, const Teuchos::ArrayRCP<SC>& scalingVector,
274  bool doFillComplete, bool doOptimizeStorage); // FIXME
275 
276  static RCP<Teuchos::FancyOStream> MakeFancy(std::ostream& os) { return Utils::MakeFancy(os); }
277 
282  static typename Teuchos::ScalarTraits<Scalar>::magnitudeType Distance2(const MultiVector& v, LocalOrdinal i0, LocalOrdinal i1) { return Utils::Distance2(v, i0, i1); }
283 
291  static Teuchos::ArrayRCP<const bool> DetectDirichletRows(const Matrix& A, const Magnitude& tol = Teuchos::ScalarTraits<SC>::zero()); // FIXME
292 
302  static void SetRandomSeed(const Teuchos::Comm<int> &comm) { return Utils::SetRandomSeed(comm); }
303 
304  static void findDirichletRows(Teuchos::RCP<Matrix> A,
305  std::vector<LO>& dirichletRows); // FIXME
306  static void findDirichletCols(Teuchos::RCP<Matrix> A,
307  std::vector<LO>& dirichletRows,
308  std::vector<LO>& dirichletCols); // FIXME
309  static void Apply_BCsToMatrixRows(Teuchos::RCP<Matrix>& A,
310  std::vector<LO>& dirichletRows); // FIXME
311  static void Apply_BCsToMatrixCols(Teuchos::RCP<Matrix>& A,
312  std::vector<LO>& dirichletCols); // FIXME
313  static void Remove_Zeroed_Rows(Teuchos::RCP<Matrix>& A, double tol=1.0e-14); // FIXME
314 
315  }; // class Utils
316 
323  template <class Scalar,
324  class LocalOrdinal = int,
325  class GlobalOrdinal = LocalOrdinal,
326  class Node = KokkosClassic::DefaultNode::DefaultNodeType>
327  class Utils2_kokkos {
328 #include "MueLu_UseShortNames.hpp"
329 
330  public:
331 
337  static RCP<Matrix> Transpose(Matrix& Op, bool optimizeTranspose = false, const std::string & label = std::string()) {
338  return Utils2::Transpose(Op, optimizeTranspose, label);
339  }
340 
341  static RCP<MultiVector> ReadMultiVector (const std::string& fileName, const RCP<const Map>& map) {
342  return Utils2::ReadMultiVector(fileName, map);
343  }
344  static RCP<const Map> ReadMap (const std::string& fileName, Xpetra::UnderlyingLib lib, const RCP<const Teuchos::Comm<int> >& comm) {
345  return Utils2::ReadMap(fileName, lib, comm);
346  }
347 
348  };
349 
350 } //namespace MueLu
351 
352 #define MUELU_UTILITIES_KOKKOS_SHORT
353 
354 #endif // #if defined(HAVE_MUELU_KOKKOS_REFACTOR)
355 
356 #endif // MUELU_UTILITIES_KOKKOS_DECL_HPP
static void SetRandomSeed(const Teuchos::Comm< int > &comm)
Set seed for random number generator.
static RCP< Epetra_MultiVector > MV2NonConstEpetraMV(RCP< MultiVector > Vec)
static void Write(const std::string &fileName, const Map &M)
Read/Write methods.
static RCP< Matrix > Transpose(Matrix &Op, bool optimizeTranspose=false, const std::string &label=std::string())
Transpose a Xpetra::Matrix.
static const Epetra_Map & Map2EpetraMap(const Map &map)
static RCP< const Tpetra::MultiVector< SC, LO, GO, NO > > MV2TpetraMV(RCP< MultiVector > const Vec)
Helper utility to pull out the underlying Tpetra objects from an Xpetra object.
static Teuchos::Array< Magnitude > ResidualNorm(const Operator &Op, const MultiVector &X, const MultiVector &RHS)
static const RCP< const Tpetra::Map< LO, GO, NO > > Map2TpetraMap(const Map &map)
Namespace for MueLu classes and methods.
static RCP< Tpetra::MultiVector< SC, LO, GO, NO > > MV2NonConstTpetraMV(RCP< MultiVector > Vec)
static RCP< Xpetra::Matrix< SC, LO, GO, NO > > Crs2Op(RCP< CrsMatrix > Op)
static RCP< Tpetra::MultiVector< SC, LO, GO, NO > > MV2NonConstTpetraMV2(MultiVector &Vec)
static RCP< const Epetra_MultiVector > MV2EpetraMV(RCP< MultiVector > const Vec)
Helper utility to pull out the underlying Epetra objects from an Xpetra object.
static RCP< const Tpetra::RowMatrix< SC, LO, GO, NO > > Op2TpetraRow(RCP< const Matrix > Op)
static Teuchos::RCP< Matrix > Read(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm, bool binary=false)
Read matrix from file in Matrix Market or binary format.
static RCP< const Epetra_CrsMatrix > Op2EpetraCrs(RCP< const Matrix > Op)
static RCP< const Tpetra::CrsMatrix< SC, LO, GO, NO > > Op2TpetraCrs(RCP< const Matrix > Op)
static RCP< Teuchos::FancyOStream > MakeFancy(std::ostream &os)
static RCP< Tpetra::CrsMatrix< SC, LO, GO, NO > > Op2NonConstTpetraCrs(RCP< Matrix > Op)
static RCP< Tpetra::RowMatrix< SC, LO, GO, NO > > Op2NonConstTpetraRow(RCP< Matrix > Op)
static Teuchos::ScalarTraits< Scalar >::magnitudeType Distance2(const MultiVector &v, LocalOrdinal i0, LocalOrdinal i1)
Squared distance between two rows in a multivector.
static RCP< const Map > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
static RCP< MultiVector > ReadMultiVector(const std::string &fileName, const RCP< const Map > &map)
static Scalar PowerMethod(const Matrix &A, bool scaleByDiag=true, LO niters=10, Magnitude tolerance=1e-2, bool verbose=false, unsigned int seed=123)
Simple transpose for Tpetra::CrsMatrix types.
static RCP< Epetra_CrsMatrix > Op2NonConstEpetraCrs(RCP< Matrix > Op)
static RCP< MultiVector > Residual(const Operator &Op, const MultiVector &X, const MultiVector &RHS)