42 #ifndef TPETRA_ROWMATRIX_DEF_HPP 43 #define TPETRA_ROWMATRIX_DEF_HPP 45 #include <Tpetra_ConfigDefs.hpp> 46 #include <Tpetra_CrsMatrix.hpp> 47 #include <Tpetra_Map.hpp> 51 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
54 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
57 add (
const Scalar& alpha,
70 using Teuchos::rcp_implicit_cast;
71 using Teuchos::sublist;
72 typedef LocalOrdinal LO;
73 typedef GlobalOrdinal GO;
79 const this_type& B = *
this;
93 if (domainMap.is_null ()) {
96 A_domainMap.
is_null (), std::invalid_argument,
97 "Tpetra::RowMatrix::add: If neither A nor B have a domain Map, " 98 "then you must supply a nonnull domain Map to this method.");
99 theDomainMap = A_domainMap;
101 theDomainMap = B_domainMap;
104 if (rangeMap.is_null ()) {
107 A_rangeMap.
is_null (), std::invalid_argument,
108 "Tpetra::RowMatrix::add: If neither A nor B have a range Map, " 109 "then you must supply a nonnull range Map to this method.");
110 theRangeMap = A_rangeMap;
112 theRangeMap = B_rangeMap;
116 #ifdef HAVE_TPETRA_DEBUG 123 ! B_domainMap->isSameAs (*A_domainMap), std::invalid_argument,
124 "Tpetra::RowMatrix::add: The input RowMatrix A must have a domain Map " 125 "which is the same as (isSameAs) this RowMatrix's domain Map.");
127 ! B_rangeMap->isSameAs (*A_rangeMap), std::invalid_argument,
128 "Tpetra::RowMatrix::add: The input RowMatrix A must have a range Map " 129 "which is the same as (isSameAs) this RowMatrix's range Map.");
131 ! domainMap.is_null () && ! domainMap->isSameAs (*B_domainMap),
132 std::invalid_argument,
133 "Tpetra::RowMatrix::add: The input domain Map must be the same as " 134 "(isSameAs) this RowMatrix's domain Map.");
136 ! rangeMap.is_null () && ! rangeMap->isSameAs (*B_rangeMap),
137 std::invalid_argument,
138 "Tpetra::RowMatrix::add: The input range Map must be the same as " 139 "(isSameAs) this RowMatrix's range Map.");
144 ! domainMap.is_null () && ! domainMap->isSameAs (*B_domainMap),
145 std::invalid_argument,
146 "Tpetra::RowMatrix::add: The input domain Map must be the same as " 147 "(isSameAs) this RowMatrix's domain Map.");
149 ! rangeMap.is_null () && ! rangeMap->isSameAs (*B_rangeMap),
150 std::invalid_argument,
151 "Tpetra::RowMatrix::add: The input range Map must be the same as " 152 "(isSameAs) this RowMatrix's range Map.");
156 domainMap.is_null () || rangeMap.is_null (), std::invalid_argument,
157 "Tpetra::RowMatrix::add: If neither A nor B have a domain and range " 158 "Map, then you must supply a nonnull domain and range Map to this " 161 #endif // HAVE_TPETRA_DEBUG 166 bool callFillComplete =
true;
170 callFillComplete = params->
get (
"Call fillComplete", callFillComplete);
171 constructorSublist = sublist (params,
"Constructor parameters");
172 fillCompleteSublist = sublist (params,
"fillComplete parameters");
185 if (A_rowMap->isSameAs (*B_rowMap)) {
186 const LO localNumRows = as<LO> (A_rowMap->getNodeNumElements ());
190 if (alpha != STS::zero ()) {
191 for (LO localRow = 0; localRow < localNumRows; ++localRow) {
193 C_maxNumEntriesPerRow[localRow] += A_numEntries;
197 if (beta != STS::zero ()) {
198 for (LO localRow = 0; localRow < localNumRows; ++localRow) {
199 const size_t B_numEntries = B.getNumEntriesInLocalRow (localRow);
200 C_maxNumEntriesPerRow[localRow] += B_numEntries;
204 if (constructorSublist.
is_null ()) {
205 C =
rcp (
new crs_matrix_type (C_rowMap, C_maxNumEntriesPerRow,
208 C =
rcp (
new crs_matrix_type (C_rowMap, C_maxNumEntriesPerRow,
219 if (constructorSublist.
is_null ()) {
223 constructorSublist));
227 #ifdef HAVE_TPETRA_DEBUG 229 "Tpetra::RowMatrix::add: C should not be null at this point. " 230 "Please report this bug to the Tpetra developers.");
231 #endif // HAVE_TPETRA_DEBUG 238 if (alpha != STS::zero ()) {
239 const LO A_localNumRows = as<LO> (A_rowMap->getNodeNumElements ());
240 for (LO localRow = 0; localRow < A_localNumRows; ++localRow) {
242 const GO globalRow = A_rowMap->getGlobalElement (localRow);
243 if (A_numEntries > as<size_t> (ind.size ())) {
244 ind.resize (A_numEntries);
245 val.
resize (A_numEntries);
251 if (alpha != STS::one ()) {
252 for (
size_t k = 0; k < A_numEntries; ++k) {
256 C->insertGlobalValues (globalRow, indView, valView);
260 if (beta != STS::zero ()) {
261 const LO B_localNumRows = as<LO> (B_rowMap->getNodeNumElements ());
262 for (LO localRow = 0; localRow < B_localNumRows; ++localRow) {
263 size_t B_numEntries = B.getNumEntriesInLocalRow (localRow);
264 const GO globalRow = B_rowMap->getGlobalElement (localRow);
265 if (B_numEntries > as<size_t> (ind.size ())) {
266 ind.resize (B_numEntries);
267 val.
resize (B_numEntries);
271 B.getGlobalRowCopy (globalRow, indView, valView, B_numEntries);
273 if (beta != STS::one ()) {
274 for (
size_t k = 0; k < B_numEntries; ++k) {
278 C->insertGlobalValues (globalRow, indView, valView);
282 if (callFillComplete) {
283 if (fillCompleteSublist.
is_null ()) {
284 C->fillComplete (theDomainMap, theRangeMap);
286 C->fillComplete (theDomainMap, theRangeMap, fillCompleteSublist);
290 return rcp_implicit_cast<this_type> (C);
294 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
300 size_t& constantNumPackets,
306 using Teuchos::av_reinterpret_cast;
308 typedef LocalOrdinal LO;
309 typedef GlobalOrdinal GO;
312 const char tfecfFuncName[] =
"pack";
315 exportLIDs.
size() != numPacketsPerLID.
size(),
316 std::invalid_argument,
"exportLIDs.size() = " << exportLIDs.
size()
317 <<
"!= numPacketsPerLID.size() = " << numPacketsPerLID.
size() <<
".");
322 rowMapPtr.is_null (), std::runtime_error,
323 "The source object's row Map is null.");
324 const map_type& rowMap = *rowMapPtr;
329 constantNumPackets = 0;
333 const size_type numExportGIDs = exportGIDs.
size ();
334 for (size_type i = 0; i < numExportGIDs; ++i) {
335 exportGIDs[i] = rowMap.getGlobalElement (exportLIDs[i]);
356 const size_t sizeOfOrdValPair =
sizeof (GO) +
sizeof (Scalar);
357 size_t totalNumEntries = 0;
358 size_t maxRowLength = 0;
359 for (size_type i = 0; i < exportGIDs.size(); ++i) {
360 const size_t curNumEntries =
361 this->getNumEntriesInGlobalRow (exportGIDs[i]);
362 numPacketsPerLID[i] = curNumEntries * sizeOfOrdValPair;
363 totalNumEntries += curNumEntries;
364 maxRowLength = std::max (curNumEntries, maxRowLength);
371 if (totalNumEntries > 0) {
375 const size_t totalNumBytes = totalNumEntries * sizeOfOrdValPair;
376 exports.
resize (totalNumBytes);
379 Array<GO> inds (as<size_type> (maxRowLength));
382 size_t curOffsetInBytes = 0;
395 for (size_type i = 0; i < exportGIDs.size(); ++i) {
397 size_t curNumEntries = 0;
398 this->getGlobalRowCopy (exportGIDs[i], inds (), vals (), curNumEntries);
407 exports (curOffsetInBytes, curNumEntries *
sizeof (GO));
409 exports (curOffsetInBytes + curNumEntries *
sizeof (GO),
410 curNumEntries *
sizeof (Scalar));
413 ArrayView<GO> outInds = av_reinterpret_cast<GO> (outIndsChar);
419 std::copy (curVals.
begin(), curVals.
end(), outVals.begin());
420 curOffsetInBytes += sizeOfOrdValPair * curNumEntries;
423 #ifdef HAVE_TPETRA_DEBUG 425 std::logic_error,
": At end of method, the final offset bytes count " 426 "curOffsetInBytes=" << curOffsetInBytes <<
" does not equal the total " 427 "number of bytes packed totalNumBytes=" << totalNumBytes <<
". Please " 428 "report this bug to the Tpetra developers.");
429 #endif // HAVE_TPETRA_DEBUG 441 #define TPETRA_ROWMATRIX_INSTANT(SCALAR,LO,GO,NODE) \ 443 template class RowMatrix< SCALAR , LO , GO , NODE >; 446 #endif // TPETRA_ROWMATRIX_DEF_HPP Namespace Tpetra contains the class and methods constituting the Tpetra library.
Sparse matrix that presents a compressed sparse row interface.
virtual void getGlobalRowCopy(GlobalOrdinal GlobalRow, const Teuchos::ArrayView< GlobalOrdinal > &Indices, const Teuchos::ArrayView< Scalar > &Values, size_t &NumEntries) const =0
Get a copy of the given global row's entries.
virtual const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getRangeMap() const =0
Returns the Map associated with the range of this operator, which must be compatible with Y...
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getRowMap() const =0
The Map that describes the distribution of rows over processes.
virtual const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getDomainMap() const =0
Returns the Map associated with the domain of this operator, which must be compatible with X...
virtual ~RowMatrix()
Destructor (virtual for memory safety of derived classes).
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const =0
The current number of entries on the calling process in the specified local row.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Sets up and executes a communication plan for a Tpetra DistObject.
virtual void pack(const Teuchos::ArrayView< const LocalOrdinal > &exportLIDs, Teuchos::Array< char > &exports, const Teuchos::ArrayView< size_t > &numPacketsPerLID, size_t &constantNumPackets, Distributor &distor) const
Implementation of SrcDistObjectWithPack.
void resize(size_type new_size, const value_type &x=value_type())
virtual Teuchos::RCP< RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > add(const Scalar &alpha, const RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta, const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &domainMap=Teuchos::null, const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null) const
Return a new RowMatrix which is the result of beta*this + alpha*A.
Describes a parallel distribution of objects over processes.
A read-only, row-oriented interface to a sparse matrix.
TypeTo as(const TypeFrom &t)