Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Container_decl.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2009 NTESS and the Ifpack2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef IFPACK2_CONTAINER_DECL_HPP
11 #define IFPACK2_CONTAINER_DECL_HPP
12 
15 
16 #include "Ifpack2_ConfigDefs.hpp"
17 #include "Tpetra_RowMatrix.hpp"
18 #include "Teuchos_Describable.hpp"
19 #include <Tpetra_Map.hpp>
20 #include <Tpetra_BlockCrsMatrix.hpp>
22 #include <iostream>
23 
24 #ifndef DOXYGEN_SHOULD_SKIP_THIS
25 namespace Teuchos {
26 // Forward declaration to avoid include.
27 class ParameterList;
28 } // namespace Teuchos
29 #endif // DOXYGEN_SHOULD_SKIP_THIS
30 
31 namespace Ifpack2 {
32 
78 template <class MatrixType>
80  protected:
81  using scalar_type = typename MatrixType::scalar_type;
82  using local_ordinal_type = typename MatrixType::local_ordinal_type;
83  using global_ordinal_type = typename MatrixType::global_ordinal_type;
84  using node_type = typename MatrixType::node_type;
85  using SC = scalar_type;
86  using LO = local_ordinal_type;
87  using GO = global_ordinal_type;
88  using NO = node_type;
89  using import_type = Tpetra::Import<LO, GO, NO>;
90  using mv_type = Tpetra::MultiVector<SC, LO, GO, NO>;
91  using vector_type = Tpetra::Vector<SC, LO, GO, NO>;
92  using map_type = Tpetra::Map<LO, GO, NO>;
93  using crs_matrix_type = Tpetra::CrsMatrix<SC, LO, GO, NO>;
94  using block_crs_matrix_type = Tpetra::BlockCrsMatrix<SC, LO, GO, NO>;
95  using row_matrix_type = Tpetra::RowMatrix<SC, LO, GO, NO>;
96 
97  static_assert(std::is_same<MatrixType, row_matrix_type>::value,
98  "Ifpack2::Container: Please use MatrixType = Tpetra::RowMatrix.");
99 
101 #if KOKKOS_VERSION >= 40799
102  using ISC = typename KokkosKernels::ArithTraits<SC>::val_type;
103 #else
104  using ISC = typename Kokkos::ArithTraits<SC>::val_type;
105 #endif
106 
109  using HostView = typename mv_type::dual_view_type::t_host;
110  using ConstHostView = typename HostView::const_type;
111 
112  public:
124  const Teuchos::Array<Teuchos::Array<LO> >& partitions,
125  bool pointIndexed);
126 
128  virtual ~Container();
129 
145  Teuchos::ArrayView<const LO> getBlockRows(int blockIndex) const;
146 
155  virtual void initialize() = 0;
156 
158  void setBlockSizes(const Teuchos::Array<Teuchos::Array<LO> >& partitions);
159 
160  void getMatDiag() const;
161 
163  bool isInitialized() const;
164 
166  bool isComputed() const;
167 
176  virtual void compute() = 0;
177 
178  void DoJacobi(ConstHostView X, HostView Y, SC dampingFactor) const;
179  void DoOverlappingJacobi(ConstHostView X, HostView Y, ConstHostView W, SC dampingFactor, bool nonsymScaling) const;
180  void DoGaussSeidel(ConstHostView X, HostView Y, HostView Y2, SC dampingFactor) const;
181  void DoSGS(ConstHostView X, HostView Y, HostView Y2, SC dampingFactor) const;
182 
184  virtual void setParameters(const Teuchos::ParameterList& List) = 0;
185 
198  virtual void
199  apply(ConstHostView X,
200  HostView Y,
201  int blockIndex,
203  SC alpha = Teuchos::ScalarTraits<SC>::one(),
204  SC beta = Teuchos::ScalarTraits<SC>::zero()) const = 0;
205 
207  virtual void
208  weightedApply(ConstHostView X,
209  HostView Y,
210  ConstHostView D,
211  int blockIndex,
213  SC alpha = Teuchos::ScalarTraits<SC>::one(),
214  SC beta = Teuchos::ScalarTraits<SC>::zero()) const = 0;
215 
218  // The underlying container implements the splitting <tt>A = D + R</tt>. Only
219  // it can have efficient access to D and R, as these are constructed in the
220  // symbolic and numeric phases.
221  //
222  // This is the first performance-portable implementation of a block
223  // relaxation, and it is supported currently only by BlockTriDiContainer.
224  virtual void applyInverseJacobi(const mv_type& /* X */, mv_type& /* Y */,
225  SC dampingFactor,
226  bool /* zeroStartingSolution = false */,
227  int /* numSweeps = 1 */) const = 0;
228 
230  virtual void applyMV(const mv_type& X, mv_type& Y) const;
231 
233  virtual void weightedApplyMV(const mv_type& X,
234  mv_type& Y,
235  vector_type& W) const;
236 
237  virtual void clearBlocks();
238 
240  virtual std::ostream& print(std::ostream& os) const = 0;
241 
244  static std::string getName();
245 
246  protected:
248  virtual void DoGSBlock(ConstHostView X, HostView Y, HostView Y2, HostView Resid,
249  SC dampingFactor, LO i) const;
250 
253 
256 
259 
263  Teuchos::Array<LO> blockRows_; // size: total # of local rows (in all local blocks)
265  Teuchos::Array<LO> blockSizes_; // size: # of blocks
287 
292 
293  LO maxBlockSize_;
294 };
295 
296 namespace Details {
297 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
299 }
300 
309 
310 template <class MatrixType, class LocalScalarType>
311 class ContainerImpl : public Container<MatrixType> {
313 
314  protected:
315  using local_scalar_type = LocalScalarType;
316  using SC = typename Container<MatrixType>::scalar_type;
317  using LO = typename Container<MatrixType>::local_ordinal_type;
318  using GO = typename Container<MatrixType>::global_ordinal_type;
319  using NO = typename Container<MatrixType>::node_type;
321  using typename Container<MatrixType>::import_type;
322  using typename Container<MatrixType>::row_matrix_type;
323  using typename Container<MatrixType>::crs_matrix_type;
324  using typename Container<MatrixType>::block_crs_matrix_type;
325  using typename Container<MatrixType>::mv_type;
326  using typename Container<MatrixType>::vector_type;
327  using typename Container<MatrixType>::map_type;
328  using typename Container<MatrixType>::ISC;
330  using LSC = LocalScalarType;
331 #if KOKKOS_VERSION >= 40799
332  using LISC = typename KokkosKernels::ArithTraits<LSC>::val_type;
333 #else
334  using LISC = typename Kokkos::ArithTraits<LSC>::val_type;
335 #endif
336 
337  using local_mv_type = Tpetra::MultiVector<LSC, LO, GO, NO>;
338 
339  using typename Container<MatrixType>::HostView;
340  using typename Container<MatrixType>::ConstHostView;
341  using HostViewLocal = typename local_mv_type::dual_view_type::t_host;
342  using HostSubviewLocal = Kokkos::View<LISC**, Kokkos::LayoutStride, typename HostViewLocal::memory_space>;
343  using ConstHostSubviewLocal = Kokkos::View<const LISC**, Kokkos::LayoutStride, typename HostViewLocal::memory_space>;
344 
345  static_assert(std::is_same<MatrixType, row_matrix_type>::value,
346  "Ifpack2::Container: Please use MatrixType = Tpetra::RowMatrix.");
348  //
349 
356  LO translateRowToCol(LO row);
357 
360 
365  virtual void extract() = 0;
366 
367  public:
369  const Teuchos::Array<Teuchos::Array<LO> >& partitions,
370  bool pointIndexed);
371 
373  virtual ~ContainerImpl();
374 
383  virtual void initialize() = 0;
384 
393  virtual void compute() = 0;
394 
396  virtual void setParameters(const Teuchos::ParameterList& List);
397 
410  virtual void
411  apply(ConstHostView X,
412  HostView Y,
413  int blockIndex,
415  SC alpha = Teuchos::ScalarTraits<SC>::one(),
416  SC beta = Teuchos::ScalarTraits<SC>::zero()) const;
417 
419  virtual void
420  weightedApply(ConstHostView X,
421  HostView Y,
422  ConstHostView D,
423  int blockIndex,
425  SC alpha = Teuchos::ScalarTraits<SC>::one(),
426  SC beta = Teuchos::ScalarTraits<SC>::zero()) const;
427 
430  // The underlying container implements the splitting <tt>A = D + R</tt>. Only
431  // it can have efficient access to D and R, as these are constructed in the
432  // symbolic and numeric phases.
433  //
434  // This is the first performance-portable implementation of a block
435  // relaxation, and it is supported currently only by BlockTriDiContainer.
436  virtual void applyInverseJacobi(const mv_type& /* X */, mv_type& /* Y */,
437  SC dampingFactor,
438  bool /* zeroStartingSolution = false */,
439  int /* numSweeps = 1 */) const;
440 
442  void applyMV(const mv_type& X, mv_type& Y) const;
443 
445  void weightedApplyMV(const mv_type& X,
446  mv_type& Y,
447  vector_type& W) const;
448 
449  virtual void clearBlocks();
450 
452  virtual std::ostream& print(std::ostream& os) const = 0;
453 
456  static std::string getName();
457 
458  protected:
459  // Do Gauss-Seidel on only block i (this is used by DoGaussSeidel and DoSGS)
460  void DoGSBlock(ConstHostView X, HostView Y, HostView Y2, HostView Resid,
461  SC dampingFactor, LO i) const;
462 
468  virtual void
469  solveBlock(ConstHostSubviewLocal X,
470  HostSubviewLocal Y,
471  int blockIndex,
472  Teuchos::ETransp mode,
473  const LSC alpha,
474  const LSC beta) const;
475 
477  mutable HostViewLocal X_local_; // length: blockRows_.size()
478  mutable HostViewLocal Y_local_; // length: blockRows_.size()
479 
490  mutable HostViewLocal weightedApplyScratch_;
491 
493  mutable std::vector<HostSubviewLocal> X_localBlocks_;
494 
496  mutable std::vector<HostSubviewLocal> Y_localBlocks_;
497 };
498 
499 namespace Details {
505 template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
506 struct StridedRowView {
507  using SC = Scalar;
508  using LO = LocalOrdinal;
509 
510  using block_crs_matrix_type = Tpetra::BlockCrsMatrix<SC, LO, GlobalOrdinal, Node>;
511 
512  using h_inds_type = typename block_crs_matrix_type::local_inds_host_view_type;
513  using h_vals_type = typename block_crs_matrix_type::values_host_view_type;
515  StridedRowView(h_vals_type vals_, h_inds_type inds_, int blockSize_, size_t nnz_);
516 
518  // StridedRowView(const SC* vals_, const LO* inds_, int blockSize_, size_t nnz_);
519 
522 
523  SC val(size_t i) const;
524  LO ind(size_t i) const;
525 
526  size_t size() const;
527 
528  private:
529  h_vals_type vals;
530  h_inds_type inds;
531  int blockSize;
532  size_t nnz;
533  // These arrays are only used if the inputMatrix_ doesn't support row views.
534  Teuchos::Array<SC> valsCopy;
535  Teuchos::Array<LO> indsCopy;
536 };
537 } // namespace Details
538 
539 } // namespace Ifpack2
540 
542 template <class MatrixType>
543 std::ostream& operator<<(std::ostream& os, const Ifpack2::Container<MatrixType>& obj);
544 
545 namespace Teuchos {
546 
551 template <class MatrixType>
552 class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits< ::Ifpack2::Container<MatrixType> > {
553  public:
554  static std::string name() {
555  return std::string("Ifpack2::Container<") +
557  }
558 
559  static std::string concreteName(const ::Ifpack2::Container<MatrixType>&) {
560  return name();
561  }
562 };
563 
564 } // namespace Teuchos
565 
566 #endif // IFPACK2_CONTAINER_HPP
virtual void applyInverseJacobi(const mv_type &, mv_type &, SC dampingFactor, bool, int) const =0
Compute Y := (1 - a) Y + a D^{-1} (X - R*Y).
virtual void apply(ConstHostView X, HostView Y, int blockIndex, Teuchos::ETransp mode=Teuchos::NO_TRANS, SC alpha=Teuchos::ScalarTraits< SC >::one(), SC beta=Teuchos::ScalarTraits< SC >::zero()) const
Compute Y := alpha * M^{-1} X + beta*Y.
Definition: Ifpack2_Container_def.hpp:539
virtual void initialize()=0
Do all set-up operations that only require matrix structure.
void weightedApplyMV(const mv_type &X, mv_type &Y, vector_type &W) const
Wrapper for weightedApply with MVs, used in unit tests (never called by BlockRelaxation) ...
Definition: Ifpack2_Container_def.hpp:464
bool isComputed() const
Whether the container has been successfully computed.
Definition: Ifpack2_Container_def.hpp:128
void DoGSBlock(ConstHostView X, HostView Y, HostView Y2, HostView Resid, SC dampingFactor, LO i) const
Do one step of Gauss-Seidel on block i (used by DoGaussSeidel and DoSGS)
Definition: Ifpack2_Container_def.hpp:231
std::vector< HostSubviewLocal > X_localBlocks_
Views for holding pieces of X corresponding to each block.
Definition: Ifpack2_Container_decl.hpp:493
Teuchos::Array< LO > blockRows_
Local indices of the rows of the input matrix that belong to this block.
Definition: Ifpack2_Container_decl.hpp:263
void setBlockSizes(const Teuchos::Array< Teuchos::Array< LO > > &partitions)
Initialize arrays with information about block sizes.
Definition: Ifpack2_Container_def.hpp:89
bool IsInitialized_
If true, the container has been successfully initialized.
Definition: Ifpack2_Container_decl.hpp:289
Teuchos::Array< LO > blockSizes_
Number of rows in each block.
Definition: Ifpack2_Container_decl.hpp:265
typename Kokkos::ArithTraits< SC >::val_type ISC
Internal representation of Scalar in Kokkos::View.
Definition: Ifpack2_Container_decl.hpp:104
StridedRowView(h_vals_type vals_, h_inds_type inds_, int blockSize_, size_t nnz_)
Constructor for row views (preferred)
Definition: Ifpack2_Container_def.hpp:857
std::vector< HostSubviewLocal > Y_localBlocks_
Views for holding pieces of Y corresponding to each block.
Definition: Ifpack2_Container_decl.hpp:496
virtual void applyMV(const mv_type &X, mv_type &Y) const
Wrapper for apply with MultiVector.
Definition: Ifpack2_Container_def.hpp:134
virtual void initialize()=0
Do all set-up operations that only require matrix structure.
bool pointIndexed_
(If hasBlockCrs_) Whether the blocks are described using sub-block row indices instead of full block ...
Definition: Ifpack2_Container_decl.hpp:283
int numBlocks_
The number of blocks (partitions) in the container.
Definition: Ifpack2_Container_decl.hpp:261
The implementation of the numerical features of Container (Jacobi, Gauss-Seidel, SGS). This class allows a custom scalar type (LocalScalarType) to be used for storing blocks and solving the block systems. Hiding this template parameter from the Container interface simplifies the BlockRelaxation and ContainerFactory classes.
Definition: Ifpack2_Container_decl.hpp:311
virtual void setParameters(const Teuchos::ParameterList &List)
Set parameters, if any.
Definition: Ifpack2_Container_def.hpp:443
bool isInitialized() const
Whether the container has been successfully initialized.
Definition: Ifpack2_Container_def.hpp:123
LO scalarsPerRow_
Definition: Ifpack2_Container_decl.hpp:286
virtual ~Container()
Destructor.
Definition: Ifpack2_Container_def.hpp:80
bool hasBlockCrs_
Whether the input matrix is a BlockCRS matrix.
Definition: Ifpack2_Container_decl.hpp:279
Details::StridedRowView< SC, LO, GO, NO > getInputRowView(LO row) const
View a row of the input matrix.
Definition: Ifpack2_Container_def.hpp:803
HostViewLocal weightedApplyScratch_
Definition: Ifpack2_Container_decl.hpp:490
virtual void compute()=0
Extract the local diagonal blocks and prepare the solver.
InverseType::scalar_type LSC
The internal representation of LocalScalarType in Kokkos::View.
Definition: Ifpack2_Container_decl.hpp:330
Container(const Teuchos::RCP< const row_matrix_type > &matrix, const Teuchos::Array< Teuchos::Array< LO > > &partitions, bool pointIndexed)
Constructor.
Definition: Ifpack2_Container_def.hpp:21
int bcrsBlockSize_
If hasBlockCrs_, the number of DOFs per vertex. Otherwise 1.
Definition: Ifpack2_Container_decl.hpp:281
virtual void apply(ConstHostView X, HostView Y, int blockIndex, Teuchos::ETransp mode=Teuchos::NO_TRANS, SC alpha=Teuchos::ScalarTraits< SC >::one(), SC beta=Teuchos::ScalarTraits< SC >::zero()) const =0
Compute Y := alpha * M^{-1} X + beta*Y.
LO translateRowToCol(LO row)
Definition: Ifpack2_Container_def.hpp:493
HostViewLocal X_local_
Scratch vectors used in apply().
Definition: Ifpack2_Container_decl.hpp:477
Teuchos::RCP< const block_crs_matrix_type > inputBlockMatrix_
The input matrix, dynamic cast to BlockCrsMatrix. May be null.
Definition: Ifpack2_Container_decl.hpp:258
Teuchos::RCP< const row_matrix_type > inputMatrix_
The input matrix to the constructor.
Definition: Ifpack2_Container_decl.hpp:252
virtual void weightedApplyMV(const mv_type &X, mv_type &Y, vector_type &W) const
Wrapper for weightedApply with MultiVector.
Definition: Ifpack2_Container_def.hpp:140
void applyMV(const mv_type &X, mv_type &Y) const
Wrapper for apply with MVs, used in unit tests (never called by BlockRelaxation)
Definition: Ifpack2_Container_def.hpp:456
virtual void applyInverseJacobi(const mv_type &, mv_type &, SC dampingFactor, bool, int) const
Compute Y := (1 - a) Y + a D^{-1} (X - R*Y).
Definition: Ifpack2_Container_def.hpp:447
typename mv_type::dual_view_type::t_host HostView
Definition: Ifpack2_Container_decl.hpp:109
virtual void weightedApply(ConstHostView X, HostView Y, ConstHostView D, int blockIndex, Teuchos::ETransp mode=Teuchos::NO_TRANS, SC alpha=Teuchos::ScalarTraits< SC >::one(), SC beta=Teuchos::ScalarTraits< SC >::zero()) const
Compute Y := alpha * diag(D) * M^{-1} (diag(D) * X) + beta*Y.
Definition: Ifpack2_Container_def.hpp:648
Teuchos::RCP< vector_type > Diag_
Diagonal elements.
Definition: Ifpack2_Container_decl.hpp:269
virtual ~ContainerImpl()
Destructor.
Definition: Ifpack2_Container_def.hpp:439
bool IsParallel_
Whether the problem is distributed across multiple MPI processes.
Definition: Ifpack2_Container_decl.hpp:271
static std::string getName()
Definition: Ifpack2_Container_def.hpp:475
bool IsComputed_
If true, the container has been successfully computed.
Definition: Ifpack2_Container_decl.hpp:291
virtual void weightedApply(ConstHostView X, HostView Y, ConstHostView D, int blockIndex, Teuchos::ETransp mode=Teuchos::NO_TRANS, SC alpha=Teuchos::ScalarTraits< SC >::one(), SC beta=Teuchos::ScalarTraits< SC >::zero()) const =0
Compute Y := alpha * diag(D) * M^{-1} (diag(D) * X) + beta*Y.
virtual void compute()=0
Extract the local diagonal blocks and prepare the solver.
Interface for creating and solving a set of local linear problems.
Definition: Ifpack2_Container_decl.hpp:79
Teuchos::ArrayView< const LO > getBlockRows(int blockIndex) const
Local indices of the rows of the input matrix that belong to this block.
Definition: Ifpack2_Container_def.hpp:84
virtual void extract()=0
Extract the submatrices identified by the local indices set by the constructor. BlockMatrix may be an...
virtual void setParameters(const Teuchos::ParameterList &List)=0
Set parameters, if any.
Structure for read-only views of general matrix rows.
Definition: Ifpack2_Container_decl.hpp:298
virtual std::ostream & print(std::ostream &os) const =0
Print basic information about the container to os.
static std::string getName()
Definition: Ifpack2_Container_def.hpp:146
GO NumGlobalRows_
Number of global rows in input matrix.
Definition: Ifpack2_Container_decl.hpp:275
GO NumGlobalNonzeros_
Number of nonzeros in input matrix.
Definition: Ifpack2_Container_decl.hpp:277
LO NumLocalRows_
Number of local rows in input matrix.
Definition: Ifpack2_Container_decl.hpp:273
Teuchos::Array< LO > blockOffsets_
Starting index in blockRows_ of local row indices for each block.
Definition: Ifpack2_Container_decl.hpp:267
virtual std::ostream & print(std::ostream &os) const =0
Print basic information about the container to os.
static std::string name()
virtual void solveBlock(ConstHostSubviewLocal X, HostSubviewLocal Y, int blockIndex, Teuchos::ETransp mode, const LSC alpha, const LSC beta) const
Definition: Ifpack2_Container_def.hpp:481
Teuchos::RCP< const crs_matrix_type > inputCrsMatrix_
The input matrix, dynamic cast to CrsMatrix. May be null.
Definition: Ifpack2_Container_decl.hpp:255
virtual void DoGSBlock(ConstHostView X, HostView Y, HostView Y2, HostView Resid, SC dampingFactor, LO i) const
Do one step of Gauss-Seidel on block i (used by DoGaussSeidel and DoSGS)
Definition: Ifpack2_Container_def.hpp:151