22#ifndef IFPACK2_ADDITIVESCHWARZ_DEF_HPP
23#define IFPACK2_ADDITIVESCHWARZ_DEF_HPP
26#include "Trilinos_Details_LinearSolverFactory.hpp"
30#include "Ifpack2_Details_LinearSolver.hpp"
31#include "Ifpack2_Details_getParamTryingTypes.hpp"
33#if defined(HAVE_IFPACK2_XPETRA) && defined(HAVE_IFPACK2_ZOLTAN2)
34#include "Zoltan2_TpetraRowGraphAdapter.hpp"
35#include "Zoltan2_OrderingProblem.hpp"
36#include "Zoltan2_OrderingSolution.hpp"
40#include "Ifpack2_Parameters.hpp"
41#include "Ifpack2_LocalFilter.hpp"
42#include "Ifpack2_ReorderFilter.hpp"
43#include "Ifpack2_SingletonFilter.hpp"
44#include "Ifpack2_Details_AdditiveSchwarzFilter.hpp"
48#include "Teuchos_DefaultMpiComm.hpp"
51#include "Teuchos_StandardParameterEntryValidators.hpp"
57#include <Tpetra_BlockMultiVector.hpp>
71bool anyBad(
const MV& X) {
72 using STS = Teuchos::ScalarTraits<typename MV::scalar_type>;
73 using magnitude_type =
typename STS::magnitudeType;
74 using STM = Teuchos::ScalarTraits<magnitude_type>;
76 Teuchos::Array<magnitude_type> norms(X.getNumVectors());
79 for (
size_t j = 0; j < X.getNumVectors(); ++j) {
80 if (STM::isnaninf(norms[j])) {
88template <
class RowMatrixType>
89void writeLocalMatrixMarketPerRank(
const Teuchos::RCP<RowMatrixType>& A_local,
91 const std::string& basePath) {
92 typedef typename RowMatrixType::local_ordinal_type local_ordinal_type;
93 typedef typename RowMatrixType::scalar_type scalar_type;
94 typedef typename RowMatrixType::nonconst_local_inds_host_view_type nonconst_local_inds_host_view_type;
95 typedef typename RowMatrixType::nonconst_values_host_view_type nonconst_values_host_view_type;
96 typedef Teuchos::ScalarTraits<scalar_type> STS;
98 std::ostringstream fname;
99 fname << basePath <<
".rank_" << rank <<
".mtx";
101 std::ofstream out(fname.str().c_str());
102 TEUCHOS_TEST_FOR_EXCEPTION(
103 !out.is_open(), std::runtime_error,
104 "Ifpack2::AdditiveSchwarz: Failed to open debug MatrixMarket file \""
105 << fname.str() <<
"\".");
107 const auto numRows = A_local->getLocalNumRows();
108 const auto numCols = A_local->getLocalNumCols();
109 const auto nnz = A_local->getLocalNumEntries();
111 if (STS::isComplex) {
112 out <<
"%%MatrixMarket matrix coordinate complex general\n";
114 out <<
"%%MatrixMarket matrix coordinate real general\n";
116 out << numRows <<
" " << numCols <<
" " << nnz <<
"\n";
118 nonconst_local_inds_host_view_type indices(
"indices", A_local->getLocalMaxNumRowEntries());
119 nonconst_values_host_view_type values(
"values", A_local->getLocalMaxNumRowEntries());
121 for (local_ordinal_type i = 0; i < static_cast<local_ordinal_type>(numRows); ++i) {
122 size_t numEntries = 0;
123 A_local->getLocalRowCopy(i, indices, values, numEntries);
124 for (
size_t k = 0; k < numEntries; ++k) {
125 out << (i + 1) <<
" " << (indices[k] + 1);
126 if (STS::isComplex) {
127 out <<
" " << STS::real(values[k]) <<
" " << STS::imag(values[k]) <<
"\n";
129 out <<
" " << values[k] <<
"\n";
139template <
class MatrixType,
class LocalInverseType>
140bool AdditiveSchwarz<MatrixType, LocalInverseType>::hasInnerPrecName()
const {
141 const char* options[4] = {
142 "inner preconditioner name",
143 "subdomain solver name",
144 "schwarz: inner preconditioner name",
145 "schwarz: subdomain solver name"};
146 const int numOptions = 4;
148 for (
int k = 0; k < numOptions && !match; ++k) {
149 if (List_.isParameter(options[k])) {
156template <
class MatrixType,
class LocalInverseType>
157void AdditiveSchwarz<MatrixType, LocalInverseType>::removeInnerPrecName() {
158 const char* options[4] = {
159 "inner preconditioner name",
160 "subdomain solver name",
161 "schwarz: inner preconditioner name",
162 "schwarz: subdomain solver name"};
163 const int numOptions = 4;
164 for (
int k = 0; k < numOptions; ++k) {
165 List_.remove(options[k],
false);
169template <
class MatrixType,
class LocalInverseType>
171AdditiveSchwarz<MatrixType, LocalInverseType>::innerPrecName()
const {
172 const char* options[4] = {
173 "inner preconditioner name",
174 "subdomain solver name",
175 "schwarz: inner preconditioner name",
176 "schwarz: subdomain solver name"};
177 const int numOptions = 4;
182 for (
int k = 0; k < numOptions && !match; ++k) {
183 const Teuchos::ParameterEntry* paramEnt =
184 List_.getEntryPtr(options[k]);
185 if (paramEnt !=
nullptr && paramEnt->isType<std::string>()) {
186 newName = Teuchos::getValue<std::string>(*paramEnt);
190 return match ? newName : defaultInnerPrecName();
193template <
class MatrixType,
class LocalInverseType>
194void AdditiveSchwarz<MatrixType, LocalInverseType>::removeInnerPrecParams() {
195 const char* options[4] = {
196 "inner preconditioner parameters",
197 "subdomain solver parameters",
198 "schwarz: inner preconditioner parameters",
199 "schwarz: subdomain solver parameters"};
200 const int numOptions = 4;
203 for (
int k = 0; k < numOptions; ++k) {
204 List_.remove(options[k],
false);
208template <
class MatrixType,
class LocalInverseType>
209std::pair<Teuchos::ParameterList, bool>
210AdditiveSchwarz<MatrixType, LocalInverseType>::innerPrecParams()
const {
211 const char* options[4] = {
212 "inner preconditioner parameters",
213 "subdomain solver parameters",
214 "schwarz: inner preconditioner parameters",
215 "schwarz: subdomain solver parameters"};
216 const int numOptions = 4;
217 Teuchos::ParameterList params;
221 for (
int k = 0; k < numOptions && !match; ++k) {
222 if (List_.isSublist(options[k])) {
223 params = List_.sublist(options[k]);
228 return std::make_pair(params, match);
231template <
class MatrixType,
class LocalInverseType>
233AdditiveSchwarz<MatrixType, LocalInverseType>::defaultInnerPrecName() {
239template <
class MatrixType,
class LocalInverseType>
244template <
class MatrixType,
class LocalInverseType>
247 const Teuchos::RCP<const coord_type>& coordinates)
249 , Coordinates_(coordinates) {}
251template <
class MatrixType,
class LocalInverseType>
254 const int overlapLevel)
256 , OverlapLevel_(overlapLevel) {}
258template <
class MatrixType,
class LocalInverseType>
259Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type>>
262 TEUCHOS_TEST_FOR_EXCEPTION(
263 Matrix_.is_null(), std::runtime_error,
264 "Ifpack2::AdditiveSchwarz::"
265 "getDomainMap: The matrix to precondition is null. You must either pass "
266 "a nonnull matrix to the constructor, or call setMatrix() with a nonnull "
267 "input, before you may call this method.");
268 return Matrix_->getDomainMap();
271template <
class MatrixType,
class LocalInverseType>
272Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type>>
274 TEUCHOS_TEST_FOR_EXCEPTION(
275 Matrix_.is_null(), std::runtime_error,
276 "Ifpack2::AdditiveSchwarz::"
277 "getRangeMap: The matrix to precondition is null. You must either pass "
278 "a nonnull matrix to the constructor, or call setMatrix() with a nonnull "
279 "input, before you may call this method.");
280 return Matrix_->getRangeMap();
283template <
class MatrixType,
class LocalInverseType>
288template <
class MatrixType,
class LocalInverseType>
289Teuchos::RCP<const Tpetra::MultiVector<typename Teuchos::ScalarTraits<typename MatrixType::scalar_type>::magnitudeType,
typename MatrixType::local_ordinal_type,
typename MatrixType::global_ordinal_type,
typename MatrixType::node_type>>
AdditiveSchwarz<MatrixType, LocalInverseType>::getCoord()
const {
295template <
class MatrixType,
class map_type>
296Teuchos::RCP<const map_type>
297pointMapFromMeshMap(
const Teuchos::RCP<const map_type>& meshMap,
const typename MatrixType::local_ordinal_type blockSize) {
298 using BMV = Tpetra::BlockMultiVector<
299 typename MatrixType::scalar_type,
300 typename MatrixType::local_ordinal_type,
301 typename MatrixType::global_ordinal_type,
302 typename MatrixType::node_type>;
304 if (blockSize == 1)
return meshMap;
306 return Teuchos::RCP<const map_type>(
new map_type(BMV::makePointMap(*meshMap, blockSize)));
309template <
typename MV,
typename Map>
310void resetMultiVecIfNeeded(std::unique_ptr<MV>& mv_ptr,
const Map& map,
const size_t numVectors,
bool initialize) {
311 if (!mv_ptr || mv_ptr->getNumVectors() != numVectors) {
312 mv_ptr.reset(
new MV(map, numVectors, initialize));
318template <
class MatrixType,
class LocalInverseType>
320 apply(
const Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& B,
321 Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type>& Y,
322 Teuchos::ETransp mode,
327 using Teuchos::rcp_dynamic_cast;
329 using Teuchos::TimeMonitor;
330 typedef Teuchos::ScalarTraits<scalar_type> STS;
331 const char prefix[] =
"Ifpack2::AdditiveSchwarz::apply: ";
333 TEUCHOS_TEST_FOR_EXCEPTION(!IsComputed_, std::runtime_error,
334 prefix <<
"isComputed() must be true before you may call apply().");
335 TEUCHOS_TEST_FOR_EXCEPTION(Matrix_.is_null(), std::logic_error, prefix <<
"The input matrix A is null, but the preconditioner says that it has "
336 "been computed (isComputed() is true). This should never happen, since "
337 "setMatrix() should always mark the preconditioner as not computed if "
338 "its argument is null. "
339 "Please report this bug to the Ifpack2 developers.");
340 TEUCHOS_TEST_FOR_EXCEPTION(Inverse_.is_null(), std::runtime_error,
341 prefix <<
"The subdomain solver is null. "
342 "This can only happen if you called setInnerPreconditioner() with a null "
343 "input, after calling initialize() or compute(). If you choose to call "
344 "setInnerPreconditioner() with a null input, you must then call it with "
345 "a nonnull input before you may call initialize() or compute().");
346 TEUCHOS_TEST_FOR_EXCEPTION(B.getNumVectors() != Y.getNumVectors(), std::invalid_argument,
347 prefix <<
"B and Y must have the same number of columns. B has " << B.getNumVectors() <<
" columns, but Y has " << Y.getNumVectors() <<
".");
348 TEUCHOS_TEST_FOR_EXCEPTION(IsOverlapping_ && OverlappingMatrix_.is_null(), std::logic_error,
349 prefix <<
"The overlapping matrix is null. "
350 "This should never happen if IsOverlapping_ is true. "
351 "Please report this bug to the Ifpack2 developers.");
352 TEUCHOS_TEST_FOR_EXCEPTION(!IsOverlapping_ && localMap_.is_null(), std::logic_error,
353 prefix <<
"localMap_ is null. "
354 "This should never happen if IsOverlapping_ is false. "
355 "Please report this bug to the Ifpack2 developers.");
356 TEUCHOS_TEST_FOR_EXCEPTION(alpha != STS::one(), std::logic_error,
357 prefix <<
"Not implemented for alpha != 1.");
358 TEUCHOS_TEST_FOR_EXCEPTION(beta != STS::zero(), std::logic_error,
359 prefix <<
"Not implemented for beta != 0.");
362 const bool bad = anyBad(B);
363 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
364 "Ifpack2::AdditiveSchwarz::apply: "
365 "The 2-norm of the input B is NaN or Inf.");
369 if (!ZeroStartingSolution_) {
370 const bool bad = anyBad(Y);
371 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
372 "Ifpack2::AdditiveSchwarz::apply: "
373 "On input, the initial guess Y has 2-norm NaN or Inf "
374 "(ZeroStartingSolution_ is false).");
378 const std::string timerName(
"Ifpack2::AdditiveSchwarz::apply");
379 RCP<Time> timer = TimeMonitor::lookupCounter(timerName);
380 if (timer.is_null()) {
381 timer = TimeMonitor::getNewCounter(timerName);
383 double startTime = timer->wallTime();
386 TimeMonitor timeMon(*timer);
388 const scalar_type ZERO = Teuchos::ScalarTraits<scalar_type>::zero();
389 const size_t numVectors = B.getNumVectors();
393 if (ZeroStartingSolution_) {
398 MV* OverlappingB =
nullptr;
399 MV* OverlappingY =
nullptr;
401 RCP<const map_type> B_and_Y_map = pointMapFromMeshMap<MatrixType>(IsOverlapping_ ? OverlappingMatrix_->getRowMap() : localMap_, Matrix_->getBlockSize());
402 resetMultiVecIfNeeded(overlapping_B_, B_and_Y_map, numVectors,
false);
403 resetMultiVecIfNeeded(overlapping_Y_, B_and_Y_map, numVectors,
false);
404 OverlappingB = overlapping_B_.get();
405 OverlappingY = overlapping_Y_.get();
408 OverlappingB->putScalar(ZERO);
409 OverlappingY->putScalar(ZERO);
412 RCP<MV> globalOverlappingB;
413 if (!IsOverlapping_) {
414 auto matrixPointRowMap = pointMapFromMeshMap<MatrixType>(Matrix_->getRowMap(), Matrix_->getBlockSize());
417 OverlappingB->offsetViewNonConst(matrixPointRowMap, 0);
420 if (DistributedImporter_.is_null()) {
424 DistributedImporter_ =
425 rcp(
new import_type(matrixPointRowMap,
426 Matrix_->getDomainMap()));
430 resetMultiVecIfNeeded(R_, B.getMap(), numVectors,
false);
431 resetMultiVecIfNeeded(C_, Y.getMap(), numVectors,
false);
434 Teuchos::ArrayRCP<scalar_type> dataNumOverlapCopies;
435 if (IsOverlapping_ && AvgOverlap_) {
436 if (num_overlap_copies_.get() ==
nullptr) {
437 num_overlap_copies_.reset(
new MV(Y.getMap(), 1,
false));
438 RCP<MV> onesVec(
new MV(OverlappingMatrix_->getRowMap(), 1,
false));
439 onesVec->putScalar(Teuchos::ScalarTraits<scalar_type>::one());
440 rcp_dynamic_cast<OverlappingRowMatrix<row_matrix_type>>(OverlappingMatrix_)->exportMultiVector(*onesVec, *(num_overlap_copies_.get()), CombineMode_);
442 dataNumOverlapCopies = num_overlap_copies_.get()->getDataNonConst(0);
452 for (
int ni = 0; ni < NumIterations_; ++ni) {
454 const bool bad = anyBad(Y);
455 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
456 "Ifpack2::AdditiveSchwarz::apply: "
457 "At top of iteration "
458 << ni <<
", the 2-norm of Y is NaN or Inf.");
461 Tpetra::deep_copy(*R, B);
466 if (!ZeroStartingSolution_ || ni > 0) {
468 Matrix_->apply(Y, *R, mode, -STS::one(), STS::one());
471 const bool bad = anyBad(*R);
472 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
473 "Ifpack2::AdditiveSchwarz::apply: "
475 << ni <<
", the 2-norm of R (result of computing "
476 "residual with Y) is NaN or Inf.");
481 if (IsOverlapping_) {
482 TEUCHOS_TEST_FOR_EXCEPTION(OverlappingMatrix_.is_null(), std::logic_error, prefix <<
"IsOverlapping_ is true, but OverlappingMatrix_, while nonnull, is "
483 "not an OverlappingRowMatrix<row_matrix_type>. Please report this "
484 "bug to the Ifpack2 developers.");
485 OverlappingMatrix_->importMultiVector(*R, *OverlappingB, Tpetra::INSERT);
503 const bool bad = anyBad(*OverlappingB);
504 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
505 "Ifpack2::AdditiveSchwarz::apply: "
507 << ni <<
", result of importMultiVector from R "
508 "to OverlappingB, has 2-norm NaN or Inf.");
511 globalOverlappingB->doImport(*R, *DistributedImporter_, Tpetra::INSERT);
514 const bool bad = anyBad(*globalOverlappingB);
515 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
516 "Ifpack2::AdditiveSchwarz::apply: "
518 << ni <<
", result of doImport from R, has 2-norm "
524 const bool bad = anyBad(*OverlappingB);
525 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
526 "Ifpack2::AdditiveSchwarz::apply: "
528 << ni <<
", right before localApply, the 2-norm of "
529 "OverlappingB is NaN or Inf.");
533 localApply(*OverlappingB, *OverlappingY);
536 const bool bad = anyBad(*OverlappingY);
537 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
538 "Ifpack2::AdditiveSchwarz::apply: "
540 << ni <<
", after localApply and before export / "
541 "copy, the 2-norm of OverlappingY is NaN or Inf.");
545 const bool bad = anyBad(*C);
546 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
547 "Ifpack2::AdditiveSchwarz::apply: "
549 << ni <<
", before export / copy, the 2-norm of C "
554 if (IsOverlapping_) {
555 TEUCHOS_TEST_FOR_EXCEPTION(OverlappingMatrix_.is_null(), std::logic_error, prefix <<
"OverlappingMatrix_ is null when it shouldn't be. "
556 "Please report this bug to the Ifpack2 developers.");
557 OverlappingMatrix_->exportMultiVector(*OverlappingY, *C, CombineMode_);
561 Teuchos::ArrayRCP<scalar_type> dataC = C->getDataNonConst(0);
562 for (
int i = 0; i < (int)C->getMap()->getLocalNumElements(); i++) {
563 dataC[i] = dataC[i] / dataNumOverlapCopies[i];
573 RCP<MV> C_view = C->offsetViewNonConst(OverlappingY->getMap(), 0);
574 Tpetra::deep_copy(*C_view, *OverlappingY);
578 const bool bad = anyBad(*C);
579 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
580 "Ifpack2::AdditiveSchwarz::apply: "
582 << ni <<
", before Y := C + Y, the 2-norm of C "
587 const bool bad = anyBad(Y);
588 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
589 "Ifpack2::AdditiveSchwarz::apply: "
590 "Before Y := C + Y, at iteration "
591 << ni <<
", the 2-norm of Y "
595 Y.update(UpdateDamping_, *C, STS::one());
598 const bool bad = anyBad(Y);
599 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
600 "Ifpack2::AdditiveSchwarz::apply: "
602 << ni <<
", after Y := C + Y, the 2-norm of Y "
610 const bool bad = anyBad(Y);
611 TEUCHOS_TEST_FOR_EXCEPTION(bad, std::runtime_error,
612 "Ifpack2::AdditiveSchwarz::apply: "
613 "The 2-norm of the output Y is NaN or Inf.");
618 ApplyTime_ += (timer->wallTime() - startTime);
621template <
class MatrixType,
class LocalInverseType>
623 localApply(MV& OverlappingB, MV& OverlappingY)
const {
625 using Teuchos::rcp_dynamic_cast;
627 const size_t numVectors = OverlappingB.getNumVectors();
629 auto additiveSchwarzFilter = rcp_dynamic_cast<Details::AdditiveSchwarzFilter<MatrixType>>(innerMatrix_);
630 if (additiveSchwarzFilter) {
636 resetMultiVecIfNeeded(reduced_reordered_B_, additiveSchwarzFilter->getRowMap(), numVectors,
true);
637 resetMultiVecIfNeeded(reduced_reordered_Y_, additiveSchwarzFilter->getRowMap(), numVectors,
true);
638 additiveSchwarzFilter->CreateReducedProblem(OverlappingB, OverlappingY, *reduced_reordered_B_);
640 if (additiveSchwarzFilter->isEquilibrated()) {
641 additiveSchwarzFilter->scaleReducedRHS(*reduced_reordered_B_);
645 Inverse_->solve(*reduced_reordered_Y_, *reduced_reordered_B_);
647 if (additiveSchwarzFilter->isEquilibrated()) {
648 additiveSchwarzFilter->unscaleReducedLHS(*reduced_reordered_Y_);
652 additiveSchwarzFilter->UpdateLHS(*reduced_reordered_Y_, OverlappingY);
654 if (FilterSingletons_) {
656 resetMultiVecIfNeeded(reduced_B_, SingletonMatrix_->getRowMap(), numVectors,
true);
657 resetMultiVecIfNeeded(reduced_Y_, SingletonMatrix_->getRowMap(), numVectors,
true);
659 RCP<SingletonFilter<row_matrix_type>> singletonFilter =
660 rcp_dynamic_cast<SingletonFilter<row_matrix_type>>(SingletonMatrix_);
661 TEUCHOS_TEST_FOR_EXCEPTION(!SingletonMatrix_.is_null() && singletonFilter.is_null(),
663 "Ifpack2::AdditiveSchwarz::localApply: "
664 "SingletonFilter_ is nonnull but is not a SingletonFilter"
665 "<row_matrix_type>. This should never happen. Please report this bug "
666 "to the Ifpack2 developers.");
667 singletonFilter->SolveSingletons(OverlappingB, OverlappingY);
668 singletonFilter->CreateReducedRHS(OverlappingY, OverlappingB, *reduced_B_);
671 if (!UseReordering_) {
672 Inverse_->solve(*reduced_Y_, *reduced_B_);
674 RCP<ReorderFilter<row_matrix_type>> rf =
675 rcp_dynamic_cast<ReorderFilter<row_matrix_type>>(ReorderedLocalizedMatrix_);
676 TEUCHOS_TEST_FOR_EXCEPTION(!ReorderedLocalizedMatrix_.is_null() && rf.is_null(), std::logic_error,
677 "Ifpack2::AdditiveSchwarz::localApply: ReorderedLocalizedMatrix_ is "
678 "nonnull but is not a ReorderFilter<row_matrix_type>. This should "
679 "never happen. Please report this bug to the Ifpack2 developers.");
680 resetMultiVecIfNeeded(reordered_B_, reduced_B_->getMap(), numVectors,
false);
681 resetMultiVecIfNeeded(reordered_Y_, reduced_Y_->getMap(), numVectors,
false);
682 rf->permuteOriginalToReordered(*reduced_B_, *reordered_B_);
683 Inverse_->solve(*reordered_Y_, *reordered_B_);
684 rf->permuteReorderedToOriginal(*reordered_Y_, *reduced_Y_);
688 singletonFilter->UpdateLHS(*reduced_Y_, OverlappingY);
691 if (!UseReordering_) {
692 Inverse_->solve(OverlappingY, OverlappingB);
694 resetMultiVecIfNeeded(reordered_B_, OverlappingB.getMap(), numVectors,
false);
695 resetMultiVecIfNeeded(reordered_Y_, OverlappingY.getMap(), numVectors,
false);
697 RCP<ReorderFilter<row_matrix_type>> rf =
698 rcp_dynamic_cast<ReorderFilter<row_matrix_type>>(ReorderedLocalizedMatrix_);
699 TEUCHOS_TEST_FOR_EXCEPTION(!ReorderedLocalizedMatrix_.is_null() && rf.is_null(), std::logic_error,
700 "Ifpack2::AdditiveSchwarz::localApply: ReorderedLocalizedMatrix_ is "
701 "nonnull but is not a ReorderFilter<row_matrix_type>. This should "
702 "never happen. Please report this bug to the Ifpack2 developers.");
703 rf->permuteOriginalToReordered(OverlappingB, *reordered_B_);
704 Inverse_->solve(*reordered_Y_, *reordered_B_);
705 rf->permuteReorderedToOriginal(*reordered_Y_, OverlappingY);
711template <
class MatrixType,
class LocalInverseType>
718 this->setParameterList(Teuchos::rcpFromRef(List_));
721template <
class MatrixType,
class LocalInverseType>
724 using Details::getParamTryingTypes;
725 using Teuchos::ParameterEntry;
726 using Teuchos::ParameterEntryValidator;
727 using Teuchos::ParameterList;
730 using Teuchos::rcp_dynamic_cast;
731 using Teuchos::StringToIntegralParameterEntryValidator;
732 using Tpetra::CombineMode;
733 const char prefix[] =
"Ifpack2::AdditiveSchwarz: ";
735 if (plist.is_null()) {
738 this->setParameterList(rcp(
new ParameterList()));
744 TEUCHOS_TEST_FOR_EXCEPTION(
745 plist.is_null(), std::logic_error,
746 "Ifpack2::AdditiveSchwarz::"
747 "setParameterList: plist is null. This should never happen, since the "
748 "method should have replaced a null input list with a nonnull empty list "
749 "by this point. Please report this bug to the Ifpack2 developers.");
770 const std::string cmParamName(
"schwarz: combine mode");
771 const ParameterEntry* cmEnt = plist->getEntryPtr(cmParamName);
772 if (cmEnt !=
nullptr) {
773 if (cmEnt->isType<CombineMode>()) {
774 CombineMode_ = Teuchos::getValue<CombineMode>(*cmEnt);
775 }
else if (cmEnt->isType<
int>()) {
776 const int cm = Teuchos::getValue<int>(*cmEnt);
777 CombineMode_ =
static_cast<CombineMode
>(cm);
778 }
else if (cmEnt->isType<std::string>()) {
783 const ParameterEntry& validEntry =
785 RCP<const ParameterEntryValidator> v = validEntry.validator();
786 using vs2e_type = StringToIntegralParameterEntryValidator<CombineMode>;
787 RCP<const vs2e_type> vs2e = rcp_dynamic_cast<const vs2e_type>(v,
true);
789 ParameterEntry& inputEntry = plist->getEntry(cmParamName);
794 if (strncmp(Teuchos::getValue<std::string>(inputEntry).c_str(),
"AVG", 3) == 0) {
795 inputEntry.template setValue<std::string>(
"ADD");
798 CombineMode_ = vs2e->getIntegralValue(inputEntry, cmParamName);
805 if (plist->isParameter(
"subdomain solver name")) {
806 if (plist->get<std::string>(
"subdomain solver name") ==
"BLOCK_RELAXATION") {
807 if (plist->isSublist(
"subdomain solver parameters")) {
808 if (plist->sublist(
"subdomain solver parameters").isParameter(
"relaxation: type")) {
809 if (plist->sublist(
"subdomain solver parameters").get<std::string>(
"relaxation: type") ==
"Jacobi") {
810 if (plist->sublist(
"subdomain solver parameters").isParameter(
"partitioner: type")) {
811 if (plist->sublist(
"subdomain solver parameters").get<std::string>(
"partitioner: type") ==
"user") {
812 if (CombineMode_ == Tpetra::ADD) plist->sublist(
"subdomain solver parameters").set(
"partitioner: combine mode",
"ADD");
813 if (CombineMode_ == Tpetra::ZERO) plist->sublist(
"subdomain solver parameters").set(
"partitioner: combine mode",
"ZERO");
823 OverlapLevel_ = plist->get(
"schwarz: overlap level", OverlapLevel_);
829 UseReordering_ = plist->get(
"schwarz: use reordering", UseReordering_);
831#if !defined(HAVE_IFPACK2_XPETRA) || !defined(HAVE_IFPACK2_ZOLTAN2)
832 TEUCHOS_TEST_FOR_EXCEPTION(
833 UseReordering_, std::invalid_argument,
834 "Ifpack2::AdditiveSchwarz::"
835 "setParameters: You specified \"schwarz: use reordering\" = true. "
836 "This is only valid when Trilinos was built with Ifpack2, Xpetra, and "
837 "Zoltan2 enabled. Either Xpetra or Zoltan2 was not enabled in your build "
849 FilterSingletons_ = plist->get(
"schwarz: filter singletons", FilterSingletons_);
851 EquilibrateSubdomainMatrix_ =
852 plist->get(
"schwarz: subdomain 1-norm equilibration", EquilibrateSubdomainMatrix_);
855 getParamTryingTypes<scalar_type, scalar_type, double>(UpdateDamping_, *plist,
"schwarz: update damping", prefix);
890 if (!Inverse_.is_null()) {
893 if (hasInnerPrecName() && innerPrecName() !=
"CUSTOM") {
896 Inverse_ = Teuchos::null;
900 std::pair<ParameterList, bool> result = innerPrecParams();
904 Inverse_->setParameters(rcp(
new ParameterList(result.first)));
909 NumIterations_ = plist->get(
"schwarz: num iterations", NumIterations_);
910 ZeroStartingSolution_ =
911 plist->get(
"schwarz: zero starting solution", ZeroStartingSolution_);
914template <
class MatrixType,
class LocalInverseType>
915Teuchos::RCP<const Teuchos::ParameterList>
918 using Teuchos::ParameterList;
919 using Teuchos::parameterList;
921 using Teuchos::rcp_const_cast;
923 if (validParams_.is_null()) {
924 const int overlapLevel = 0;
925 const bool useReordering =
false;
926 const bool filterSingletons =
false;
927 const bool equilibrateSubdomainMatrix =
false;
928 const int numIterations = 1;
929 const bool zeroStartingSolution =
true;
930 const scalar_type updateDamping = Teuchos::ScalarTraits<scalar_type>::one();
931 ParameterList reorderingSublist;
932 reorderingSublist.set(
"order_method", std::string(
"rcm"));
934 RCP<ParameterList> plist = parameterList(
"Ifpack2::AdditiveSchwarz");
936 Tpetra::setCombineModeParameter(*plist,
"schwarz: combine mode");
937 plist->set(
"schwarz: overlap level", overlapLevel);
938 plist->set(
"schwarz: use reordering", useReordering);
939 plist->set(
"schwarz: reordering list", reorderingSublist);
942 plist->set(
"schwarz: compute condest",
false);
943 plist->set(
"schwarz: filter singletons", filterSingletons);
944 plist->set(
"schwarz: num iterations", numIterations);
945 plist->set(
"schwarz: zero starting solution", zeroStartingSolution);
946 plist->set(
"schwarz: update damping", updateDamping);
947 plist->set(
"schwarz: subdomain 1-norm equilibration", equilibrateSubdomainMatrix);
957 validParams_ = rcp_const_cast<const ParameterList>(plist);
962template <
class MatrixType,
class LocalInverseType>
966 using Teuchos::SerialComm;
968 using Teuchos::TimeMonitor;
969 using Tpetra::global_size_t;
971 const std::string timerName(
"Ifpack2::AdditiveSchwarz::initialize");
972 RCP<Time> timer = TimeMonitor::lookupCounter(timerName);
973 if (timer.is_null()) {
974 timer = TimeMonitor::getNewCounter(timerName);
976 double startTime = timer->wallTime();
979 TimeMonitor timeMon(*timer);
981 TEUCHOS_TEST_FOR_EXCEPTION(
982 Matrix_.is_null(), std::runtime_error,
983 "Ifpack2::AdditiveSchwarz::"
984 "initialize: The matrix to precondition is null. You must either pass "
985 "a nonnull matrix to the constructor, or call setMatrix() with a nonnull "
986 "input, before you may call this method.");
988 IsInitialized_ =
false;
990 overlapping_B_.reset(
nullptr);
991 overlapping_Y_.reset(
nullptr);
994 reduced_reordered_B_.reset(
nullptr);
995 reduced_reordered_Y_.reset(
nullptr);
996 reduced_B_.reset(
nullptr);
997 reduced_Y_.reset(
nullptr);
998 reordered_B_.reset(
nullptr);
999 reordered_Y_.reset(
nullptr);
1001 RCP<const Teuchos::Comm<int>> comm = Matrix_->getComm();
1002 RCP<const map_type> rowMap = Matrix_->getRowMap();
1003 const global_size_t INVALID =
1004 Teuchos::OrdinalTraits<global_size_t>::invalid();
1008 if (comm->getSize() == 1) {
1010 IsOverlapping_ =
false;
1011 }
else if (OverlapLevel_ != 0) {
1012 IsOverlapping_ =
true;
1015 if (OverlapLevel_ == 0) {
1017 RCP<const SerialComm<int>> localComm(
new SerialComm<int>());
1021 rcp(
new map_type(INVALID, rowMap->getLocalNumElements(),
1022 indexBase, localComm));
1026 if (IsOverlapping_) {
1027 Teuchos::TimeMonitor t(*Teuchos::TimeMonitor::getNewTimer(
"OverlappingRowMatrix construction"));
1034 if (Inverse_.is_null())
return;
1035 const std::string innerName = innerPrecName();
1036 if (innerName.compare(
"RILUK") != 0)
return;
1037 if (Coordinates_ == Teuchos::null)
return;
1040 if (rowMap->getGlobalNumElements() != Coordinates_->getMap()->getGlobalNumElements())
return;
1042 auto ifpack2_Inverse = Teuchos::rcp_dynamic_cast<Ifpack2::Details::LinearSolver<scalar_type, local_ordinal_type, global_ordinal_type, node_type>>(Inverse_);
1043 if (!IsOverlapping_ && !UseReordering_) {
1044 ifpack2_Inverse->setCoord(Coordinates_);
1048 RCP<coord_type> tmp_Coordinates_;
1049 if (IsOverlapping_) {
1050 tmp_Coordinates_ = rcp(
new coord_type(OverlappingMatrix_->getRowMap(), Coordinates_->getNumVectors(),
false));
1051 Tpetra::Import<local_ordinal_type, global_ordinal_type, node_type> importer(Coordinates_->getMap(), tmp_Coordinates_->getMap());
1052 tmp_Coordinates_->doImport(*Coordinates_, importer, Tpetra::INSERT);
1054 tmp_Coordinates_ = rcp(
new coord_type(*Coordinates_, Teuchos::Copy));
1056 if (UseReordering_) {
1057 auto coorDevice = tmp_Coordinates_->getLocalViewDevice(Tpetra::Access::ReadWrite);
1058 auto permDevice = perm_coors.view_device();
1059 Kokkos::View<magnitude_type**, Kokkos::LayoutLeft> tmp_coor(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"tmp_coor"), coorDevice.extent(0), coorDevice.extent(1));
1060 Kokkos::parallel_for(
1061 Kokkos::RangePolicy<typename crs_matrix_type::execution_space>(0,
static_cast<int>(coorDevice.extent(0))), KOKKOS_LAMBDA(
const int& i) {
1062 for (
int j = 0; j < static_cast<int>(coorDevice.extent(1)); j++) {
1063 tmp_coor(permDevice(i), j) = coorDevice(i, j);
1066 Kokkos::deep_copy(coorDevice, tmp_coor);
1068 ifpack2_Inverse->setCoord(tmp_Coordinates_);
1071 if (!Inverse_.is_null()) {
1072 Inverse_->symbolic();
1077 IsInitialized_ =
true;
1080 InitializeTime_ += (timer->wallTime() - startTime);
1083template <
class MatrixType,
class LocalInverseType>
1085 return IsInitialized_;
1088template <
class MatrixType,
class LocalInverseType>
1091 using Teuchos::Time;
1092 using Teuchos::TimeMonitor;
1094 if (!IsInitialized_) {
1098 TEUCHOS_TEST_FOR_EXCEPTION(
1099 !isInitialized(), std::logic_error,
1100 "Ifpack2::AdditiveSchwarz::compute: "
1101 "The preconditioner is not yet initialized, "
1102 "even though initialize() supposedly has been called. "
1103 "This should never happen. "
1104 "Please report this bug to the Ifpack2 developers.");
1106 TEUCHOS_TEST_FOR_EXCEPTION(
1107 Inverse_.is_null(), std::runtime_error,
1108 "Ifpack2::AdditiveSchwarz::compute: The subdomain solver is null. "
1109 "This can only happen if you called setInnerPreconditioner() with a null "
1110 "input, after calling initialize() or compute(). If you choose to call "
1111 "setInnerPreconditioner() with a null input, you must then call it with a "
1112 "nonnull input before you may call initialize() or compute().");
1114 const std::string timerName(
"Ifpack2::AdditiveSchwarz::compute");
1115 RCP<Time> timer = TimeMonitor::lookupCounter(timerName);
1116 if (timer.is_null()) {
1117 timer = TimeMonitor::getNewCounter(timerName);
1119 TimeMonitor timeMon(*timer);
1120 double startTime = timer->wallTime();
1124 if (IsOverlapping_) {
1125 Teuchos::TimeMonitor t(*Teuchos::TimeMonitor::getNewTimer(
"Halo Import"));
1126 OverlappingMatrix_->doExtImport();
1132 Teuchos::TimeMonitor t(*Teuchos::TimeMonitor::getNewTimer(
"Fill Local Matrix"));
1135 asf->updateMatrixValues();
1141 const int rank = Matrix_->getComm()->getRank();
1142 writeLocalMatrixMarketPerRank(innerMatrix_, rank,
"Ifpack2_AdditiveSchwarz_innerMatrix");
1147 IsComputed_ =
false;
1148 Inverse_->numeric();
1154 ComputeTime_ += (timer->wallTime() - startTime);
1159template <
class MatrixType,
class LocalInverseType>
1164template <
class MatrixType,
class LocalInverseType>
1166 return NumInitialize_;
1169template <
class MatrixType,
class LocalInverseType>
1174template <
class MatrixType,
class LocalInverseType>
1179template <
class MatrixType,
class LocalInverseType>
1181 return InitializeTime_;
1184template <
class MatrixType,
class LocalInverseType>
1186 return ComputeTime_;
1189template <
class MatrixType,
class LocalInverseType>
1194template <
class MatrixType,
class LocalInverseType>
1196 std::ostringstream out;
1198 out <<
"\"Ifpack2::AdditiveSchwarz\": {";
1199 if (this->getObjectLabel() !=
"") {
1200 out <<
"Label: \"" << this->getObjectLabel() <<
"\", ";
1202 out <<
"Initialized: " << (isInitialized() ?
"true" :
"false")
1203 <<
", Computed: " << (isComputed() ?
"true" :
"false")
1204 <<
", Iterations: " << NumIterations_
1205 <<
", Overlap level: " << OverlapLevel_
1206 <<
", Subdomain reordering: \"" << ReorderingAlgorithm_ <<
"\"";
1207 out <<
", Combine mode: \"";
1208 if (CombineMode_ == Tpetra::INSERT) {
1210 }
else if (CombineMode_ == Tpetra::ADD) {
1212 }
else if (CombineMode_ == Tpetra::REPLACE) {
1214 }
else if (CombineMode_ == Tpetra::ABSMAX) {
1216 }
else if (CombineMode_ == Tpetra::ZERO) {
1220 if (Matrix_.is_null()) {
1221 out <<
", Matrix: null";
1223 out <<
", Global matrix dimensions: ["
1224 << Matrix_->getGlobalNumRows() <<
", "
1225 << Matrix_->getGlobalNumCols() <<
"]";
1227 out <<
", Inner solver: ";
1228 if (!Inverse_.is_null()) {
1229 Teuchos::RCP<Teuchos::Describable> inv =
1230 Teuchos::rcp_dynamic_cast<Teuchos::Describable>(Inverse_);
1231 if (!inv.is_null()) {
1232 out <<
"{" << inv->description() <<
"}";
1235 <<
"Some inner solver"
1246template <
class MatrixType,
class LocalInverseType>
1248 describe(Teuchos::FancyOStream& out,
1249 const Teuchos::EVerbosityLevel verbLevel)
const {
1251 using Teuchos::OSTab;
1252 using Teuchos::TypeNameTraits;
1254 const int myRank = Matrix_->getComm()->getRank();
1255 const int numProcs = Matrix_->getComm()->getSize();
1256 const Teuchos::EVerbosityLevel vl =
1257 (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
1259 if (vl > Teuchos::VERB_NONE) {
1263 out <<
"\"Ifpack2::AdditiveSchwarz\":";
1267 out <<
"MatrixType: " << TypeNameTraits<MatrixType>::name() << endl;
1268 out <<
"LocalInverseType: " << TypeNameTraits<LocalInverseType>::name() << endl;
1269 if (this->getObjectLabel() !=
"") {
1270 out <<
"Label: \"" << this->getObjectLabel() <<
"\"" << endl;
1273 out <<
"Overlap level: " << OverlapLevel_ << endl
1274 <<
"Combine mode: \"";
1275 if (CombineMode_ == Tpetra::INSERT) {
1277 }
else if (CombineMode_ == Tpetra::ADD) {
1279 }
else if (CombineMode_ == Tpetra::REPLACE) {
1281 }
else if (CombineMode_ == Tpetra::ABSMAX) {
1283 }
else if (CombineMode_ == Tpetra::ZERO) {
1287 <<
"Subdomain reordering: \"" << ReorderingAlgorithm_ <<
"\"" << endl;
1290 if (Matrix_.is_null()) {
1292 out <<
"Matrix: null" << endl;
1296 out <<
"Matrix:" << endl;
1299 Matrix_->getComm()->barrier();
1300 Matrix_->describe(out, Teuchos::VERB_LOW);
1304 out <<
"Number of initialize calls: " << getNumInitialize() << endl
1305 <<
"Number of compute calls: " << getNumCompute() << endl
1306 <<
"Number of apply calls: " << getNumApply() << endl
1307 <<
"Total time in seconds for initialize: " << getInitializeTime() << endl
1308 <<
"Total time in seconds for compute: " << getComputeTime() << endl
1309 <<
"Total time in seconds for apply: " << getApplyTime() << endl;
1312 if (Inverse_.is_null()) {
1314 out <<
"Subdomain solver: null" << endl;
1317 if (vl < Teuchos::VERB_EXTREME) {
1319 auto ifpack2_inverse = Teuchos::rcp_dynamic_cast<Ifpack2::Details::LinearSolver<scalar_type, local_ordinal_type, global_ordinal_type, node_type>>(Inverse_);
1320 if (ifpack2_inverse.is_null())
1321 out <<
"Subdomain solver: not null" << endl;
1323 out <<
"Subdomain solver: ";
1324 ifpack2_inverse->describe(out, Teuchos::VERB_LOW);
1328 for (
int p = 0; p < numProcs; ++p) {
1330 out <<
"Subdomain solver on Process " << myRank <<
":";
1331 if (Inverse_.is_null()) {
1332 out <<
"null" << endl;
1334 Teuchos::RCP<Teuchos::Describable> inv =
1335 Teuchos::rcp_dynamic_cast<Teuchos::Describable>(Inverse_);
1336 if (!inv.is_null()) {
1338 inv->describe(out, vl);
1340 out <<
"null" << endl;
1344 Matrix_->getComm()->barrier();
1345 Matrix_->getComm()->barrier();
1346 Matrix_->getComm()->barrier();
1351 Matrix_->getComm()->barrier();
1355template <
class MatrixType,
class LocalInverseType>
1357 Teuchos::FancyOStream fos(Teuchos::rcp(&os,
false));
1358 fos.setOutputToRootOnly(0);
1363template <
class MatrixType,
class LocalInverseType>
1365 return OverlapLevel_;
1368template <
class MatrixType,
class LocalInverseType>
1371 using Teuchos::MpiComm;
1373 using Teuchos::ArrayRCP;
1374 using Teuchos::ParameterList;
1377 using Teuchos::rcp_dynamic_cast;
1378 using Teuchos::rcpFromRef;
1380 TEUCHOS_TEST_FOR_EXCEPTION(
1381 Matrix_.is_null(), std::runtime_error,
1382 "Ifpack2::AdditiveSchwarz::"
1383 "initialize: The matrix to precondition is null. You must either pass "
1384 "a nonnull matrix to the constructor, or call setMatrix() with a nonnull "
1385 "input, before you may call this method.");
1389 auto matrixCrs = rcp_dynamic_cast<const crs_matrix_type>(Matrix_);
1390 if (!OverlappingMatrix_.is_null() || !matrixCrs.is_null()) {
1391 ArrayRCP<local_ordinal_type> perm;
1392 ArrayRCP<local_ordinal_type> revperm;
1393 if (UseReordering_) {
1394 Teuchos::TimeMonitor t(*Teuchos::TimeMonitor::getNewTimer(
"Reordering"));
1395#if defined(HAVE_IFPACK2_XPETRA) && defined(HAVE_IFPACK2_ZOLTAN2)
1397 Teuchos::ParameterList zlist = List_.sublist(
"schwarz: reordering list");
1398 ReorderingAlgorithm_ = zlist.get<std::string>(
"order_method",
"rcm");
1400 if (ReorderingAlgorithm_ ==
"user") {
1402 perm = zlist.get<Teuchos::ArrayRCP<local_ordinal_type>>(
"user ordering");
1403 revperm = zlist.get<Teuchos::ArrayRCP<local_ordinal_type>>(
"user reverse ordering");
1406 typedef Tpetra::RowGraph<local_ordinal_type, global_ordinal_type, node_type> row_graph_type;
1407 typedef Zoltan2::TpetraRowGraphAdapter<row_graph_type> z2_adapter_type;
1408 auto constActiveGraph = Teuchos::rcp_const_cast<const row_graph_type>(
1409 IsOverlapping_ ? OverlappingMatrix_->getGraph() : Matrix_->getGraph());
1410 z2_adapter_type Zoltan2Graph(constActiveGraph);
1412 typedef Zoltan2::OrderingProblem<z2_adapter_type> ordering_problem_type;
1417 RCP<const MpiComm<int>> mpicomm =
1418 rcp_dynamic_cast<const MpiComm<int>>(Matrix_->getComm());
1419 if (mpicomm == Teuchos::null) {
1420 myRawComm = MPI_COMM_SELF;
1422 myRawComm = *(mpicomm->getRawMpiComm());
1424 ordering_problem_type MyOrderingProblem(&Zoltan2Graph, &zlist, myRawComm);
1426 ordering_problem_type MyOrderingProblem(&Zoltan2Graph, &zlist);
1428 MyOrderingProblem.solve();
1431 typedef Zoltan2::LocalOrderingSolution<local_ordinal_type>
1432 ordering_solution_type;
1434 ordering_solution_type sol(*MyOrderingProblem.getLocalOrderingSolution());
1440 perm = sol.getPermutationRCPConst(
true);
1441 revperm = sol.getPermutationRCPConst();
1447 TEUCHOS_TEST_FOR_EXCEPTION(
1448 true, std::logic_error,
1449 "Ifpack2::AdditiveSchwarz::setup: "
1450 "The Zoltan2 and Xpetra packages must be enabled in order "
1451 "to support reordering.");
1454 local_ordinal_type numLocalRows = OverlappingMatrix_.is_null() ? matrixCrs->getLocalNumRows() : OverlappingMatrix_->getLocalNumRows();
1458 perm = ArrayRCP<local_ordinal_type>(numLocalRows);
1459 revperm = ArrayRCP<local_ordinal_type>(numLocalRows);
1460 for (local_ordinal_type i = 0; i < numLocalRows; i++) {
1468 Teuchos::TimeMonitor t(*Teuchos::TimeMonitor::getNewTimer(
"Filter construction"));
1469 RCP<Details::AdditiveSchwarzFilter<MatrixType>> asf;
1470 if (OverlappingMatrix_.is_null())
1471 asf = rcp(
new Details::AdditiveSchwarzFilter<MatrixType>(matrixCrs, perm, revperm, FilterSingletons_, EquilibrateSubdomainMatrix_));
1473 asf = rcp(
new Details::AdditiveSchwarzFilter<MatrixType>(OverlappingMatrix_, perm, revperm, FilterSingletons_, EquilibrateSubdomainMatrix_));
1477 if (UseReordering_ && (Coordinates_ != Teuchos::null)) {
1478 perm_coors = perm_dualview_type(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"perm_coors"), perm.size());
1479 perm_coors.modify_host();
1480 auto permHost = perm_coors.view_host();
1481 for (local_ordinal_type i = 0; i < static_cast<local_ordinal_type>(perm.size()); i++) {
1482 permHost(i) = perm[i];
1484 perm_coors.sync_device();
1488 RCP<row_matrix_type> LocalizedMatrix;
1492 RCP<row_matrix_type> ActiveMatrix;
1495 if (!OverlappingMatrix_.is_null()) {
1496 LocalizedMatrix = rcp(
new LocalFilter<row_matrix_type>(OverlappingMatrix_));
1498 LocalizedMatrix = rcp(
new LocalFilter<row_matrix_type>(Matrix_));
1502 TEUCHOS_TEST_FOR_EXCEPTION(
1503 LocalizedMatrix.is_null(), std::logic_error,
1504 "Ifpack2::AdditiveSchwarz::setup: LocalizedMatrix is null, after the code "
1505 "that claimed to have created it. This should never be the case. Please "
1506 "report this bug to the Ifpack2 developers.");
1509 ActiveMatrix = LocalizedMatrix;
1512 if (FilterSingletons_) {
1513 SingletonMatrix_ = rcp(
new SingletonFilter<row_matrix_type>(LocalizedMatrix));
1514 ActiveMatrix = SingletonMatrix_;
1518 if (UseReordering_) {
1519#if defined(HAVE_IFPACK2_XPETRA) && defined(HAVE_IFPACK2_ZOLTAN2)
1521 typedef ReorderFilter<row_matrix_type> reorder_filter_type;
1522 Teuchos::ParameterList zlist = List_.sublist(
"schwarz: reordering list");
1523 ReorderingAlgorithm_ = zlist.get<std::string>(
"order_method",
"rcm");
1525 ArrayRCP<local_ordinal_type> perm;
1526 ArrayRCP<local_ordinal_type> revperm;
1528 if (ReorderingAlgorithm_ ==
"user") {
1530 perm = zlist.get<Teuchos::ArrayRCP<local_ordinal_type>>(
"user ordering");
1531 revperm = zlist.get<Teuchos::ArrayRCP<local_ordinal_type>>(
"user reverse ordering");
1534 typedef Tpetra::RowGraph<local_ordinal_type, global_ordinal_type, node_type> row_graph_type;
1535 typedef Zoltan2::TpetraRowGraphAdapter<row_graph_type> z2_adapter_type;
1536 RCP<const row_graph_type> constActiveGraph =
1537 Teuchos::rcp_const_cast<const row_graph_type>(ActiveMatrix->getGraph());
1538 z2_adapter_type Zoltan2Graph(constActiveGraph);
1540 typedef Zoltan2::OrderingProblem<z2_adapter_type> ordering_problem_type;
1545 RCP<const MpiComm<int>> mpicomm =
1546 rcp_dynamic_cast<const MpiComm<int>>(ActiveMatrix->getComm());
1547 if (mpicomm == Teuchos::null) {
1548 myRawComm = MPI_COMM_SELF;
1550 myRawComm = *(mpicomm->getRawMpiComm());
1552 ordering_problem_type MyOrderingProblem(&Zoltan2Graph, &zlist, myRawComm);
1554 ordering_problem_type MyOrderingProblem(&Zoltan2Graph, &zlist);
1556 MyOrderingProblem.solve();
1559 typedef Zoltan2::LocalOrderingSolution<local_ordinal_type>
1560 ordering_solution_type;
1562 ordering_solution_type sol(*MyOrderingProblem.getLocalOrderingSolution());
1568 perm = sol.getPermutationRCPConst(
true);
1569 revperm = sol.getPermutationRCPConst();
1573 ReorderedLocalizedMatrix_ = rcp(
new reorder_filter_type(ActiveMatrix, perm, revperm));
1575 ActiveMatrix = ReorderedLocalizedMatrix_;
1579 TEUCHOS_TEST_FOR_EXCEPTION(
1580 true, std::logic_error,
1581 "Ifpack2::AdditiveSchwarz::setup: "
1582 "The Zoltan2 and Xpetra packages must be enabled in order "
1583 "to support reordering.");
1586 innerMatrix_ = ActiveMatrix;
1589 TEUCHOS_TEST_FOR_EXCEPTION(
1590 innerMatrix_.is_null(), std::logic_error,
1591 "Ifpack2::AdditiveSchwarz::"
1592 "setup: Inner matrix is null right before constructing inner solver. "
1593 "Please report this bug to the Ifpack2 developers.");
1596 if (Inverse_.is_null()) {
1597 const std::string innerName = innerPrecName();
1598 TEUCHOS_TEST_FOR_EXCEPTION(
1599 innerName ==
"INVALID", std::logic_error,
1600 "Ifpack2::AdditiveSchwarz::initialize: AdditiveSchwarz doesn't "
1601 "know how to create an instance of your LocalInverseType \""
1602 << Teuchos::TypeNameTraits<LocalInverseType>::name() <<
"\". "
1603 "Please talk to the Ifpack2 developers for details.");
1605 TEUCHOS_TEST_FOR_EXCEPTION(
1606 innerName ==
"CUSTOM", std::runtime_error,
1607 "Ifpack2::AdditiveSchwarz::"
1608 "initialize: If the \"inner preconditioner name\" parameter (or any "
1609 "alias thereof) has the value \"CUSTOM\", then you must first call "
1610 "setInnerPreconditioner with a nonnull inner preconditioner input before "
1611 "you may call initialize().");
1615 if (!Trilinos::Details::Impl::registeredSomeLinearSolverFactory(
"Ifpack2")) {
1621 typedef typename MV::mag_type MT;
1622 RCP<inner_solver_type> innerPrec =
1623 Trilinos::Details::getLinearSolver<MV, OP, MT>(
"Ifpack2", innerName);
1624 TEUCHOS_TEST_FOR_EXCEPTION(
1625 innerPrec.is_null(), std::logic_error,
1626 "Ifpack2::AdditiveSchwarz::setup: Failed to create inner preconditioner "
1628 << innerName <<
"\".");
1629 innerPrec->setMatrix(innerMatrix_);
1633 std::pair<Teuchos::ParameterList, bool> result = innerPrecParams();
1634 if (result.second) {
1637 innerPrec->setParameters(rcp(
new ParameterList(result.first)));
1639 Inverse_ = innerPrec;
1640 }
else if (Inverse_->getMatrix().getRawPtr() != innerMatrix_.getRawPtr()) {
1644 Inverse_->setMatrix(innerMatrix_);
1646 TEUCHOS_TEST_FOR_EXCEPTION(
1647 Inverse_.is_null(), std::logic_error,
1648 "Ifpack2::AdditiveSchwarz::"
1649 "setup: Inverse_ is null right after we were supposed to have created it."
1650 " Please report this bug to the Ifpack2 developers.");
1659template <
class MatrixType,
class LocalInverseType>
1665 if (!innerPrec.is_null()) {
1668 can_change_type* innerSolver =
dynamic_cast<can_change_type*
>(&*innerPrec);
1669 TEUCHOS_TEST_FOR_EXCEPTION(
1670 innerSolver == NULL, std::invalid_argument,
1671 "Ifpack2::AdditiveSchwarz::"
1672 "setInnerPreconditioner: The input preconditioner does not implement the "
1673 "setMatrix() feature. Only input preconditioners that inherit from "
1674 "Ifpack2::Details::CanChangeMatrix implement this feature.");
1692 innerSolver->setMatrix(asf->getFilteredMatrix());
1694 innerSolver->setMatrix(innerMatrix_);
1704 removeInnerPrecName();
1705 removeInnerPrecParams();
1706 List_.set(
"inner preconditioner name",
"CUSTOM");
1710 if (isInitialized()) {
1711 innerPrec->initialize();
1714 innerPrec->compute();
1727 inner_solver_impl_type;
1728 Inverse_ = Teuchos::rcp(
new inner_solver_impl_type(innerPrec,
"CUSTOM"));
1731template <
class MatrixType,
class LocalInverseType>
1733 setMatrix(
const Teuchos::RCP<const row_matrix_type>& A) {
1735 if (A.getRawPtr() != Matrix_.getRawPtr()) {
1736 IsInitialized_ =
false;
1737 IsComputed_ =
false;
1740 OverlappingMatrix_ = Teuchos::null;
1741 ReorderedLocalizedMatrix_ = Teuchos::null;
1742 innerMatrix_ = Teuchos::null;
1743 SingletonMatrix_ = Teuchos::null;
1744 localMap_ = Teuchos::null;
1745 overlapping_B_.reset(
nullptr);
1746 overlapping_Y_.reset(
nullptr);
1749 DistributedImporter_ = Teuchos::null;
1755template <
class MatrixType,
class LocalInverseType>
1757 setCoord(
const Teuchos::RCP<const coord_type>& Coordinates) {
1759 if (Coordinates.getRawPtr() != Coordinates_.getRawPtr()) {
1760 Coordinates_ = Coordinates;
1769#define IFPACK2_ADDITIVESCHWARZ_INSTANT(S, LO, GO, N) \
1770 template class Ifpack2::AdditiveSchwarz<Tpetra::RowMatrix<S, LO, GO, N>>;
Declaration of Ifpack2::AdditiveSchwarz, which implements additive Schwarz preconditioning with an ar...
void registerLinearSolverFactory()
Register Ifpack2's LinearSolverFactory with the central repository, for all enabled combinations of t...
Definition Ifpack2_Details_registerLinearSolverFactory.cpp:33
Declaration of Ifpack2::Details::Behavior, a class that describes Ifpack2's run-time behavior.
Declaration of interface for preconditioners that can change their matrix after construction.
Additive Schwarz domain decomposition for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:260
typename MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:283
std::string description() const
Return a simple one-line description of this object.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1195
virtual bool isInitialized() const
Returns true if the preconditioner has been successfully initialized, false otherwise.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1084
void setCoord(const Teuchos::RCP< const coord_type > &Coordinates)
Set the matrix rows' coordinates.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1757
virtual int getOverlapLevel() const
Returns the level of overlap.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1364
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a list of the preconditioner's default parameters.
Definition Ifpack2_AdditiveSchwarz_def.hpp:917
typename MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:286
virtual int getNumCompute() const
Returns the number of calls to compute().
Definition Ifpack2_AdditiveSchwarz_def.hpp:1170
Teuchos::RCP< const coord_type > getCoord() const
Get the coordinates associated with the input matrix's rows.
Definition Ifpack2_AdditiveSchwarz_def.hpp:289
virtual int getNumApply() const
Returns the number of calls to apply().
Definition Ifpack2_AdditiveSchwarz_def.hpp:1175
virtual double getComputeTime() const
Returns the time spent in compute().
Definition Ifpack2_AdditiveSchwarz_def.hpp:1185
virtual double getApplyTime() const
Returns the time spent in apply().
Definition Ifpack2_AdditiveSchwarz_def.hpp:1190
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Change the matrix to be preconditioned.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1733
typename MatrixType::node_type node_type
The Node type used by the input MatrixType.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:289
virtual void initialize()
Computes all (graph-related) data necessary to initialize the preconditioner.
Definition Ifpack2_AdditiveSchwarz_def.hpp:963
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
Set the preconditioner's parameters.
Definition Ifpack2_AdditiveSchwarz_def.hpp:723
virtual void setInnerPreconditioner(const Teuchos::RCP< Preconditioner< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > &innerPrec)
Set the inner preconditioner.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1661
virtual double getInitializeTime() const
Returns the time spent in initialize().
Definition Ifpack2_AdditiveSchwarz_def.hpp:1180
virtual Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > getRangeMap() const
The range Map of this operator.
Definition Ifpack2_AdditiveSchwarz_def.hpp:273
virtual Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > getDomainMap() const
The domain Map of this operator.
Definition Ifpack2_AdditiveSchwarz_def.hpp:261
virtual void compute()
Computes all (coefficient) data necessary to apply the preconditioner.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1089
Tpetra::MultiVector< magnitude_type, local_ordinal_type, global_ordinal_type, node_type > coord_type
The Tpetra::MultiVector specialization used for containing coordinates.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:308
virtual std::ostream & print(std::ostream &os) const
Prints basic information on iostream. This function is used by operator<<.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1356
typename MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:280
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1248
virtual void setParameters(const Teuchos::ParameterList &plist)
Set the preconditioner's parameters.
Definition Ifpack2_AdditiveSchwarz_def.hpp:713
virtual Teuchos::RCP< const row_matrix_type > getMatrix() const
The input matrix.
Definition Ifpack2_AdditiveSchwarz_def.hpp:284
virtual 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, putting the result in Y.
Definition Ifpack2_AdditiveSchwarz_def.hpp:320
virtual bool isComputed() const
Returns true if the preconditioner has been successfully computed, false otherwise.
Definition Ifpack2_AdditiveSchwarz_def.hpp:1160
virtual int getNumInitialize() const
Returns the number of calls to initialize().
Definition Ifpack2_AdditiveSchwarz_def.hpp:1165
AdditiveSchwarz(const Teuchos::RCP< const row_matrix_type > &A)
Constructor that takes a matrix.
Definition Ifpack2_AdditiveSchwarz_def.hpp:241
static bool writeAdditiveSchwarzLocalMatrix()
Whether to write the AdditiveSchwarz local matrix.
Definition Ifpack2_Details_Behavior.cpp:40
static bool debug()
Whether Ifpack2 is in debug mode.
Definition Ifpack2_Details_Behavior.cpp:31
Ifpack2's implementation of Trilinos::Details::LinearSolver interface.
Definition Ifpack2_Details_LinearSolver_decl.hpp:75
Sparse matrix (Tpetra::RowMatrix subclass) with ghost rows.
Definition Ifpack2_OverlappingRowMatrix_decl.hpp:25
Interface for all Ifpack2 preconditioners.
Definition Ifpack2_Preconditioner.hpp:74
void registerLinearSolverFactory()
Ifpack2 implementation details.
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:40
void getValidParameters(Teuchos::ParameterList ¶ms)
Fills a list which contains all the parameters possibly used by Ifpack2.
Definition Ifpack2_Parameters.cpp:18