10 #ifndef IFPACK2_EXPERIMENTAL_CRSRBILUK_DEF_HPP
11 #define IFPACK2_EXPERIMENTAL_CRSRBILUK_DEF_HPP
13 #include "Tpetra_BlockMultiVector.hpp"
14 #include "Tpetra_BlockView.hpp"
15 #include "Tpetra_BlockCrsMatrix_Helpers_decl.hpp"
16 #include "Ifpack2_OverlappingRowMatrix.hpp"
17 #include "Ifpack2_Details_getCrsMatrix.hpp"
18 #include "Ifpack2_LocalFilter.hpp"
20 #include "Ifpack2_RILUK.hpp"
21 #include "KokkosSparse_sptrsv.hpp"
26 #ifndef IFPACK2_RBILUK_INITIAL_NOKK
27 #include "KokkosBatched_Gemm_Decl.hpp"
28 #include "KokkosBatched_Gemm_Serial_Impl.hpp"
29 #include "KokkosBatched_Util.hpp"
34 namespace Experimental {
38 template<
class MatrixType>
39 struct LocalRowHandler
41 using LocalOrdinal =
typename MatrixType::local_ordinal_type;
42 using row_matrix_type = Tpetra::RowMatrix<
43 typename MatrixType::scalar_type,
45 typename MatrixType::global_ordinal_type,
46 typename MatrixType::node_type>;
47 using inds_type =
typename row_matrix_type::local_inds_host_view_type;
48 using vals_type =
typename row_matrix_type::values_host_view_type;
53 if (!A_->supportsRowViews())
55 const auto maxNumRowEntr = A_->getLocalMaxNumRowEntries();
56 const auto blockSize = A_->getBlockSize();
57 ind_nc_ = inds_type_nc(
"Ifpack2::RBILUK::LocalRowHandler::indices",maxNumRowEntr);
58 val_nc_ = vals_type_nc(
"Ifpack2::RBILUK::LocalRowHandler::values",maxNumRowEntr*blockSize*blockSize);
62 void getLocalRow(LocalOrdinal local_row, inds_type & InI, vals_type & InV, LocalOrdinal & NumIn)
64 if (A_->supportsRowViews())
66 A_->getLocalRowView(local_row,InI,InV);
67 NumIn = (LocalOrdinal)InI.size();
72 A_->getLocalRowCopy(local_row,ind_nc_,val_nc_,cnt);
75 NumIn = (LocalOrdinal)cnt;
81 using inds_type_nc =
typename row_matrix_type::nonconst_local_inds_host_view_type;
82 using vals_type_nc =
typename row_matrix_type::nonconst_values_host_view_type;
91 template<
class MatrixType>
96 template<
class MatrixType>
102 template<
class MatrixType>
106 template<
class MatrixType>
118 this->isAllocated_ =
false;
119 this->isInitialized_ =
false;
120 this->isComputed_ =
false;
121 this->Graph_ = Teuchos::null;
122 L_block_ = Teuchos::null;
123 U_block_ = Teuchos::null;
124 D_block_ = Teuchos::null;
130 template<
class MatrixType>
131 const typename RBILUK<MatrixType>::block_crs_matrix_type&
135 L_block_.is_null (), std::runtime_error,
"Ifpack2::RILUK::getL: The L factor "
136 "is null. Please call initialize() (and preferably also compute()) "
137 "before calling this method. If the input matrix has not yet been set, "
138 "you must first call setMatrix() with a nonnull input matrix before you "
139 "may call initialize() or compute().");
144 template<
class MatrixType>
145 const typename RBILUK<MatrixType>::block_crs_matrix_type&
149 D_block_.is_null (), std::runtime_error,
"Ifpack2::RILUK::getD: The D factor "
150 "(of diagonal entries) is null. Please call initialize() (and "
151 "preferably also compute()) before calling this method. If the input "
152 "matrix has not yet been set, you must first call setMatrix() with a "
153 "nonnull input matrix before you may call initialize() or compute().");
158 template<
class MatrixType>
159 const typename RBILUK<MatrixType>::block_crs_matrix_type&
163 U_block_.is_null (), std::runtime_error,
"Ifpack2::RILUK::getU: The U factor "
164 "is null. Please call initialize() (and preferably also compute()) "
165 "before calling this method. If the input matrix has not yet been set, "
166 "you must first call setMatrix() with a nonnull input matrix before you "
167 "may call initialize() or compute().");
171 template<
class MatrixType>
177 if (! this->isAllocated_) {
189 L_block_ =
rcp(
new block_crs_matrix_type(*this->Graph_->getL_Graph (), blockSize_) );
190 U_block_ =
rcp(
new block_crs_matrix_type(*this->Graph_->getU_Graph (), blockSize_) );
191 D_block_ =
rcp(
new block_crs_matrix_type(*(Ifpack2::Details::computeDiagonalGraph(*(this->Graph_->getOverlapGraph()))),
193 L_block_->setAllToScalar (STM::zero ());
194 U_block_->setAllToScalar (STM::zero ());
195 D_block_->setAllToScalar (STM::zero ());
198 if (this->isKokkosKernelsSpiluk_) {
199 const auto numRows = L_block_->getLocalNumRows();
200 tmp_ = decltype(tmp_)(
"RBILUK::tmp_", numRows * blockSize_);
203 this->isAllocated_ =
true;
209 template<
class MatrixType>
211 getBlockCrsGraph(
const Teuchos::RCP<
const typename RBILUK<MatrixType>::row_matrix_type>& A)
213 using local_ordinal_type =
typename MatrixType::local_ordinal_type;
216 using Teuchos::rcp_dynamic_cast;
217 using Teuchos::rcp_const_cast;
218 using Teuchos::rcpFromRef;
219 using row_matrix_type =
typename RBILUK<MatrixType>::row_matrix_type;
220 using crs_graph_type =
typename RBILUK<MatrixType>::crs_graph_type;
221 using block_crs_matrix_type =
typename RBILUK<MatrixType>::block_crs_matrix_type;
226 RCP<const LocalFilter<row_matrix_type> > filteredA =
227 rcp_dynamic_cast<
const LocalFilter<row_matrix_type> >(A_local);
228 RCP<const OverlappingRowMatrix<row_matrix_type> > overlappedA = Teuchos::null;
229 RCP<const block_crs_matrix_type > A_block = Teuchos::null;
230 if (!filteredA.is_null ())
232 overlappedA = rcp_dynamic_cast<
const OverlappingRowMatrix<row_matrix_type> > (filteredA->getUnderlyingMatrix ());
235 if (! overlappedA.is_null ()) {
236 A_block = rcp_dynamic_cast<
const block_crs_matrix_type>(overlappedA->getUnderlyingMatrix());
238 else if (!filteredA.is_null ()){
240 A_block = rcp_dynamic_cast<
const block_crs_matrix_type>(filteredA->getUnderlyingMatrix());
244 A_block = rcp_dynamic_cast<
const block_crs_matrix_type>(A_local);
247 if (!A_block.is_null()){
248 return rcpFromRef(A_block->getCrsGraph());
254 local_ordinal_type numRows = A_local->getLocalNumRows();
256 for(local_ordinal_type i = 0; i < numRows; i++) {
257 entriesPerRow[i] = A_local->getNumEntriesInLocalRow(i);
259 RCP<crs_graph_type> A_local_crs_nc =
260 rcp (
new crs_graph_type (A_local->getRowMap (),
261 A_local->getColMap (),
265 using LocalRowHandler_t = LocalRowHandler<MatrixType>;
266 LocalRowHandler_t localRowHandler(A_local);
267 typename LocalRowHandler_t::inds_type indices;
268 typename LocalRowHandler_t::vals_type values;
269 for(local_ordinal_type i = 0; i < numRows; i++) {
270 local_ordinal_type numEntries = 0;
271 localRowHandler.getLocalRow(i, indices, values, numEntries);
272 A_local_crs_nc->insertLocalIndices(i, numEntries,indices.data());
276 A_local_crs_nc->fillComplete (A_local->getDomainMap (), A_local->getRangeMap ());
277 return rcp_const_cast<
const crs_graph_type> (A_local_crs_nc);
285 template<
class MatrixType>
290 using Teuchos::rcp_dynamic_cast;
291 const char prefix[] =
"Ifpack2::Experimental::RBILUK::initialize: ";
294 (this->A_.
is_null (), std::runtime_error, prefix <<
"The matrix (A_, the "
295 "RowMatrix) is null. Please call setMatrix() with a nonnull input "
296 "before calling this method.");
298 (! this->A_->isFillComplete (), std::runtime_error, prefix <<
"The matrix "
299 "(A_, the BlockCrsMatrix) is not fill complete. You may not invoke "
300 "initialize() or compute() with this matrix until the matrix is fill "
301 "complete. Note: BlockCrsMatrix is fill complete if and only if its "
302 "underlying graph is fill complete.");
304 blockSize_ = this->A_->getBlockSize();
305 this->A_local_ = this->makeLocalFilter(this->A_);
308 double startTime = timer.
wallTime();
319 this->isInitialized_ =
false;
320 this->isAllocated_ =
false;
321 this->isComputed_ =
false;
322 this->Graph_ = Teuchos::null;
324 RCP<const crs_graph_type> matrixCrsGraph = getBlockCrsGraph<MatrixType>(this->A_);
326 this->LevelOfFill_, 0));
328 if (this->isKokkosKernelsSpiluk_) {
329 this->KernelHandle_ =
Teuchos::rcp (
new kk_handle_type ());
330 const auto numRows = this->A_local_->getLocalNumRows();
331 KernelHandle_->create_spiluk_handle( KokkosSparse::Experimental::SPILUKAlgorithm::SEQLVLSCHD_TP1,
333 2*this->A_local_->getLocalNumEntries()*(this->LevelOfFill_+1),
334 2*this->A_local_->getLocalNumEntries()*(this->LevelOfFill_+1),
336 this->Graph_->initialize(KernelHandle_);
338 this->L_Sptrsv_KernelHandle_ =
Teuchos::rcp (
new kk_handle_type ());
339 this->U_Sptrsv_KernelHandle_ =
Teuchos::rcp (
new kk_handle_type ());
341 KokkosSparse::Experimental::SPTRSVAlgorithm alg = KokkosSparse::Experimental::SPTRSVAlgorithm::SEQLVLSCHD_TP1;
343 this->L_Sptrsv_KernelHandle_->create_sptrsv_handle(alg, numRows,
true , blockSize_);
344 this->U_Sptrsv_KernelHandle_->create_sptrsv_handle(alg, numRows,
false , blockSize_);
347 this->Graph_->initialize ();
350 allocate_L_and_U_blocks ();
352 #ifdef IFPACK2_RBILUK_INITIAL
357 this->isInitialized_ =
true;
358 this->numInitialize_ += 1;
359 this->initializeTime_ += (timer.
wallTime() - startTime);
363 template<
class MatrixType>
369 typedef Tpetra::Map<LO,GO,node_type> map_type;
371 LO NumIn = 0, NumL = 0, NumU = 0;
372 bool DiagFound =
false;
373 size_t NumNonzeroDiags = 0;
374 size_t MaxNumEntries = this->A_->getLocalMaxNumRowEntries();
375 LO blockMatSize = blockSize_*blockSize_;
382 bool gidsAreConsistentlyOrdered=
true;
383 GO indexOfInconsistentGID=0;
384 for (GO i=0; i<rowGIDs.
size(); ++i) {
385 if (rowGIDs[i] != colGIDs[i]) {
386 gidsAreConsistentlyOrdered=
false;
387 indexOfInconsistentGID=i;
392 "The ordering of the local GIDs in the row and column maps is not the same"
393 << std::endl <<
"at index " << indexOfInconsistentGID
394 <<
". Consistency is required, as all calculations are done with"
395 << std::endl <<
"local indexing.");
406 L_block_->setAllToScalar (STM::zero ());
407 U_block_->setAllToScalar (STM::zero ());
408 D_block_->setAllToScalar (STM::zero ());
425 RCP<const map_type> rowMap = L_block_->getRowMap ();
437 using LocalRowHandler_t = LocalRowHandler<MatrixType>;
438 LocalRowHandler_t localRowHandler(this->A_);
439 typename LocalRowHandler_t::inds_type InI;
440 typename LocalRowHandler_t::vals_type InV;
441 for (
size_t myRow=0; myRow<this->A_->getLocalNumRows(); ++myRow) {
442 LO local_row = myRow;
444 localRowHandler.getLocalRow(local_row, InI, InV, NumIn);
451 for (LO j = 0; j < NumIn; ++j) {
453 const LO blockOffset = blockMatSize*j;
455 if (k == local_row) {
458 for (LO jj = 0; jj < blockMatSize; ++jj)
459 diagValues[jj] = this->Rthresh_ * InV[blockOffset+jj] + IFPACK2_SGN(InV[blockOffset+jj]) * this->Athresh_;
460 D_block_->replaceLocalValues(local_row, &InI[j], diagValues.getRawPtr(), 1);
464 true, std::runtime_error,
"Ifpack2::RILUK::initAllValues: current "
465 "GID k = " << k <<
" < 0. I'm not sure why this is an error; it is "
466 "probably an artifact of the undocumented assumptions of the "
467 "original implementation (likely copied and pasted from Ifpack). "
468 "Nevertheless, the code I found here insisted on this being an error "
469 "state, so I will throw an exception here.");
471 else if (k < local_row) {
473 const LO LBlockOffset = NumL*blockMatSize;
474 for (LO jj = 0; jj < blockMatSize; ++jj)
475 LV[LBlockOffset+jj] = InV[blockOffset+jj];
478 else if (Teuchos::as<size_t>(k) <= rowMap->getLocalNumElements()) {
480 const LO UBlockOffset = NumU*blockMatSize;
481 for (LO jj = 0; jj < blockMatSize; ++jj)
482 UV[UBlockOffset+jj] = InV[blockOffset+jj];
493 for (LO jj = 0; jj < blockSize_; ++jj)
494 diagValues[jj*(blockSize_+1)] = this->Athresh_;
495 D_block_->replaceLocalValues(local_row, &local_row, diagValues.getRawPtr(), 1);
499 L_block_->replaceLocalValues(local_row, &LI[0], &LV[0], NumL);
503 U_block_->replaceLocalValues(local_row, &UI[0], &UV[0], NumU);
518 this->isInitialized_ =
true;
527 template<
class LittleBlockType>
528 struct GetManagedView {
529 static_assert (Kokkos::is_view<LittleBlockType>::value,
530 "LittleBlockType must be a Kokkos::View.");
531 typedef Kokkos::View<
typename LittleBlockType::non_const_data_type,
532 typename LittleBlockType::array_layout,
533 typename LittleBlockType::device_type> managed_non_const_type;
534 static_assert (static_cast<int> (managed_non_const_type::rank) ==
535 static_cast<int> (LittleBlockType::rank),
536 "managed_non_const_type::rank != LittleBlock::rank. "
537 "Please report this bug to the Ifpack2 developers.");
542 template<
class MatrixType>
549 typedef impl_scalar_type IST;
550 const char prefix[] =
"Ifpack2::Experimental::RBILUK::compute: ";
556 (this->A_.
is_null (), std::runtime_error, prefix <<
"The matrix (A_, "
557 "the BlockCrsMatrix) is null. Please call setMatrix() with a nonnull "
558 "input before calling this method.");
560 (! this->A_->isFillComplete (), std::runtime_error, prefix <<
"The matrix "
561 "(A_, the BlockCrsMatrix) is not fill complete. You may not invoke "
562 "initialize() or compute() with this matrix until the matrix is fill "
563 "complete. Note: BlockCrsMatrix is fill complete if and only if its "
564 "underlying graph is fill complete.");
566 if (! this->isInitialized ()) {
589 double startTime = timer.
wallTime();
592 this->isComputed_ =
false;
599 if (!this->isKokkosKernelsSpiluk_) {
602 LO NumL, NumU, NumURead;
605 const size_t MaxNumEntries =
606 L_block_->getLocalMaxNumRowEntries () + U_block_->getLocalMaxNumRowEntries () + 1;
608 const LO blockMatSize = blockSize_*blockSize_;
613 const LO rowStride = blockSize_;
616 Kokkos::View<
int*, Kokkos::HostSpace,
617 Kokkos::MemoryUnmanaged> ipiv (ipiv_teuchos.
getRawPtr (), blockSize_);
619 Kokkos::View<IST*, Kokkos::HostSpace,
620 Kokkos::MemoryUnmanaged> work (work_teuchos.getRawPtr (), blockSize_);
622 size_t num_cols = U_block_->getColMap()->getLocalNumElements();
625 typename GetManagedView<little_block_host_type>::managed_non_const_type diagModBlock (
"diagModBlock", blockSize_, blockSize_);
626 typename GetManagedView<little_block_host_type>::managed_non_const_type matTmp (
"matTmp", blockSize_, blockSize_);
627 typename GetManagedView<little_block_host_type>::managed_non_const_type multiplier (
"multiplier", blockSize_, blockSize_);
635 for (
size_t j = 0; j < num_cols; ++j) {
641 const LO numLocalRows = L_block_->getLocalNumRows ();
642 for (LO local_row = 0; local_row < numLocalRows; ++local_row) {
646 NumIn = MaxNumEntries;
647 local_inds_host_view_type colValsL;
648 values_host_view_type valsL;
650 L_block_->getLocalRowView(local_row, colValsL, valsL);
651 NumL = (LO) colValsL.size();
652 for (LO j = 0; j < NumL; ++j)
654 const LO matOffset = blockMatSize*j;
655 little_block_host_type lmat((
typename little_block_host_type::value_type*) &valsL[matOffset],blockSize_,rowStride);
656 little_block_host_type lmatV((
typename little_block_host_type::value_type*) &InV[matOffset],blockSize_,rowStride);
658 Tpetra::COPY (lmat, lmatV);
659 InI[j] = colValsL[j];
662 little_block_host_type dmat = D_block_->getLocalBlockHostNonConst(local_row, local_row);
663 little_block_host_type dmatV((
typename little_block_host_type::value_type*) &InV[NumL*blockMatSize], blockSize_, rowStride);
665 Tpetra::COPY (dmat, dmatV);
666 InI[NumL] = local_row;
668 local_inds_host_view_type colValsU;
669 values_host_view_type valsU;
670 U_block_->getLocalRowView(local_row, colValsU, valsU);
671 NumURead = (LO) colValsU.
size();
673 for (LO j = 0; j < NumURead; ++j)
675 if (!(colValsU[j] < numLocalRows))
continue;
676 InI[NumL+1+j] = colValsU[j];
677 const LO matOffset = blockMatSize*(NumL+1+j);
678 little_block_host_type umat((
typename little_block_host_type::value_type*) &valsU[blockMatSize*j], blockSize_, rowStride);
679 little_block_host_type umatV((
typename little_block_host_type::value_type*) &InV[matOffset], blockSize_, rowStride);
681 Tpetra::COPY (umat, umatV);
687 for (
size_t j = 0; j < NumIn; ++j) {
691 #ifndef IFPACK2_RBILUK_INITIAL
692 for (LO i = 0; i < blockSize_; ++i)
693 for (LO j = 0; j < blockSize_; ++j){
695 diagModBlock(i,j) = 0;
700 Kokkos::deep_copy (diagModBlock, diagmod);
703 for (LO jj = 0; jj < NumL; ++jj) {
705 little_block_host_type currentVal((
typename little_block_host_type::value_type*) &InV[jj*blockMatSize], blockSize_, rowStride);
707 Tpetra::COPY (currentVal, multiplier);
709 const little_block_host_type dmatInverse = D_block_->getLocalBlockHostNonConst(j,j);
711 #ifndef IFPACK2_RBILUK_INITIAL_NOKK
712 KokkosBatched::SerialGemm
713 <KokkosBatched::Trans::NoTranspose,
714 KokkosBatched::Trans::NoTranspose,
715 KokkosBatched::Algo::Gemm::Blocked>::invoke
716 (STS::one (), currentVal, dmatInverse, STS::zero (), matTmp);
718 Tpetra::GEMM (
"N",
"N", STS::one (), currentVal, dmatInverse,
719 STS::zero (), matTmp);
723 Tpetra::COPY (matTmp, currentVal);
724 local_inds_host_view_type UUI;
725 values_host_view_type UUV;
727 U_block_->getLocalRowView(j, UUI, UUV);
728 NumUU = (LO) UUI.size();
730 if (this->RelaxValue_ == STM::zero ()) {
731 for (LO k = 0; k < NumUU; ++k) {
732 if (!(UUI[k] < numLocalRows))
continue;
733 const int kk = colflag[UUI[k]];
735 little_block_host_type kkval((
typename little_block_host_type::value_type*) &InV[kk*blockMatSize], blockSize_, rowStride);
736 little_block_host_type uumat((
typename little_block_host_type::value_type*) &UUV[k*blockMatSize], blockSize_, rowStride);
737 #ifndef IFPACK2_RBILUK_INITIAL_NOKK
738 KokkosBatched::SerialGemm
739 <KokkosBatched::Trans::NoTranspose,
740 KokkosBatched::Trans::NoTranspose,
741 KokkosBatched::Algo::Gemm::Blocked>::invoke
742 (
magnitude_type(-STM::one ()), multiplier, uumat, STM::one (), kkval);
744 Tpetra::GEMM (
"N",
"N",
magnitude_type(-STM::one ()), multiplier, uumat,
752 for (LO k = 0; k < NumUU; ++k) {
753 if (!(UUI[k] < numLocalRows))
continue;
754 const int kk = colflag[UUI[k]];
755 little_block_host_type uumat((
typename little_block_host_type::value_type*) &UUV[k*blockMatSize], blockSize_, rowStride);
757 little_block_host_type kkval((
typename little_block_host_type::value_type*) &InV[kk*blockMatSize], blockSize_, rowStride);
758 #ifndef IFPACK2_RBILUK_INITIAL_NOKK
759 KokkosBatched::SerialGemm
760 <KokkosBatched::Trans::NoTranspose,
761 KokkosBatched::Trans::NoTranspose,
762 KokkosBatched::Algo::Gemm::Blocked>::invoke
763 (
magnitude_type(-STM::one ()), multiplier, uumat, STM::one (), kkval);
765 Tpetra::GEMM (
"N",
"N",
magnitude_type(-STM::one ()), multiplier, uumat,
771 #ifndef IFPACK2_RBILUK_INITIAL_NOKK
772 KokkosBatched::SerialGemm
773 <KokkosBatched::Trans::NoTranspose,
774 KokkosBatched::Trans::NoTranspose,
775 KokkosBatched::Algo::Gemm::Blocked>::invoke
776 (
magnitude_type(-STM::one ()), multiplier, uumat, STM::one (), diagModBlock);
778 Tpetra::GEMM (
"N",
"N",
magnitude_type(-STM::one ()), multiplier, uumat,
779 STM::one (), diagModBlock);
792 Tpetra::COPY (dmatV, dmat);
794 if (this->RelaxValue_ != STM::zero ()) {
796 Tpetra::AXPY (this->RelaxValue_, diagModBlock, dmat);
810 for (
int k = 0; k < blockSize_; ++k) {
814 Tpetra::GETF2 (dmat, ipiv, lapackInfo);
817 lapackInfo != 0, std::runtime_error,
"Ifpack2::Experimental::RBILUK::compute: "
818 "lapackInfo = " << lapackInfo <<
" which indicates an error in the factorization GETRF.");
820 Tpetra::GETRI (dmat, ipiv, work, lapackInfo);
823 lapackInfo != 0, std::runtime_error,
"Ifpack2::Experimental::RBILUK::compute: "
824 "lapackInfo = " << lapackInfo <<
" which indicates an error in the matrix inverse GETRI.");
827 for (LO j = 0; j < NumU; ++j) {
828 little_block_host_type currentVal((
typename little_block_host_type::value_type*) &InV[(NumL+1+j)*blockMatSize], blockSize_, rowStride);
830 #ifndef IFPACK2_RBILUK_INITIAL_NOKK
831 KokkosBatched::SerialGemm
832 <KokkosBatched::Trans::NoTranspose,
833 KokkosBatched::Trans::NoTranspose,
834 KokkosBatched::Algo::Gemm::Blocked>::invoke
835 (STS::one (), dmat, currentVal, STS::zero (), matTmp);
837 Tpetra::GEMM (
"N",
"N", STS::one (), dmat, currentVal,
838 STS::zero (), matTmp);
842 Tpetra::COPY (matTmp, currentVal);
847 U_block_->replaceLocalValues (local_row, &InI[NumL+1], &InV[blockMatSize*(NumL+1)], NumU);
850 #ifndef IFPACK2_RBILUK_INITIAL
852 for (
size_t j = 0; j < NumIn; ++j) {
853 colflag[InI[j]] = -1;
857 for (
size_t j = 0; j < num_cols; ++j) {
864 RCP<const block_crs_matrix_type> A_local_bcrs = Details::getBcrsMatrix(this->A_local_);
865 RCP<const crs_matrix_type> A_local_crs = Details::getCrsMatrix(this->A_local_);
866 if (A_local_bcrs.is_null()) {
867 if (A_local_crs.is_null()) {
869 Array<size_t> entriesPerRow(numRows);
871 entriesPerRow[i] = this->A_local_->getNumEntriesInLocalRow(i);
873 RCP<crs_matrix_type> A_local_crs_nc =
875 this->A_local_->getColMap (),
878 nonconst_local_inds_host_view_type indices(
"indices",this->A_local_->getLocalMaxNumRowEntries());
879 nonconst_values_host_view_type values(
"values",this->A_local_->getLocalMaxNumRowEntries());
881 size_t numEntries = 0;
882 this->A_local_->getLocalRowCopy(i, indices, values, numEntries);
883 A_local_crs_nc->insertLocalValues(i, numEntries, reinterpret_cast<scalar_type*>(values.data()),indices.data());
885 A_local_crs_nc->fillComplete (this->A_local_->getDomainMap (), this->A_local_->getRangeMap ());
886 A_local_crs = Teuchos::rcp_const_cast<
const crs_matrix_type> (A_local_crs_nc);
890 if (blockSize_ > 1) {
891 auto crs_matrix_block_filled = Tpetra::fillLogicalBlocks(*A_local_crs, blockSize_);
892 A_local_bcrs = Tpetra::convertToBlockCrsMatrix(*crs_matrix_block_filled, blockSize_);
895 A_local_bcrs = Tpetra::convertToBlockCrsMatrix(*A_local_crs, blockSize_);
900 this->isKokkosKernelsStream_, std::runtime_error,
"Ifpack2::RBILUK::compute: "
901 "streams are not yet supported.");
903 auto lclMtx = A_local_bcrs->getLocalMatrixDevice();
904 auto A_local_rowmap = lclMtx.graph.row_map;
905 auto A_local_entries = lclMtx.graph.entries;
906 auto A_local_values = lclMtx.values;
911 if (L_block_->isLocallyIndexed ()) {
912 L_block_->setAllToScalar (STS::zero ());
913 U_block_->setAllToScalar (STS::zero ());
916 using row_map_type =
typename local_matrix_device_type::row_map_type;
918 auto lclL = L_block_->getLocalMatrixDevice();
919 row_map_type L_rowmap = lclL.graph.row_map;
920 auto L_entries = lclL.graph.entries;
921 auto L_values = lclL.values;
923 auto lclU = U_block_->getLocalMatrixDevice();
924 row_map_type U_rowmap = lclU.graph.row_map;
925 auto U_entries = lclU.graph.entries;
926 auto U_values = lclU.values;
928 KokkosSparse::Experimental::spiluk_numeric( KernelHandle_.getRawPtr(), this->LevelOfFill_,
929 A_local_rowmap, A_local_entries, A_local_values,
930 L_rowmap, L_entries, L_values, U_rowmap, U_entries, U_values );
933 KokkosSparse::Experimental::sptrsv_symbolic(L_Sptrsv_KernelHandle_.getRawPtr(), L_rowmap, L_entries, L_values);
934 KokkosSparse::Experimental::sptrsv_symbolic(U_Sptrsv_KernelHandle_.getRawPtr(), U_rowmap, U_entries, U_values);
953 this->isComputed_ =
true;
954 this->numCompute_ += 1;
955 this->computeTime_ += (timer.
wallTime() - startTime);
959 template<
class MatrixType>
962 apply (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
963 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
973 this->A_.
is_null (), std::runtime_error,
"Ifpack2::Experimental::RBILUK::apply: The matrix is "
974 "null. Please call setMatrix() with a nonnull input, then initialize() "
975 "and compute(), before calling this method.");
977 ! this->isComputed (), std::runtime_error,
978 "Ifpack2::Experimental::RBILUK::apply: If you have not yet called compute(), "
979 "you must call compute() before calling this method.");
980 TEUCHOS_TEST_FOR_EXCEPTION(
981 X.getNumVectors () != Y.getNumVectors (), std::invalid_argument,
982 "Ifpack2::Experimental::RBILUK::apply: X and Y do not have the same number of columns. "
983 "X.getNumVectors() = " << X.getNumVectors ()
984 <<
" != Y.getNumVectors() = " << Y.getNumVectors () <<
".");
985 TEUCHOS_TEST_FOR_EXCEPTION(
987 "Ifpack2::Experimental::RBILUK::apply: mode = Teuchos::CONJ_TRANS is not implemented for "
988 "complex Scalar type. Please talk to the Ifpack2 developers to get this "
989 "fixed. There is a FIXME in this file about this very issue.");
991 const LO blockMatSize = blockSize_*blockSize_;
993 const LO rowStride = blockSize_;
995 BMV yBlock (Y, * (this->Graph_->getA_Graph()->getDomainMap ()), blockSize_);
996 const BMV xBlock (X, * (this->A_->getColMap ()), blockSize_);
999 little_host_vec_type lclvec((
typename little_host_vec_type::value_type*)&lclarray[0], blockSize_);
1000 const scalar_type one = STM::one ();
1001 const scalar_type zero = STM::zero ();
1004 double startTime = timer.
wallTime();
1007 if (!this->isKokkosKernelsSpiluk_) {
1008 if (alpha == one && beta == zero) {
1016 const LO numVectors = xBlock.getNumVectors();
1017 BMV cBlock (* (this->Graph_->getA_Graph()->getDomainMap ()), blockSize_, numVectors);
1018 BMV rBlock (* (this->Graph_->getA_Graph()->getDomainMap ()), blockSize_, numVectors);
1019 for (LO imv = 0; imv < numVectors; ++imv)
1021 for (
size_t i = 0; i < D_block_->getLocalNumRows(); ++i)
1024 const_host_little_vec_type xval =
1025 xBlock.getLocalBlockHost(local_row, imv, Tpetra::Access::ReadOnly);
1026 little_host_vec_type cval =
1027 cBlock.getLocalBlockHost(local_row, imv, Tpetra::Access::OverwriteAll);
1029 Tpetra::COPY (xval, cval);
1031 local_inds_host_view_type colValsL;
1032 values_host_view_type valsL;
1033 L_block_->getLocalRowView(local_row, colValsL, valsL);
1034 LO NumL = (LO) colValsL.size();
1036 for (LO j = 0; j < NumL; ++j)
1038 LO col = colValsL[j];
1039 const_host_little_vec_type prevVal =
1040 cBlock.getLocalBlockHost(col, imv, Tpetra::Access::ReadOnly);
1042 const LO matOffset = blockMatSize*j;
1043 little_block_host_type lij((
typename little_block_host_type::value_type*) &valsL[matOffset],blockSize_,rowStride);
1046 Tpetra::GEMV (-one, lij, prevVal, cval);
1052 D_block_->applyBlock(cBlock, rBlock);
1055 for (LO imv = 0; imv < numVectors; ++imv)
1057 const LO numRows = D_block_->getLocalNumRows();
1058 for (LO i = 0; i < numRows; ++i)
1060 LO local_row = (numRows-1)-i;
1061 const_host_little_vec_type rval =
1062 rBlock.getLocalBlockHost(local_row, imv, Tpetra::Access::ReadOnly);
1063 little_host_vec_type yval =
1064 yBlock.getLocalBlockHost(local_row, imv, Tpetra::Access::OverwriteAll);
1066 Tpetra::COPY (rval, yval);
1068 local_inds_host_view_type colValsU;
1069 values_host_view_type valsU;
1070 U_block_->getLocalRowView(local_row, colValsU, valsU);
1071 LO NumU = (LO) colValsU.size();
1073 for (LO j = 0; j < NumU; ++j)
1075 LO col = colValsU[NumU-1-j];
1076 const_host_little_vec_type prevVal =
1077 yBlock.getLocalBlockHost(col, imv, Tpetra::Access::ReadOnly);
1079 const LO matOffset = blockMatSize*(NumU-1-j);
1080 little_block_host_type uij((
typename little_block_host_type::value_type*) &valsU[matOffset], blockSize_, rowStride);
1083 Tpetra::GEMV (-one, uij, prevVal, yval);
1089 TEUCHOS_TEST_FOR_EXCEPTION(
1090 true, std::runtime_error,
1091 "Ifpack2::Experimental::RBILUK::apply: transpose apply is not implemented for the block algorithm");
1095 if (alpha == zero) {
1102 MV Y_tmp (Y.getMap (), Y.getNumVectors ());
1103 apply (X, Y_tmp, mode);
1104 Y.update (alpha, Y_tmp, beta);
1110 auto X_views = X.getLocalViewDevice(Tpetra::Access::ReadOnly);
1111 auto Y_views = Y.getLocalViewDevice(Tpetra::Access::ReadWrite);
1113 auto lclL = L_block_->getLocalMatrixDevice();
1114 auto L_rowmap = lclL.graph.row_map;
1115 auto L_entries = lclL.graph.entries;
1116 auto L_values = lclL.values;
1118 auto lclU = U_block_->getLocalMatrixDevice();
1119 auto U_rowmap = lclU.graph.row_map;
1120 auto U_entries = lclU.graph.entries;
1121 auto U_values = lclU.values;
1125 const LO numVecs = X.getNumVectors();
1126 for (LO vec = 0; vec < numVecs; ++vec) {
1127 auto X_view = Kokkos::subview(X_views, Kokkos::ALL(), vec);
1128 auto Y_view = Kokkos::subview(Y_views, Kokkos::ALL(), vec);
1129 KokkosSparse::Experimental::sptrsv_solve(L_Sptrsv_KernelHandle_.getRawPtr(), L_rowmap, L_entries, L_values, X_view, tmp_);
1134 const LO numVecs = X.getNumVectors();
1135 for (LO vec = 0; vec < numVecs; ++vec) {
1136 auto Y_view = Kokkos::subview(Y_views, Kokkos::ALL(), vec);
1137 KokkosSparse::Experimental::sptrsv_solve(U_Sptrsv_KernelHandle_.getRawPtr(), U_rowmap, U_entries, U_values, tmp_, Y_view);
1141 KokkosBlas::axpby(alpha, Y_views, beta, Y_views);
1144 TEUCHOS_TEST_FOR_EXCEPTION(
1145 true, std::runtime_error,
1146 "Ifpack2::Experimental::RBILUK::apply: transpose apply is not implemented for the block algorithm");
1153 this->numApply_ += 1;
1154 this->applyTime_ += (timer.
wallTime() - startTime);
1158 template<
class MatrixType>
1161 std::ostringstream os;
1166 os <<
"\"Ifpack2::Experimental::RBILUK\": {";
1167 os <<
"Initialized: " << (this->isInitialized () ?
"true" :
"false") <<
", "
1168 <<
"Computed: " << (this->isComputed () ?
"true" :
"false") <<
", ";
1170 os <<
"Level-of-fill: " << this->getLevelOfFill() <<
", ";
1173 os <<
"Matrix: null";
1176 os <<
"Global matrix dimensions: ["
1177 << this->A_->getGlobalNumRows () <<
", " << this->A_->getGlobalNumCols () <<
"]"
1178 <<
", Global nnz: " << this->A_->getGlobalNumEntries();
1193 #define IFPACK2_EXPERIMENTAL_RBILUK_INSTANT(S,LO,GO,N) \
1194 template class Ifpack2::Experimental::RBILUK< Tpetra::BlockCrsMatrix<S, LO, GO, N> >; \
1195 template class Ifpack2::Experimental::RBILUK< Tpetra::RowMatrix<S, LO, GO, N> >;
void initialize()
Initialize by computing the symbolic incomplete factorization.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:286
MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:113
virtual ~RBILUK()
Destructor (declared virtual for memory safety).
Definition: Ifpack2_Experimental_RBILUK_def.hpp:103
static Teuchos::RCP< const row_matrix_type > makeLocalFilter(const Teuchos::RCP< const row_matrix_type > &A)
Return A, wrapped in a LocalFilter, if necessary.
const block_crs_matrix_type & getUBlock() const
Return the U factor of the ILU factorization.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:160
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:103
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 (inverse of the) incomplete factorization to X, resulting in Y.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:962
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:120
ILU(k) factorization of a given Tpetra::RowMatrix.
Definition: Ifpack2_RILUK_decl.hpp:213
MatrixType::node_type node_type
The Node type used by the input MatrixType.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:117
Tpetra::CrsMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > crs_matrix_type
Tpetra::CrsMatrix specialization used by this class for representing L and U.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:132
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:109
const block_crs_matrix_type & getDBlock() const
Return the diagonal entries of the ILU factorization.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:146
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void compute()
Compute the (numeric) incomplete factorization.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:543
File for utility functions.
Construct a level filled graph for use in computing an ILU(k) incomplete factorization.
Definition: Ifpack2_IlukGraph.hpp:67
void setMatrix(const Teuchos::RCP< const block_crs_matrix_type > &A)
Change the matrix to be preconditioned.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:108
const block_crs_matrix_type & getLBlock() const
Return the L factor of the ILU factorization.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:132
Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > row_matrix_type
Tpetra::RowMatrix specialization used by this class.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:126
std::string description() const
A one-line description of this object.
Definition: Ifpack2_Experimental_RBILUK_def.hpp:1159
ILU(k) factorization of a given Tpetra::BlockCrsMatrix.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:95