55 #ifndef XPETRA_BLOCKEDCRSMATRIX_HPP 56 #define XPETRA_BLOCKEDCRSMATRIX_HPP 61 #include <Teuchos_SerialDenseMatrix.hpp> 62 #include <Teuchos_Hashtable.hpp> 78 #define sumAll(rcpComm, in, out) \ 79 Teuchos::reduceAll(*rcpComm, Teuchos::REDUCE_SUM, in, Teuchos::outArg(out)); 89 template <
class Scalar,
90 class LocalOrdinal = int,
91 class GlobalOrdinal = LocalOrdinal,
101 #ifdef HAVE_CTHULHU_TPETRA 120 Teuchos::RCP<const MapExtractorClass>& domainMaps,
132 for (
size_t r=0; r<
Rows(); ++r)
134 for(
size_t c=0; c<
Cols(); ++c)
166 void insertGlobalValues(GlobalOrdinal globalRow,
const ArrayView<const GlobalOrdinal> &cols,
const ArrayView<const Scalar> &vals)
169 "insertGlobalValues not supported by BlockedCrsMatrix" );
180 void insertLocalValues(LocalOrdinal localRow,
const ArrayView<const LocalOrdinal> &cols,
const ArrayView<const Scalar> &vals) {
182 "insertLocalValues not supported by BlockedCrsMatrix" );
187 "removeEmptyProcesses not supported by BlockedCrsMatrix");
197 const ArrayView<const GlobalOrdinal> &cols,
198 const ArrayView<const Scalar> &vals) {
200 "replaceGlobalValues not supported by BlockedCrsMatrix" );
208 const ArrayView<const LocalOrdinal> &cols,
209 const ArrayView<const Scalar> &vals) {
211 "replaceLocalValues not supported by BlockedCrsMatrix" );
217 "setAllToScalar not supported by BlockedCrsMatrix");
223 "scale not supported by BlockedCrsMatrix" );
242 "resumeFill not supported for block matrices" );
256 void fillComplete(
const RCP<const MapClass> &domainMap,
const RCP<const MapClass> &rangeMap,
const RCP<ParameterList> ¶ms=null)
259 "fillComplete with arguments not supported for block matrices" );
278 for (
size_t r=0; r<
Rows(); ++r)
280 for (
size_t c=0; c<
Cols(); ++c)
282 if(
getMatrix(r,c)->isFillComplete() ==
false)
295 std::vector<GlobalOrdinal> colmapentries;
296 for (
size_t c=0; c<
Cols(); ++c)
298 std::set<GlobalOrdinal> colset;
299 for (
size_t r=0; r<
Rows(); ++r)
302 Teuchos::RCP<const MapClass> colmap =
getMatrix(r,c)->getColMap();
303 copy(colmap->getNodeElementList().getRawPtr(),
304 colmap->getNodeElementList().getRawPtr()+colmap->getNodeNumElements(),
305 inserter(colset,colset.begin()));
308 colmapentries.reserve(colmapentries.size()+colset.size());
309 copy(colset.begin(), colset.end(), back_inserter(colmapentries));
310 sort(colmapentries.begin(), colmapentries.end());
311 typename std::vector<GlobalOrdinal>::iterator gendLocation;
312 gendLocation = std::unique(colmapentries.begin(), colmapentries.end());
313 colmapentries.erase(gendLocation,colmapentries.end());
317 size_t numGlobalElements = 0;
318 sumAll(
rangemaps_->getFullMap()->getComm(), colmapentries.size(), numGlobalElements)
320 const Teuchos::ArrayView<const GlobalOrdinal> aView = Teuchos::ArrayView<const GlobalOrdinal>(colmapentries);
336 for (
size_t r=0; r<
Rows(); ++r)
338 globalNumRows +=
getMatrix(r,0)->getGlobalNumRows();
340 return globalNumRows;
349 for (
size_t c=0; c<
Cols(); ++c)
351 globalNumCols +=
getMatrix(0,c)->getGlobalNumCols();
353 return globalNumCols;
360 for (
size_t r=0; r<
Rows(); ++r)
362 nodeNumRows +=
getMatrix(r,0)->getNodeNumRows();
371 for (
size_t r=0; r<
Rows(); ++r)
373 for(
size_t c=0; c<
Cols(); ++c)
375 globalNumEntries +=
getMatrix(r,c)->getGlobalNumEntries();
378 return globalNumEntries;
385 for (
size_t r=0; r<
Rows(); ++r)
387 for(
size_t c=0; c<
Cols(); ++c)
389 nodeNumEntries +=
getMatrix(r,c)->getNodeNumEntries();
392 return nodeNumEntries;
400 "getNumEntriesInLocalRow not supported by BlockedCrsMatrix" );
410 "getGlobalNumDiags() not supported by BlockedCrsMatrix" );
420 "getNodeNumDiags() not supported by BlockedCrsMatrix" );
430 "getGlobalMaxNumRowEntries() not supported by BlockedCrsMatrix" );
440 "getNodeMaxNumRowEntries() not supported by BlockedCrsMatrix" );
450 for (
size_t i=0; i<
blocks_.size(); ++i)
462 for (
size_t i=0; i<
blocks_.size(); ++i)
471 for (
size_t i=0; i<
blocks_.size(); ++i)
491 const ArrayView<LocalOrdinal> &Indices,
492 const ArrayView<Scalar> &Values,
497 "getLocalRowCopy not supported by BlockedCrsMatrix" );
510 void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView<const GlobalOrdinal> &indices, ArrayView<const Scalar> &values)
const 513 "getGlobalRowView not supported by BlockedCrsMatrix" );
526 void getLocalRowView(LocalOrdinal LocalRow, ArrayView<const LocalOrdinal> &indices, ArrayView<const Scalar> &values)
const 529 "getLocalRowView not supported by BlockedCrsMatrix" );
538 "getLocalDiagCopy not supported by BlockedCrsMatrix" );
545 "getFrobeniusNorm() not supported by BlockedCrsMatrix, yet" );
578 Teuchos::ETransp mode = Teuchos::NO_TRANS,
579 Scalar alpha = ScalarTraits<Scalar>::one(),
580 Scalar beta = ScalarTraits<Scalar>::zero())
const 584 Teuchos::RCP<const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > tX = Teuchos::rcp(&X,
false);
587 if (mode == Teuchos::NO_TRANS)
589 for(
size_t rblock=0; rblock<
Rows(); ++rblock)
591 Teuchos::RCP<MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > rowresult =
rangemaps_->getVector(rblock,Y.
getNumVectors());
592 Teuchos::RCP<MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > rowy =
rangemaps_->getVector(rblock,Y.
getNumVectors());
593 for (
size_t cblock=0; cblock<
Cols(); ++cblock)
595 Teuchos::RCP<const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > colx =
domainmaps_->ExtractVector(tX,cblock);
596 Teuchos::RCP<CrsMatrixClass> bmat =
getMatrix(rblock,cblock);
597 bmat->apply(*colx,*rowy);
598 rowresult->update(ScalarTraits< Scalar >::one(),*rowy,ScalarTraits< Scalar >::one());
600 rangemaps_->InsertVector(rowresult,rblock,tmpY);
602 Y.
update(alpha,*tmpY,beta);
604 else if (mode == Teuchos::TRANS)
607 for (
size_t cblock = 0; cblock<
Cols(); ++cblock)
609 Teuchos::RCP<MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > rowresult =
domainmaps_->getVector(cblock,Y.
getNumVectors());
610 Teuchos::RCP<MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > rowy =
domainmaps_->getVector(cblock,Y.
getNumVectors());
611 for (
size_t rblock = 0; rblock<
Rows(); ++rblock)
613 Teuchos::RCP<const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > colx =
rangemaps_->ExtractVector(tX,rblock);
614 Teuchos::RCP<CrsMatrixClass> bmat =
getMatrix(rblock,cblock);
615 bmat->apply(*colx,*rowy,Teuchos::TRANS);
616 rowresult->update(ScalarTraits< Scalar >::one(),*rowy,ScalarTraits< Scalar >::one());
620 Y.
update(alpha,*tmpY,beta);
624 "apply() only supports NO_TRANS and TRANS." );
674 const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > >
getMap()
const {
709 std::ostringstream oss;
710 oss <<
"Xpetra_BlockedCrsMatrix.description()" << std::endl;
715 void describe(Teuchos::FancyOStream &out,
const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default)
const {
724 out <<
"Xpetra::BlockedCrsMatrix: " <<
Rows() <<
" x " <<
Cols() << std::endl;
728 out <<
"BlockMatrix is filled" << std::endl;
729 out <<
"fullRowMap" << std::endl;
731 out <<
"fullColMap" << std::endl;
735 out <<
"BlockMatrix is NOT filled" << std::endl;
737 for (
size_t r=0; r<
Rows(); ++r)
739 for(
size_t c=0; c<
Cols(); ++c)
741 out <<
"Block(" << r <<
"," << c <<
")" << std::endl;
758 "getCrsGraph() not supported by BlockedCrsMatrix");
759 return Teuchos::null;
777 void setMatrix(
size_t r,
size_t c, Teuchos::RCP<CrsMatrixClass>& mat)
781 TEUCHOS_TEST_FOR_EXCEPTION(r >
Rows(), std::out_of_range,
"Error, r = " <<
Rows() <<
" is too big");
782 TEUCHOS_TEST_FOR_EXCEPTION(c >
Cols(), std::out_of_range,
"Error, c = " <<
Cols() <<
" is too big");
796 Teuchos::RCP<CrsMatrixClass>
Merge()
const 799 for (
size_t i=0; i<
blocks_.size(); ++i)
801 Teuchos::RCP<CrsMatrixClass> block = Teuchos::rcp_dynamic_cast<CrsMatrixClass>(
blocks_[i]);
802 this->
Add(block,ScalarTraits< Scalar >::one(),sparse,ScalarTraits< Scalar >::one());
826 void Add(Teuchos::RCP<CrsMatrixClass>& A,
const Scalar scalarA, Teuchos::RCP<CrsMatrixClass>& B,
const Scalar scalarB)
const 829 "Matrix A is not completed");
833 size_t MaxNumEntries = std::max(A->getNodeMaxNumRowEntries(),B->getNodeMaxNumRowEntries());
834 std::vector<GlobalOrdinal> vecIndices(MaxNumEntries);
835 std::vector<Scalar> vecValues (MaxNumEntries);
837 const Teuchos::ArrayView<GlobalOrdinal> Indices(&vecIndices[0], vecIndices.size());
838 const Teuchos::ArrayView<Scalar> Values (&vecValues[0], vecValues.size());
841 Teuchos::ArrayView<const GlobalOrdinal> MyGlobalRowIds = A->getRowMap()->getNodeElementList();
845 for(
size_t i=0; i<A->getNodeNumRows(); ++i)
847 GlobalOrdinal Row = MyGlobalRowIds[i];
850 A->getLocalRowCopy(i, Indices, Values, NumEntries);
852 if(scalarA != ScalarTraits< Scalar >::one())
853 for (
size_t j=0; j<NumEntries; ++j)
854 Values[j] *= scalarA;
858 for (
size_t j=0; j<NumEntries; ++j)
860 std::vector<GlobalOrdinal> tempVec; tempVec.push_back(Indices[j]);
861 std::vector<Scalar> tempVal; tempVal.push_back(Values[j]);
862 Teuchos::ArrayView<GlobalOrdinal> tempIndex(&tempVec[0], 1);
863 Teuchos::ArrayView<Scalar> tempValue(&tempVal[0], 1);
864 B->insertGlobalValues(Row, tempIndex, tempValue);
868 std::vector<GlobalOrdinal> tempvecIndices(NumEntries);
869 std::copy(Indices.getRawPtr(),
870 Indices.getRawPtr()+NumEntries,
871 std::inserter(tempvecIndices,tempvecIndices.begin()));
872 std::vector<Scalar> tempvecValues(NumEntries);
873 std::copy(Values.getRawPtr(),
874 Values.getRawPtr()+NumEntries,
875 std::inserter(tempvecValues,tempvecValues.begin()));
876 Teuchos::ArrayView<GlobalOrdinal> tempIndex(&tempvecIndices[0], NumEntries);
877 Teuchos::ArrayView<Scalar> tempValue(&tempvecValues[0], NumEntries);
878 B->insertGlobalValues(Row, tempIndex, tempValue);
910 std::vector<Teuchos::RCP<CrsMatrixClass> >
blocks_;
923 #define XPETRA_BLOCKEDCRSMATRIX_SHORT #define sumAll(rcpComm, in, out)
void doImport(const Matrix &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)
Import (using an Exporter).
Xpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > CrsMatrixClass
Xpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > CrsGraph
Teuchos::RCP< const MapExtractorClass > rangemaps_
the full range map together with all partial domain maps
void CreateView(viewLabel_t viewLabel, const RCP< const Map > &rowMap, const RCP< const Map > &colMap)
void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView< const GlobalOrdinal > &indices, ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of global indices in a specified row of the matrix.
const RCP< const MapClass > getDomainMap() const
Returns the Map associated with the full domain of this operator. This will be null until fillComplet...
void replaceGlobalValues(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Replace matrix entries, using global IDs.
virtual void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=ScalarTraits< Scalar >::one(), Scalar beta=ScalarTraits< Scalar >::zero()) const
Computes the sparse matrix-multivector multiplication.
const RCP< const MapExtractorClass > getRangeMapExtractor()
Returns map extractor class for range map.
const RCP< const MapClass > getRangeMap() const
size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
Teuchos::RCP< CrsMatrixClass > Merge() const
merge BlockedCrsMatrix blocks in a CrsMatrix
void doExport(const Matrix &dest, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)
Export (using an Importer).
void setMatrix(size_t r, size_t c, Teuchos::RCP< CrsMatrixClass > &mat)
set matrix block
void doExport(const Matrix &dest, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Export.
const RCP< const MapExtractorClass > getDomainMapExtractor()
Returns map extractor for domain map.
static Teuchos::RCP< Map< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalGlobal lg=Xpetra::GloballyDistributed, const Teuchos::RCP< Node > &node=Kokkos::Details::getNode< Node >())
Map constructor with Xpetra-defined contiguous uniform distribution.
Exception throws to report errors in the internal logical of the program.
std::vector< Teuchos::RCP< CrsMatrixClass > > blocks_
row major matrix block storage
void insertGlobalValues(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Insert matrix entries, using global IDs.
bool isFillComplete() const
Returns true if fillComplete() has been called and the matrix is in compute mode. ...
virtual void update(const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta)=0
Update multi-vector values with scaled values of A, this = beta*this + alpha*A.
virtual size_t Rows() const
number of row blocks
void getLocalDiagCopy(Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const
Get a copy of the diagonal entries owned by this node, with local row idices.
void insertLocalValues(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Insert matrix entries, using local IDs.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
RCP< const CrsGraph > getCrsGraph() const
Returns the CrsGraph associated with this matrix.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
Teuchos::RCP< MapClass > fullrowmap_
full matrix row map
viewLabel_t currentViewLabel_
size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
void getLocalRowView(LocalOrdinal LocalRow, ArrayView< const LocalOrdinal > &indices, ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of local indices in a specified row of the matrix.
void fillComplete(const RCP< ParameterList > ¶ms=null)
Signal that data entry is complete.
const RCP< const MapClass > getRangeMap(size_t i) const
Teuchos::RCP< const MapExtractorClass > domainmaps_
the full domain map together with all partial domain maps
virtual const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const =0
The Map describing the parallel distribution of this object.
void replaceLocalValues(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Replace matrix entries, using local IDs.
size_t global_size_t
Global size_t object.
Xpetra::CrsMatrixFactory< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > CrsMatrixFactory
void Add(Teuchos::RCP< CrsMatrixClass > &A, const Scalar scalarA, Teuchos::RCP< CrsMatrixClass > &B, const Scalar scalarB) const
Add a Xpetra::CrsMatrix to another: B = B*scalarB + A*scalarA.
global_size_t getGlobalNumRows() const
Returns the number of global rows.
Xpetra::MatrixView< LocalOrdinal, GlobalOrdinal, Node > MatrixView
BlockedCrsMatrix(Teuchos::RCP< const MapExtractorClass > &rangeMaps, Teuchos::RCP< const MapExtractorClass > &domainMaps, size_t npr, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor.
Teuchos::RCP< CrsMatrixClass > getMatrix(size_t r, size_t c) const
return block (r,c)
viewLabel_t defaultViewLabel_
std::string description() const
Return a simple one-line description of this object.
size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Returns the current number of entries on this node in the specified local row.
Xpetra::TpetraCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > TpetraCrsMatrix
const RCP< const MapClass > getDomainMap(size_t i) const
Returns the Map associated with the i'th block domain of this operator. This will be null until fillC...
virtual ~BlockedCrsMatrix()
Destructor.
bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
bool isLocallyIndexed() const
If matrix indices of all matrix blocks are in the local range, this function returns true...
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, size_t NumVectors, bool zeroOut=true)
Constructor specifying the number of non-zeros for all rows.
virtual ScalarTraits< Scalar >::magnitudeType getFrobeniusNorm() const
Get Frobenius norm of the matrix.
CombineMode
Xpetra::Combine Mode enumerable type.
virtual void getLocalRowCopy(LocalOrdinal LocalRow, const ArrayView< LocalOrdinal > &Indices, const ArrayView< Scalar > &Values, size_t &NumEntries) const
Extract a list of entries in a specified local row of the matrix. Put into storage allocated by calli...
void scale(const Scalar &alpha)
Scale the current values of a matrix, this = alpha*this.
virtual size_t Cols() const
number of column blocks
static RCP< CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile, const Teuchos::RCP< Teuchos::ParameterList > &plist=Teuchos::null)
Constructor specifying fixed number of entries for each row.
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const
Implements DistObject interface.
void resumeFill(const RCP< ParameterList > ¶ms=null)
size_t getNodeNumDiags() const
Returns the number of local diagonal entries, based on global row/column index comparisons.
const viewLabel_t & GetDefaultViewLabel() const
void removeEmptyProcessesInPlace(const Teuchos::RCP< const MapClass > &newMap)
size_t getNodeMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
size_t getNodeNumRows() const
Returns the number of matrix rows owned on the calling node.
global_size_t getGlobalNumCols() const
Returns the number of global columns in the matrix.
virtual void setAllToScalar(const Scalar &alpha)
Set all matrix entries equal to scalar.
Teuchos::RCP< MapClass > fullcolmap_
full matrix column map
void fillComplete(const RCP< const MapClass > &domainMap, const RCP< const MapClass > &rangeMap, const RCP< ParameterList > ¶ms=null)
Signal that data entry is complete, specifying domain and range maps.
virtual size_t getNumVectors() const =0
Number of columns in the multivector.
global_size_t getGlobalNumDiags() const
Returns the number of global diagonal entries, based on global row/column index comparisons.
Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > MapClass
Xpetra-specific matrix class.
global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > Matrix
void doImport(const Matrix &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Import.