Ifpack2 Templated Preconditioning Package Version 1.0
Loading...
Searching...
No Matches
Ifpack2_Container_decl.hpp
Go to the documentation of this file.
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>
21#include <Teuchos_ParameterList.hpp>
22#include <iostream>
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25namespace Teuchos {
26// Forward declaration to avoid include.
27class ParameterList;
28} // namespace Teuchos
29#endif // DOXYGEN_SHOULD_SKIP_THIS
30
31namespace Ifpack2 {
32
78template <class MatrixType>
79class Container : public Teuchos::Describable {
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 using ISC = typename KokkosKernels::ArithTraits<SC>::val_type;
102
105 using HostView = typename mv_type::dual_view_type::t_host;
106 using ConstHostView = typename HostView::const_type;
107
108 public:
118 Container(const Teuchos::RCP<const row_matrix_type>& matrix,
119 const Teuchos::Array<Teuchos::Array<LO> >& partitions,
120 bool pointIndexed);
121
123 virtual ~Container();
124
140 Teuchos::ArrayView<const LO> getBlockRows(int blockIndex) const;
141
150 virtual void initialize() = 0;
151
153 void setBlockSizes(const Teuchos::Array<Teuchos::Array<LO> >& partitions);
154
155 void getMatDiag() const;
156
158 bool isInitialized() const;
159
161 bool isComputed() const;
162
171 virtual void compute() = 0;
172
173 void DoJacobi(ConstHostView X, HostView Y, SC dampingFactor) const;
174 void DoOverlappingJacobi(ConstHostView X, HostView Y, ConstHostView W, SC dampingFactor, bool nonsymScaling) const;
175 void DoGaussSeidel(ConstHostView X, HostView Y, HostView Y2, SC dampingFactor) const;
176 void DoSGS(ConstHostView X, HostView Y, HostView Y2, SC dampingFactor) const;
177
179 virtual void setParameters(const Teuchos::ParameterList& List) = 0;
180
193 virtual void
194 apply(ConstHostView X,
195 HostView Y,
196 int blockIndex,
197 Teuchos::ETransp mode = Teuchos::NO_TRANS,
198 SC alpha = Teuchos::ScalarTraits<SC>::one(),
199 SC beta = Teuchos::ScalarTraits<SC>::zero()) const = 0;
200
202 virtual void
203 weightedApply(ConstHostView X,
204 HostView Y,
205 ConstHostView D,
206 int blockIndex,
207 Teuchos::ETransp mode = Teuchos::NO_TRANS,
208 SC alpha = Teuchos::ScalarTraits<SC>::one(),
209 SC beta = Teuchos::ScalarTraits<SC>::zero()) const = 0;
210
213 // The underlying container implements the splitting <tt>A = D + R</tt>. Only
214 // it can have efficient access to D and R, as these are constructed in the
215 // symbolic and numeric phases.
216 //
217 // This is the first performance-portable implementation of a block
218 // relaxation, and it is supported currently only by BlockTriDiContainer.
219 virtual void applyInverseJacobi(const mv_type& /* X */, mv_type& /* Y */,
220 SC dampingFactor,
221 bool /* zeroStartingSolution = false */,
222 int /* numSweeps = 1 */) const = 0;
223
225 virtual void applyMV(const mv_type& X, mv_type& Y) const;
226
228 virtual void weightedApplyMV(const mv_type& X,
229 mv_type& Y,
230 vector_type& W) const;
231
232 virtual void clearBlocks();
233
235 virtual std::ostream& print(std::ostream& os) const = 0;
236
239 static std::string getName();
240
241 protected:
243 virtual void DoGSBlock(ConstHostView X, HostView Y, HostView Y2, HostView Resid,
244 SC dampingFactor, LO i) const;
245
247 Teuchos::RCP<const row_matrix_type> inputMatrix_;
248
250 Teuchos::RCP<const crs_matrix_type> inputCrsMatrix_;
251
253 Teuchos::RCP<const block_crs_matrix_type> inputBlockMatrix_;
254
258 Teuchos::Array<LO> blockRows_; // size: total # of local rows (in all local blocks)
260 Teuchos::Array<LO> blockSizes_; // size: # of blocks
262 Teuchos::Array<LO> blockOffsets_;
264 mutable Teuchos::RCP<vector_type> Diag_;
282
287
288 LO maxBlockSize_;
289};
290
291namespace Details {
292template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
293struct StridedRowView;
294}
295
304
305template <class MatrixType, class LocalScalarType>
306class ContainerImpl : public Container<MatrixType> {
308
309 protected:
310 using local_scalar_type = LocalScalarType;
311 using SC = typename Container<MatrixType>::scalar_type;
312 using LO = typename Container<MatrixType>::local_ordinal_type;
313 using GO = typename Container<MatrixType>::global_ordinal_type;
314 using NO = typename Container<MatrixType>::node_type;
316 using typename Container<MatrixType>::import_type;
317 using typename Container<MatrixType>::row_matrix_type;
318 using typename Container<MatrixType>::crs_matrix_type;
319 using typename Container<MatrixType>::block_crs_matrix_type;
320 using typename Container<MatrixType>::mv_type;
321 using typename Container<MatrixType>::vector_type;
322 using typename Container<MatrixType>::map_type;
323 using typename Container<MatrixType>::ISC;
325 using LSC = LocalScalarType;
326 using LISC = typename KokkosKernels::ArithTraits<LSC>::val_type;
327
328 using local_mv_type = Tpetra::MultiVector<LSC, LO, GO, NO>;
329
330 using typename Container<MatrixType>::HostView;
331 using typename Container<MatrixType>::ConstHostView;
332 using HostViewLocal = typename local_mv_type::dual_view_type::t_host;
333 using HostSubviewLocal = Kokkos::View<LISC**, Kokkos::LayoutStride, typename HostViewLocal::memory_space>;
334 using ConstHostSubviewLocal = Kokkos::View<const LISC**, Kokkos::LayoutStride, typename HostViewLocal::memory_space>;
335
336 static_assert(std::is_same<MatrixType, row_matrix_type>::value,
337 "Ifpack2::Container: Please use MatrixType = Tpetra::RowMatrix.");
339 //
340
347 LO translateRowToCol(LO row);
348
351
356 virtual void extract() = 0;
357
358 public:
359 ContainerImpl(const Teuchos::RCP<const row_matrix_type>& matrix,
360 const Teuchos::Array<Teuchos::Array<LO> >& partitions,
361 bool pointIndexed);
362
364 virtual ~ContainerImpl();
365
374 virtual void initialize() = 0;
375
384 virtual void compute() = 0;
385
387 virtual void setParameters(const Teuchos::ParameterList& List);
388
401 virtual void
402 apply(ConstHostView X,
403 HostView Y,
404 int blockIndex,
405 Teuchos::ETransp mode = Teuchos::NO_TRANS,
406 SC alpha = Teuchos::ScalarTraits<SC>::one(),
407 SC beta = Teuchos::ScalarTraits<SC>::zero()) const;
408
410 virtual void
411 weightedApply(ConstHostView X,
412 HostView Y,
413 ConstHostView D,
414 int blockIndex,
415 Teuchos::ETransp mode = Teuchos::NO_TRANS,
416 SC alpha = Teuchos::ScalarTraits<SC>::one(),
417 SC beta = Teuchos::ScalarTraits<SC>::zero()) const;
418
421 // The underlying container implements the splitting <tt>A = D + R</tt>. Only
422 // it can have efficient access to D and R, as these are constructed in the
423 // symbolic and numeric phases.
424 //
425 // This is the first performance-portable implementation of a block
426 // relaxation, and it is supported currently only by BlockTriDiContainer.
427 virtual void applyInverseJacobi(const mv_type& /* X */, mv_type& /* Y */,
428 SC dampingFactor,
429 bool /* zeroStartingSolution = false */,
430 int /* numSweeps = 1 */) const;
431
433 void applyMV(const mv_type& X, mv_type& Y) const;
434
436 void weightedApplyMV(const mv_type& X,
437 mv_type& Y,
438 vector_type& W) const;
439
440 virtual void clearBlocks();
441
443 virtual std::ostream& print(std::ostream& os) const = 0;
444
447 static std::string getName();
448
449 protected:
450 // Do Gauss-Seidel on only block i (this is used by DoGaussSeidel and DoSGS)
451 void DoGSBlock(ConstHostView X, HostView Y, HostView Y2, HostView Resid,
452 SC dampingFactor, LO i) const;
453
459 virtual void
460 solveBlock(ConstHostSubviewLocal X,
461 HostSubviewLocal Y,
462 int blockIndex,
463 Teuchos::ETransp mode,
464 const LSC alpha,
465 const LSC beta) const;
466
468 mutable HostViewLocal X_local_; // length: blockRows_.size()
469 mutable HostViewLocal Y_local_; // length: blockRows_.size()
470
481 mutable HostViewLocal weightedApplyScratch_;
482
484 mutable std::vector<HostSubviewLocal> X_localBlocks_;
485
487 mutable std::vector<HostSubviewLocal> Y_localBlocks_;
488};
489
490namespace Details {
496template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
498 using SC = Scalar;
499 using LO = LocalOrdinal;
500
501 using block_crs_matrix_type = Tpetra::BlockCrsMatrix<SC, LO, GlobalOrdinal, Node>;
502
503 using h_inds_type = typename block_crs_matrix_type::local_inds_host_view_type;
504 using h_vals_type = typename block_crs_matrix_type::values_host_view_type;
506 StridedRowView(h_vals_type vals_, h_inds_type inds_, int blockSize_, size_t nnz_);
507
509 // StridedRowView(const SC* vals_, const LO* inds_, int blockSize_, size_t nnz_);
510
512 StridedRowView(Teuchos::Array<SC>& vals_, Teuchos::Array<LO>& inds_);
513
514 SC val(size_t i) const;
515 LO ind(size_t i) const;
516
517 size_t size() const;
518
519 private:
520 h_vals_type vals;
521 h_inds_type inds;
522 int blockSize;
523 size_t nnz;
524 // These arrays are only used if the inputMatrix_ doesn't support row views.
525 Teuchos::Array<SC> valsCopy;
526 Teuchos::Array<LO> indsCopy;
527};
528} // namespace Details
529
530} // namespace Ifpack2
531
533template <class MatrixType>
534std::ostream& operator<<(std::ostream& os, const Ifpack2::Container<MatrixType>& obj);
535
536namespace Teuchos {
537
542template <class MatrixType>
543class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits< ::Ifpack2::Container<MatrixType> > {
544 public:
545 static std::string name() {
546 return std::string("Ifpack2::Container<") +
547 TypeNameTraits<MatrixType>::name() + ">";
548 }
549
550 static std::string concreteName(const ::Ifpack2::Container<MatrixType>&) {
551 return name();
552 }
553};
554
555} // namespace Teuchos
556
557#endif // IFPACK2_CONTAINER_HPP
std::ostream & operator<<(std::ostream &os, const Ifpack2::Container< MatrixType > &obj)
Print information about the given Container to the output stream os.
Definition Ifpack2_Container_def.hpp:918
Interface for creating and solving a set of local linear problems.
Definition Ifpack2_Container_decl.hpp:79
virtual ~Container()
Destructor.
Definition Ifpack2_Container_def.hpp:81
bool hasBlockCrs_
Whether the input matrix is a BlockCRS matrix.
Definition Ifpack2_Container_decl.hpp:274
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.
Teuchos::RCP< const crs_matrix_type > inputCrsMatrix_
The input matrix, dynamic cast to CrsMatrix. May be null.
Definition Ifpack2_Container_decl.hpp:250
bool IsParallel_
Whether the problem is distributed across multiple MPI processes.
Definition Ifpack2_Container_decl.hpp:266
int numBlocks_
The number of blocks (partitions) in the container.
Definition Ifpack2_Container_decl.hpp:256
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:85
static std::string getName()
Definition Ifpack2_Container_def.hpp:147
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).
Teuchos::RCP< vector_type > Diag_
Diagonal elements.
Definition Ifpack2_Container_decl.hpp:264
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.
int bcrsBlockSize_
If hasBlockCrs_, the number of DOFs per vertex. Otherwise 1.
Definition Ifpack2_Container_decl.hpp:276
Teuchos::RCP< const block_crs_matrix_type > inputBlockMatrix_
The input matrix, dynamic cast to BlockCrsMatrix. May be null.
Definition Ifpack2_Container_decl.hpp:253
virtual void setParameters(const Teuchos::ParameterList &List)=0
Set parameters, if any.
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:152
virtual void applyMV(const mv_type &X, mv_type &Y) const
Wrapper for apply with MultiVector.
Definition Ifpack2_Container_def.hpp:135
LO NumLocalRows_
Number of local rows in input matrix.
Definition Ifpack2_Container_decl.hpp:268
bool isInitialized() const
Whether the container has been successfully initialized.
Definition Ifpack2_Container_def.hpp:124
void setBlockSizes(const Teuchos::Array< Teuchos::Array< LO > > &partitions)
Initialize arrays with information about block sizes.
Definition Ifpack2_Container_def.hpp:90
LO scalarsPerRow_
Definition Ifpack2_Container_decl.hpp:281
Teuchos::Array< LO > blockSizes_
Number of rows in each block.
Definition Ifpack2_Container_decl.hpp:260
GO NumGlobalNonzeros_
Number of nonzeros in input matrix.
Definition Ifpack2_Container_decl.hpp:272
virtual void initialize()=0
Do all set-up operations that only require matrix structure.
virtual std::ostream & print(std::ostream &os) const =0
Print basic information about the container to os.
virtual void weightedApplyMV(const mv_type &X, mv_type &Y, vector_type &W) const
Wrapper for weightedApply with MultiVector.
Definition Ifpack2_Container_def.hpp:141
Teuchos::RCP< const row_matrix_type > inputMatrix_
The input matrix to the constructor.
Definition Ifpack2_Container_decl.hpp:247
bool pointIndexed_
(If hasBlockCrs_) Whether the blocks are described using sub-block row indices instead of full block ...
Definition Ifpack2_Container_decl.hpp:278
bool IsInitialized_
If true, the container has been successfully initialized.
Definition Ifpack2_Container_decl.hpp:284
typename KokkosKernels::ArithTraits< SC >::val_type ISC
Internal representation of Scalar in Kokkos::View.
Definition Ifpack2_Container_decl.hpp:101
Teuchos::Array< LO > blockRows_
Local indices of the rows of the input matrix that belong to this block.
Definition Ifpack2_Container_decl.hpp:258
GO NumGlobalRows_
Number of global rows in input matrix.
Definition Ifpack2_Container_decl.hpp:270
bool isComputed() const
Whether the container has been successfully computed.
Definition Ifpack2_Container_def.hpp:129
typename mv_type::dual_view_type::t_host HostView
Definition Ifpack2_Container_decl.hpp:105
Teuchos::Array< LO > blockOffsets_
Starting index in blockRows_ of local row indices for each block.
Definition Ifpack2_Container_decl.hpp:262
bool IsComputed_
If true, the container has been successfully computed.
Definition Ifpack2_Container_decl.hpp:286
virtual void compute()=0
Extract the local diagonal blocks and prepare the solver.
The implementation of the numerical features of Container (Jacobi, Gauss-Seidel, SGS)....
Definition Ifpack2_Container_decl.hpp:306
std::vector< HostSubviewLocal > X_localBlocks_
Views for holding pieces of X corresponding to each block.
Definition Ifpack2_Container_decl.hpp:484
HostViewLocal X_local_
Scratch vectors used in apply().
Definition Ifpack2_Container_decl.hpp:468
virtual std::ostream & print(std::ostream &os) const =0
Print basic information about the container to os.
virtual void setParameters(const Teuchos::ParameterList &List)
Set parameters, if any.
Definition Ifpack2_Container_def.hpp:444
Details::StridedRowView< SC, LO, GO, NO > getInputRowView(LO row) const
View a row of the input matrix.
Definition Ifpack2_Container_def.hpp:804
virtual void extract()=0
Extract the submatrices identified by the local indices set by the constructor. BlockMatrix may be an...
std::vector< HostSubviewLocal > Y_localBlocks_
Views for holding pieces of Y corresponding to each block.
Definition Ifpack2_Container_decl.hpp:487
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:649
virtual void compute()=0
Extract the local diagonal blocks and prepare the solver.
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:457
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:540
LocalScalarType LSC
The internal representation of LocalScalarType in Kokkos::View.
Definition Ifpack2_Container_decl.hpp:325
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:232
virtual void solveBlock(ConstHostSubviewLocal X, HostSubviewLocal Y, int blockIndex, Teuchos::ETransp mode, const LSC alpha, const LSC beta) const
Definition Ifpack2_Container_def.hpp:482
HostViewLocal weightedApplyScratch_
Definition Ifpack2_Container_decl.hpp:481
static std::string getName()
Definition Ifpack2_Container_def.hpp:476
LO translateRowToCol(LO row)
Definition Ifpack2_Container_def.hpp:494
virtual void initialize()=0
Do all set-up operations that only require matrix structure.
virtual ~ContainerImpl()
Destructor.
Definition Ifpack2_Container_def.hpp:440
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:465
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:448
Ifpack2's implementation of Trilinos::Details::LinearSolver interface.
Definition Ifpack2_Details_LinearSolver_decl.hpp:75
Ifpack2 implementation details.
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:40
Structure for read-only views of general matrix rows.
Definition Ifpack2_Container_decl.hpp:497