10#ifndef MUELU_INVERSEAPPROXIMATIONFACTORY_DEF_HPP_
11#define MUELU_INVERSEAPPROXIMATIONFACTORY_DEF_HPP_
13#include <Xpetra_BlockedCrsMatrix.hpp>
14#include <Xpetra_CrsGraph.hpp>
15#include <Xpetra_CrsMatrixWrap.hpp>
16#include <Xpetra_CrsMatrix.hpp>
17#include <Xpetra_VectorFactory.hpp>
18#include <Xpetra_MatrixFactory.hpp>
19#include <Xpetra_Matrix.hpp>
21#include "Kokkos_Sort.hpp"
22#include "KokkosBlas1_set.hpp"
23#include "KokkosBatched_QR_Decl.hpp"
24#include "KokkosBatched_ApplyQ_Decl.hpp"
25#include "KokkosBatched_Trsv_Decl.hpp"
26#include "KokkosBatched_Util.hpp"
27#include <KokkosKernels_SimpleUtils.hpp>
31#include "MueLu_Utilities.hpp"
34#if KOKKOSKERNELS_VERSION < 50200
35#include "Teuchos_SerialDenseVector.hpp"
36#include "Teuchos_SerialDenseMatrix.hpp"
37#include "Teuchos_SerialQRDenseSolver.hpp"
42template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
44 RCP<ParameterList> validParamList = rcp(
new ParameterList());
45 using Magnitude =
typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
47 validParamList->set<RCP<const FactoryBase>>(
"A",
NoFactory::getRCP(),
"Matrix to build the approximate inverse on.\n");
49 validParamList->set<std::string>(
"inverse: approximation type",
"diagonal",
"Method used to approximate the inverse.");
50 validParamList->set<Magnitude>(
"inverse: drop tolerance", 0.0,
"Values below this threshold are dropped from the matrix (or fixed if diagonal fixing is active).");
51 validParamList->set<
bool>(
"inverse: fixing",
false,
"Keep diagonal and fix small entries with 1.0. Only used when sparse inverse method is either diagonal or lumping.");
53 return validParamList;
56template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
58 Input(currentLevel,
"A");
61template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
65 using STS = Teuchos::ScalarTraits<SC>;
66 const SC one = STS::one();
67 using Magnitude =
typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
70 const bool fixing = pL.get<
bool>(
"inverse: fixing");
73 const std::string method = pL.get<std::string>(
"inverse: approximation type");
74 TEUCHOS_TEST_FOR_EXCEPTION(method !=
"diagonal" && method !=
"lumping" && method !=
"sparseapproxinverse" && method !=
"factoredsparseapproxinverse",
Exceptions::RuntimeError,
75 "MueLu::InverseApproximationFactory::Build: Approximation type can be 'diagonal' or 'lumping' or "
76 "'sparseapproxinverse' or 'factoredsparseapproxinverse'.");
79 RCP<BlockedCrsMatrix> bA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A);
80 const bool isBlocked = (bA == Teuchos::null ? false :
true);
83 if (isBlocked) A = bA->getMatrix(0, 0);
85 Magnitude tol = pL.get<Magnitude>(
"inverse: drop tolerance");
86 RCP<Matrix> Ainv = Teuchos::null;
88 if (method ==
"diagonal") {
89 const auto diag = VectorFactory::Build(A->getRangeMap(),
true);
90 A->getLocalDiagCopy(*diag);
92 Ainv = MatrixFactory::Build(D);
93 }
else if (method ==
"lumping") {
96 Ainv = MatrixFactory::Build(D);
97#if KOKKOSKERNELS_VERSION >= 50200
98 }
else if (method ==
"sparseapproxinverse") {
100 }
else if ((method ==
"sparseapproxinverse") || (method ==
"factoredsparseapproxinverse")) {
101 if (method ==
"factoredsparseapproxinverse")
102 GetOStream(
Warnings) <<
"Method \"factoredsparseapproxinverse\" requires KokkosKernels >= 5.2. Falling back to \"sparseapproxinverse\".";
106 sparsityPattern->computeGlobalConstants();
107 GetOStream(
Statistics1) <<
"NNZ Graph(A): " << A->getCrsGraph()->getGlobalNumEntries() <<
" , NNZ Tresholded Graph(A): " << sparsityPattern->getGlobalNumEntries() << std::endl;
110 if (tol != STS::magnitude(STS::zero()))
115 rcp_const_cast<CrsGraph>(Ainv->getCrsGraph())->computeGlobalConstants();
116 GetOStream(
Statistics1) <<
"NNZ Ainv: " << pAinv->getGlobalNumEntries() <<
", NNZ Tresholded Ainv (parameter: " << tol <<
"): " << Ainv->getGlobalNumEntries() << std::endl;
119#if KOKKOSKERNELS_VERSION >= 50200
120 else if (method ==
"factoredsparseapproxinverse") {
123 sparsityPattern->computeGlobalConstants();
124 GetOStream(
Statistics1) <<
"NNZ Graph(A): " << A->getCrsGraph()->getGlobalNumEntries() <<
" , NNZ Tresholded Graph(triLower(A)): " << sparsityPattern->getGlobalNumEntries() << std::endl;
126 RCP<Matrix> pLinvFactor = GetFactoredSparseInverse(A, sparsityPattern);
127 RCP<Matrix> LinvFactor;
128 if (tol != STS::magnitude(STS::zero()))
131 LinvFactor = pLinvFactor;
139 Ainv = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*LinvTrans,
false, *LinvFactor,
false,
GetOStream(
Statistics2),
true,
true, std::string(
"Ainv"));
142 rcp_const_cast<CrsGraph>(Ainv->getCrsGraph())->computeGlobalConstants();
143 GetOStream(
Statistics1) <<
"NNZ Linv: " << LinvFactor->getGlobalNumEntries() <<
", NNZ Tresholded Linv (parameter: " << tol <<
"): " << pLinvFactor->getGlobalNumEntries() << std::endl;
149 GetOStream(
Statistics1) <<
"Ainv has " << Ainv->getGlobalNumRows() <<
"x" << Ainv->getGlobalNumCols() <<
" rows and columns." << std::endl;
151 Set(currentLevel,
"Ainv", Ainv);
154#if KOKKOSKERNELS_VERSION >= 50200
156template <
class local_matrix_type>
157class LocalSPAIFunctor {
159 using scalar_type =
typename local_matrix_type::value_type;
160 using local_ordinal_type =
typename local_matrix_type::ordinal_type;
161 using execution_space =
typename local_matrix_type::execution_space;
162 using impl_scalar_type =
typename KokkosKernels::ArithTraits<scalar_type>::val_type;
163 using impl_ATS = KokkosKernels::ArithTraits<impl_scalar_type>;
166 using shared_matrix = Kokkos::View<impl_scalar_type**, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged>;
167 using shared_vector = Kokkos::View<impl_scalar_type*, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged>;
168 using shared_lo_vector = Kokkos::View<local_ordinal_type*, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged>;
171 const local_matrix_type lclA;
172 local_matrix_type lclAinv;
173 const local_ordinal_type maxUniqueColEntries;
174 const int scratchLevel;
177 LocalSPAIFunctor(
const local_matrix_type& lclA_, local_matrix_type& lclAinv_, local_ordinal_type maxUniqueColEntries_,
int scratchLevel_)
180 , maxUniqueColEntries(maxUniqueColEntries_)
181 , scratchLevel(scratchLevel_) {}
183 KOKKOS_INLINE_FUNCTION
184 void operator()(
const typename Kokkos::TeamPolicy<execution_space>::member_type& thread)
const {
185 auto rlid = thread.league_rank();
186 auto rowAinv = lclAinv.row(rlid);
189 shared_lo_vector column_indices(thread.team_scratch(scratchLevel), maxUniqueColEntries);
190 local_ordinal_type numColEntries = 0;
191 for (local_ordinal_type ii = 0; ii < rowAinv.length; ++ii) {
192 auto i = rowAinv.colidx(ii);
193 auto rowA = lclA.rowConst(i);
194 for (local_ordinal_type jj = 0; jj < rowA.length; ++jj) {
195 auto j = rowA.colidx(jj);
196 column_indices(numColEntries) = j;
202 local_ordinal_type numUniqeColEntries = 0;
203 local_ordinal_type diagOffset = 0;
206 Kokkos::Experimental::sort_thread(thread, Kokkos::subview(column_indices, Kokkos::make_pair(0, numColEntries)));
208 if (numColEntries > 0)
209 ++numUniqeColEntries;
210 local_ordinal_type pos = 0;
211 for (local_ordinal_type m = 1; m < numColEntries; ++m) {
212 if (column_indices(pos) != column_indices(m)) {
213 column_indices(pos + 1) = column_indices(m);
215 ++numUniqeColEntries;
216 if (column_indices(pos) == rlid)
224 shared_lo_vector uniqueColIndicies(thread.team_scratch(scratchLevel), numUniqeColEntries);
225 for (local_ordinal_type m = 0; m < numUniqeColEntries; ++m) {
226 uniqueColIndicies(m) = column_indices(m);
230 shared_matrix localA(thread.team_scratch(scratchLevel), numUniqeColEntries, rowAinv.length);
231 KokkosBlas::SerialSet::invoke(impl_ATS::zero(), localA);
234 for (local_ordinal_type ii = 0; ii < rowAinv.length; ++ii) {
235 auto i = rowAinv.colidx(ii);
236 auto rowA = lclA.rowConst(i);
237 for (local_ordinal_type jj = 0; jj < rowA.length; ++jj) {
238 auto j = rowA.colidx(jj);
239 auto v = rowA.value(jj);
242 auto it = KokkosKernels::lower_bound_thread(uniqueColIndicies, j);
247 shared_matrix ek(thread.team_scratch(scratchLevel), numUniqeColEntries, 1);
249 for (local_ordinal_type i = 0; i < numUniqeColEntries; ++i) {
250 ek(i, 0) = (i == diagOffset) ? impl_ATS::one() : impl_ATS::zero();
254 shared_vector tau(thread.team_scratch(scratchLevel), rowAinv.length);
255 shared_vector work(thread.team_scratch(scratchLevel), numUniqeColEntries);
257 KokkosBatched::SerialQR<KokkosBatched::Algo::QR::Unblocked>::invoke(localA, tau, work);
259 KokkosBatched::SerialApplyQ<KokkosBatched::Side::Left, KokkosBatched::Trans::Transpose, KokkosBatched::Algo::ApplyQ::Unblocked>::invoke(localA, tau, ek, work);
261 auto sub_A = Kokkos::subview(localA, Kokkos::make_pair(0, rowAinv.length), Kokkos::ALL());
262 auto sub_ek = Kokkos::subview(ek, Kokkos::make_pair(0, rowAinv.length), 0);
263 KokkosBatched::SerialTrsv<KokkosBatched::Uplo::Upper, KokkosBatched::Trans::NoTranspose, KokkosBatched::Diag::NonUnit, KokkosBatched::Algo::Trsv::Unblocked>::invoke(impl_ATS::one(), sub_A, sub_ek);
266 for (local_ordinal_type i = 0; i < rowAinv.length; ++i) {
267 rowAinv.value(i) = sub_ek(i);
272template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
273RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
275 using execution_space =
typename Node::execution_space;
278 RCP<Matrix> Ainv = MatrixFactory::Build(sparsityPattern);
282 RCP<Import> rowImport = ImportFactory::Build(sparsityPattern->getRowMap(), sparsityPattern->getColMap());
283 RCP<Matrix> A = MatrixFactory::Build(Aorg, *rowImport);
285 auto maxRowEntriesAinv = Ainv->getLocalMaxNumRowEntries();
286 auto maxRowEntriesA = A->getLocalMaxNumRowEntries();
287 auto maxUniqueColEntries = maxRowEntriesAinv * maxRowEntriesA;
289 auto lclA = A->getLocalMatrixDevice();
290 auto lclAinv = Ainv->getLocalMatrixDevice();
292 Kokkos::TeamPolicy<execution_space> policy(lclAinv.numRows(), 1);
294 using spai_functor_type = LocalSPAIFunctor<
decltype(lclAinv)>;
295 using shared_matrix =
typename spai_functor_type::shared_matrix;
296 using shared_vector =
typename spai_functor_type::shared_vector;
297 using shared_lo_vector =
typename spai_functor_type::shared_lo_vector;
299 int size = shared_matrix::shmem_size(maxUniqueColEntries, maxRowEntriesAinv) + shared_matrix::shmem_size(maxUniqueColEntries, 1) + shared_vector::shmem_size(3 * maxUniqueColEntries) + shared_vector::shmem_size(maxRowEntriesAinv) + shared_lo_vector::shmem_size(maxUniqueColEntries);
301 int scratchLevel = -1;
302 if (size < policy.scratch_size_max((
int)0)) {
303 policy.set_scratch_size((
int)0, Kokkos::PerTeam(size));
305 }
else if (size < policy.scratch_size_max((
int)1)) {
306 policy.set_scratch_size((
int)1, Kokkos::PerTeam(size));
309 throw Exceptions::RuntimeError(
"Neither L0 scratch memory (max size " + std::to_string(policy.scratch_size_max((
int)0)) +
310 "), nor L1 scratch memory (max size " + std::to_string(policy.scratch_size_max((
int)1)) +
311 ") is large enough for requested allocation of size " + std::to_string(size));
313 LocalSPAIFunctor spaiFunctor(lclA, lclAinv, maxUniqueColEntries, scratchLevel);
315 Kokkos::parallel_for(
"MueLu::InverseFactory::LocalSpai", policy, spaiFunctor);
318 Ainv->fillComplete();
327template <
class local_matrix_type,
typename global_ordinal_type>
328class LocalFSAIFunctor {
330 using scalar_type =
typename local_matrix_type::value_type;
331 using local_ordinal_type =
typename local_matrix_type::ordinal_type;
332 using execution_space =
typename local_matrix_type::execution_space;
333 using impl_scalar_type =
typename KokkosKernels::ArithTraits<scalar_type>::val_type;
334 using impl_ATS = KokkosKernels::ArithTraits<impl_scalar_type>;
335 using device_type =
typename local_matrix_type::device_type;
336 using local_map_type = Tpetra::Details::LocalMap<local_ordinal_type, global_ordinal_type, device_type>;
339 using shared_matrix = Kokkos::View<impl_scalar_type**, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged>;
340 using shared_vector = Kokkos::View<impl_scalar_type*, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged>;
341 using shared_lo_vector = Kokkos::View<local_ordinal_type*, typename execution_space::scratch_memory_space, Kokkos::MemoryUnmanaged>;
344 const local_matrix_type lclA;
345 local_matrix_type lclAinv;
346 local_map_type lclARowMap;
347 local_map_type lclAColMap;
348 local_map_type lclAinvRowMap;
349 local_map_type lclAinvColMap;
350 const int scratchLevel;
353 LocalFSAIFunctor(
const local_matrix_type& lclA_, local_matrix_type& lclAinv_,
const local_map_type& lclARowMap_,
const local_map_type& lclAColMap_,
354 const local_map_type& lclAinvRowMap_,
const local_map_type& lclAinvColMap_,
int scratchLevel_)
357 , lclARowMap(lclARowMap_)
358 , lclAColMap(lclAColMap_)
359 , lclAinvRowMap(lclAinvRowMap_)
360 , lclAinvColMap(lclAinvColMap_)
361 , scratchLevel(scratchLevel_) {}
363 KOKKOS_INLINE_FUNCTION
364 void operator()(
const typename Kokkos::TeamPolicy<execution_space>::member_type& thread)
const {
365 auto rlid = thread.league_rank();
366 auto rowAinv = lclAinv.row(rlid);
389 auto A_rowGid = lclARowMap.getGlobalElement(rlid);
391 auto numRowEntries = rowAinv.length;
392 local_ordinal_type diagOffset = -1;
393 scalar_type diagValue = 0.0;
394 local_ordinal_type A_lclRowIndForDiag = -1;
397 shared_lo_vector column_indices(thread.team_scratch(scratchLevel), numRowEntries);
398 local_ordinal_type numColEntries = rowAinv.length;
399 for (local_ordinal_type ii = 0; ii < rowAinv.length; ++ii) {
400 auto i = rowAinv.colidx(ii);
401 auto Ainv_colGid = lclAinvColMap.getGlobalElement(i);
402 local_ordinal_type A_lclRowInd = lclARowMap.getLocalElement(Ainv_colGid);
403 local_ordinal_type A_lclColInd = lclAColMap.getLocalElement(Ainv_colGid);
404 column_indices(ii) = A_lclColInd;
405 if (A_rowGid == Ainv_colGid) {
406 A_lclRowIndForDiag = A_lclRowInd;
409#ifdef HAVE_MUELU_DEBUG
410 KOKKOS_ASSERT(A_lclRowIndForDiag != -1 &&
"MueLu::InverseApproximationFactory::GetSparseInverse: no diagonal entry found in A.");
413 Kokkos::Experimental::sort_thread(thread, column_indices);
414 for (
int kkk = 0; kkk < numColEntries; kkk++) {
415 if (column_indices(kkk) == A_lclRowIndForDiag) diagOffset = kkk;
417#ifdef HAVE_MUELU_DEBUG
418 KOKKOS_ASSERT(diagOffset != -1 &&
"MueLu::InverseApproximationFactory::GetSparseInverse: no diagonal entry offset found in A.");
422 shared_matrix localA(thread.team_scratch(scratchLevel), numRowEntries, rowAinv.length);
423 KokkosBlas::SerialSet::invoke(impl_ATS::zero(), localA);
426 for (local_ordinal_type ii = 0; ii < rowAinv.length; ++ii) {
427 auto i = rowAinv.colidx(ii);
428 auto Ainv_colGid = lclAinvColMap.getGlobalElement(i);
429 local_ordinal_type A_lclRowInd = lclARowMap.getLocalElement(Ainv_colGid);
430#ifdef HAVE_MUELU_DEBUG
431 KOKKOS_ASSERT(A_lclRowInd != -1 &&
"MueLu::InverseApproximationFactory: Column global ID in Ainv not found in A rowmap");
433 auto rowA = lclA.rowConst(A_lclRowInd);
435 for (local_ordinal_type jj = 0; jj < rowA.length; ++jj) {
436 auto j = rowA.colidx(jj);
439 auto A_colGid = lclAColMap.getGlobalElement(j);
440 if (A_colGid <= A_rowGid) {
441 auto newIndex = KokkosKernels::lower_bound_thread(column_indices, j);
442 if ((newIndex < column_indices.extent(0)) && (column_indices(newIndex) == j))
443 localA(newIndex, ii) = rowA.value(jj);
447 shared_matrix ek(thread.team_scratch(scratchLevel), numRowEntries, 1);
449 for (local_ordinal_type i = 0; i < numRowEntries; ++i) {
450 ek(i, 0) = (i == diagOffset) ? impl_ATS::one() : impl_ATS::zero();
454 shared_vector tau(thread.team_scratch(scratchLevel), rowAinv.length);
455 shared_vector work(thread.team_scratch(scratchLevel), numRowEntries);
459 KokkosBatched::SerialQR<KokkosBatched::Algo::QR::Unblocked>::invoke(localA, tau, work);
461 KokkosBatched::SerialCholesky<KokkosBatched::Uplo::Lower, KokkosBatched::Algo::Cholesky::Unblocked>::invoke(localA);
465 KokkosBatched::SerialApplyQ<KokkosBatched::Side::Left, KokkosBatched::Trans::Transpose, KokkosBatched::Algo::ApplyQ::Unblocked>::invoke(localA, tau, ek, work);
467 auto sub_A = Kokkos::subview(localA, Kokkos::make_pair(0, rowAinv.length), Kokkos::ALL());
469 auto sub_A = Kokkos::subview(localA, Kokkos::make_pair(0, rowAinv.length), Kokkos::make_pair(0, rowAinv.length));
471 auto sub_ek = Kokkos::subview(ek, Kokkos::make_pair(0, rowAinv.length), 0);
473 KokkosBatched::SerialTrsv<KokkosBatched::Uplo::Upper, KokkosBatched::Trans::NoTranspose, KokkosBatched::Diag::NonUnit, KokkosBatched::Algo::Trsv::Unblocked>::invoke(impl_ATS::one(), sub_A, sub_ek);
475 KokkosBatched::SerialTrsv<KokkosBatched::Uplo::Lower, KokkosBatched::Trans::NoTranspose, KokkosBatched::Diag::NonUnit, KokkosBatched::Algo::Trsv::Unblocked>::invoke(impl_ATS::one(), sub_A, sub_ek);
476 KokkosBatched::SerialTrsv<KokkosBatched::Uplo::Lower, KokkosBatched::Trans::Transpose, KokkosBatched::Diag::NonUnit, KokkosBatched::Algo::Trsv::Unblocked>::invoke(impl_ATS::one(), sub_A, sub_ek);
481 diagValue = sub_ek(diagOffset);
482#ifdef HAVE_MUELU_DEBUG
483 KOKKOS_ASSERT(impl_ATS::real(diagValue) > 0.0 &&
"MueLu::InverseApproximationFactory::GetSparseInverse: non positive diagonal entry.");
485 auto scale_factor = impl_ATS::one() / impl_ATS::sqrt(diagValue);
486 for (local_ordinal_type i = 0; i < rowAinv.length; ++i) {
487 typename KokkosKernels::ArithTraits<
decltype(diagValue)>::val_type thevalue = sub_ek(i) * scale_factor;
489 if (thevalue == impl_ATS::zero()) thevalue = impl_ATS::eps();
490 rowAinv.value(i) = thevalue;
495template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
496RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
498 using execution_space =
typename Node::execution_space;
501 RCP<Matrix> Ainv = MatrixFactory::Build(sparsityPattern);
505 RCP<Import> rowImport = ImportFactory::Build(sparsityPattern->getRowMap(), sparsityPattern->getColMap());
506 RCP<Matrix> A = MatrixFactory::Build(Aorg, *rowImport);
508 auto maxRowEntriesAinv = Ainv->getLocalMaxNumRowEntries();
510 auto lclA = A->getLocalMatrixDevice();
511 auto lclAinv = Ainv->getLocalMatrixDevice();
512 auto lclARowmap = A->getRowMap()->getLocalMap();
513 auto lclAColmap = A->getColMap()->getLocalMap();
514 auto lclAinvRowmap = Ainv->getRowMap()->getLocalMap();
515 auto lclAinvColmap = Ainv->getColMap()->getLocalMap();
516 auto lclAorgRowmap = Aorg->getRowMap()->getLocalMap();
518 Kokkos::TeamPolicy<execution_space> policy(lclAinv.numRows(), 1);
520 using fsai_functor_type = LocalFSAIFunctor<
decltype(lclAinv),
GlobalOrdinal>;
521 using shared_matrix =
typename fsai_functor_type::shared_matrix;
522 using shared_vector =
typename fsai_functor_type::shared_vector;
523 using shared_lo_vector =
typename fsai_functor_type::shared_lo_vector;
525 int size = shared_matrix::shmem_size(maxRowEntriesAinv, maxRowEntriesAinv) + shared_matrix::shmem_size(maxRowEntriesAinv, 1) + shared_vector::shmem_size(3 * maxRowEntriesAinv) + shared_vector::shmem_size(maxRowEntriesAinv) + shared_lo_vector::shmem_size(maxRowEntriesAinv);
527 int scratchLevel = -1;
528 if (size < policy.scratch_size_max((
int)0)) {
529 policy.set_scratch_size((
int)0, Kokkos::PerTeam(size));
531 }
else if (size < policy.scratch_size_max((
int)1)) {
532 policy.set_scratch_size((
int)1, Kokkos::PerTeam(size));
535 throw Exceptions::RuntimeError(
"Neither L0 scratch memory (max size " + std::to_string(policy.scratch_size_max((
int)0)) +
536 "), nor L1 scratch memory (max size " + std::to_string(policy.scratch_size_max((
int)1)) +
537 ") is large enough for requested allocation of size " + std::to_string(size));
539 LocalFSAIFunctor fsaiFunctor(lclA, lclAinv, lclARowmap, lclAColmap, lclAinvRowmap, lclAinvColmap, scratchLevel);
541 Kokkos::parallel_for(
"MueLu::InverseFactory::LocalSpai", policy, fsaiFunctor);
544 Ainv->fillComplete();
551template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
552RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
555 RCP<Matrix> Ainv = MatrixFactory::Build(sparsityPattern);
559 RCP<Import> rowImport = ImportFactory::Build(sparsityPattern->getRowMap(), sparsityPattern->getColMap());
560 RCP<Matrix> A = MatrixFactory::Build(Aorg, *rowImport);
563 for (
size_t k = 0; k < sparsityPattern->getLocalNumRows(); k++) {
565 ArrayView<const LO> Ik;
566 sparsityPattern->getLocalRowView(k, Ik);
569 Array<ArrayView<const LO>> J(Ik.size());
570 Array<ArrayView<const SC>> Ak(Ik.size());
572 for (LO i = 0; i < Ik.size(); i++) {
573 A->getLocalRowView(Ik[i], J[i], Ak[i]);
574 for (LO j = 0; j < J[i].size(); j++)
578 std::sort(Jk.begin(), Jk.end());
579 Jk.erase(std::unique(Jk.begin(), Jk.end()), Jk.end());
582 for (LO i = 0; i < Jk.size(); i++) G.insert(std::pair<LO, LO>(Jk[i], i));
585 Teuchos::SerialDenseMatrix<LO, SC> localA(Jk.size(), Ik.size(),
true);
586 for (LO i = 0; i < Ik.size(); i++) {
587 for (LO j = 0; j < J[i].size(); j++) {
588 localA(G.at(J[i][j]), i) = Ak[i][j];
594 Teuchos::SerialDenseVector<LO, SC> ek(Jk.size(),
true);
595 ek[std::find(Jk.begin(), Jk.end(), k) - Jk.begin()] = Teuchos::ScalarTraits<Scalar>::one();
599 Teuchos::SerialDenseVector<LO, SC> localX(Ik.size());
600 Teuchos::SerialQRDenseSolver<LO, SC> qrSolver;
601 qrSolver.setMatrix(Teuchos::rcp(&localA,
false));
602 qrSolver.setVectors(Teuchos::rcp(&localX,
false), Teuchos::rcp(&ek,
false));
603 const int err = qrSolver.solve();
605 "MueLu::InverseApproximationFactory::GetSparseInverse: Error in serial QR solve.");
608 ArrayView<const SC> Mk(localX.values(), localX.length());
609 Ainv->replaceLocalValues(k, Ik, Mk);
611 Ainv->fillComplete();
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Exception throws to report errors in the internal logical of the program.
void Input(Level &level, const std::string &varName) const
T Get(Level &level, const std::string &varName) const
void Set(Level &level, const std::string &varName, const T &data) const
Timer to be used in factories. Similar to Monitor but with additional timers.
Factory for building the approximate inverse of a matrix.
void Build(Level ¤tLevel) const
Build an object with this factory.
void DeclareInput(Level ¤tLevel) const
Input.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
RCP< Matrix > GetSparseInverse(const RCP< Matrix > &A, const RCP< const CrsGraph > &sparsityPattern) const
Sparse inverse calculation method.
Class that holds all level-specific information.
static const RCP< const NoFactory > getRCP()
Static Get() functions.
virtual const Teuchos::ParameterList & GetParameterList() const
static RCP< Xpetra::CrsGraph< DefaultLocalOrdinal, DefaultGlobalOrdinal, DefaultNode > > GetThresholdedLowerTriangularGraph(const RCP< Matrix > &A, const Magnitude threshold, const bool symScaled=false)
static Teuchos::RCP< Vector > GetInverse(Teuchos::RCP< const Vector > v, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps() *100, Scalar valReplacement=Teuchos::ScalarTraits< Scalar >::zero())
static RCP< Matrix > GetThresholdedMatrix(const RCP< Matrix > &Ain, const Magnitude threshold, const bool keepDiagonal=true)
static RCP< Xpetra::CrsGraph< DefaultLocalOrdinal, DefaultGlobalOrdinal, DefaultNode > > GetThresholdedGraph(const RCP< Matrix > &A, const Magnitude threshold, const bool symScaled=false)
static Teuchos::RCP< Vector > GetLumpedMatrixDiagonal(Matrix const &A, const bool doReciprocal=false, Magnitude tol=Teuchos::ScalarTraits< Scalar >::magnitude(Teuchos::ScalarTraits< Scalar >::zero()), Scalar valReplacement=Teuchos::ScalarTraits< Scalar >::zero(), const bool replaceSingleEntryRowWithZero=false, const bool useAverageAbsDiagVal=false)
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
Namespace for MueLu classes and methods.
@ Statistics2
Print even more statistics.
@ Warnings
Print all warning messages.
@ Statistics1
Print more statistics.