42 #ifndef TPETRA_VBRMATRIX_DEF_HPP 43 #define TPETRA_VBRMATRIX_DEF_HPP 45 #include <Tpetra_BlockMap.hpp> 46 #include <Tpetra_BlockCrsGraph.hpp> 47 #include <Tpetra_Vector.hpp> 48 #include <Kokkos_NodeHelpers.hpp> 49 #include <Kokkos_VbrMatrix.hpp> 54 #ifdef DOXYGEN_USE_ONLY 60 template<
class Scalar,
class Node>
63 Kokkos::ReadyBufferHelper<Node> rbh(node);
64 Kokkos::InitOp<Scalar> wdp;
67 wdp.x = rbh.addNonConstBuffer(ptr);
69 node->template parallel_for<Kokkos::InitOp<Scalar> >(0, ptr.
size(), wdp);
73 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
75 :
DistObject<char, LocalOrdinal, GlobalOrdinal, Node>(convertBlockMapToPointMap(*blkRowMap)),
76 blkGraph_(
Teuchos::
rcp(new
BlockCrsGraph<LocalOrdinal,GlobalOrdinal,Node>(blkRowMap, maxNumEntriesPerRow, pftype))),
77 constBlkGraph_(blkGraph_),
78 lclMatrix_ (
Teuchos::
rcp (new
Kokkos::
VbrMatrix<Scalar, LocalOrdinal, Node> (blkRowMap->getNodeNumBlocks (), blkRowMap->getPointMap()->getNode ()))),
81 lclMatOps_(blkRowMap->getPointMap()->getNode()),
88 is_fill_completed_(false),
89 is_storage_optimized_(false)
98 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
102 constBlkGraph_(blkGraph),
114 is_fill_completed_(false),
115 is_storage_optimized_(false)
123 "Tpetra::VbrMatrix::VbrMatrix(BlockCrsGraph) ERROR, this constructor requires graph.isFillComplete()==true.");
125 createImporterExporter();
127 is_fill_completed_ =
true;
136 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
142 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
150 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
158 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
162 return is_fill_completed_;
166 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
167 template <
class DomainScalar,
class RangeScalar>
169 VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::multiply(
const MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> & X,
MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
Teuchos::ETransp trans, RangeScalar alpha, RangeScalar beta)
const 172 "Tpetra::VbrMatrix::multiply ERROR, multiply may only be called after fillComplete has been called.");
174 const Kokkos::MultiVector<Scalar,Node> *lclX = &X.
getLocalMV();
177 lclMatOps_.template multiply<Scalar,Scalar>(trans,alpha,*lclX,beta,*lclY);
181 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
182 template<
class DomainScalar,
class RangeScalar>
184 VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::solve(
const MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node>& Y,
MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node>& X,
Teuchos::ETransp trans)
const 187 "Tpetra::VbrMatrix::solve(X,Y): X and Y must be constant stride.");
190 "Tpetra::VbrMatrix::solve ERROR, solve may only be called after fillComplete has been called.");
192 TEUCHOS_TEST_FOR_EXCEPTION(constBlkGraph_->isUpperTriangular()==
false && constBlkGraph_->isLowerTriangular()==
false, std::runtime_error,
193 "Tpetra::VbrMatrix::solve ERROR, matrix must be either upper or lower triangular.");
195 const Kokkos::MultiVector<RangeScalar,Node> *lclY = &Y.
getLocalMV();
201 lclMatOps_.template solve<DomainScalar,RangeScalar>(trans,triang,diag, *lclY,*lclX);
205 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
209 if (importer_ != Teuchos::null) {
210 if (importedVec_ == Teuchos::null || importedVec_->getNumVectors() != X.
getNumVectors()) {
214 importedVec_->doImport(X, *importer_,
REPLACE);
219 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
223 if (exporter_ != Teuchos::null) {
224 if (exportedVec_ == Teuchos::null || exportedVec_->getNumVectors() != Y.
getNumVectors()) {
228 exportedVec_->doExport(Y, *exporter_,
REPLACE);
233 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
245 this->
template multiply<Scalar,Scalar>(Xref, Y, trans, alpha, beta);
250 this->
template multiply<Scalar,Scalar>(X, Yref, trans, alpha, beta);
251 if (importedVec_ != Teuchos::null) {
258 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
268 this->
template solve<Scalar,Scalar>(Y, Xref, trans);
273 this->
template solve<Scalar,Scalar>(Yref, X, trans);
274 if (importedVec_ != Teuchos::null) {
275 X.doExport(*importedVec_, *importer_,
ADD);
281 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
289 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
293 return constBlkGraph_->getBlockRowMap();
297 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
301 return constBlkGraph_->getBlockRowMap()->getPointMap();
305 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
309 return constBlkGraph_->getBlockColMap();
313 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
317 return constBlkGraph_->getBlockDomainMap();
321 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
325 return constBlkGraph_->getBlockRangeMap();
329 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
333 return constBlkGraph_->getBlockColMap()->getPointMap();
337 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
340 GlobalOrdinal globalBlockRow,
341 LocalOrdinal& numPtRows,
347 "Tpetra::VbrMatrix::getGlobalBlockRowView internal ERROR, isFillComplete() is required to be false.");
351 LocalOrdinal localRow =
getBlockRowMap()->getLocalBlockID(globalBlockRow);
353 constBlkGraph_->getGlobalBlockRowView(globalBlockRow, blockCols);
355 blockEntries.resize(blockCols.
size());
356 for(Tsize_t i=0; i<blockCols.
size(); ++i) {
358 numPtRows, ptColsPerBlockCol[i], blockEntries[i]);
363 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
366 LocalOrdinal localBlockRow,
367 LocalOrdinal& numPtRows,
373 "Tpetra::VbrMatrix::getGlobalBlockRowView internal ERROR, isFillComplete() is required to be true.");
380 constBlkGraph_->getLocalBlockRowView(localBlockRow, blockCols);
382 LocalOrdinal num_scalars_in_block_row = 0;
383 for(Tsize_t i=0; i<blockCols.
size(); ++i) {
384 ptColsPerBlockCol[i] =
getBlockColMap()->getLocalBlockSize(blockCols[i]);
385 num_scalars_in_block_row += numPtRows*ptColsPerBlockCol[i];
389 Host_View bpr = constBlkGraph_->getNodeRowOffsets();
390 const size_t bindx_offset = bpr[localBlockRow];
391 Host_View_LO indx = node->template viewBuffer<LocalOrdinal>(1, pbuf_indx_+bindx_offset);
392 const LocalOrdinal offset = indx[0];
393 blockEntries = node->template viewBuffer<Scalar>(num_scalars_in_block_row, pbuf_values1D_+offset);
397 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
400 GlobalOrdinal globalBlockRow,
401 GlobalOrdinal globalBlockCol,
402 LocalOrdinal& numPtRows,
403 LocalOrdinal& numPtCols,
411 LocalOrdinal localBlockRow =
getBlockRowMap()->getLocalBlockID(globalBlockRow);
414 VbrUtils::getGlobalBlockEntryViewNonConst(nonlocal_data_,
415 globalBlockRow, globalBlockCol,
416 numPtRows, numPtCols, blockEntry);
420 if (is_storage_optimized_) {
422 "Tpetra::VbrMatrix::getGlobalBlockEntryViewNonConst internal ERROR, storage is optimized but isFillComplete() is false.");
424 LocalOrdinal localBlockCol =
getBlockColMap()->getLocalBlockID(globalBlockCol);
427 numPtRows, numPtCols, blockEntry);
434 if (data_2D_->size() == 0) {
435 LocalOrdinal numBlockRows = constBlkGraph_->getNodeNumBlockRows();
436 data_2D_->resize(numBlockRows);
439 RowGlobalCols& blkrow = (*data_2D_)[localBlockRow];
441 typename RowGlobalCols::iterator col_iter = blkrow.find(globalBlockCol);
443 if (col_iter != blkrow.end()) {
444 blockEntry = col_iter->second;
451 "Tpetra::VbrMatrix::getGlobalBlockEntryViewNonConst ERROR: creating block-entry, but numPtRows and/or numPtCols is 0.");
454 size_t blockSize = numPtRows*numPtCols;
455 blockEntry = Teuchos::arcp(
new Scalar[blockSize], 0, blockSize);
456 std::fill(blockEntry.
begin(), blockEntry.
end(), 0);
457 blkrow.insert(std::make_pair(globalBlockCol, blockEntry));
463 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
466 GlobalOrdinal globalBlockRow,
467 GlobalOrdinal globalBlockCol,
468 LocalOrdinal& numPtRows,
469 LocalOrdinal& numPtCols,
475 if (is_storage_optimized_) {
477 "Tpetra::VbrMatrix::getGlobalBlockEntryView internal ERROR, storage is optimized but isFillComplete() is false.");
479 LocalOrdinal localBlockRow =
getBlockRowMap()->getLocalBlockID(globalBlockRow);
480 LocalOrdinal localBlockCol =
getBlockColMap()->getLocalBlockID(globalBlockCol);
486 "Tpetra::VbrMatrix::getGlobalBlockEntryView ERROR, matrix storage not yet allocated, can't return a const view.");
489 LocalOrdinal localBlockRow =
getBlockRowMap()->getLocalBlockID(globalBlockRow);
492 "Tpetra::VbrMatrix::getGlobalBlockEntryView, globalBlockRow not on the local processor.");
494 RowGlobalCols& blkrow = (*data_2D_)[localBlockRow];
495 typename RowGlobalCols::iterator col_iter = blkrow.find(globalBlockCol);
497 if (col_iter == blkrow.end()) {
498 throw std::runtime_error(
"Tpetra::VbrMatrix::getGlobalBlockEntryView ERROR, specified block-entry doesn't exist.");
504 "Tpetra::VbrMatrix::getGlobalBlockEntryView ERROR, numPtRows == 0.");
506 blockEntry = col_iter->second;
507 numPtCols = blockEntry.
size() / numPtRows;
511 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
514 LocalOrdinal localBlockRow,
515 LocalOrdinal localBlockCol,
516 LocalOrdinal& numPtRows,
517 LocalOrdinal& numPtCols,
522 typedef typename Host_View_LO::iterator ITER;
526 "Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, this method can only be called after fillComplete() has been called.");
529 "Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, this method can only be called if storage is optimized.");
533 Host_View bptr = constBlkGraph_->getNodeRowOffsets();
534 LocalOrdinal bindx_offset = bptr[localBlockRow];
535 LocalOrdinal length = bptr[localBlockRow+1] - bindx_offset;
538 "Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, specified localBlockCol not found in localBlockRow.");
540 Host_View_LO bindx = constBlkGraph_->getNodePackedIndices();
541 ITER bindx_beg = bindx.begin() + bindx_offset,
542 bindx_end = bindx_beg + length;
543 ITER it = std::lower_bound(bindx_beg, bindx_end, localBlockCol);
546 "Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, specified localBlockCol not found.");
551 const LocalOrdinal blkSize = numPtRows*numPtCols;
552 Host_View_LO indx = node->template viewBuffer<LocalOrdinal>(1,pbuf_indx_+bptr[localBlockRow]+(it-bindx_beg));
553 const LocalOrdinal offset = indx[0];
554 blockEntry = node->template viewBufferNonConst<Scalar>(Kokkos::ReadWrite, blkSize, pbuf_values1D_ + offset);
558 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
565 std::runtime_error,
"Tpetra::VbrMatrix::getLocalDiagCopy ERROR, vector must be distributed the same as this matrix' row-map.");
571 for(Tsize_t i=0; i<blockIDs.
size(); ++i) {
572 LocalOrdinal localBlockID = rowmap->getLocalBlockID(blockIDs[i]);
573 LocalOrdinal blockSize = rowmap->getLocalBlockSize(localBlockID);
577 for(LocalOrdinal j=0; j<blockSize; ++j) {
578 diag_view[offset++] = blockEntry[j*blockSize+j];
584 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
590 const this_type* srcVbrMat =
dynamic_cast<const this_type*
> (&source);
592 if (srcVbrMat == NULL) {
593 typedef VbrUtils::VbrDataDist<LocalOrdinal,GlobalOrdinal,Scalar,Node> VDD;
594 VDD* vdd =
const_cast<VDD*
> (
dynamic_cast<const VDD*
> (&source));
598 return (vdd != NULL);
601 bool ok = this->
getMap ()->getMinAllGlobalIndex() <=
602 srcVbrMat->getMap ()->getMinAllGlobalIndex ();
603 ok = ok && this->
getMap ()->getMaxAllGlobalIndex () >=
604 srcVbrMat->getMap ()->getMaxAllGlobalIndex ();
610 template<
class Scalar,
624 const this_type* src_mat_ptr =
dynamic_cast<const this_type*
> (&source);
626 if (src_mat_ptr == NULL) {
627 throw std::runtime_error (
"VbrMatrix::copyAndPermute ERROR, dynamic_cast failed.");
629 const this_type& src_mat = *src_mat_ptr;
632 LocalOrdinal numPtRows;
634 LocalOrdinal numSame = numSameIDs;
635 for(LocalOrdinal i=0; i<numSame; ++i) {
640 src_mat.getLocalBlockRowView (i, numPtRows, src_blkCols,
641 src_ptColsPerBlkCol, src_blkEntries);
643 for (Tsize_t j=0; j<src_blkCols.
size(); ++j) {
644 LocalOrdinal numPtCols = src_ptColsPerBlkCol[j];
646 src_blkEntries.
view (offset, numPtRows*numPtCols));
647 offset += numPtRows*numPtCols;
655 src_mat.getGlobalBlockRowView (gRow, numPtRows, src_blkCols,
656 src_ptColsPerBlkCol, src_blkEntries);
657 for (Tsize_t j = 0; j < src_blkCols.
size (); ++j) {
658 LocalOrdinal numPtCols = src_ptColsPerBlkCol[j];
660 numPtRows, numPtCols, numPtRows,
661 src_blkEntries[j].view (0, numPtRows * numPtCols));
666 for (Tsize_t i = 0; i < permuteToLIDs.
size (); ++i) {
671 src_mat.getLocalBlockRowView (permuteFromLIDs[i], numPtRows,
672 src_blkCols, src_ptColsPerBlkCol,
675 for (Tsize_t j=0; j<src_blkCols.
size(); ++j) {
676 LocalOrdinal numPtCols = src_ptColsPerBlkCol[j];
677 setLocalBlockEntry (permuteToLIDs[i], src_blkCols[j], numPtRows, numPtCols, numPtCols,
678 src_blkEntries.
view(offset, numPtRows*numPtCols));
679 offset += numPtRows*numPtCols;
683 GlobalOrdinal gRow =
getBlockRowMap ()->getGlobalBlockID (permuteToLIDs[i]);
687 src_mat.getGlobalBlockRowView (gRow, numPtRows, src_blkCols,
688 src_ptColsPerBlkCol, src_blkEntries);
689 for (Tsize_t j = 0; j < src_blkCols.
size (); ++j) {
690 LocalOrdinal numPtCols = src_ptColsPerBlkCol[j];
692 numPtRows, numPtCols, numPtRows,
693 src_blkEntries[j].view (0, numPtRows * numPtCols));
700 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
707 size_t& constantNumPackets,
712 const this_type* src_mat_ptr =
dynamic_cast<const this_type*
> (&source);
714 if (src_mat_ptr == NULL) {
715 typedef VbrUtils::VbrDataDist<LocalOrdinal,GlobalOrdinal,Scalar,Node> VDD;
716 VDD* vdd =
const_cast<VDD*
> (
dynamic_cast<const VDD*
> (&source));
718 vdd->
packAndPrepare (source, exportLIDs, exports, numPacketsPerLID,
719 constantNumPackets, distor);
721 throw std::runtime_error(
"VbrMatrix::packAndPrepare ERROR, dynamic_cast failed.");
725 const this_type& src_mat = *src_mat_ptr;
734 size_t total_exports_size = 0;
735 for (Tsize_t i = 0; i < exportLIDs.
size (); ++i) {
737 LocalOrdinal numPtRows;
738 LocalOrdinal numBlkCols = 0;
739 LocalOrdinal numScalars = 0;
741 if (src_mat.isFillComplete ()) {
744 src_mat.getLocalBlockRowView (exportLIDs[i], numPtRows,
745 src_blkCols, src_ptColsPerBlkCol,
747 numBlkCols = src_blkCols.
size ();
748 numScalars = src_blkEntries.
size ();
753 src_mat.getBlockRowMap ()->getGlobalBlockID (exportLIDs[i]);
755 src_mat.getGlobalBlockRowView (gRow, numPtRows, src_blkCols,
756 src_ptColsPerBlkCol, src_blkEntries);
757 numBlkCols = src_blkCols.
size ();
758 for (Tsize_t j = 0; j < src_blkEntries.
size (); ++j) {
759 numScalars += src_blkEntries[j].
size ();
763 const size_t size_for_this_row =
764 sizeof (GlobalOrdinal) * (2 + 2*numBlkCols) +
765 sizeof (Scalar) * numScalars;
766 numPacketsPerLID[i] = size_for_this_row;
767 total_exports_size += size_for_this_row;
770 exports.
resize (total_exports_size);
777 for (Tsize_t i = 0; i < exportLIDs.
size (); ++i) {
779 LocalOrdinal numPtRows;
780 LocalOrdinal numBlkCols = 0;
781 LocalOrdinal numScalars = 0;
783 if (src_mat.isFillComplete ()) {
786 src_mat.getLocalBlockRowView (exportLIDs[i], numPtRows, src_blkCols,
787 src_ptColsPerBlkCol, src_blkEntries);
788 numBlkCols = src_blkCols.
size ();
789 numScalars = src_blkEntries.
size ();
791 LocalOrdinal num_chars_for_ordinals =
792 (2*numBlkCols + 2) *
sizeof (LocalOrdinal);
794 avIndsC = exports (offset, num_chars_for_ordinals);
795 avValsC = exports (offset + num_chars_for_ordinals, numScalars *
sizeof (Scalar));
800 src_mat.getBlockColMap ();
803 *ind_it++ = numBlkCols;
804 *ind_it++ = numPtRows;
805 for (Tsize_t j = 0; j < src_blkCols.
size (); ++j) {
806 *ind_it++ = col_map->getGlobalBlockID (src_blkCols[j]);
808 std::copy (src_ptColsPerBlkCol.
begin(), src_ptColsPerBlkCol.
end(), ind_it);
809 avVals = av_reinterpret_cast<Scalar> (avValsC);
810 std::copy (src_blkEntries.
begin(), src_blkEntries.
end(), avVals.begin());
814 GlobalOrdinal gRow = src_mat.getBlockRowMap()->getGlobalBlockID(exportLIDs[i]);
816 src_mat.getGlobalBlockRowView (gRow, numPtRows, src_blkCols,
817 src_ptColsPerBlkCol, src_blkEntries);
818 numBlkCols = src_blkCols.
size();
820 for(Tsize_t j=0; j<src_blkEntries.
size(); ++j) {
821 numScalars += numPtRows*src_ptColsPerBlkCol[j];
824 LocalOrdinal num_chars_for_ordinals = (2*numBlkCols+2)*
sizeof(GlobalOrdinal);
826 avIndsC = exports(offset, num_chars_for_ordinals);
827 avValsC = exports(offset+ num_chars_for_ordinals, numScalars*
sizeof(Scalar));
832 *ind_it++ = numBlkCols;
833 *ind_it++ = numPtRows;
834 std::copy(src_blkCols.
begin(), src_blkCols.
end(), ind_it);
835 ind_it += src_blkCols.
size();
836 std::copy(src_ptColsPerBlkCol.
begin(), src_ptColsPerBlkCol.
end(), ind_it);
837 avVals = av_reinterpret_cast<Scalar>(avValsC);
839 for(Tsize_t j=0; j<src_blkEntries.
size(); ++j) {
840 std::copy(src_blkEntries[j].begin(), src_blkEntries[j].end(), val_it);
841 val_it += src_blkEntries[j].
size();
845 const size_t size_for_this_row =
846 sizeof (GlobalOrdinal) * (2 + 2*numBlkCols)
847 +
sizeof (Scalar) * numScalars;
848 offset += size_for_this_row;
851 constantNumPackets = 0;
855 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
861 size_t constantNumPackets,
868 std::cout <<
"Warning, VbrMatrix Import/Export doesn't support combine-mode==ABSMAX; use ADD, INSERT or REPLACE. (REPLACE is being used now.)"<<std::endl;
872 for(Tsize_t i=0; i<importLIDs.
size(); ++i) {
875 GlobalOrdinal gRow = this->
getBlockRowMap()->getGlobalBlockID(importLIDs[i]);
876 size_t avOrdsOffset = 0;
877 LocalOrdinal numBlkCols = avOrds[avOrdsOffset++];
878 LocalOrdinal numPtRows = avOrds[avOrdsOffset++];
879 LocalOrdinal num_chars_for_ordinals = (2*numBlkCols+2)*
sizeof(GlobalOrdinal);
883 size_t avValsOffset = 0;
884 for(Tsize_t j=0; j<numBlkCols; ++j) {
885 GlobalOrdinal blkCol = avOrds[avOrdsOffset+j];
886 LocalOrdinal numPtCols = avOrds[avOrdsOffset+numBlkCols+j];
887 LocalOrdinal LDA = numPtRows;
890 avVals.
view(avValsOffset, numPtRows*numPtCols));
894 avVals.
view(avValsOffset, numPtRows*numPtCols));
899 avVals.
view(avValsOffset, numPtRows*numPtCols));
901 avValsOffset += numPtRows*numPtCols;
907 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
910 LocalOrdinal localBlockRow,
911 LocalOrdinal localBlockCol,
912 LocalOrdinal& numPtRows,
913 LocalOrdinal& numPtCols,
918 typedef typename Host_View_LO::iterator ITER;
922 "Tpetra::VbrMatrix::getLocalBlockEntryView ERROR, this method can only be called after fillComplete() has been called.");
925 "Tpetra::VbrMatrix::getLocalBlockEntryView ERROR, this method can only be called if storage is optimized.");
929 Host_View bpr = constBlkGraph_->getNodeRowOffsets();
930 const size_t bindx_offset = bpr[localBlockRow];
931 const size_t length = bpr[localBlockRow+1] - bindx_offset;
933 Host_View_LO bindx = constBlkGraph_->getNodePackedIndices();
934 ITER bindx_beg = bindx.begin()+bindx_offset,
935 bindx_end = bindx_beg + length;
936 ITER it = std::lower_bound(bindx_beg, bindx_end, localBlockCol);
939 "Tpetra::VbrMatrix::getLocalBlockEntryView ERROR, specified localBlockCol not found.");
944 const LocalOrdinal blkSize = numPtRows*numPtCols;
945 Host_View_LO indx = node->template viewBuffer<LocalOrdinal>(1, pbuf_indx_+bpr[localBlockRow]+(it-bindx_beg));
946 const LocalOrdinal offset = indx[0];
947 blockEntry = node->template viewBuffer<Scalar>(blkSize, pbuf_values1D_ + offset);
951 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
959 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
966 fill_device_ArrayRCP(node, pbuf_values1D_, s);
971 Tsize_t numBlockRows = rows.
size();
972 for(Tsize_t r=0; r<numBlockRows; ++r) {
973 typename RowGlobalCols::iterator
974 col_iter = rows[r].
begin(),
975 col_end = rows[r].
end();
976 for(; col_iter != col_end; ++col_iter) {
978 std::fill(vals.
begin(), vals.
end(), s);
985 template<
class ArrayType1,
class ArrayType2,
class Ordinal>
986 void set_array_values(ArrayType1& dest, ArrayType2& src, Ordinal rows, Ordinal cols, Ordinal stride,
Tpetra::CombineMode mode)
989 size_t src_offset = 0;
992 for(Ordinal col=0; col<cols; ++col) {
993 for(Ordinal row=0; row<rows; ++row) {
994 dest[offset++] += src[src_offset + row];
996 src_offset += stride;
1000 for(Ordinal col=0; col<cols; ++col) {
1001 for(Ordinal row=0; row<rows; ++row) {
1002 dest[offset++] = src[src_offset + row];
1004 src_offset += stride;
1010 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1016 LocalOrdinal blkRowSize = blockEntry.
numRows();
1017 LocalOrdinal blkColSize = blockEntry.
numCols();
1021 const Scalar* inputvalues = blockEntry.
values();
1022 set_array_values<Teuchos::ArrayRCP<Scalar>,
const Scalar*, LocalOrdinal >(
1023 internalBlockEntry, inputvalues, blkRowSize, blkColSize, blockEntry.
stride(),
REPLACE);
1027 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1033 LocalOrdinal blkRowSize = blockEntry.
numRows();
1034 LocalOrdinal blkColSize = blockEntry.
numCols();
1038 const Scalar* inputvalues = blockEntry.
values();
1039 set_array_values<Teuchos::ArrayRCP<Scalar>,
const Scalar*, LocalOrdinal>(
1040 internalBlockEntry, inputvalues, blkRowSize, blkColSize, blockEntry.
stride(),
REPLACE);
1044 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1050 LocalOrdinal blkRowSize = blockEntry.
numRows();
1051 LocalOrdinal blkColSize = blockEntry.
numCols();
1055 const Scalar* inputvalues = blockEntry.
values();
1056 set_array_values<Teuchos::ArrayRCP<Scalar>,
const Scalar*, LocalOrdinal>(
1057 internalBlockEntry, inputvalues, blkRowSize, blkColSize, blockEntry.
stride(),
ADD);
1061 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1067 LocalOrdinal blkRowSize = blockEntry.
numRows();
1068 LocalOrdinal blkColSize = blockEntry.
numCols();
1072 const Scalar* inputvalues = blockEntry.
values();
1073 set_array_values<Teuchos::ArrayRCP<Scalar>,
const Scalar*, LocalOrdinal>(
1074 internalBlockEntry, inputvalues, blkRowSize, blkColSize, blockEntry.
stride(),
ADD);
1078 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1086 LocalOrdinal blk_size = blockEntry.
size();
1088 "Tpetra::VbrMatrix::setGlobalBlockEntry ERROR, inconsistent block-entry sizes.");
1091 set_array_values(internalBlockEntry, blockEntry, blkRowSize, blkColSize, LDA,
REPLACE);
1095 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1103 LocalOrdinal blk_size = blockEntry.
size();
1105 "Tpetra::VbrMatrix::setLocalBlockEntry ERROR, inconsistent block-entry sizes.");
1108 set_array_values(internalBlockEntry, blockEntry, blkRowSize, blkColSize, LDA,
REPLACE);
1112 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1120 LocalOrdinal blk_size = blockEntry.
size();
1122 "Tpetra::VbrMatrix::setGlobalBlockEntry ERROR, inconsistent block-entry sizes.");
1125 set_array_values(internalBlockEntry, blockEntry, blkRowSize, blkColSize, LDA,
ADD);
1129 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1137 LocalOrdinal blk_size = blockEntry.
size();
1139 "Tpetra::VbrMatrix::setLocalBlockEntry ERROR, inconsistent block-entry sizes.");
1142 set_array_values(internalBlockEntry, blockEntry, blkRowSize, blkColSize, LDA,
ADD);
1146 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1154 if (is_storage_optimized_ ==
true)
return;
1157 "Tpetra::VbrMatrix::optimizeStorage ERROR, isFillComplete() is false, required to be true before optimizeStorage is called.");
1159 size_t num_block_nonzeros = constBlkGraph_->getNodeNumBlockEntries();
1167 size_t num_point_entries = 0;
1168 Host_View bptr = constBlkGraph_->getNodeRowOffsets();
1170 if (bptr.size() == 0) {
1171 is_storage_optimized_ =
true;
1175 Host_View_LO bindx = constBlkGraph_->getNodePackedIndices();
1177 for(Tsize_t r=0; r<bptr.size()-1; ++r) {
1178 size_t rbeg = bptr[r];
1179 size_t rend = bptr[r+1];
1181 LocalOrdinal rsize = rowmap->getLocalBlockSize(r);
1183 for(
size_t j=rbeg; j<rend; ++j) {
1184 LocalOrdinal csize = colmap->getLocalBlockSize(bindx[j]);
1185 num_point_entries += rsize*csize;
1189 pbuf_indx_ = node->template allocBuffer<LocalOrdinal>(num_block_nonzeros+1);
1190 pbuf_values1D_ = node->template allocBuffer<Scalar>(num_point_entries);
1193 Teuchos::ArrayRCP<Scalar> view_values1D = node->template viewBufferNonConst<Scalar>(Kokkos::WriteOnly, num_point_entries, pbuf_values1D_);
1195 bool have_2D_data = data_2D_->size() > 0;
1200 for(Tsize_t r=0; r<bptr.size()-1; ++r) {
1201 LocalOrdinal rsize = rowmap->getLocalBlockSize(r);
1203 RowGlobalCols* blk_row = have_2D_data ? &((*data_2D_)[r]) : NULL;
1205 for(
size_t c=bptr[r]; c<bptr[r+1]; ++c) {
1206 view_indx[ioffset++] = offset;
1208 LocalOrdinal csize = colmap->getLocalBlockSize(bindx[c]);
1209 Tsize_t blkSize = rsize*csize;
1211 if (blk_row != NULL) {
1212 GlobalOrdinal global_col = colmap->getGlobalBlockID(bindx[c]);
1213 typename RowGlobalCols::iterator iter = blk_row->find(global_col);
1215 "Tpetra::VbrMatrix::optimizeStorage ERROR, global_col not found in row.");
1218 for(Tsize_t n=0; n<blkSize; ++n) {
1219 view_values1D[offset+n] = vals[n];
1223 for(Tsize_t n=0; n<blkSize; ++n) view_values1D[offset+n] = zero;
1228 view_indx[ioffset] = offset;
1231 data_2D_ = Teuchos::null;
1233 is_storage_optimized_ =
true;
1237 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1244 "Tpetra::VbrMatrix::fillLocalMatrix ERROR, optimizeStorage is required to have already been called.");
1248 if (Node::isHostNode ==
false) {
1252 node->copyToBuffer(nodeRowOffsets.
size(), nodeRowOffsets(), dev_nodeRowOffsets);
1253 nodeRowOffsets = dev_nodeRowOffsets;
1256 node->copyToBuffer(nodePackedInds.
size(), nodePackedInds(), dev_nodePackedInds);
1257 nodePackedInds = dev_nodePackedInds;
1259 lclMatrix_->setPackedValues (pbuf_values1D_,
1268 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1275 "Tpetra::VbrMatrix::fillLocalMatrix ERROR, optimizeStorage is required to have already been called.");
1277 lclMatOps_.initializeValues (*lclMatrix_);
1281 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1287 const PtMap& ptDomMap = (constBlkGraph_->getBlockDomainMap())->getPointMap();
1288 const PtMap& ptRngMap = (constBlkGraph_->getBlockRangeMap())->getPointMap();
1289 const PtMap& ptRowMap = (constBlkGraph_->getBlockRowMap())->getPointMap();
1290 const PtMap& ptColMap = (constBlkGraph_->getBlockColMap())->getPointMap();
1292 if (!ptDomMap->isSameAs(*ptColMap)) {
1295 if (!ptRngMap->isSameAs(*ptRowMap)) {
1301 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1309 blkGraph_->fillComplete(blockDomainMap, blockRangeMap);
1311 createImporterExporter();
1313 is_fill_completed_ =
true;
1322 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1332 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1343 overlapMap = VbrUtils::createOverlapMap(nonlocal_data_, *
getBlockRowMap());
1345 if (overlapMap->getGlobalNumBlocks() == 0) {
1352 VbrUtils::VbrDataDist<LocalOrdinal,GlobalOrdinal,Scalar,Node>
1358 importer( vdd.getMap(), convertBlockMapToPointMap(*
getBlockRowMap()));
1366 VbrUtils::zeroEntries(nonlocal_data_);
1370 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1374 std::ostringstream oss;
1377 oss <<
"{status = fill complete, global num block rows = " <<
getBlockRowMap()->getGlobalNumBlocks() <<
"}";
1380 oss <<
"{status = fill not complete, global num block rows = " <<
getBlockRowMap()->getGlobalNumBlocks() <<
"}";
1386 template<
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
1396 const int myProc = comm->getRank();
1398 if (myProc == 0) out <<
"VbrMatrix::describe is under construction..." << std::endl;
1409 #define TPETRA_VBRMATRIX_INSTANT(SCALAR,LO,GO,NODE) \ 1411 template class VbrMatrix< SCALAR , LO , GO , NODE >; 1413 #endif //TPETRA_VBRMATRIX_DEF_HPP ScalarType * values() const
void getLocalBlockEntryView(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, LocalOrdinal &numPtRows, LocalOrdinal &numPtCols, Teuchos::ArrayRCP< const Scalar > &blockEntry) const
Returns a const read-only view of a block-entry.
Block-entry counterpart to Tpetra::Map.
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getRangeMap() const
Returns the (point-entry) Map associated with the range of this operator.
VbrMatrix(const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > &blkRowMap, size_t maxNumEntriesPerRow, ProfileType pftype=DynamicProfile)
Constructor specifying the row-map and the max number of (block) non-zeros for all rows...
void setLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, const Teuchos::SerialDenseMatrix< int, Scalar > &blockEntry)
Copy the contents of the input block-entry into the matrix.
Teuchos::ArrayRCP< Scalar > get1dViewNonConst()
Nonconst persisting (1-D) view of this multivector's local values.
Block-entry counterpart to Tpetra::CrsGraph.
size_t getNumVectors() const
Number of columns in the multivector.
std::string description() const
One-line descriptiion of this object.
bool isConstantStride() const
Whether this multivector has constant stride between columns.
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockRangeMap() const
Returns the block-range map.
One or more distributed dense vectors.
bool isFillComplete() const
Return true if fillComplete has been called, false otherwise.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void putScalar(Scalar s)
Set the specified scalar throughout the matrix.
void fillComplete()
Transition the matrix to the packed, optimized-storage state.
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockRowMap() const
Returns the block-row map.
void sumIntoLocalBlockEntry(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, const Teuchos::SerialDenseMatrix< int, Scalar > &blockEntry)
Add the contents of the input block-entry into the matrix.
virtual void unpackAndCombine(const Teuchos::ArrayView< const LocalOrdinal > &importLIDs, const Teuchos::ArrayView< const char > &imports, const Teuchos::ArrayView< size_t > &numPacketsPerLID, size_t constantNumPackets, Distributor &distor, CombineMode CM)
Perform any unpacking and combining after communication.
VbrMatrix: Variable block row matrix.
virtual void copyAndPermute(const SrcDistObject &source, size_t numSameIDs, const Teuchos::ArrayView< const LocalOrdinal > &permuteToLIDs, const Teuchos::ArrayView< const LocalOrdinal > &permuteFromLIDs)
Perform copies and permutations that are local to this process.
void getLocalBlockEntryViewNonConst(LocalOrdinal localBlockRow, LocalOrdinal localBlockCol, LocalOrdinal &numPtRows, LocalOrdinal &numPtCols, Teuchos::ArrayRCP< Scalar > &blockEntry)
Returns a non-const read-write view of a block-entry.
void setGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix< int, Scalar > &blockEntry)
Copy the contents of the input block-entry into the matrix.
void applyInverse(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Teuchos::ETransp trans) const
Triangular Solve – Matrix must be triangular.
void getGlobalBlockEntryViewNonConst(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal &numPtRows, LocalOrdinal &numPtCols, Teuchos::ArrayRCP< Scalar > &blockEntry)
Returns a non-const read-write view of a block-entry.
void getGlobalBlockEntryView(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal &numPtRows, LocalOrdinal &numPtCols, Teuchos::ArrayRCP< const Scalar > &blockEntry) const
Returns a const read-only view of a block-entry.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void multiply(const MultiVector< DomainScalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< RangeScalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp trans, RangeScalar alpha, RangeScalar beta) const
Multiply this matrix by a MultiVector.
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getDomainMap() const
Returns the (point-entry) Map associated with the domain of this operator.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
Sets up and executes a communication plan for a Tpetra DistObject.
CombineMode
Tpetra::Combine Mode enumerable type.
virtual std::string description() const
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getMap() const
The Map describing the parallel distribution of this object.
virtual void packAndPrepare(const SrcDistObject &source, const Teuchos::ArrayView< const LocalOrdinal > &exportLIDs, Teuchos::Array< char > &exports, const Teuchos::ArrayView< size_t > &numPacketsPerLID, size_t &constantNumPackets, Distributor &distor)
Perform any packing or preparation required for communication.
Abstract base class for objects that can be the source of an Import or Export operation.
void resize(size_type new_size, const value_type &x=value_type())
void globalAssemble()
Communicate non-local contributions to the processors that own those contributions.
void getLocalDiagCopy(Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const
Return a copy of the (point-entry) diagonal values.
OrdinalType numCols() const
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getPointColMap() const
Returns the point-column map.
const Kokkos::MultiVector< Scalar, Node > & getLocalMV() const
Return a const reference to the underlying Kokkos::MultiVector object (advanced use only) ...
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockColMap() const
Returns the block-column map.
virtual ~VbrMatrix()
Destructor.
void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp trans=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Computes the operator-multivector application.
A distributed dense vector.
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getPointRowMap() const
Returns the point-row map.
void getGlobalBlockRowView(GlobalOrdinal globalBlockRow, LocalOrdinal &numPtRows, Teuchos::ArrayView< const GlobalOrdinal > &blockCols, Teuchos::Array< LocalOrdinal > &ptColsPerBlockCol, Teuchos::Array< Teuchos::ArrayRCP< const Scalar > > &blockEntries) const
Returns a const read-only view of the block-entries in the specified row.
ArrayView< T > view(size_type offset, size_type size) const
void doImport(const SrcDistObject &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Import data into this object using an Import object ("forward mode").
void solve(const MultiVector< RangeScalar, LocalOrdinal, GlobalOrdinal, Node > &Y, MultiVector< DomainScalar, LocalOrdinal, GlobalOrdinal, Node > &X, Teuchos::ETransp trans) const
Triangular Solve – Matrix must be triangular.
void sumIntoGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix< int, Scalar > &blockEntry)
Add the contents of the input block-entry into the matrix.
Declarations for the class Tpetra::VbrMatrix.
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockDomainMap() const
Returns the block-domain map.
Base class for distributed Tpetra objects that support data redistribution.
void getLocalBlockRowView(LocalOrdinal localBlockRow, LocalOrdinal &numPtRows, Teuchos::ArrayView< const LocalOrdinal > &blockCols, Teuchos::Array< LocalOrdinal > &ptColsPerBlockCol, Teuchos::ArrayRCP< const Scalar > &blockEntries) const
Returns a const read-only view of the block-entries in the specified row.
void doExport(const SrcDistObject &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)
Export data into this object using an Export object ("forward mode").
virtual bool checkSizes(const SrcDistObject &source)
Compare the source and target (this) objects for compatibility.
OrdinalType stride() const
bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
OrdinalType numRows() const
Kokkos::MultiVector< Scalar, Node > & getLocalMVNonConst()
Return a non-const reference to the underlying Kokkos::MultiVector object (advanced use only) ...
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to a FancyOStream object.
ArrayView< T > view(size_type lowerOffset, size_type size) const