10 #ifndef IFPACK2_DATABASESCHWARZ_DEF_HPP
11 #define IFPACK2_DATABASESCHWARZ_DEF_HPP
13 #include "Ifpack2_Parameters.hpp"
15 #include "Tpetra_CrsMatrix.hpp"
16 #include "Teuchos_FancyOStream.hpp"
17 #include "Teuchos_oblackholestream.hpp"
26 template<
class MatrixType>
30 IsInitialized_(false),
41 PatchTolerance_(1e-3),
45 this->setObjectLabel(
"Ifpack2::DatabaseSchwarz");
49 template<
class MatrixType>
54 IsInitialized_(false),
65 PatchTolerance_(1e-3),
69 this->setObjectLabel(
"Ifpack2::DatabaseSchwarz");
74 template<
class MatrixType>
79 template<
class MatrixType>
84 IsInitialized_ =
false;
91 template<
class MatrixType>
96 this->setParametersImpl(const_cast<Teuchos::ParameterList&>(params));
100 template<
class MatrixType>
106 const int defaultPatchSize = 9;
107 const double defaultPatchTolerance = 1e-3;
108 const bool defaultSkipDatabase =
false;
109 const bool defaultVerbose =
false;
112 PatchSize_ = params.
get<
int>(
"database schwarz: patch size",defaultPatchSize);
115 PatchSize_ < 0, std::invalid_argument,
116 "Ifpack2::DatabaseSchwarz::setParameters: \"database schwarz: patch size\" parameter "
117 "must be a nonnegative integer. You gave a value of " << PatchSize_ <<
".");
120 PatchTolerance_ = params.
get(
"database schwarz: patch tolerance", defaultPatchTolerance);
123 PatchTolerance_ <= 0, std::invalid_argument,
124 "Ifpack2::DatabaseSchwarz::setParameters: \"database schwarz: patch tolerance\" parameter "
125 "must be a positive double. You gave a value of " << PatchTolerance_ <<
".");
128 SkipDatabase_ = params.
get<
bool>(
"database schwarz: skip database",defaultSkipDatabase);
131 Verbose_ = params.
get<
bool>(
"database schwarz: print database summary",defaultVerbose);
135 template<
class MatrixType>
139 (void) zeroStartingSolution;
142 template<
class MatrixType>
148 A.
is_null(), std::runtime_error,
"Ifpack2::DatabaseSchwarz::getComm: The input "
149 "matrix A is null. Please call setMatrix() with a nonnull input matrix "
150 "before calling this method.");
151 return A->getRowMap()->getComm();
155 template<
class MatrixType>
163 template<
class MatrixType>
164 Teuchos::RCP<
const Tpetra::CrsMatrix<
typename MatrixType::scalar_type,
165 typename MatrixType::local_ordinal_type,
166 typename MatrixType::global_ordinal_type,
167 typename MatrixType::node_type> >
172 return Teuchos::rcp_dynamic_cast<
const crs_matrix_type> (getMatrix());
176 template<
class MatrixType>
183 A.
is_null(), std::runtime_error,
"Ifpack2::DatabaseSchwarz::getDomainMap: The "
184 "input matrix A is null. Please call setMatrix() with a nonnull input "
185 "matrix before calling this method.");
186 return A->getDomainMap();
190 template<
class MatrixType>
197 A.
is_null(), std::runtime_error,
"Ifpack2::DatabaseSchwarz::getRangeMap: The "
198 "input matrix A is null. Please call setMatrix() with a nonnull input "
199 "matrix before calling this method.");
200 return A->getRangeMap();
204 template<
class MatrixType>
210 template<
class MatrixType>
212 return NumInitialize_;
216 template<
class MatrixType>
222 template<
class MatrixType>
228 template<
class MatrixType>
230 return InitializeTime_;
234 template<
class MatrixType>
240 template<
class MatrixType>
246 template<
class MatrixType>
248 return ComputeFlops_;
252 template<
class MatrixType>
257 template<
class MatrixType>
261 A.
is_null(), std::runtime_error,
"Ifpack2::DatabaseSchwarz::getNodeSmootherComplexity: "
262 "The input matrix A is null. Please call setMatrix() with a nonnull "
263 "input matrix, then call compute(), before calling this method.");
265 return A->getLocalNumRows() + A->getLocalNumEntries();
270 template<
class MatrixType>
273 apply(
const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
274 Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
279 const std::string timerName (
"Ifpack2::DatabaseSchwarz::apply");
285 double startTime = timer->
wallTime();
294 ! isComputed(), std::runtime_error,
295 "Ifpack2::DatabaseSchwarz::apply(): You must call the compute() method before "
296 "you may call apply().");
298 X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
299 "Ifpack2::DatabaseSchwarz::apply(): X and Y must have the same number of "
300 "columns. X.getNumVectors() = " << X.getNumVectors() <<
" != "
301 <<
"Y.getNumVectors() = " << Y.getNumVectors() <<
".");
307 auto X_view = X.getLocalViewHost(Tpetra::Access::ReadOnly);
308 auto Y_view = Y.getLocalViewHost(Tpetra::Access::ReadWrite);
312 for(
unsigned int ipatch=0; ipatch<NumPatches_; ipatch++) {
313 int idatabase = DatabaseIndices_[ipatch];
317 for(
unsigned int c=0; c<X_view.extent(1); ++c) {
318 for(
unsigned int i=0; i<x_patch.
size(); ++i) {
319 x_patch[i] = X_view(PatchIndices_[ipatch][i],c);
328 int* ipiv = &ipiv_[idatabase*PatchSize_];
330 lapack.
GETRS(
'N', DatabaseMatrices_[idatabase]->numRows(), numRhs,
331 DatabaseMatrices_[idatabase]->values(), DatabaseMatrices_[idatabase]->numRows(),
333 DatabaseMatrices_[idatabase]->numRows(), &INFO);
337 INFO < 0, std::logic_error,
"Ifpack2::DatabaseSchwarz::compute: "
338 "LAPACK's _GETRF (LU factorization with partial pivoting) was called "
339 "incorrectly. INFO = " << INFO <<
" < 0. "
340 "Please report this bug to the Ifpack2 developers.");
345 INFO > 0, std::runtime_error,
"Ifpack2::DatabaseSchwarz::compute: "
346 "LAPACK's _GETRF (LU factorization with partial pivoting) reports that the "
347 "computed U factor is exactly singular. U(" << INFO <<
"," << INFO <<
") "
348 "(one-based index i) is exactly zero. This probably means that the input "
349 "patch is singular.");
352 for(
unsigned int c=0; c<Y_view.extent(1); ++c) {
353 for(
unsigned int i=0; i<x_patch.
size(); ++i) {
354 Y_view(PatchIndices_[ipatch][i],c) += alpha*Weights_[PatchIndices_[ipatch][i]]*x_patch[i];
360 ApplyTime_ += (timer->
wallTime() - startTime);
364 template<
class MatrixType>
367 applyMat (
const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& X,
368 Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
372 X.getNumVectors() != Y.getNumVectors(), std::invalid_argument,
373 "Ifpack2::DatabaseSchwarz::applyMat: X.getNumVectors() != Y.getNumVectors().");
377 A.is_null(), std::runtime_error,
"Ifpack2::DatabaseSchwarz::applyMat: The input "
378 "matrix A is null. Please call setMatrix() with a nonnull input matrix "
379 "before calling this method.");
381 A->apply (X, Y, mode);
385 template<
class MatrixType>
390 const std::string timerName (
"Ifpack2::DatabaseSchwarz::initialize");
395 IsInitialized_ =
true;
400 template<
class MatrixType>
403 const std::string timerName (
"Ifpack2::DatabaseSchwarz::compute");
409 double startTime = timer->
wallTime();
414 if (!isInitialized()) {
418 const int maxNnzPerRow = A_->getGlobalMaxNumRowEntries();
421 PatchIndices_.resize(0);
425 size_t num_entries = A_->getNumEntriesInLocalRow(irow);
430 typename row_matrix_type::nonconst_local_inds_host_view_type row_inds(
"row indices", maxNnzPerRow);
431 typename row_matrix_type::nonconst_values_host_view_type row_vals(
"row values", maxNnzPerRow);
432 A_->getLocalRowCopy(irow, row_inds, row_vals, num_entries);
435 bool is_new_patch =
true;
436 for(
size_t ipatch=0; ipatch<PatchIndices_.size(); ++ipatch) {
437 for(
size_t i=0; i<PatchIndices_[ipatch].size(); ++i) {
438 if(PatchIndices_[ipatch][i] == irow) {
439 is_new_patch =
false;
440 ipatch=PatchIndices_.size();
449 std::vector<typename row_matrix_type::local_ordinal_type> indices_vector = Teuchos::createVector(indices_array_view);
450 PatchIndices_.push_back(indices_vector);
459 DatabaseIndices_.resize(NumPatches_,-1);
460 Weights_.resize(A_->getLocalNumRows(),0);
461 std::vector<double> index_count(A_->getLocalNumRows(),0);
463 for(
unsigned int ipatch=0; ipatch< NumPatches_; ++ipatch) {
465 DenseMatRCP patch_matrix =
Teuchos::rcp(
new DenseMatType(PatchSize_, PatchSize_));
466 auto indices_vector = PatchIndices_[ipatch];
469 index_count[indices_vector[i]]++;
471 typename row_matrix_type::nonconst_local_inds_host_view_type row_inds(
"row indices", maxNnzPerRow);
472 typename row_matrix_type::nonconst_values_host_view_type row_vals(
"row values", maxNnzPerRow);
474 A_->getLocalRowCopy(indices_vector[i], row_inds, row_vals, num_entries);
476 for(
size_t k=0; k<num_entries; ++k) {
477 if(row_inds(k) == indices_vector[j]) {
478 (*patch_matrix)(i,j) = row_vals(k);
486 bool found_match =
false;
488 for(
size_t idatabase=0; idatabase<DatabaseMatrices_.size(); ++idatabase) {
494 DenseMatRCP database_candidate = DatabaseMatrices_[idatabase];
504 DatabaseIndices_[ipatch] = idatabase;
513 DatabaseMatrices_.push_back(patch_matrix);
514 DatabaseIndices_[ipatch] = DatabaseMatrices_.size()-1;
516 "Ifpack2::DatabaseSchwarz::compute: A matrix was added to the database, but appears to be null!"
517 "Please report this bug to the Ifpack2 developers.");
522 for(
unsigned int i=0; i<index_count.size(); ++i) {
524 "Ifpack2::DatabaseSchwarz::compute: DOF " << i <<
" has no corresponding patch! "
525 "All DOFs must be able to locate in a patch to use this method! "
526 "Have you considered adjusting the patch size? Currently it is " << PatchSize_ <<
".");
527 Weights_[i] = 1./index_count[i];
529 DatabaseSize_ = DatabaseMatrices_.size();
532 std::vector<int> database_counts(DatabaseSize_,0);
533 for(
unsigned int ipatch=0; ipatch<NumPatches_; ++ipatch) {
534 database_counts[DatabaseIndices_[ipatch]]++;
540 ipiv_.resize(DatabaseSize_*PatchSize_);
541 std::fill(ipiv_.begin (), ipiv_.end (), 0);
542 for(
unsigned int idatabase=0; idatabase<DatabaseSize_; idatabase++) {
543 int* ipiv = &ipiv_[idatabase*PatchSize_];
545 lapack.
GETRF(DatabaseMatrices_[idatabase]->numRows(),
546 DatabaseMatrices_[idatabase]->numCols(),
547 DatabaseMatrices_[idatabase]->values(),
548 DatabaseMatrices_[idatabase]->stride(),
553 INFO < 0, std::logic_error,
"Ifpack2::DatabaseSchwarz::compute: "
554 "LAPACK's _GETRF (LU factorization with partial pivoting) was called "
555 "incorrectly. INFO = " << INFO <<
" < 0. "
556 "Please report this bug to the Ifpack2 developers.");
561 std::cout <<
"SINGULAR LOCAL MATRIX, COUNT=" << database_counts[idatabase] << std::endl;
564 INFO > 0, std::runtime_error,
"Ifpack2::DatabaseSchwarz::compute: "
565 "LAPACK's _GETRF (LU factorization with partial pivoting) reports that the "
566 "computed U factor is exactly singular. U(" << INFO <<
"," << INFO <<
") "
567 "(one-based index i) is exactly zero. This probably means that the input "
568 "patch is singular.");
574 ComputeTime_ += (timer->
wallTime() - startTime);
578 std::cout <<
"Ifpack2::DatabaseSchwarz()::Compute() summary\n";
579 std::cout <<
"Found " << NumPatches_ <<
" patches of size " << PatchSize_ <<
" in matrix A\n";
580 std::cout <<
"Database tol = " << PatchTolerance_ <<
"\n";
581 std::cout <<
"Database size = " << DatabaseSize_ <<
" patches\n";
586 template <
class MatrixType>
588 std::ostringstream out;
593 out <<
"\"Ifpack2::DatabaseSchwarz\": {";
594 out <<
"Initialized: " << (isInitialized() ?
"true" :
"false")
595 <<
", Computed: " << (isComputed() ?
"true" :
"false")
596 <<
", patch size: " << PatchSize_
597 <<
", patch tolerance: " << PatchTolerance_
598 <<
", skip database: " << (SkipDatabase_ ?
"true" :
"false")
599 <<
", print database summary: " << (Verbose_ ?
"true" :
"false");
601 if (getMatrix().is_null()) {
602 out <<
"Matrix: null";
605 out <<
"Global matrix dimensions: ["
606 << getMatrix()->getGlobalNumRows() <<
", "
607 << getMatrix()->getGlobalNumCols() <<
"]"
608 <<
", Global nnz: " << getMatrix()->getGlobalNumEntries();
616 template <
class MatrixType>
639 const int myRank = this->getComm()->getRank();
643 out <<
"\"Ifpack2::DatabaseSchwarz\":" << endl;
648 out <<
"Template parameters:" << endl;
651 out <<
"Scalar: " << TypeNameTraits<scalar_type>::name() << endl
652 <<
"LocalOrdinal: " << TypeNameTraits<local_ordinal_type>::name() << endl
653 <<
"GlobalOrdinal: " << TypeNameTraits<global_ordinal_type>::name() << endl
654 <<
"Device: " << TypeNameTraits<device_type>::name() << endl;
656 out <<
"Initialized: " << (isInitialized() ?
"true" :
"false") << endl
657 <<
"Computed: " << (isComputed() ?
"true" :
"false") << endl;
659 if (getMatrix().is_null()) {
660 out <<
"Matrix: null" << endl;
663 out <<
"Global matrix dimensions: ["
664 << getMatrix()->getGlobalNumRows() <<
", "
665 << getMatrix()->getGlobalNumCols() <<
"]" << endl
666 <<
"Global nnz: " << getMatrix()->getGlobalNumEntries() << endl;
675 #define IFPACK2_DATABASESCHWARZ_INSTANT(S,LO,GO,N) \
676 template class Ifpack2::DatabaseSchwarz< Tpetra::RowMatrix<S, LO, GO, N> >;
678 #endif // IFPACK2_DATABASESCHWARZ_DEF_HPP
Teuchos::RCP< const row_matrix_type > getMatrix() const
The matrix for which this is a preconditioner.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:157
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_DatabaseSchwarz_decl.hpp:84
void apply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
Apply the preconditioner to X, returning the result in Y. Y = alpha*Op(A)*X + beta*Y.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:273
Teuchos::RCP< const map_type > getRangeMap() const
The Tpetra::Map representing the range of this operator.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:193
double getApplyTime() const
The total time spent in all calls to apply().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:241
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:258
int getNumApply() const
The total number of successful calls to apply().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:223
void initialize()
Initialize the preconditioner.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:386
bool hasTransposeApply() const
Whether it's possible to apply the transpose of this operator.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:205
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
double getInitializeTime() const
The total time spent in all calls to initialize().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:229
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to a Teuchos::FancyOStream.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:618
void setParameters(const Teuchos::ParameterList ¶ms)
Set (or reset) parameters.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:93
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The communicator over which the matrix is distributed.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:144
Teuchos::RCP< const map_type > getDomainMap() const
The Tpetra::Map representing the domain of this operator.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:179
virtual ~DatabaseSchwarz()
Destructor.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:75
double getComputeTime() const
The total time spent in all calls to compute().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:235
double getApplyFlops() const
The total number of floating-point operations taken by all calls to apply().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:253
void GETRF(const OrdinalType &m, const OrdinalType &n, ScalarType *A, const OrdinalType &lda, OrdinalType *IPIV, OrdinalType *info) const
void compute()
(Re)compute the left scaling, and (if applicable) estimate max and min eigenvalues of D_inv * A...
Definition: Ifpack2_DatabaseSchwarz_def.hpp:401
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
DatabaseSchwarz(const Teuchos::RCP< const row_matrix_type > &A)
Constructor.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:28
Teuchos::RCP< const Tpetra::CrsMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > getCrsMatrix() const
Attempt to return the matrix A as a Tpetra::CrsMatrix.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:169
MatrixType::node_type node_type
The Node type used by the input MatrixType.
Definition: Ifpack2_DatabaseSchwarz_decl.hpp:93
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Change the matrix to be preconditioned.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:80
void setZeroStartingSolution(bool zeroStartingSolution)
Set this preconditioner's parameters.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:137
double getComputeFlops() const
The total number of floating-point operations taken by all calls to compute().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:247
std::string description() const
A simple one-line description of this object.
Definition: Ifpack2_DatabaseSchwarz_def.hpp:587
int getNumInitialize() const
The total number of successful calls to initialize().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:211
static magnitudeType magnitude(T a)
Overlapping Schwarz where redundant patches are not stored explicitly.
Definition: Ifpack2_DatabaseSchwarz_decl.hpp:63
void applyMat(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS) const
Compute Y = Op(A)*X, where Op(A) is either A, , or .
Definition: Ifpack2_DatabaseSchwarz_def.hpp:367
void GETRS(const char &TRANS, const OrdinalType &n, const OrdinalType &nrhs, const ScalarType *A, const OrdinalType &lda, const OrdinalType *IPIV, ScalarType *B, const OrdinalType &ldb, OrdinalType *info) const
TypeTo as(const TypeFrom &t)
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_DatabaseSchwarz_decl.hpp:81
MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition: Ifpack2_DatabaseSchwarz_decl.hpp:87
int getNumCompute() const
The total number of successful calls to compute().
Definition: Ifpack2_DatabaseSchwarz_def.hpp:217