MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_UtilitiesBase_decl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef MUELU_UTILITIESBASE_DECL_HPP
11 #define MUELU_UTILITIESBASE_DECL_HPP
12 
13 #include <string>
14 
15 #include "MueLu_ConfigDefs.hpp"
16 
17 #include <Teuchos_DefaultComm.hpp>
18 #include <Teuchos_ScalarTraits.hpp>
20 
21 #include "Kokkos_ArithTraits.hpp"
22 
27 #include <Xpetra_CrsGraph_fwd.hpp>
28 #include <Xpetra_CrsMatrix_fwd.hpp>
30 #include <Xpetra_Import_fwd.hpp>
32 #include <Xpetra_Map_fwd.hpp>
34 #include <Xpetra_Matrix_fwd.hpp>
37 #include <Xpetra_Operator_fwd.hpp>
38 #include <Xpetra_Vector_fwd.hpp>
40 
41 namespace MueLu {
42 
43 // MPI helpers
44 #define MueLu_sumAll(rcpComm, in, out) \
45  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_SUM, in, Teuchos::outArg(out))
46 #define MueLu_minAll(rcpComm, in, out) \
47  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MIN, in, Teuchos::outArg(out))
48 #define MueLu_maxAll(rcpComm, in, out) \
49  Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_MAX, in, Teuchos::outArg(out))
50 
58 template <class Scalar,
61  class Node = DefaultNode>
62 class UtilitiesBase {
63  public:
64 #undef MUELU_UTILITIESBASE_SHORT
65 #include "MueLu_UseShortNames.hpp"
66  public:
68 
70 
77  static RCP<CrsMatrixWrap> GetThresholdedMatrix(const RCP<Matrix>& Ain, const Magnitude threshold, const bool keepDiagonal = true, const GlobalOrdinal expectedNNZperRow = -1);
78 
85  static RCP<Xpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node>> GetThresholdedGraph(const RCP<Matrix>& A, const Magnitude threshold, const GlobalOrdinal expectedNNZperRow = -1);
86 
93  static Teuchos::ArrayRCP<Scalar> GetMatrixDiagonal_arcp(const Matrix& A);
94 
101  static RCP<Vector> GetMatrixDiagonal(const Matrix& A);
102 
109  // static RCP<Vector> GetMatrixDiagonalInverse(const Matrix& A, Magnitude tol = Teuchos::ScalarTraits<Scalar>::eps()*100, Scalar valReplacement = Teuchos::ScalarTraits<Scalar>::zero());
110  static RCP<Vector> GetMatrixDiagonalInverse(const Matrix& A, Magnitude tol = Teuchos::ScalarTraits<Scalar>::eps() * 100, Scalar valReplacement = Teuchos::ScalarTraits<Scalar>::zero(), const bool doLumped = false);
111 
118  static Teuchos::RCP<Vector> GetLumpedMatrixDiagonal(Matrix const& A, const bool doReciprocal = false,
120  Scalar valReplacement = Teuchos::ScalarTraits<Scalar>::zero(),
121  const bool replaceSingleEntryRowWithZero = false,
122  const bool useAverageAbsDiagVal = false);
123 
131 
133 
142 
150  static RCP<Vector> GetMatrixOverlappedDiagonal(const Matrix& A);
151 
160  static RCP<Vector> GetMatrixOverlappedDeletedRowsum(const Matrix& A);
161 
163  GetMatrixOverlappedAbsDeletedRowsum(const Matrix& A);
164 
165  // TODO: should NOT return an Array. Definition must be changed to:
166  // - ArrayRCP<> ResidualNorm(Matrix const &Op, MultiVector const &X, MultiVector const &RHS)
167  // or
168  // - void ResidualNorm(Matrix const &Op, MultiVector const &X, MultiVector const &RHS, Array &)
169  static Teuchos::Array<Magnitude> ResidualNorm(const Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Op, const MultiVector& X, const MultiVector& RHS);
170 
171  static Teuchos::Array<Magnitude> ResidualNorm(const Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Op, const MultiVector& X, const MultiVector& RHS, MultiVector& Resid);
172 
173  static RCP<MultiVector> Residual(const Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Op, const MultiVector& X, const MultiVector& RHS);
174 
175  static void Residual(const Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Op, const MultiVector& X, const MultiVector& RHS, MultiVector& Resid);
176 
188  static Scalar PowerMethod(const Matrix& A, bool scaleByDiag = true,
189  LocalOrdinal niters = 10, Magnitude tolerance = 1e-2, bool verbose = false, unsigned int seed = 123);
190 
202  static Scalar PowerMethod(const Matrix& A, const RCP<Vector>& diagInvVec,
203  LocalOrdinal niters = 10, Magnitude tolerance = 1e-2, bool verbose = false, unsigned int seed = 123);
204 
205  static RCP<Teuchos::FancyOStream> MakeFancy(std::ostream& os);
206 
212 
218 
232 
240  static Kokkos::View<bool*, typename NO::device_type::memory_space> DetectDirichletRows_kokkos(const Matrix& A, const Magnitude& tol = Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<SC>::magnitudeType>::zero(), const bool count_twos_as_dirichlet = false);
241  static Kokkos::View<bool*, typename Kokkos::HostSpace> DetectDirichletRows_kokkos_host(const Matrix& A, const Magnitude& tol = Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<SC>::magnitudeType>::zero(), const bool count_twos_as_dirichlet = false);
242 
256 
264  static void FindNonZeros(const Teuchos::ArrayRCP<const Scalar> vals,
265  Teuchos::ArrayRCP<bool> nonzeros);
266 
274  Kokkos::View<bool*, typename Node::device_type> nonzeros);
275 
285  const Teuchos::ArrayRCP<bool>& dirichletRows,
286  Teuchos::ArrayRCP<bool> dirichletCols,
287  Teuchos::ArrayRCP<bool> dirichletDomain);
288 
297  const Kokkos::View<bool*, typename Node::device_type>& dirichletRows,
298  Kokkos::View<bool*, typename Node::device_type> dirichletCols,
299  Kokkos::View<bool*, typename Node::device_type> dirichletDomain);
300 
313 
315 
316  static void ApplyRowSumCriterion(const Matrix& A,
317  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
318  Kokkos::View<bool*, typename NO::device_type::memory_space>& dirichletRows);
319 
320  static void ApplyRowSumCriterionHost(const Matrix& A,
321  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
322  Kokkos::View<bool*, Kokkos::HostSpace>& dirichletRows);
323 
324  static void ApplyRowSumCriterion(const Matrix& A,
326  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
327  Kokkos::View<bool*, typename NO::device_type::memory_space>& dirichletRows);
328 
329  static void ApplyRowSumCriterionHost(const Matrix& A,
331  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
332  Kokkos::View<bool*, Kokkos::HostSpace>& dirichletRows);
333 
345  const Teuchos::ArrayRCP<const bool>& dirichletRows);
346 
357  static Kokkos::View<bool*, typename NO::device_type> DetectDirichletCols(const Matrix& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows);
358 
364 
374  static void SetRandomSeed(const Teuchos::Comm<int>& comm);
375 
376  // Finds the OAZ Dirichlet rows for this matrix
377  // so far only used in IntrepidPCoarsenFactory
378  // TODO check whether we can use DetectDirichletRows instead
380  std::vector<LocalOrdinal>& dirichletRows, bool count_twos_as_dirichlet = false);
381 
382  // Applies Ones-and-Zeros to matrix rows
383  // Takes a vector of row indices
385  const std::vector<LocalOrdinal>& dirichletRows);
386 
387  // Applies Ones-and-Zeros to matrix rows
388  // Takes a Boolean array.
390  const Teuchos::ArrayRCP<const bool>& dirichletRows);
391 
392  static void ApplyOAZToMatrixRows(RCP<Matrix>& A, const Kokkos::View<const bool*, typename Node::device_type>& dirichletRows);
393 
394  // Zeros out rows
395  // Takes a vector containg Dirichlet row indices
397  const std::vector<LocalOrdinal>& dirichletRows,
399 
400  // Zeros out rows
401  // Takes a Boolean ArrayRCP
403  const Teuchos::ArrayRCP<const bool>& dirichletRows,
405 
406  // Zeros out rows
407  // Takes a Boolean ArrayRCP
409  const Teuchos::ArrayRCP<const bool>& dirichletRows,
411 
412  static void ZeroDirichletRows(RCP<Matrix>& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows, SC replaceWith = Teuchos::ScalarTraits<SC>::zero());
413 
414  static void ZeroDirichletRows(RCP<MultiVector>& X, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows, SC replaceWith = Teuchos::ScalarTraits<SC>::zero());
415 
416  // Zeros out columns
417  // Takes a Boolean vector
419  const Teuchos::ArrayRCP<const bool>& dirichletCols,
421 
422  static void ZeroDirichletCols(RCP<Matrix>& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletCols, SC replaceWith = Teuchos::ScalarTraits<SC>::zero());
423 
424  // Finds the OAZ Dirichlet rows for this matrix
428 
430  // You can use this to de-normalize a tenative prolongator, for instance
432 
433  // This routine takes a BlockedMap and an Importer (assuming that the BlockedMap matches the source of the importer) and generates a BlockedMap corresponding
434  // to the Importer's target map. We assume that the targetMap is unique (which, is not a strict requirement of an Importer, but is here and no, we don't check)
435  // This is largely intended to be used in repartitioning of blocked matrices
438 
439  // Checks to see if the first chunk of the colMap is also the row map. This simiplifies a bunch of
440  // operation in coarsening
442 
447 
451 
452 }; // class UtilitiesBase
453 
454 // Useful Kokkos conversions
455 template <class View, unsigned AppendValue>
456 struct AppendTrait {
457  // static_assert(false, "Error: NOT a Kokkos::View");
458 };
459 
460 template <class MT, unsigned T>
462  // empty
463 };
464 
465 template <unsigned U, unsigned T>
466 struct CombineMemoryTraits<Kokkos::MemoryTraits<U>, T> {
467  typedef Kokkos::MemoryTraits<U | T> type;
468 };
469 
470 template <class DataType, unsigned T, class... Pack>
471 struct AppendTrait<Kokkos::View<DataType, Pack...>, T> {
472  typedef Kokkos::View<DataType, Pack...> view_type;
473  using type = Kokkos::View<DataType, typename view_type::array_layout, typename view_type::device_type, typename CombineMemoryTraits<typename view_type::memory_traits, T>::type>;
474 };
475 
477 
478 } // namespace MueLu
479 
480 #define MUELU_UTILITIESBASE_SHORT
481 #endif // MUELU_UTILITIESBASE_DECL_HPP
static void FindNonZeros(const Teuchos::ArrayRCP< const Scalar > vals, Teuchos::ArrayRCP< bool > nonzeros)
Find non-zero values in an ArrayRCP Compares the value to 2 * machine epsilon.
static RCP< Vector > GetMatrixDiagonal(const Matrix &A)
Extract Matrix Diagonal.
static void ApplyOAZToMatrixRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &A, const std::vector< LocalOrdinal > &dirichletRows)
static void ZeroDirichletCols(Teuchos::RCP< Matrix > &A, const Teuchos::ArrayRCP< const bool > &dirichletCols, Scalar replaceWith=Teuchos::ScalarTraits< Scalar >::zero())
static void DetectDirichletColsAndDomains(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Teuchos::ArrayRCP< bool > &dirichletRows, Teuchos::ArrayRCP< bool > dirichletCols, Teuchos::ArrayRCP< bool > dirichletDomain)
Detects Dirichlet columns &amp; domains from a list of Dirichlet rows.
MueLu::DefaultLocalOrdinal LocalOrdinal
static bool MapsAreNested(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &rowMap, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &colMap)
static RCP< Xpetra::Vector< LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node > > ReverseCuthillMcKee(const Matrix &Op)
static void FindDirichletRowsAndPropagateToCols(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &A, Teuchos::RCP< Xpetra::Vector< int, LocalOrdinal, GlobalOrdinal, Node >> &isDirichletRow, Teuchos::RCP< Xpetra::Vector< int, LocalOrdinal, GlobalOrdinal, Node >> &isDirichletCol)
static void ApplyRowSumCriterionHost(const Matrix &A, const typename Teuchos::ScalarTraits< Scalar >::magnitudeType rowSumTol, Kokkos::View< bool *, Kokkos::HostSpace > &dirichletRows)
Tpetra::KokkosClassic::DefaultNode::DefaultNodeType DefaultNode
static Scalar PowerMethod(const Matrix &A, bool scaleByDiag=true, LocalOrdinal niters=10, Magnitude tolerance=1e-2, bool verbose=false, unsigned int seed=123)
Power method.
static void ApplyRowSumCriterion(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Magnitude rowSumTol, Teuchos::ArrayRCP< bool > &dirichletRows)
Apply Rowsum Criterion.
static RCP< Vector > GetMatrixOverlappedDeletedRowsum(const Matrix &A)
Extract Overlapped Matrix Deleted Rowsum.
static RCP< Xpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > GetThresholdedGraph(const RCP< Matrix > &A, const Magnitude threshold, const GlobalOrdinal expectedNNZperRow=-1)
Threshold a graph.
static void ZeroDirichletRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &A, const std::vector< LocalOrdinal > &dirichletRows, Scalar replaceWith=Teuchos::ScalarTraits< Scalar >::zero())
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReplaceNonZerosWithOnes(const RCP< Matrix > &original)
Creates a copy of a matrix where the non-zero entries are replaced by ones.
static void SetRandomSeed(const Teuchos::Comm< int > &comm)
Set seed for random number generator.
MueLu::DefaultNode Node
static RCP< Xpetra::Vector< LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node > > CuthillMcKee(const Matrix &Op)
static RCP< MultiVector > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
static Teuchos::ArrayRCP< const bool > DetectDirichletCols(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Teuchos::ArrayRCP< const bool > &dirichletRows)
Detect Dirichlet columns based on Dirichlet rows.
static void FindDirichletRows(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &A, std::vector< LocalOrdinal > &dirichletRows, bool count_twos_as_dirichlet=false)
Kokkos::View< DataType, typename view_type::array_layout, typename view_type::device_type, typename CombineMemoryTraits< typename view_type::memory_traits, T >::type > type
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
static RCP< CrsMatrixWrap > GetThresholdedMatrix(const RCP< Matrix > &Ain, const Magnitude threshold, const bool keepDiagonal=true, const GlobalOrdinal expectedNNZperRow=-1)
Threshold a matrix.
static Teuchos::ArrayRCP< const bool > DetectDirichletRows(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Magnitude &tol=Teuchos::ScalarTraits< Magnitude >::zero(), bool count_twos_as_dirichlet=false)
Detect Dirichlet rows.
static RCP< Xpetra::Vector< Magnitude, LocalOrdinal, GlobalOrdinal, Node > > GetMatrixOverlappedAbsDeletedRowsum(const Matrix &A)
static Teuchos::ArrayRCP< Scalar > GetMatrixDiagonal_arcp(const Matrix &A)
Extract Matrix Diagonal.
static RCP< const Xpetra::BlockedMap< LocalOrdinal, GlobalOrdinal, Node > > GeneratedBlockedTargetMap(const Xpetra::BlockedMap< LocalOrdinal, GlobalOrdinal, Node > &sourceBlockedMap, const Xpetra::Import< LocalOrdinal, GlobalOrdinal, Node > &Importer)
static RCP< Vector > GetMatrixOverlappedDiagonal(const Matrix &A)
Extract Overlapped Matrix Diagonal.
Teuchos::ScalarTraits< Scalar >::magnitudeType Magnitude
static Teuchos::RCP< Xpetra::Vector< Magnitude, LocalOrdinal, GlobalOrdinal, Node > > GetMatrixMaxMinusOffDiagonal(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A)
Return vector containing: max_{i=k}(-a_ik), for each for i in the matrix.
static RCP< Vector > GetMatrixDiagonalInverse(const Matrix &A, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps()*100, Scalar valReplacement=Teuchos::ScalarTraits< Scalar >::zero(), const bool doLumped=false)
Extract Matrix Diagonal.
static Teuchos::ArrayRCP< const bool > DetectDirichletRowsExt(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, bool &bHasZeroDiagonal, const Magnitude &tol=Teuchos::ScalarTraits< Scalar >::zero())
Detect Dirichlet rows (extended version)
static Teuchos::Array< Magnitude > ResidualNorm(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
Scalar SC
static RCP< Teuchos::FancyOStream > MakeFancy(std::ostream &os)
static Scalar Frobenius(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B)
Frobenius inner product of two matrices.
static Teuchos::RCP< Vector > GetInverse(Teuchos::RCP< const Vector > v, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps()*100, Scalar valReplacement=Teuchos::ScalarTraits< Scalar >::zero())
Return vector containing inverse of input vector.
static Kokkos::View< bool *, typename Kokkos::HostSpace > DetectDirichletRows_kokkos_host(const Matrix &A, const Magnitude &tol=Teuchos::ScalarTraits< typename Teuchos::ScalarTraits< SC >::magnitudeType >::zero(), const bool count_twos_as_dirichlet=false)
static Teuchos::ScalarTraits< Scalar >::magnitudeType Distance2(const Teuchos::Array< Teuchos::ArrayRCP< const Scalar >> &v, LocalOrdinal i0, LocalOrdinal i1)
Squared distance between two rows in a multivector.
static Teuchos::RCP< Vector > GetLumpedMatrixDiagonal(Matrix const &A, const bool doReciprocal=false, Magnitude tol=Teuchos::ScalarTraits< Scalar >::magnitude(Teuchos::ScalarTraits< Scalar >::zero()), Scalar valReplacement=Teuchos::ScalarTraits< Scalar >::zero(), const bool replaceSingleEntryRowWithZero=false, const bool useAverageAbsDiagVal=false)
Extract Matrix Diagonal of lumped matrix.
static Kokkos::View< bool *, typename NO::device_type::memory_space > DetectDirichletRows_kokkos(const Matrix &A, const Magnitude &tol=Teuchos::ScalarTraits< typename Teuchos::ScalarTraits< SC >::magnitudeType >::zero(), const bool count_twos_as_dirichlet=false)
Detect Dirichlet rows.
static RCP< Matrix > Crs2Op(RCP< CrsMatrix > Op)