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 
263 
271  static void FindNonZeros(const Teuchos::ArrayRCP<const Scalar> vals,
272  Teuchos::ArrayRCP<bool> nonzeros);
273 
281  Kokkos::View<bool*, typename Node::device_type> nonzeros);
282 
292  const Teuchos::ArrayRCP<bool>& dirichletRows,
293  Teuchos::ArrayRCP<bool> dirichletCols,
294  Teuchos::ArrayRCP<bool> dirichletDomain);
295 
304  const Kokkos::View<bool*, typename Node::device_type>& dirichletRows,
305  Kokkos::View<bool*, typename Node::device_type> dirichletCols,
306  Kokkos::View<bool*, typename Node::device_type> dirichletDomain);
307 
320 
322 
323  static void ApplyRowSumCriterion(const Matrix& A,
324  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
325  Kokkos::View<bool*, typename NO::device_type::memory_space>& dirichletRows);
326 
327  static void ApplyRowSumCriterionHost(const Matrix& A,
328  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
329  Kokkos::View<bool*, Kokkos::HostSpace>& dirichletRows);
330 
331  static void ApplyRowSumCriterion(const Matrix& A,
333  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
334  Kokkos::View<bool*, typename NO::device_type::memory_space>& dirichletRows);
335 
336  static void ApplyRowSumCriterionHost(const Matrix& A,
338  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType rowSumTol,
339  Kokkos::View<bool*, Kokkos::HostSpace>& dirichletRows);
340 
352  const Teuchos::ArrayRCP<const bool>& dirichletRows);
353 
364  static Kokkos::View<bool*, typename NO::device_type> DetectDirichletCols(const Matrix& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows);
365 
371 
381  static void SetRandomSeed(const Teuchos::Comm<int>& comm);
382 
383  // Finds the OAZ Dirichlet rows for this matrix
384  // so far only used in IntrepidPCoarsenFactory
385  // TODO check whether we can use DetectDirichletRows instead
387  std::vector<LocalOrdinal>& dirichletRows, bool count_twos_as_dirichlet = false);
388 
389  // Applies Ones-and-Zeros to matrix rows
390  // Takes a vector of row indices
392  const std::vector<LocalOrdinal>& dirichletRows);
393 
394  // Applies Ones-and-Zeros to matrix rows
395  // Takes a Boolean array.
397  const Teuchos::ArrayRCP<const bool>& dirichletRows);
398 
399  static void ApplyOAZToMatrixRows(RCP<Matrix>& A, const Kokkos::View<const bool*, typename Node::device_type>& dirichletRows);
400 
401  // Zeros out rows
402  // Takes a vector containg Dirichlet row indices
404  const std::vector<LocalOrdinal>& dirichletRows,
406 
407  // Zeros out rows
408  // Takes a Boolean ArrayRCP
410  const Teuchos::ArrayRCP<const bool>& dirichletRows,
412 
413  // Zeros out rows
414  // Takes a Boolean ArrayRCP
416  const Teuchos::ArrayRCP<const bool>& dirichletRows,
418 
419  static void ZeroDirichletRows(RCP<Matrix>& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows, SC replaceWith = Teuchos::ScalarTraits<SC>::zero());
420 
421  static void ZeroDirichletRows(RCP<MultiVector>& X, const Kokkos::View<const bool*, typename NO::device_type>& dirichletRows, SC replaceWith = Teuchos::ScalarTraits<SC>::zero());
422 
423  // Zeros out columns
424  // Takes a Boolean vector
426  const Teuchos::ArrayRCP<const bool>& dirichletCols,
428 
429  static void ZeroDirichletCols(RCP<Matrix>& A, const Kokkos::View<const bool*, typename NO::device_type>& dirichletCols, SC replaceWith = Teuchos::ScalarTraits<SC>::zero());
430 
431  // Finds the OAZ Dirichlet rows for this matrix
435 
437  // You can use this to de-normalize a tenative prolongator, for instance
439 
440  // This routine takes a BlockedMap and an Importer (assuming that the BlockedMap matches the source of the importer) and generates a BlockedMap corresponding
441  // 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)
442  // This is largely intended to be used in repartitioning of blocked matrices
445 
446  // Checks to see if the first chunk of the colMap is also the row map. This simiplifies a bunch of
447  // operation in coarsening
449 
454 
458 
459 }; // class UtilitiesBase
460 
461 // Useful Kokkos conversions
462 template <class View, unsigned AppendValue>
463 struct AppendTrait {
464  // static_assert(false, "Error: NOT a Kokkos::View");
465 };
466 
467 template <class MT, unsigned T>
469  // empty
470 };
471 
472 template <unsigned U, unsigned T>
473 struct CombineMemoryTraits<Kokkos::MemoryTraits<U>, T> {
474  typedef Kokkos::MemoryTraits<U | T> type;
475 };
476 
477 template <class DataType, unsigned T, class... Pack>
478 struct AppendTrait<Kokkos::View<DataType, Pack...>, T> {
479  typedef Kokkos::View<DataType, Pack...> view_type;
480  using type = Kokkos::View<DataType, typename view_type::array_layout, typename view_type::device_type, typename CombineMemoryTraits<typename view_type::memory_traits, T>::type>;
481 };
482 
484 
485 } // namespace MueLu
486 
487 #define MUELU_UTILITIESBASE_SHORT
488 #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 Teuchos::RCP< Vector > 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 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 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)
static void EnforceInitialCondition(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &RHS, Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &InitialGuess, const Magnitude &tol=Teuchos::ScalarTraits< Magnitude >::zero(), const bool count_twos_as_dirichlet=false)
Detect Dirichlet rows and copy values from RHS multivector to InitialGuess for Dirichlet rows...
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)