42 #ifndef TPETRA_EXPERIMENTAL_BLOCKCRSMATRIX_DECL_HPP 43 #define TPETRA_EXPERIMENTAL_BLOCKCRSMATRIX_DECL_HPP 49 #include <Tpetra_ConfigDefs.hpp> 50 #include <Tpetra_CrsGraph.hpp> 51 #include <Tpetra_RowMatrix.hpp> 52 #include <Tpetra_Experimental_BlockMultiVector.hpp> 55 namespace Experimental {
126 class GO = Details::DefaultTypes::global_ordinal_type,
135 typedef Teuchos::ScalarTraits<Scalar> STS;
138 typedef char packet_type;
162 typedef ::Tpetra::Map<LO, GO, node_type>
map_type;
197 const map_type& domainPointMap,
198 const map_type& rangePointMap,
215 Teuchos::RCP<const map_type>
getRowMap ()
const;
218 Teuchos::RCP<const map_type>
getColMap ()
const;
238 apply (
const mv_type& X,
240 Teuchos::ETransp mode = Teuchos::NO_TRANS,
241 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one (),
242 Scalar beta = Teuchos::ScalarTraits<Scalar>::zero ())
const;
286 describe (Teuchos::FancyOStream& out,
287 const Teuchos::EVerbosityLevel verbLevel)
const;
297 virtual Teuchos::RCP<const Tpetra::RowGraph<LO,GO,Node> >
getGraph ()
const;
299 const crs_graph_type & getCrsGraph ()
const {
return graph_; }
308 Teuchos::ETransp mode = Teuchos::NO_TRANS,
309 const Scalar alpha = Teuchos::ScalarTraits<Scalar>::one (),
310 const Scalar beta = Teuchos::ScalarTraits<Scalar>::zero ());
319 const Scalar& dampingFactor,
322 const bool zeroInitialGuess)
const;
331 const ArrayView<LO>& rowIndices,
332 const Scalar& dampingFactor,
335 const bool zeroInitialGuess)
const;
349 const int * factorizationPivots,
375 const LO numColInds)
const;
399 const LO numColInds)
const;
419 Teuchos::ArrayView<const LO> &indices,
420 Teuchos::ArrayView<const Scalar> &values)
const;
425 const Teuchos::ArrayView<LO> &Indices,
426 const Teuchos::ArrayView<Scalar> &Values,
427 size_t &NumEntries)
const;
430 getLocalBlock (
const LO localRowInd,
const LO localColInd)
const;
459 const LO numColInds)
const;
468 const ptrdiff_t offsets[],
470 const LO numOffsets)
const;
479 const ptrdiff_t offsets[],
481 const LO numOffsets)
const;
526 return (*errs_).is_null () ? std::string (
"") : (*errs_)->str ();
573 const Teuchos::ArrayView<const size_t>& offsets)
const;
582 Teuchos::RCP<crs_graph_type> getDiagonalGraph ()
const;
590 const LO numColInds)
const;
595 const ptrdiff_t offsets[],
597 const LO numOffsets)
const;
612 const Teuchos::ArrayView<const LO>& permuteToLIDs,
613 const Teuchos::ArrayView<const LO>& permuteFromLIDs);
617 const Teuchos::ArrayView<const LO>& exportLIDs,
618 Teuchos::Array<packet_type>& exports,
619 const Teuchos::ArrayView<size_t>& numPacketsPerLID,
620 size_t& constantNumPackets,
624 unpackAndCombine (
const Teuchos::ArrayView<const LO> &importLIDs,
625 const Teuchos::ArrayView<const packet_type> &imports,
626 const Teuchos::ArrayView<size_t> &numPacketsPerLID,
627 size_t constantNumPackets,
634 crs_graph_type graph_;
635 Teuchos::RCP<crs_graph_type> graphRCP_;
644 map_type rowMeshMap_;
651 map_type domainPointMap_;
658 map_type rangePointMap_;
666 typename crs_graph_type::local_graph_type::row_map_type::HostMirror ptr_;
675 Teuchos::ArrayRCP<impl_scalar_type> valView_;
680 impl_scalar_type* val_;
703 Teuchos::RCP<Teuchos::RCP<BMV> > X_colMap_;
707 Teuchos::RCP<Teuchos::RCP<BMV> > Y_rowMap_;
732 Teuchos::RCP<bool> localError_;
741 Teuchos::RCP<Teuchos::RCP<std::ostringstream> > errs_;
744 std::ostream& markLocalErrorAndGetStream ();
761 const Teuchos::ETransp mode,
791 Teuchos::RCP<crs_graph_type> diagonalGraph_;
792 bool computedDiagonalGraph_;
827 findRelOffsetOfColumnIndex (
const LO localRowIndex,
828 const LO colIndexToFind,
829 const size_t hint)
const;
833 LO offsetPerBlock ()
const;
835 const_little_block_type
836 getConstLocalBlockFromInput (
const impl_scalar_type* val,
const size_t pointOffset)
const;
839 getNonConstLocalBlockFromInput (impl_scalar_type* val,
const size_t pointOffset)
const;
841 const_little_block_type
842 getConstLocalBlockFromAbsOffset (
const size_t absBlockOffset)
const;
845 getNonConstLocalBlockFromAbsOffset (
const size_t absBlockOffset)
const;
849 virtual Teuchos::RCP<const Teuchos::Comm<int> >
getComm()
const;
852 virtual Teuchos::RCP<Node>
getNode()
const;
950 const Teuchos::ArrayView<GO> &Indices,
951 const Teuchos::ArrayView<Scalar> &Values,
952 size_t& NumEntries)
const;
980 Teuchos::ArrayView<const GO>& indices,
981 Teuchos::ArrayView<const Scalar>& values)
const;
1029 template<
class Scalar,
class LO,
class GO,
class Node>
1031 Teuchos::ParameterList pl;
1033 out.open(fileName.c_str());
1038 template<
class Scalar,
class LO,
class GO,
class Node>
1041 out.open(fileName.c_str());
1046 template<
class Scalar,
class LO,
class GO,
class Node>
1048 Teuchos::ParameterList pl;
1061 template<
class Scalar,
class LO,
class GO,
class Node>
1067 typedef Teuchos::OrdinalTraits<GO> TOT;
1074 RCP<const map_type>
const rowMap = A.
getRowMap();
1075 RCP<const Teuchos::Comm<int> > comm = rowMap->getComm();
1076 const int myRank = comm->getRank();
1077 const size_t numProcs = comm->getSize();
1080 bool alwaysUseParallelAlgorithm =
false;
1081 if (params.isParameter(
"always use parallel algorithm"))
1082 alwaysUseParallelAlgorithm = params.get<
bool>(
"always use parallel algorithm");
1083 bool printMatrixMarketHeader =
true;
1084 if (params.isParameter(
"print MatrixMarket header"))
1085 printMatrixMarketHeader = params.get<
bool>(
"print MatrixMarket header");
1087 if (printMatrixMarketHeader && myRank==0) {
1088 std::time_t now = std::time(NULL);
1089 os <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
1090 os <<
"% time stamp: " << ctime(&now);
1091 os <<
"% written from " << numProcs <<
" processes" << std::endl;
1092 os <<
"% point representation of Tpetra::Experimental::BlockCrsMatrix" << std::endl;
1095 os <<
"% " << numRows <<
" block rows, " << numCols <<
" block columns" << std::endl;
1097 os <<
"% block size " << blockSize << std::endl;
1098 os << numRows*blockSize <<
" " << numCols*blockSize <<
" " << A.
getGlobalNumEntries()*blockSize*blockSize << std::endl;
1101 if (numProcs==1 && !alwaysUseParallelAlgorithm) {
1104 size_t numRows = rowMap->getNodeNumElements();
1107 RCP<const map_type> allMeshGidsMap = rcp(
new map_type(TOT::invalid(), numRows, A.
getIndexBase(), comm));
1110 mv_type allMeshGids(allMeshGidsMap,1);
1111 Teuchos::ArrayRCP<GO> allMeshGidsData = allMeshGids.getDataNonConst(0);
1113 for (
size_t i=0; i<numRows; i++)
1114 allMeshGidsData[i] = rowMap->getGlobalElement(i);
1115 allMeshGidsData = Teuchos::null;
1118 size_t stripSize = allMeshGids.getGlobalLength() / numProcs;
1119 size_t remainder = allMeshGids.getGlobalLength() % numProcs;
1120 size_t curStart = 0;
1121 size_t curStripSize = 0;
1122 Teuchos::Array<GO> importMeshGidList;
1123 for (
size_t i=0; i<numProcs; i++) {
1125 curStripSize = stripSize;
1126 if (i<remainder) curStripSize++;
1127 importMeshGidList.resize(curStripSize);
1128 for (
size_t j=0; j<curStripSize; j++) importMeshGidList[j] = j + curStart + A.
getIndexBase();
1129 curStart += curStripSize;
1132 TEUCHOS_TEST_FOR_EXCEPTION(myRank>0 && curStripSize!=0,
1133 std::runtime_error,
"Tpetra::Experimental::blockCrsMatrixWriter: (pid " 1134 << myRank <<
") map size should be zero, but is " << curStripSize);
1135 RCP<map_type> importMeshGidMap = rcp(
new map_type(TOT::invalid(), importMeshGidList(), A.
getIndexBase(), comm));
1136 import_type gidImporter(allMeshGidsMap, importMeshGidMap);
1137 mv_type importMeshGids(importMeshGidMap, 1);
1138 importMeshGids.doImport(allMeshGids, gidImporter,
INSERT);
1144 Teuchos::ArrayRCP<const GO> importMeshGidsData = importMeshGids.getData(0);
1145 Teuchos::Array<GO> importMeshGidsGO;
1146 importMeshGidsGO.reserve(importMeshGidsData.size());
1147 for (
typename Teuchos::ArrayRCP<const GO>::size_type j=0; j<importMeshGidsData.size(); ++j)
1148 importMeshGidsGO.push_back(importMeshGidsData[j]);
1149 RCP<const map_type> importMap = rcp(
new map_type(TOT::invalid(), importMeshGidsGO(), rowMap->getIndexBase(), comm) );
1151 import_type importer(rowMap,importMap );
1153 RCP<crs_graph_type> graph =
createCrsGraph(importMap,numEntriesPerRow);
1154 RCP<const map_type> domainMap = A.getCrsGraph().
getDomainMap();
1155 graph->doImport(A.getCrsGraph(), importer,
INSERT);
1156 graph->fillComplete(domainMap, importMap);
1158 block_crs_matrix_type importA(*graph, A.
getBlockSize());
1159 importA.doImport(A, importer,
INSERT);
1171 template<
class Scalar,
class LO,
class GO,
class Node>
1177 RCP<const map_type> rowMap = A.
getRowMap();
1178 RCP<const map_type> colMap = A.
getColMap();
1179 RCP<const Teuchos::Comm<int> > comm = rowMap->getComm();
1180 const int myRank = comm->getRank();
1182 const size_t meshRowOffset = rowMap->getIndexBase();
1183 const size_t meshColOffset = colMap->getIndexBase();
1184 TEUCHOS_TEST_FOR_EXCEPTION(meshRowOffset != meshColOffset,
1185 std::runtime_error,
"Tpetra::Experimental::writeMatrixStrip: " 1186 "mesh row index base != mesh column index base");
1191 std::runtime_error,
"Tpetra::Experimental::writeMatrixStrip: pid " 1192 << myRank <<
" should have 0 rows but has " << A.
getNodeNumRows());
1194 std::runtime_error,
"Tpetra::Experimental::writeMatrixStrip: pid " 1195 << myRank <<
" should have 0 columns but has " << A.
getNodeNumCols());
1200 std::runtime_error,
"Tpetra::Experimental::writeMatrixStrip: " 1201 "number of rows on pid 0 does not match global number of rows");
1207 bool precisionChanged=
false;
1209 if (params.isParameter(
"precision")) {
1210 oldPrecision = os.precision(params.get<
int>(
"precision"));
1211 precisionChanged=
true;
1213 int pointOffset = 1;
1214 if (params.isParameter(
"zero-based indexing")) {
1215 if (params.get<
bool>(
"zero-based indexing") ==
true)
1220 for (localRowInd = 0; localRowInd < numLocalRows; ++localRowInd) {
1223 const LO* localColInds;
1226 err = A.
getLocalRowView (localRowInd, localColInds, vals, numEntries);
1229 GO globalMeshRowID = rowMap->getGlobalElement(localRowInd) - meshRowOffset;
1231 for (LO k = 0; k < numEntries; ++k) {
1232 GO globalMeshColID = colMap->getGlobalElement(localColInds[k]) - meshColOffset;
1233 Scalar*
const curBlock = vals + blockSize * blockSize * k;
1235 for (LO j = 0; j < blockSize; ++j) {
1236 GO globalPointRowID = globalMeshRowID * blockSize + j + pointOffset;
1237 for (LO i = 0; i < blockSize; ++i) {
1238 GO globalPointColID = globalMeshColID * blockSize + i + pointOffset;
1239 const Scalar curVal = curBlock[i + j * blockSize];
1240 os << globalPointRowID <<
" " << globalPointColID <<
" " << curVal << std::endl;
1245 if (precisionChanged)
1246 os.precision(oldPrecision);
1247 TEUCHOS_TEST_FOR_EXCEPTION(err != 0,
1248 std::runtime_error,
"Tpetra::Experimental::writeMatrixStrip: " 1249 "error getting view of local row " << localRowInd);
1258 #endif // TPETRA_EXPERIMENTAL_BLOCKCRSMATRIX_DECL_HPP LO absMaxLocalValuesByOffsets(const LO localRowInd, const ptrdiff_t offsets[], const Scalar vals[], const LO numOffsets) const
Like sumIntoLocalValuesByOffsets, but for the ABSMAX combine mode.
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.
virtual bool supportsRowViews() const
Whether this object implements getLocalRowView() and getGlobalRowView().
virtual void copyAndPermute(const Tpetra::SrcDistObject &source, size_t numSameIDs, const Teuchos::ArrayView< const LO > &permuteToLIDs, const Teuchos::ArrayView< const LO > &permuteFromLIDs)
Perform copies and permutations that are local to this process.
virtual bool isLowerTriangular() const
Whether this matrix is lower triangular.
Teuchos::RCP< const map_type > getColMap() const
get the (mesh) map for the columns of this block matrix.
LO getBlockSize() const
The number of degrees of freedom per mesh point.
Teuchos::RCP< const map_type > getRowMap() const
get the (mesh) map for the rows of this block matrix.
LO replaceLocalValues(const LO localRowInd, const LO colInds[], const Scalar vals[], const LO numColInds) const
Replace values at the given column indices, in the given row.
Scalar scalar_type
The type of entries in the matrix.
LO replaceLocalValuesByOffsets(const LO localRowInd, const ptrdiff_t offsets[], const Scalar vals[], const LO numOffsets) const
Like replaceLocalValues, but avoids computing row offsets.
KokkosClassic::DefaultNode::DefaultNodeType node_type
Default value of Node template parameter.
void writeMatrixStrip(BlockCrsMatrix< Scalar, LO, GO, Node > const &A, std::ostream &os, Teuchos::ParameterList const ¶ms)
Helper function called by blockCrsMatrixWriter.
size_t getGlobalMaxNumRowEntries() const
Maximum number of entries in all rows over all processes.
size_t getNodeNumRows() const
get the local number of block rows
void getLocalDiagOffsets(Teuchos::ArrayRCP< size_t > &offsets) const
Get offsets of the diagonal entries in the matrix.
void applyBlock(const BlockMultiVector< Scalar, LO, GO, Node > &X, BlockMultiVector< Scalar, LO, GO, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, const Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), const Scalar beta=Teuchos::ScalarTraits< Scalar >::zero())
Version of apply() that takes BlockMultiVector input and output.
One or more distributed dense vectors.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Print a description of this object to the given output stream.
MultiVector for multiple degrees of freedom per mesh point.
GO global_ordinal_type
The type of global indices.
LO getLocalRowOffsets(const LO localRowInd, ptrdiff_t offsets[], const LO colInds[], const LO numColInds) const
Get relative offsets corresponding to the given rows, given by local row index.
global_size_t getGlobalNumRows() const
get the global number of block rows
Teuchos::RCP< CrsGraph< LocalOrdinal, GlobalOrdinal, Node, classic > > createCrsGraph(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, size_t maxNumEntriesPerRow=0, const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Nonmember function to create an empty CrsGraph given a row Map and the max number of entries allowed ...
Nonowning view of a square dense block in a block matrix.
void blockCrsMatrixWriter(BlockCrsMatrix< Scalar, LO, GO, Node > const &A, std::string const &fileName)
Helper function to write a BlockCrsMatrix. Calls the 3-argument version.
LO absMaxLocalValues(const LO localRowInd, const LO colInds[], const Scalar vals[], const LO numColInds) const
Like sumIntoLocalValues, but for the ABSMAX combine mode.
Teuchos::RCP< const map_type > getRangeMap() const
Get the (point) range Map of this matrix.
virtual bool isUpperTriangular() const
Whether this matrix is upper triangular.
int local_ordinal_type
Default value of LocalOrdinal template parameter.
mv_type::impl_scalar_type impl_scalar_type
The implementation type of entries in the matrix.
virtual bool checkSizes(const Tpetra::SrcDistObject &source)
Compare the source and target (this) objects for compatibility.
virtual bool hasColMap() const
Whether this matrix has a well-defined column map.
Teuchos::RCP< const map_type > getDomainMap() const
Get the (point) domain Map of this matrix.
void setAllToScalar(const Scalar &alpha)
Set all matrix entries equal to alpha.
virtual size_t getNodeNumDiags() const
The number of local diagonal entries, based on global row/column index comparisons.
virtual GO getIndexBase() const
The index base for global indices in this matrix.
virtual bool isGloballyIndexed() const
Whether matrix indices are globally indexed.
size_t global_size_t
Global size_t object.
Insert new values that don't currently exist.
std::string description() const
One-line description of this object.
BlockCrsMatrix()
Default constructor: Makes an empty block matrix.
Teuchos::RCP< const map_type > getDomainMap() const
Returns the Map associated with the domain of this graph.
virtual global_size_t getGlobalNumCols() const
The global number of columns of this matrix.
ESweepDirection
Sweep direction for Gauss-Seidel or Successive Over-Relaxation (SOR).
virtual bool isFillComplete() const
Whether fillComplete() has been called.
Nonowning view of a set of degrees of freedom corresponding to a mesh point in a block vector or mult...
Sets up and executes a communication plan for a Tpetra DistObject.
CombineMode
Rule for combining data in an Import or Export.
void reorderedGaussSeidelCopy(MultiVector< Scalar, LO, GO, Node > &X, const MultiVector< Scalar, LO, GO, Node > &B, const MultiVector< Scalar, LO, GO, Node > &D, const ArrayView< LO > &rowIndices, const Scalar &dampingFactor, const ESweepDirection direction, const int numSweeps, const bool zeroInitialGuess) const
Version of reorderedGaussSeidel(), with fewer requirements on X.
Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >::mag_type mag_type
Type of a norm result.
virtual size_t getNodeNumCols() const
The number of columns needed to apply the forward operator on this node.
virtual size_t getNodeNumEntries() const
The local number of stored (structurally nonzero) entries.
virtual void leftScale(const Vector< Scalar, LO, GO, Node > &x)
Scale the RowMatrix on the left with the given Vector x.
void getLocalDiagCopy(BlockCrsMatrix< Scalar, LO, GO, Node > &diag, const Teuchos::ArrayView< const size_t > &offsets) const
Variant of getLocalDiagCopy() that uses precomputed offsets.
virtual global_size_t getGlobalNumEntries() const
The global number of stored (structurally nonzero) entries.
virtual void getGlobalRowCopy(GO GlobalRow, const Teuchos::ArrayView< GO > &Indices, const Teuchos::ArrayView< Scalar > &Values, size_t &NumEntries) const
Get a copy of the given global row's entries.
void localGaussSeidel(const BlockMultiVector< Scalar, LO, GO, Node > &Residual, BlockMultiVector< Scalar, LO, GO, Node > &Solution, BlockCrsMatrix< Scalar, LO, GO, Node > &factorizedDiagonal, const int *factorizationPivots, const Scalar omega, const ESweepDirection direction) const
Local Gauss-Seidel solve given a factorized diagonal.
void getLocalRowCopy(LO LocalRow, const Teuchos::ArrayView< LO > &Indices, const Teuchos::ArrayView< Scalar > &Values, size_t &NumEntries) const
Not implemented.
Abstract base class for objects that can be the source of an Import or Export operation.
double scalar_type
Default value of Scalar template parameter.
LO getLocalRowView(const LO localRowInd, const LO *&colInds, Scalar *&vals, LO &numInds) const
Get a view of the row, using local indices.
void gaussSeidelCopy(MultiVector< Scalar, LO, GO, Node > &X, const MultiVector< Scalar, LO, GO, Node > &B, const MultiVector< Scalar, LO, GO, Node > &D, const Scalar &dampingFactor, const ESweepDirection direction, const int numSweeps, const bool zeroInitialGuess) const
Version of gaussSeidel(), with fewer requirements on X.
std::string errorMessages() const
The current stream of error messages.
Node node_type
The Kokkos Node type.
LO sumIntoLocalValues(const LO localRowInd, const LO colInds[], const Scalar vals[], const LO numColInds) const
Sum into values at the given column indices, in the given row.
bool isComputedDiagonalGraph() const
Reports on whether the DiagonalGraph has been Computed.
virtual size_t getGlobalMaxNumRowEntries() const
The maximum number of entries across all rows/columns on all nodes.
LO local_ordinal_type
The type of local indices.
virtual void rightScale(const Vector< Scalar, LO, GO, Node > &x)
Scale the RowMatrix on the right with the given Vector x.
virtual Tpetra::RowMatrix< Scalar, LO, GO, Node >::mag_type getFrobeniusNorm() const
The Frobenius norm of the matrix.
A read-only, row-oriented interface to a sparse matrix.
LO sumIntoLocalValuesByOffsets(const LO localRowInd, const ptrdiff_t offsets[], const Scalar vals[], const LO numOffsets) const
Like sumIntoLocalValues, but avoids computing row offsets.
A distributed dense vector.
Constant block CRS matrix class.
void apply(const mv_type &X, mv_type &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
For this matrix A, compute Y := beta * Y + alpha * Op(A) * X.
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
The communicator over which this matrix is distributed.
virtual global_size_t getGlobalNumDiags() const
The number of global diagonal entries, based on global row/column index comparisons.
bool localError() const
Whether this object had an error on the calling process.
BlockMultiVector< Scalar, LO, GO, Node >::impl_scalar_type impl_scalar_type
The implementation type of entries in the matrix.
virtual Teuchos::RCP< Node > getNode() const
The Kokkos Node instance.
virtual ~BlockCrsMatrix()
Destructor (declared virtual for memory safety).
Base class for distributed Tpetra objects that support data redistribution.
size_t getNodeMaxNumRowEntries() const
The maximum number of entries across all rows/columns on this node.
bool hasTransposeApply() const
Whether it is valid to apply the transpose or conjugate transpose of this matrix. ...
void computeDiagonalGraph()
Computes the DiagonalGraph.
virtual size_t getNumEntriesInGlobalRow(GO globalRow) const
The current number of entries on the calling process in the specified global row. ...
virtual Teuchos::RCP< const Tpetra::RowGraph< LO, GO, Node > > getGraph() const
Get the (mesh) graph.
size_t getNumEntriesInLocalRow(const LO localRowInd) const
Return the number of entries in the given row on the calling process.
virtual void getGlobalRowView(GO GlobalRow, Teuchos::ArrayView< const GO > &indices, Teuchos::ArrayView< const Scalar > &values) const
Get a constant, nonpersisting, globally indexed view of the given row of the matrix.
virtual bool isLocallyIndexed() const
Whether matrix indices are locally indexed.