10 #ifndef IFPACK2_LOCALSPARSETRIANGULARSOLVER_DEF_HPP
11 #define IFPACK2_LOCALSPARSETRIANGULARSOLVER_DEF_HPP
16 #include "Ifpack2_LocalSparseTriangularSolver_decl.hpp"
17 #include "Tpetra_CrsMatrix.hpp"
18 #include "Tpetra_Core.hpp"
19 #include "Teuchos_StandardParameterEntryValidators.hpp"
20 #include "Tpetra_Details_determineLocalTriangularStructure.hpp"
21 #include "KokkosSparse_trsv.hpp"
23 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
24 #include "shylu_hts.hpp"
31 #if defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE) && defined(KOKKOS_ENABLE_CUDA)
33 inline void cusparse_error_throw(cusparseStatus_t cusparseStatus,
const char* name,
34 const char* file,
const int line) {
35 std::ostringstream out;
36 #if defined(CUSPARSE_VERSION) && (10300 <= CUSPARSE_VERSION)
37 out << name <<
" error( " << cusparseGetErrorName(cusparseStatus) <<
"): " << cusparseGetErrorString(cusparseStatus);
39 out << name <<
" error( ";
40 switch (cusparseStatus) {
41 case CUSPARSE_STATUS_NOT_INITIALIZED:
42 out <<
"CUSPARSE_STATUS_NOT_INITIALIZED): cusparse handle was not "
45 case CUSPARSE_STATUS_ALLOC_FAILED:
46 out <<
"CUSPARSE_STATUS_ALLOC_FAILED): you might tried to allocate too "
49 case CUSPARSE_STATUS_INVALID_VALUE: out <<
"CUSPARSE_STATUS_INVALID_VALUE)";
break;
50 case CUSPARSE_STATUS_ARCH_MISMATCH: out <<
"CUSPARSE_STATUS_ARCH_MISMATCH)";
break;
51 case CUSPARSE_STATUS_MAPPING_ERROR: out <<
"CUSPARSE_STATUS_MAPPING_ERROR)";
break;
52 case CUSPARSE_STATUS_EXECUTION_FAILED: out <<
"CUSPARSE_STATUS_EXECUTION_FAILED)";
break;
53 case CUSPARSE_STATUS_INTERNAL_ERROR: out <<
"CUSPARSE_STATUS_INTERNAL_ERROR)";
break;
54 case CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED: out <<
"CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED)";
break;
55 case CUSPARSE_STATUS_ZERO_PIVOT: out <<
"CUSPARSE_STATUS_ZERO_PIVOT)";
break;
56 default: out <<
"unrecognized error code): this is bad!";
break;
58 #endif // CUSPARSE_VERSION
60 out <<
" " << file <<
":" << line;
62 throw std::runtime_error(out.str());
65 inline void cusparse_safe_call(cusparseStatus_t cusparseStatus,
const char* name,
const char* file =
nullptr,
67 if (CUSPARSE_STATUS_SUCCESS != cusparseStatus) {
68 cusparse_error_throw(cusparseStatus, name, file, line);
72 #define IFPACK2_DETAILS_CUSPARSE_SAFE_CALL(call) \
73 Ifpack2::Details::cusparse_safe_call(call, #call, __FILE__, __LINE__)
75 #endif // defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE) && defined(KOKKOS_ENABLE_CUDA)
77 struct TrisolverType {
86 type_strs[0] =
"Internal";
88 type_strs[2] =
"KSPTRSV";
90 type_enums[0] = Internal;
92 type_enums[2] = KSPTRSV;
97 template <
class MatrixType>
98 class LocalSparseTriangularSolver<MatrixType>::HtsImpl {
100 typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type>
crs_matrix_type;
103 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
104 Timpl_ = Teuchos::null;
105 levelset_block_size_ = 1;
111 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
112 const char* block_size_s =
"trisolver: block size";
115 "The parameter \"" << block_size_s <<
"\" must be of type int.");
116 levelset_block_size_ = pl.
get<
int>(block_size_s);
118 if (levelset_block_size_ < 1)
119 levelset_block_size_ = 1;
128 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
130 transpose_ = conjugate_ =
false;
137 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
140 auto rowptr = T_in.getLocalRowPtrsHost();
141 auto colidx = T_in.getLocalIndicesHost();
142 auto val = T_in.getLocalValuesHost(Tpetra::Access::ReadOnly);
146 HTST::make_CrsMatrix(rowptr.size() - 1,
147 rowptr.data(), colidx.data(),
150 transpose_, conjugate_),
151 HtsCrsMatrixDeleter());
155 HTST::reprocess_numeric(Timpl_.get(), T_hts.
get());
158 if (T_in.getCrsGraph().is_null()) {
160 *out <<
"HTS compute failed because T_in.getCrsGraph().is_null().\n";
163 if (!T_in.getCrsGraph()->isSorted()) {
165 *out <<
"HTS compute failed because ! T_in.getCrsGraph().isSorted().\n";
168 if (!T_in.isStorageOptimized()) {
170 *out <<
"HTS compute failed because ! T_in.isStorageOptimized().\n";
174 typename HTST::PreprocessArgs args;
175 args.T = T_hts.
get();
178 args.nthreads = omp_get_max_threads();
182 args.save_for_reprocess =
true;
183 typename HTST::Options opts;
184 opts.levelset_block_size = levelset_block_size_;
185 args.options = &opts;
188 Timpl_ = Teuchos::rcpWithDealloc(HTST::preprocess(args), TImplDeleter());
189 }
catch (
const std::exception& e) {
191 *out <<
"HTS preprocess threw: " << e.what() <<
"\n";
200 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
208 void localApply(
const MV& X, MV& Y,
215 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
216 const auto& X_view = X.getLocalViewHost(Tpetra::Access::ReadOnly);
217 const auto& Y_view = Y.getLocalViewHost(Tpetra::Access::ReadWrite);
220 HTST::reset_max_nrhs(Timpl_.get(), X_view.extent(1));
222 HTST::solve_omp(Timpl_.get(),
224 reinterpret_cast<const scalar_type*
>(X_view.data()),
233 #ifdef HAVE_IFPACK2_SHYLU_NODEHTS
234 typedef ::Experimental::HTS<local_ordinal_type, size_t, scalar_type> HTST;
235 typedef typename HTST::Impl TImpl;
236 typedef typename HTST::CrsMatrix HtsCrsMatrix;
238 struct TImplDeleter {
239 void free(TImpl* impl) {
240 HTST::delete_Impl(impl);
244 struct HtsCrsMatrixDeleter {
245 void free(HtsCrsMatrix* T) {
246 HTST::delete_CrsMatrix(T);
251 bool transpose_, conjugate_;
252 int levelset_block_size_;
256 template <
class MatrixType>
266 "Ifpack2::LocalSparseTriangularSolver constructor: "
267 "The input matrix A is not a Tpetra::CrsMatrix.");
272 template <
class MatrixType>
280 *out_ <<
">>> DEBUG Ifpack2::LocalSparseTriangularSolver constructor"
288 "Ifpack2::LocalSparseTriangularSolver constructor: "
289 "The input matrix A is not a Tpetra::CrsMatrix.");
294 template <
class MatrixType>
300 template <
class MatrixType>
306 *out_ <<
">>> DEBUG Ifpack2::LocalSparseTriangularSolver constructor"
311 template <
class MatrixType>
313 isInitialized_ =
false;
315 reverseStorage_ =
false;
316 isInternallyChanged_ =
false;
320 initializeTime_ = 0.0;
323 isKokkosKernelsSptrsv_ =
false;
324 isKokkosKernelsStream_ =
false;
330 template <
class MatrixType>
333 if (!isKokkosKernelsStream_) {
335 kh_->destroy_sptrsv_handle();
338 for (
int i = 0; i < num_streams_; i++) {
340 kh_v_[i]->destroy_sptrsv_handle();
346 template <
class MatrixType>
353 Details::TrisolverType::Enum trisolverType = Details::TrisolverType::Internal;
355 static const char typeName[] =
"trisolver: type";
357 if (!pl.
isType<std::string>(typeName))
break;
360 Array<std::string> trisolverTypeStrs;
361 Array<Details::TrisolverType::Enum> trisolverTypeEnums;
362 Details::TrisolverType::loadPLTypeOption(trisolverTypeStrs, trisolverTypeEnums);
364 s2i(trisolverTypeStrs(), trisolverTypeEnums(), typeName,
false);
369 if (trisolverType == Details::TrisolverType::HTS) {
371 htsImpl_->setParameters(pl);
374 if (trisolverType == Details::TrisolverType::KSPTRSV) {
375 this->isKokkosKernelsSptrsv_ =
true;
377 this->isKokkosKernelsSptrsv_ =
false;
381 reverseStorage_ = pl.
get<
bool>(
"trisolver: reverse U");
383 TEUCHOS_TEST_FOR_EXCEPTION(reverseStorage_ && (trisolverType == Details::TrisolverType::HTS || trisolverType == Details::TrisolverType::KSPTRSV),
385 "Ifpack2::LocalSparseTriangularSolver::setParameters: "
386 "You are not allowed to enable both HTS or KSPTRSV and the \"trisolver: reverse U\" "
387 "options. See GitHub issue #2647.");
390 template <
class MatrixType>
393 using Tpetra::Details::determineLocalTriangularStructure;
395 using local_matrix_type =
typename crs_matrix_type::local_matrix_device_type;
398 const char prefix[] =
"Ifpack2::LocalSparseTriangularSolver::initialize: ";
399 if (!out_.is_null()) {
400 *out_ <<
">>> DEBUG " << prefix << std::endl;
403 if (!isKokkosKernelsStream_) {
405 "setMatrix() with a nonnull input matrix before you may call "
406 "initialize() or compute().");
407 if (A_crs_.is_null()) {
410 prefix <<
"The input matrix A is not a Tpetra::CrsMatrix.");
413 auto G = A_crs_->getGraph();
415 "but A_crs_'s RowGraph G is null. "
416 "Please report this bug to the Ifpack2 developers.");
421 "If you call this method, "
422 "the matrix's graph must be fill complete. It is not.");
425 constexpr
bool ignoreMapsForTriStructure =
true;
426 auto lclTriStructure = [&] {
427 auto lclMatrix = A_crs_->getLocalMatrixDevice();
428 auto lclRowMap = A_crs_->getRowMap()->getLocalMap();
429 auto lclColMap = A_crs_->getColMap()->getLocalMap();
431 determineLocalTriangularStructure(lclMatrix.graph,
434 ignoreMapsForTriStructure);
435 const LO lclNumRows = lclRowMap.getLocalNumElements();
436 this->diag_ = (lclTriStruct.diagCount < lclNumRows) ?
"U" :
"N";
437 this->uplo_ = lclTriStruct.couldBeLowerTriangular ?
"L" : (lclTriStruct.couldBeUpperTriangular ?
"U" :
"N");
441 if (reverseStorage_ && lclTriStructure.couldBeUpperTriangular &&
442 htsImpl_.is_null()) {
444 auto Alocal = A_crs_->getLocalMatrixDevice();
445 auto ptr = Alocal.graph.row_map;
446 auto ind = Alocal.graph.entries;
447 auto val = Alocal.values;
449 auto numRows = Alocal.numRows();
450 auto numCols = Alocal.numCols();
451 auto numNnz = Alocal.nnz();
453 typename decltype(ptr)::non_const_type newptr(
"ptr", ptr.extent(0));
454 typename decltype(ind)::non_const_type newind(
"ind", ind.extent(0));
455 decltype(val) newval(
"val", val.extent(0));
458 typename crs_matrix_type::execution_space().fence();
461 auto A_r = Alocal.row(numRows - 1 - row);
463 auto numEnt = A_r.length;
465 newind(rowStart + k) = numCols - 1 - A_r.colidx(numEnt - 1 - k);
466 newval(rowStart + k) = A_r.value(numEnt - 1 - k);
469 newptr(row + 1) = rowStart;
471 typename crs_matrix_type::execution_space().fence();
477 auto rowMap = A_->getRowMap();
478 auto numElems = rowMap->getLocalNumElements();
479 auto rowElems = rowMap->getLocalElementList();
482 for (
size_t i = 0; i < numElems; i++)
483 newRowElems[i] = rowElems[numElems - 1 - i];
485 newRowMap =
Teuchos::rcp(
new map_type(rowMap->getGlobalNumElements(), newRowElems, rowMap->getIndexBase(), rowMap->getComm()));
489 auto colMap = A_->getColMap();
490 auto numElems = colMap->getLocalNumElements();
491 auto colElems = colMap->getLocalElementList();
494 for (
size_t i = 0; i < numElems; i++)
495 newColElems[i] = colElems[numElems - 1 - i];
497 newColMap =
Teuchos::rcp(
new map_type(colMap->getGlobalNumElements(), newColElems, colMap->getIndexBase(), colMap->getComm()));
501 local_matrix_type newLocalMatrix(
"Upermuted", numRows, numCols, numNnz, newval, newptr, newind);
505 isInternallyChanged_ =
true;
510 auto newLclTriStructure =
511 determineLocalTriangularStructure(newLocalMatrix.graph,
512 newRowMap->getLocalMap(),
513 newColMap->getLocalMap(),
514 ignoreMapsForTriStructure);
515 const LO newLclNumRows = newRowMap->getLocalNumElements();
516 this->diag_ = (newLclTriStructure.diagCount < newLclNumRows) ?
"U" :
"N";
517 this->uplo_ = newLclTriStructure.couldBeLowerTriangular ?
"L" : (newLclTriStructure.couldBeUpperTriangular ?
"U" :
"N");
520 for (
int i = 0; i < num_streams_; i++) {
522 "setMatrix() with a nonnull input matrix before you may call "
523 "initialize() or compute().");
524 auto G = A_crs_v_[i]->getGraph();
526 "but A_crs_'s RowGraph G is null. "
527 "Please report this bug to the Ifpack2 developers.");
532 "If you call this method, "
533 "the matrix's graph must be fill complete. It is not.");
536 constexpr
bool ignoreMapsForTriStructure =
true;
537 std::string prev_uplo_ = this->uplo_;
538 std::string prev_diag_ = this->diag_;
539 auto lclMatrix = A_crs_v_[i]->getLocalMatrixDevice();
540 auto lclRowMap = A_crs_v_[i]->getRowMap()->getLocalMap();
541 auto lclColMap = A_crs_v_[i]->getColMap()->getLocalMap();
543 determineLocalTriangularStructure(lclMatrix.graph,
546 ignoreMapsForTriStructure);
547 const LO lclNumRows = lclRowMap.getLocalNumElements();
548 this->diag_ = (lclTriStruct.diagCount < lclNumRows) ?
"U" :
"N";
549 this->uplo_ = lclTriStruct.couldBeLowerTriangular ?
"L" : (lclTriStruct.couldBeUpperTriangular ?
"U" :
"N");
552 std::logic_error, prefix <<
"A_crs_'s structures in streams "
553 "are different. Please report this bug to the Ifpack2 developers.");
559 htsImpl_->initialize(*A_crs_);
560 isInternallyChanged_ =
true;
563 const bool ksptrsv_valid_uplo = (this->uplo_ !=
"N");
564 kh_v_nonnull_ =
false;
565 if (this->isKokkosKernelsSptrsv_ && ksptrsv_valid_uplo && this->diag_ !=
"U") {
566 if (!isKokkosKernelsStream_) {
568 const bool is_lower_tri = (this->uplo_ ==
"L") ?
true :
false;
570 auto A_crs = Teuchos::rcp_dynamic_cast<
const crs_matrix_type>(A_,
true);
571 auto Alocal = A_crs->getLocalMatrixDevice();
572 auto ptr = Alocal.graph.row_map;
573 auto ind = Alocal.graph.entries;
574 auto val = Alocal.values;
576 auto numRows = Alocal.numRows();
577 kh_->create_sptrsv_handle(kokkosKernelsAlgorithm(), numRows, is_lower_tri);
578 KokkosSparse::sptrsv_symbolic(kh_.getRawPtr(), ptr, ind, val);
580 kh_v_ = std::vector<Teuchos::RCP<k_handle>>(num_streams_);
581 for (
int i = 0; i < num_streams_; i++) {
583 auto A_crs_i = Teuchos::rcp_dynamic_cast<
const crs_matrix_type>(A_crs_v_[i],
true);
584 auto Alocal_i = A_crs_i->getLocalMatrixDevice();
585 auto ptr_i = Alocal_i.graph.row_map;
586 auto ind_i = Alocal_i.graph.entries;
587 auto val_i = Alocal_i.values;
589 auto numRows_i = Alocal_i.numRows();
591 const bool is_lower_tri = (this->uplo_ ==
"L") ?
true :
false;
592 kh_v_[i]->create_sptrsv_handle(kokkosKernelsAlgorithm(), numRows_i, is_lower_tri);
593 KokkosSparse::sptrsv_symbolic(kh_v_[i].getRawPtr(), ptr_i, ind_i, val_i);
595 kh_v_nonnull_ =
true;
599 isInitialized_ =
true;
603 template <
class MatrixType>
604 KokkosSparse::Experimental::SPTRSVAlgorithm
606 #if defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE) && defined(KOKKOS_ENABLE_CUDA)
609 if constexpr (std::is_same<Kokkos::Cuda, HandleExecSpace>::value &&
610 std::is_same<int, local_ordinal_type>::value &&
611 (std::is_same<scalar_type, float>::value ||
612 std::is_same<scalar_type, double>::value ||
613 std::is_same<impl_scalar_type, Kokkos::complex<float>>::value ||
614 std::is_same<impl_scalar_type, Kokkos::complex<double>>::value)) {
615 return KokkosSparse::Experimental::SPTRSVAlgorithm::SPTRSV_CUSPARSE;
618 return KokkosSparse::Experimental::SPTRSVAlgorithm::SEQLVLSCHD_TP1;
621 template <
class MatrixType>
624 const char prefix[] =
"Ifpack2::LocalSparseTriangularSolver::compute: ";
625 if (!out_.is_null()) {
626 *out_ <<
">>> DEBUG " << prefix << std::endl;
629 if (!isKokkosKernelsStream_) {
631 "setMatrix() with a nonnull input matrix before you may call "
632 "initialize() or compute().");
634 "A_crs_ is null. Please report this bug to the Ifpack2 developers.");
638 "method, the matrix must be fill complete. It is not.");
640 for (
int i = 0; i < num_streams_; i++) {
642 "setMatrices() with a nonnull input matrix before you may call "
643 "initialize() or compute().");
647 "method, the matrix must be fill complete. It is not.");
651 if (!isInitialized_) {
655 "been called by this point, but isInitialized_ is false. "
656 "Please report this bug to the Ifpack2 developers.");
665 #if defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE) && (CUDA_VERSION >= 11030)
666 if constexpr (std::is_same_v<typename crs_matrix_type::execution_space, Kokkos::Cuda>) {
667 if (this->isKokkosKernelsSptrsv_) {
669 auto A_crs = Teuchos::rcp_dynamic_cast<
const crs_matrix_type>(A_crs_,
true);
670 auto Alocal = A_crs->getLocalMatrixDevice();
671 auto val = Alocal.values;
672 #if (CUSPARSE_VERSION >= 12100)
673 auto* sptrsv_handle = kh_->get_sptrsv_handle();
674 auto cusparse_handle = sptrsv_handle->get_cuSparseHandle();
675 cusparseSpSV_updateMatrix(cusparse_handle->handle,
676 cusparse_handle->spsvDescr,
678 CUSPARSE_SPSV_UPDATE_GENERAL);
680 auto ptr = Alocal.graph.row_map;
681 auto ind = Alocal.graph.entries;
682 KokkosSparse::sptrsv_symbolic(kh_.getRawPtr(), ptr, ind, val);
684 }
else if (kh_v_nonnull_) {
685 for (
int i = 0; i < num_streams_; i++) {
686 auto A_crs_i = Teuchos::rcp_dynamic_cast<
const crs_matrix_type>(A_crs_v_[i],
true);
687 auto Alocal_i = A_crs_i->getLocalMatrixDevice();
688 auto val_i = Alocal_i.values;
689 #if (CUSPARSE_VERSION >= 12100)
690 auto* sptrsv_handle = kh_v_[i]->get_sptrsv_handle();
691 auto cusparse_handle = sptrsv_handle->get_cuSparseHandle();
692 IFPACK2_DETAILS_CUSPARSE_SAFE_CALL(
693 cusparseSetStream(cusparse_handle->handle, exec_space_instances_[i].cuda_stream()));
694 cusparseSpSV_updateMatrix(cusparse_handle->handle,
695 cusparse_handle->spsvDescr,
697 CUSPARSE_SPSV_UPDATE_GENERAL);
699 auto ptr_i = Alocal_i.graph.row_map;
700 auto ind_i = Alocal_i.graph.entries;
701 KokkosSparse::sptrsv_symbolic(exec_space_instances_[i], kh_v_[i].getRawPtr(), ptr_i, ind_i, val_i);
711 htsImpl_->compute(*A_crs_, out_);
718 template <
class MatrixType>
729 using Teuchos::rcpFromRef;
732 const char prefix[] =
"Ifpack2::LocalSparseTriangularSolver::apply: ";
734 if (!out_.is_null()) {
735 *out_ <<
">>> DEBUG " << prefix;
736 if (!isKokkosKernelsStream_) {
737 if (A_crs_.is_null()) {
738 *out_ <<
"A_crs_ is null!" << std::endl;
742 const std::string uplo = this->uplo_;
744 const std::string diag = this->diag_;
745 *out_ <<
"uplo=\"" << uplo
746 <<
"\", trans=\"" << trans
747 <<
"\", diag=\"" << diag <<
"\"" << std::endl;
750 for (
int i = 0; i < num_streams_; i++) {
752 *out_ <<
"A_crs_v_[" << i <<
"]"
753 <<
" is null!" << std::endl;
755 const std::string uplo = this->uplo_;
757 const std::string diag = this->diag_;
758 *out_ <<
"A_crs_v_[" << i <<
"]: "
760 <<
"\", trans=\"" << trans
761 <<
"\", diag=\"" << diag <<
"\"" << std::endl;
768 "been called, or if you have changed the matrix via setMatrix(), you must "
769 "call compute() before you may call this method.");
772 if (!isKokkosKernelsStream_) {
774 "Please report this bug to the Ifpack2 developers.");
776 "Please report this bug to the Ifpack2 developers.");
781 "method, the matrix must be fill complete. It is not. This means that "
782 " you must have called resumeFill() on the matrix before calling apply(). "
783 "This is NOT allowed. Note that this class may use the matrix's data in "
784 "place without copying it. Thus, you cannot change the matrix and expect "
785 "the solver to stay the same. If you have changed the matrix, first call "
786 "fillComplete() on it, then call compute() on this object, before you call"
787 " apply(). You do NOT need to call setMatrix, as long as the matrix "
788 "itself (that is, its address in memory) is the same.");
790 for (
int i = 0; i < num_streams_; i++) {
792 "Please report this bug to the Ifpack2 developers.");
797 "method, the matrix must be fill complete. It is not. This means that "
798 " you must have called resumeFill() on the matrix before calling apply(). "
799 "This is NOT allowed. Note that this class may use the matrix's data in "
800 "place without copying it. Thus, you cannot change the matrix and expect "
801 "the solver to stay the same. If you have changed the matrix, first call "
802 "fillComplete() on it, then call compute() on this object, before you call"
803 " apply(). You do NOT need to call setMatrix, as long as the matrix "
804 "itself (that is, its address in memory) is the same.");
811 if (!isKokkosKernelsStream_) {
812 auto G = A_crs_->getGraph();
814 "but A_crs_'s RowGraph G is null. "
815 "Please report this bug to the Ifpack2 developers.");
816 auto importer = G->getImporter();
817 auto exporter = G->getExporter();
819 if (!importer.is_null()) {
820 if (X_colMap_.is_null() || X_colMap_->getNumVectors() != X.getNumVectors()) {
821 X_colMap_ =
rcp(
new MV(importer->getTargetMap(), X.getNumVectors()));
823 X_colMap_->putScalar(STS::zero());
828 X_colMap_->doImport(X, *importer, Tpetra::ZERO);
830 X_cur = importer.is_null() ? rcpFromRef(X) : Teuchos::rcp_const_cast<
const MV>(X_colMap_);
832 if (!exporter.is_null()) {
833 if (Y_rowMap_.is_null() || Y_rowMap_->getNumVectors() != Y.getNumVectors()) {
834 Y_rowMap_ =
rcp(
new MV(exporter->getSourceMap(), Y.getNumVectors()));
836 Y_rowMap_->putScalar(STS::zero());
838 Y_rowMap_->doExport(Y, *importer, Tpetra::ADD);
840 Y_cur = exporter.is_null() ? rcpFromRef(Y) : Y_rowMap_;
844 X_cur = rcpFromRef(X);
845 Y_cur = rcpFromRef(Y);
848 localApply(*X_cur, *Y_cur, mode, alpha, beta);
850 if (!isKokkosKernelsStream_) {
851 auto G = A_crs_->getGraph();
852 auto exporter = G->getExporter();
853 if (!exporter.is_null()) {
854 Y.putScalar(STS::zero());
855 Y.doExport(*Y_cur, *exporter, Tpetra::ADD);
861 template <
class MatrixType>
869 const char tfecfFuncName[] =
"localTriangularSolve: ";
871 if (!isKokkosKernelsStream_) {
873 "The matrix is not fill complete.");
875 "The matrix is neither upper triangular or lower triangular. "
876 "You may only call this method if the matrix is triangular. "
877 "Remember that this is a local (per MPI process) property, and that "
878 "Tpetra only knows how to do a local (per process) triangular solve.");
880 for (
int i = 0; i < num_streams_; i++) {
882 "The matrix is not fill complete.");
884 "The matrix is neither upper triangular or lower triangular. "
885 "You may only call this method if the matrix is triangular. "
886 "Remember that this is a local (per MPI process) property, and that "
887 "Tpetra only knows how to do a local (per process) triangular solve.");
891 "X and Y must be constant stride.");
895 "not currently support non-conjugated transposed solve (mode == "
896 "Teuchos::TRANS) for complex scalar types.");
898 const std::string uplo = this->uplo_;
900 const size_t numVecs = std::min(X.getNumVectors(), Y.getNumVectors());
902 if (
Teuchos::nonnull(kh_) && this->isKokkosKernelsSptrsv_ && trans ==
"N") {
903 auto A_crs = Teuchos::rcp_dynamic_cast<
const crs_matrix_type>(this->A_);
904 auto A_lclk = A_crs->getLocalMatrixDevice();
905 auto ptr = A_lclk.graph.row_map;
906 auto ind = A_lclk.graph.entries;
907 auto val = A_lclk.values;
909 for (
size_t j = 0; j < numVecs; ++j) {
910 auto X_j = X.getVectorNonConst(j);
911 auto Y_j = Y.getVector(j);
912 auto X_lcl = X_j->getLocalViewDevice(Tpetra::Access::ReadWrite);
913 auto Y_lcl = Y_j->getLocalViewDevice(Tpetra::Access::ReadOnly);
914 auto X_lcl_1d = Kokkos::subview(X_lcl, Kokkos::ALL(), 0);
915 auto Y_lcl_1d = Kokkos::subview(Y_lcl, Kokkos::ALL(), 0);
916 KokkosSparse::sptrsv_solve(kh_.getRawPtr(), ptr, ind, val, Y_lcl_1d, X_lcl_1d);
918 typename k_handle::HandleExecSpace().fence();
921 else if (kh_v_nonnull_ && this->isKokkosKernelsSptrsv_ && trans ==
"N") {
922 std::vector<lno_row_view_t> ptr_v(num_streams_);
923 std::vector<lno_nonzero_view_t> ind_v(num_streams_);
924 std::vector<scalar_nonzero_view_t> val_v(num_streams_);
925 std::vector<k_handle*> KernelHandle_rawptr_v_(num_streams_);
926 for (
size_t j = 0; j < numVecs; ++j) {
927 auto X_j = X.getVectorNonConst(j);
928 auto Y_j = Y.getVector(j);
929 auto X_lcl = X_j->getLocalViewDevice(Tpetra::Access::ReadWrite);
930 auto Y_lcl = Y_j->getLocalViewDevice(Tpetra::Access::ReadOnly);
931 auto X_lcl_1d = Kokkos::subview(X_lcl, Kokkos::ALL(), 0);
932 auto Y_lcl_1d = Kokkos::subview(Y_lcl, Kokkos::ALL(), 0);
933 std::vector<decltype(X_lcl_1d)> x_v(num_streams_);
934 std::vector<decltype(Y_lcl_1d)> y_v(num_streams_);
935 local_ordinal_type stream_begin = 0;
936 local_ordinal_type stream_end;
937 for (
int i = 0; i < num_streams_; i++) {
938 auto A_crs_i = Teuchos::rcp_dynamic_cast<
const crs_matrix_type>(A_crs_v_[i]);
939 auto Alocal_i = A_crs_i->getLocalMatrixDevice();
940 ptr_v[i] = Alocal_i.graph.row_map;
941 ind_v[i] = Alocal_i.graph.entries;
942 val_v[i] = Alocal_i.values;
943 stream_end = stream_begin + Alocal_i.numRows();
944 x_v[i] = Kokkos::subview(X_lcl, Kokkos::make_pair(stream_begin, stream_end), 0);
945 y_v[i] = Kokkos::subview(Y_lcl, Kokkos::make_pair(stream_begin, stream_end), 0);
947 KernelHandle_rawptr_v_[i] = kh_v_[i].getRawPtr();
948 stream_begin = stream_end;
951 KokkosSparse::Experimental::sptrsv_solve_streams(exec_space_instances_, KernelHandle_rawptr_v_,
952 ptr_v, ind_v, val_v, y_v, x_v);
957 const std::string diag = this->diag_;
962 auto A_lcl = this->A_crs_->getLocalMatrixHost();
964 if (X.isConstantStride() && Y.isConstantStride()) {
965 auto X_lcl = X.getLocalViewHost(Tpetra::Access::ReadWrite);
966 auto Y_lcl = Y.getLocalViewHost(Tpetra::Access::ReadOnly);
967 KokkosSparse::trsv(uplo.c_str(), trans.c_str(), diag.c_str(),
968 A_lcl, Y_lcl, X_lcl);
970 for (
size_t j = 0; j < numVecs; ++j) {
971 auto X_j = X.getVectorNonConst(j);
972 auto Y_j = Y.getVector(j);
973 auto X_lcl = X_j->getLocalViewHost(Tpetra::Access::ReadWrite);
974 auto Y_lcl = Y_j->getLocalViewHost(Tpetra::Access::ReadOnly);
975 KokkosSparse::trsv(uplo.c_str(), trans.c_str(),
976 diag.c_str(), A_lcl, Y_lcl, X_lcl);
982 template <
class MatrixType>
983 void LocalSparseTriangularSolver<MatrixType>::
984 localApply(
const MV& X,
987 const scalar_type& alpha,
988 const scalar_type& beta)
const {
990 htsImpl_->isComputed()) {
991 htsImpl_->localApply(X, Y, mode, alpha, beta);
996 typedef scalar_type ST;
999 if (beta == STS::zero()) {
1000 if (alpha == STS::zero()) {
1001 Y.putScalar(STS::zero());
1003 this->localTriangularSolve(X, Y, mode);
1004 if (alpha != STS::one()) {
1009 if (alpha == STS::zero()) {
1013 this->localTriangularSolve(X, Y_tmp, mode);
1014 Y.update(alpha, Y_tmp, beta);
1019 template <
class MatrixType>
1022 return numInitialize_;
1025 template <
class MatrixType>
1031 template <
class MatrixType>
1037 template <
class MatrixType>
1041 return initializeTime_;
1044 template <
class MatrixType>
1048 return computeTime_;
1051 template <
class MatrixType>
1058 template <
class MatrixType>
1062 std::ostringstream os;
1067 os <<
"\"Ifpack2::LocalSparseTriangularSolver\": {";
1068 if (this->getObjectLabel() !=
"") {
1069 os <<
"Label: \"" << this->getObjectLabel() <<
"\", ";
1071 os <<
"Initialized: " << (isInitialized() ?
"true" :
"false") <<
", "
1072 <<
"Computed: " << (isComputed() ?
"true" :
"false") <<
", ";
1074 if (isKokkosKernelsSptrsv_) os <<
"KK-SPTRSV, ";
1075 if (isKokkosKernelsStream_) os <<
"KK-SolveStream, ";
1078 os <<
"Matrix: null";
1080 os <<
"Matrix dimensions: ["
1081 << A_->getGlobalNumRows() <<
", "
1082 << A_->getGlobalNumCols() <<
"]"
1083 <<
", Number of nonzeros: " << A_->getGlobalNumEntries();
1087 os <<
", HTS computed: " << (htsImpl_->isComputed() ?
"true" :
"false");
1092 template <
class MatrixType>
1106 auto comm = A_.
is_null() ? Tpetra::getDefaultComm() : A_->getComm();
1110 if (!comm.is_null() && comm->getRank() == 0) {
1115 out <<
"\"Ifpack2::LocalSparseTriangularSolver\":" << endl;
1125 template <
class MatrixType>
1130 "Ifpack2::LocalSparseTriangularSolver::getDomainMap: "
1131 "The matrix is null. Please call setMatrix() with a nonnull input "
1132 "before calling this method.");
1133 return A_->getDomainMap();
1136 template <
class MatrixType>
1141 "Ifpack2::LocalSparseTriangularSolver::getRangeMap: "
1142 "The matrix is null. Please call setMatrix() with a nonnull input "
1143 "before calling this method.");
1144 return A_->getRangeMap();
1147 template <
class MatrixType>
1150 const char prefix[] =
"Ifpack2::LocalSparseTriangularSolver::setMatrix: ";
1159 A->getLocalNumRows() != A->getLocalNumCols(),
1160 std::runtime_error, prefix <<
"If A's communicator only contains one "
1161 "process, then A must be square. Instead, you provided a matrix A with "
1162 << A->getLocalNumRows() <<
" rows and " << A->getLocalNumCols() <<
" columns.");
1167 isInitialized_ =
false;
1168 isComputed_ =
false;
1171 A_crs_ = Teuchos::null;
1186 template <
class MatrixType>
1189 const std::vector<HandleExecSpace>& exec_space_instances) {
1190 isKokkosKernelsStream_ = isKokkosKernelsStream;
1191 num_streams_ = num_streams;
1192 exec_space_instances_ = exec_space_instances;
1193 A_crs_v_ = std::vector<Teuchos::RCP<crs_matrix_type>>(num_streams_);
1196 template <
class MatrixType>
1199 const char prefix[] =
"Ifpack2::LocalSparseTriangularSolver::setMatrixWithStreams: ";
1201 for (
int i = 0; i < num_streams_; i++) {
1206 if (A_crs_v[i].getRawPtr() != A_crs_v_[i].getRawPtr() || isInternallyChanged_) {
1209 A_crs_v[i]->getLocalNumRows() != A_crs_v[i]->getLocalNumCols(),
1210 std::runtime_error, prefix <<
"If A's communicator only contains one "
1211 "process, then A must be square. Instead, you provided a matrix A with "
1212 << A_crs_v[i]->getLocalNumRows() <<
" rows and " << A_crs_v[i]->getLocalNumCols() <<
" columns.");
1217 isInitialized_ =
false;
1218 isComputed_ =
false;
1221 A_crs_v_[i] = Teuchos::null;
1226 A_crs_v_[i] = A_crs;
1234 #define IFPACK2_LOCALSPARSETRIANGULARSOLVER_INSTANT(S, LO, GO, N) \
1235 template class Ifpack2::LocalSparseTriangularSolver<Tpetra::RowMatrix<S, LO, GO, N>>;
1237 #endif // IFPACK2_LOCALSPARSETRIANGULARSOLVER_DEF_HPP
bool is_null(const boost::shared_ptr< T > &p)
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, and put the result in Y.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:720
int getNumCompute() const
Return the number of calls to compute().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1027
std::string description() const
A one-line description of this object.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1061
T & get(const std::string &name, T def_value)
bool nonnull(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg)
void compute()
"Numeric" phase of setup
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:623
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
MatrixType::global_ordinal_type global_ordinal_type
Type of the global indices of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:62
bool isComputed() const
Return true if compute() has been called.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:191
double getApplyTime() const
Return the time spent in apply().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1054
double getComputeTime() const
Return the time spent in compute().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1047
double getInitializeTime() const
Return the time spent in initialize().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1040
MatrixType::node_type node_type
Node type of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:64
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setMatrices(const std::vector< Teuchos::RCP< crs_matrix_type > > &A_crs_v)
Set this preconditioner's matrices (used by stream interface of triangular solve).
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1198
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
Teuchos::RCP< const map_type > getDomainMap() const
The domain of this operator.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1128
"Preconditioner" that solves local sparse triangular systems.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:46
void initialize()
"Symbolic" phase of setup
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:392
int getNumInitialize() const
Return the number of calls to initialize().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1021
void setStreamInfo(const bool &isKokkosKernelsStream, const int &num_streams, const std::vector< HandleExecSpace > &exec_space_instances)
Set this triangular solver's stream information.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1188
Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > map_type
Specialization of Tpetra::Map used by this class.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:69
virtual void setMatrix(const Teuchos::RCP< const row_matrix_type > &A)
Set this preconditioner's matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1149
void resize(size_type new_size, const value_type &x=value_type())
IntegralType getIntegralValue(const std::string &str, const std::string ¶mName="", const std::string &sublistName="") const
int getNumApply() const
Return the number of calls to apply().
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1033
MatrixType::local_ordinal_type local_ordinal_type
Type of the local indices of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:60
Teuchos::RCP< const map_type > getRangeMap() const
The range of this operator.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1139
bool isType(const std::string &name) const
Tpetra::CrsMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > crs_matrix_type
Specialization of Tpetra::CrsMatrix used by this class.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:77
void setParameters(const Teuchos::ParameterList ¶ms)
Set this object's parameters.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:348
MatrixType::scalar_type scalar_type
Scalar type of the entries of the input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_decl.hpp:56
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print this object with given verbosity to the given output stream.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:1094
virtual ~LocalSparseTriangularSolver()
Destructor (virtual for memory safety).
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:332
LocalSparseTriangularSolver()
Constructor that takes no input matrix.
Definition: Ifpack2_LocalSparseTriangularSolver_def.hpp:296
static std::string name()
std::string typeName(const T &t)