Ifpack2 Templated Preconditioning Package Version 1.0
Loading...
Searching...
No Matches
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>
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#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:
123 Container(const Teuchos::RCP<const row_matrix_type>& matrix,
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,
202 Teuchos::ETransp mode = Teuchos::NO_TRANS,
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,
212 Teuchos::ETransp mode = Teuchos::NO_TRANS,
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
252 Teuchos::RCP<const row_matrix_type> inputMatrix_;
253
255 Teuchos::RCP<const crs_matrix_type> inputCrsMatrix_;
256
258 Teuchos::RCP<const block_crs_matrix_type> inputBlockMatrix_;
259
263 Teuchos::Array<LO> blockRows_; // size: total # of local rows (in all local blocks)
265 Teuchos::Array<LO> blockSizes_; // size: # of blocks
267 Teuchos::Array<LO> blockOffsets_;
269 mutable Teuchos::RCP<vector_type> Diag_;
287
292
293 LO maxBlockSize_;
294};
295
296namespace Details {
297template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
298struct StridedRowView;
299}
300
309
310template <class MatrixType, class LocalScalarType>
311class 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:
368 ContainerImpl(const Teuchos::RCP<const row_matrix_type>& matrix,
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,
414 Teuchos::ETransp mode = Teuchos::NO_TRANS,
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,
424 Teuchos::ETransp mode = Teuchos::NO_TRANS,
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
499namespace Details {
505template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
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
521 StridedRowView(Teuchos::Array<SC>& vals_, Teuchos::Array<LO>& inds_);
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
542template <class MatrixType>
543std::ostream& operator<<(std::ostream& os, const Ifpack2::Container<MatrixType>& obj);
544
545namespace Teuchos {
546
551template <class MatrixType>
552class TEUCHOSCORE_LIB_DLL_EXPORT TypeNameTraits< ::Ifpack2::Container<MatrixType> > {
553 public:
554 static std::string name() {
555 return std::string("Ifpack2::Container<") +
556 TypeNameTraits<MatrixType>::name() + ">";
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
The implementation of the numerical features of Container (Jacobi, Gauss-Seidel, SGS)....
Definition Ifpack2_Container_decl.hpp:311
std::vector< HostSubviewLocal > X_localBlocks_
Views for holding pieces of X corresponding to each block.
Definition Ifpack2_Container_decl.hpp:493
HostViewLocal X_local_
Scratch vectors used in apply().
Definition Ifpack2_Container_decl.hpp:477
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:443
Details::StridedRowView< SC, LO, GO, NO > getInputRowView(LO row) const
View a row of the input matrix.
Definition Ifpack2_Container_def.hpp:803
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:496
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
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:456
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
LocalScalarType LSC
The internal representation of LocalScalarType in Kokkos::View.
Definition Ifpack2_Container_decl.hpp:330
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
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
HostViewLocal weightedApplyScratch_
Definition Ifpack2_Container_decl.hpp:490
static std::string getName()
Definition Ifpack2_Container_def.hpp:475
LO translateRowToCol(LO row)
Definition Ifpack2_Container_def.hpp:493
virtual void initialize()=0
Do all set-up operations that only require matrix structure.
virtual ~ContainerImpl()
Destructor.
Definition Ifpack2_Container_def.hpp:439
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
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
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:80
bool hasBlockCrs_
Whether the input matrix is a BlockCRS matrix.
Definition Ifpack2_Container_decl.hpp:279
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:255
bool IsParallel_
Whether the problem is distributed across multiple MPI processes.
Definition Ifpack2_Container_decl.hpp:271
int numBlocks_
The number of blocks (partitions) in the container.
Definition Ifpack2_Container_decl.hpp:261
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
static std::string getName()
Definition Ifpack2_Container_def.hpp:146
typename Kokkos::ArithTraits< SC >::val_type ISC
Internal representation of Scalar in Kokkos::View.
Definition Ifpack2_Container_decl.hpp:104
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:269
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:281
Teuchos::RCP< const block_crs_matrix_type > inputBlockMatrix_
The input matrix, dynamic cast to BlockCrsMatrix. May be null.
Definition Ifpack2_Container_decl.hpp:258
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:151
virtual void applyMV(const mv_type &X, mv_type &Y) const
Wrapper for apply with MultiVector.
Definition Ifpack2_Container_def.hpp:134
LO NumLocalRows_
Number of local rows in input matrix.
Definition Ifpack2_Container_decl.hpp:273
bool isInitialized() const
Whether the container has been successfully initialized.
Definition Ifpack2_Container_def.hpp:123
void setBlockSizes(const Teuchos::Array< Teuchos::Array< LO > > &partitions)
Initialize arrays with information about block sizes.
Definition Ifpack2_Container_def.hpp:89
LO scalarsPerRow_
Definition Ifpack2_Container_decl.hpp:286
Teuchos::Array< LO > blockSizes_
Number of rows in each block.
Definition Ifpack2_Container_decl.hpp:265
GO NumGlobalNonzeros_
Number of nonzeros in input matrix.
Definition Ifpack2_Container_decl.hpp:277
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:140
Teuchos::RCP< const row_matrix_type > inputMatrix_
The input matrix to the constructor.
Definition Ifpack2_Container_decl.hpp:252
bool pointIndexed_
(If hasBlockCrs_) Whether the blocks are described using sub-block row indices instead of full block ...
Definition Ifpack2_Container_decl.hpp:283
bool IsInitialized_
If true, the container has been successfully initialized.
Definition Ifpack2_Container_decl.hpp:289
Teuchos::Array< LO > blockRows_
Local indices of the rows of the input matrix that belong to this block.
Definition Ifpack2_Container_decl.hpp:263
GO NumGlobalRows_
Number of global rows in input matrix.
Definition Ifpack2_Container_decl.hpp:275
bool isComputed() const
Whether the container has been successfully computed.
Definition Ifpack2_Container_def.hpp:128
typename mv_type::dual_view_type::t_host HostView
Definition Ifpack2_Container_decl.hpp:109
Teuchos::Array< LO > blockOffsets_
Starting index in blockRows_ of local row indices for each block.
Definition Ifpack2_Container_decl.hpp:267
bool IsComputed_
If true, the container has been successfully computed.
Definition Ifpack2_Container_decl.hpp:291
virtual void compute()=0
Extract the local diagonal blocks and prepare the solver.
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:506