42 #ifndef TPETRA_ROWMATRIX_DEF_HPP
43 #define TPETRA_ROWMATRIX_DEF_HPP
45 #include "Tpetra_CrsMatrix.hpp"
46 #include "Tpetra_Map.hpp"
47 #include "Tpetra_RowGraph.hpp"
51 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
54 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
55 Teuchos::RCP<RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
57 add (
const Scalar& alpha,
62 const Teuchos::RCP<Teuchos::ParameterList>& params)
const
65 using Teuchos::ArrayView;
66 using Teuchos::ParameterList;
69 using Teuchos::rcp_implicit_cast;
70 using Teuchos::sublist;
71 typedef LocalOrdinal LO;
72 typedef GlobalOrdinal GO;
73 typedef Teuchos::ScalarTraits<Scalar> STS;
78 const this_type& B = *
this;
86 RCP<const map_type> B_domainMap = B.getDomainMap ();
87 RCP<const map_type> B_rangeMap = B.getRangeMap ();
89 RCP<const map_type> theDomainMap = domainMap;
90 RCP<const map_type> theRangeMap = rangeMap;
92 if (domainMap.is_null ()) {
93 if (B_domainMap.is_null ()) {
94 TEUCHOS_TEST_FOR_EXCEPTION(
95 A_domainMap.is_null (), std::invalid_argument,
96 "Tpetra::RowMatrix::add: If neither A nor B have a domain Map, "
97 "then you must supply a nonnull domain Map to this method.");
98 theDomainMap = A_domainMap;
100 theDomainMap = B_domainMap;
103 if (rangeMap.is_null ()) {
104 if (B_rangeMap.is_null ()) {
105 TEUCHOS_TEST_FOR_EXCEPTION(
106 A_rangeMap.is_null (), std::invalid_argument,
107 "Tpetra::RowMatrix::add: If neither A nor B have a range Map, "
108 "then you must supply a nonnull range Map to this method.");
109 theRangeMap = A_rangeMap;
111 theRangeMap = B_rangeMap;
115 #ifdef HAVE_TPETRA_DEBUG
119 if (! A_domainMap.is_null () && ! A_rangeMap.is_null ()) {
120 if (! B_domainMap.is_null () && ! B_rangeMap.is_null ()) {
121 TEUCHOS_TEST_FOR_EXCEPTION(
122 ! B_domainMap->isSameAs (*A_domainMap), std::invalid_argument,
123 "Tpetra::RowMatrix::add: The input RowMatrix A must have a domain Map "
124 "which is the same as (isSameAs) this RowMatrix's domain Map.");
125 TEUCHOS_TEST_FOR_EXCEPTION(
126 ! B_rangeMap->isSameAs (*A_rangeMap), std::invalid_argument,
127 "Tpetra::RowMatrix::add: The input RowMatrix A must have a range Map "
128 "which is the same as (isSameAs) this RowMatrix's range Map.");
129 TEUCHOS_TEST_FOR_EXCEPTION(
130 ! domainMap.is_null () && ! domainMap->isSameAs (*B_domainMap),
131 std::invalid_argument,
132 "Tpetra::RowMatrix::add: The input domain Map must be the same as "
133 "(isSameAs) this RowMatrix's domain Map.");
134 TEUCHOS_TEST_FOR_EXCEPTION(
135 ! rangeMap.is_null () && ! rangeMap->isSameAs (*B_rangeMap),
136 std::invalid_argument,
137 "Tpetra::RowMatrix::add: The input range Map must be the same as "
138 "(isSameAs) this RowMatrix's range Map.");
141 else if (! B_domainMap.is_null () && ! B_rangeMap.is_null ()) {
142 TEUCHOS_TEST_FOR_EXCEPTION(
143 ! domainMap.is_null () && ! domainMap->isSameAs (*B_domainMap),
144 std::invalid_argument,
145 "Tpetra::RowMatrix::add: The input domain Map must be the same as "
146 "(isSameAs) this RowMatrix's domain Map.");
147 TEUCHOS_TEST_FOR_EXCEPTION(
148 ! rangeMap.is_null () && ! rangeMap->isSameAs (*B_rangeMap),
149 std::invalid_argument,
150 "Tpetra::RowMatrix::add: The input range Map must be the same as "
151 "(isSameAs) this RowMatrix's range Map.");
154 TEUCHOS_TEST_FOR_EXCEPTION(
155 domainMap.is_null () || rangeMap.is_null (), std::invalid_argument,
156 "Tpetra::RowMatrix::add: If neither A nor B have a domain and range "
157 "Map, then you must supply a nonnull domain and range Map to this "
160 #endif // HAVE_TPETRA_DEBUG
165 bool callFillComplete =
true;
166 RCP<ParameterList> constructorSublist;
167 RCP<ParameterList> fillCompleteSublist;
168 if (! params.is_null ()) {
169 callFillComplete = params->get (
"Call fillComplete", callFillComplete);
170 constructorSublist = sublist (params,
"Constructor parameters");
171 fillCompleteSublist = sublist (params,
"fillComplete parameters");
174 RCP<const map_type> A_rowMap = A.
getRowMap ();
175 RCP<const map_type> B_rowMap = B.getRowMap ();
176 RCP<const map_type> C_rowMap = B_rowMap;
177 RCP<crs_matrix_type> C;
184 if (A_rowMap->isSameAs (*B_rowMap)) {
185 const LO localNumRows =
static_cast<LO
> (A_rowMap->getLocalNumElements ());
186 Array<size_t> C_maxNumEntriesPerRow (localNumRows, 0);
189 if (alpha != STS::zero ()) {
190 for (LO localRow = 0; localRow < localNumRows; ++localRow) {
192 C_maxNumEntriesPerRow[localRow] += A_numEntries;
196 if (beta != STS::zero ()) {
197 for (LO localRow = 0; localRow < localNumRows; ++localRow) {
198 const size_t B_numEntries = B.getNumEntriesInLocalRow (localRow);
199 C_maxNumEntriesPerRow[localRow] += B_numEntries;
203 if (constructorSublist.is_null ()) {
204 C = rcp (
new crs_matrix_type (C_rowMap, C_maxNumEntriesPerRow ()));
206 C = rcp (
new crs_matrix_type (C_rowMap, C_maxNumEntriesPerRow (),
207 constructorSublist));
218 TEUCHOS_TEST_FOR_EXCEPTION(
true,
219 std::invalid_argument,
220 "Tpetra::RowMatrix::add: The row maps must be the same for statically "
221 "allocated matrices in order to be sure that there is sufficient space "
222 "to do the addition");
225 #ifdef HAVE_TPETRA_DEBUG
226 TEUCHOS_TEST_FOR_EXCEPTION(C.is_null (), std::logic_error,
227 "Tpetra::RowMatrix::add: C should not be null at this point. "
228 "Please report this bug to the Tpetra developers.");
229 #endif // HAVE_TPETRA_DEBUG
233 using gids_type = nonconst_global_inds_host_view_type;
234 using vals_type = nonconst_values_host_view_type;
238 if (alpha != STS::zero ()) {
239 const LO A_localNumRows =
static_cast<LO
> (A_rowMap->getLocalNumElements ());
240 for (LO localRow = 0; localRow < A_localNumRows; ++localRow) {
242 const GO globalRow = A_rowMap->getGlobalElement (localRow);
243 if (A_numEntries > static_cast<size_t> (ind.size ())) {
244 Kokkos::resize(ind,A_numEntries);
245 Kokkos::resize(val,A_numEntries);
247 gids_type indView = Kokkos::subview(ind, std::make_pair((
size_t)0, A_numEntries));
248 vals_type valView = Kokkos::subview(val, std::make_pair((
size_t)0, A_numEntries));
251 if (alpha != STS::one ()) {
252 for (
size_t k = 0; k < A_numEntries; ++k) {
256 C->insertGlobalValues (globalRow, A_numEntries,
257 reinterpret_cast<const Scalar*>(valView.data()),
262 if (beta != STS::zero ()) {
263 const LO B_localNumRows =
static_cast<LO
> (B_rowMap->getLocalNumElements ());
264 for (LO localRow = 0; localRow < B_localNumRows; ++localRow) {
265 size_t B_numEntries = B.getNumEntriesInLocalRow (localRow);
266 const GO globalRow = B_rowMap->getGlobalElement (localRow);
267 if (B_numEntries > static_cast<size_t> (ind.size ())) {
268 Kokkos::resize(ind,B_numEntries);
269 Kokkos::resize(val,B_numEntries);
271 gids_type indView = Kokkos::subview(ind, std::make_pair((
size_t)0, B_numEntries));
272 vals_type valView = Kokkos::subview(val, std::make_pair((
size_t)0, B_numEntries));
273 B.getGlobalRowCopy (globalRow, indView, valView, B_numEntries);
275 if (beta != STS::one ()) {
276 for (
size_t k = 0; k < B_numEntries; ++k) {
280 C->insertGlobalValues (globalRow, B_numEntries,
281 reinterpret_cast<const Scalar*>(valView.data()),
286 if (callFillComplete) {
287 if (fillCompleteSublist.is_null ()) {
288 C->fillComplete (theDomainMap, theRangeMap);
290 C->fillComplete (theDomainMap, theRangeMap, fillCompleteSublist);
294 return rcp_implicit_cast<this_type> (C);
298 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
301 pack (
const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs,
302 Teuchos::Array<char>& exports,
303 const Teuchos::ArrayView<size_t>& numPacketsPerLID,
304 size_t& constantNumPackets)
const
306 #ifdef HAVE_TPETRA_DEBUG
307 const char tfecfFuncName[] =
"pack: ";
309 using Teuchos::reduceAll;
310 std::ostringstream msg;
313 this->packImpl (exportLIDs, exports, numPacketsPerLID,
315 }
catch (std::exception& e) {
320 const Teuchos::Comm<int>& comm = * (this->getComm ());
321 reduceAll<int, int> (comm, Teuchos::REDUCE_MAX,
322 lclBad, Teuchos::outArg (gblBad));
324 const int myRank = comm.getRank ();
325 const int numProcs = comm.getSize ();
326 for (
int r = 0; r < numProcs; ++r) {
327 if (r == myRank && lclBad != 0) {
328 std::ostringstream os;
329 os <<
"Proc " << myRank <<
": " << msg.str () << std::endl;
330 std::cerr << os.str ();
336 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
337 true, std::logic_error,
"packImpl() threw an exception on one or "
338 "more participating processes.");
342 this->packImpl (exportLIDs, exports, numPacketsPerLID,
344 #endif // HAVE_TPETRA_DEBUG
347 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
351 size_t& totalNumEntries,
352 const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs)
const
354 typedef LocalOrdinal LO;
355 typedef GlobalOrdinal GO;
356 typedef typename Teuchos::ArrayView<const LO>::size_type size_type;
358 const size_type numExportLIDs = exportLIDs.size ();
362 for (size_type i = 0; i < numExportLIDs; ++i) {
363 const LO lclRow = exportLIDs[i];
364 size_t curNumEntries = this->getNumEntriesInLocalRow (lclRow);
367 if (curNumEntries == Teuchos::OrdinalTraits<size_t>::invalid ()) {
370 totalNumEntries += curNumEntries;
381 const size_t allocSize =
382 static_cast<size_t> (numExportLIDs) *
sizeof (LO) +
383 totalNumEntries * (
sizeof (Scalar) +
sizeof (GO));
384 if (static_cast<size_t> (exports.size ()) < allocSize) {
385 exports.resize (allocSize);
389 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
391 RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
392 packRow (
char*
const numEntOut,
396 const LocalOrdinal lclRow)
const
398 using Teuchos::Array;
399 using Teuchos::ArrayView;
400 typedef LocalOrdinal LO;
401 typedef GlobalOrdinal GO;
404 const LO numEntLO =
static_cast<LO
> (numEnt);
405 memcpy (numEntOut, &numEntLO,
sizeof (LO));
407 if (this->supportsRowViews ()) {
408 if (this->isLocallyIndexed ()) {
412 local_inds_host_view_type indIn;
413 values_host_view_type valIn;
414 this->getLocalRowView (lclRow, indIn, valIn);
415 const map_type& colMap = * (this->getColMap ());
418 for (
size_t k = 0; k < numEnt; ++k) {
419 const GO gblIndIn = colMap.getGlobalElement (indIn[k]);
420 memcpy (indOut + k *
sizeof (GO), &gblIndIn,
sizeof (GO));
422 memcpy (valOut, valIn.data (), numEnt *
sizeof (Scalar));
424 else if (this->isGloballyIndexed ()) {
430 global_inds_host_view_type indIn;
431 values_host_view_type valIn;
432 const map_type& rowMap = * (this->getRowMap ());
433 const GO gblRow = rowMap.getGlobalElement (lclRow);
434 this->getGlobalRowView (gblRow, indIn, valIn);
435 memcpy (indOut, indIn.data (), numEnt *
sizeof (GO));
436 memcpy (valOut, valIn.data (), numEnt *
sizeof (Scalar));
447 if (this->isLocallyIndexed ()) {
448 nonconst_local_inds_host_view_type indIn(
"indIn",numEnt);
449 nonconst_values_host_view_type valIn(
"valIn",numEnt);
450 size_t theNumEnt = 0;
451 this->getLocalRowCopy (lclRow, indIn, valIn, theNumEnt);
452 if (theNumEnt != numEnt) {
455 const map_type& colMap = * (this->getColMap ());
458 for (
size_t k = 0; k < numEnt; ++k) {
459 const GO gblIndIn = colMap.getGlobalElement (indIn[k]);
460 memcpy (indOut + k *
sizeof (GO), &gblIndIn,
sizeof (GO));
462 memcpy (valOut, valIn.data(), numEnt *
sizeof (Scalar));
464 else if (this->isGloballyIndexed ()) {
465 nonconst_global_inds_host_view_type indIn(
"indIn",numEnt);
466 nonconst_values_host_view_type valIn(
"valIn",numEnt);
467 const map_type& rowMap = * (this->getRowMap ());
468 const GO gblRow = rowMap.getGlobalElement (lclRow);
469 size_t theNumEnt = 0;
470 this->getGlobalRowCopy (gblRow, indIn, valIn, theNumEnt);
471 if (theNumEnt != numEnt) {
474 memcpy (indOut, indIn.data(), numEnt *
sizeof (GO));
475 memcpy (valOut, valIn.data(), numEnt *
sizeof (Scalar));
486 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
488 RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
489 packImpl (
const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs,
490 Teuchos::Array<char>& exports,
491 const Teuchos::ArrayView<size_t>& numPacketsPerLID,
492 size_t& constantNumPackets)
const
494 using Teuchos::Array;
495 using Teuchos::ArrayView;
497 using Teuchos::av_reinterpret_cast;
499 typedef LocalOrdinal LO;
500 typedef GlobalOrdinal GO;
501 typedef typename ArrayView<const LO>::size_type size_type;
502 const char tfecfFuncName[] =
"packImpl: ";
504 const size_type numExportLIDs = exportLIDs.size ();
505 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
506 numExportLIDs != numPacketsPerLID.size (), std::invalid_argument,
507 "exportLIDs.size() = " << numExportLIDs <<
" != numPacketsPerLID.size()"
508 " = " << numPacketsPerLID.size () <<
".");
513 constantNumPackets = 0;
518 size_t totalNumEntries = 0;
519 allocatePackSpace (exports, totalNumEntries, exportLIDs);
520 const size_t bufSize =
static_cast<size_t> (exports.size ());
532 size_type firstBadIndex = 0;
533 size_t firstBadOffset = 0;
534 size_t firstBadNumBytes = 0;
536 bool packErr =
false;
538 char*
const exportsRawPtr = exports.getRawPtr ();
540 for (size_type i = 0; i < numExportLIDs; ++i) {
541 const LO lclRow = exportLIDs[i];
542 const size_t numEnt = this->getNumEntriesInLocalRow (lclRow);
546 numPacketsPerLID[i] = 0;
549 char*
const numEntBeg = exportsRawPtr + offset;
550 char*
const numEntEnd = numEntBeg +
sizeof (LO);
551 char*
const valBeg = numEntEnd;
552 char*
const valEnd = valBeg + numEnt *
sizeof (Scalar);
553 char*
const indBeg = valEnd;
554 const size_t numBytes =
sizeof (LO) +
555 numEnt * (
sizeof (Scalar) +
sizeof (GO));
556 if (offset > bufSize || offset + numBytes > bufSize) {
558 firstBadOffset = offset;
559 firstBadNumBytes = numBytes;
563 packErr = ! packRow (numEntBeg, valBeg, indBeg, numEnt, lclRow);
566 firstBadOffset = offset;
567 firstBadNumBytes = numBytes;
573 numPacketsPerLID[i] = numBytes;
584 TEUCHOS_TEST_FOR_EXCEPTION(
585 outOfBounds, std::logic_error,
"First invalid offset into 'exports' "
586 "pack buffer at index i = " << firstBadIndex <<
". exportLIDs[i]: "
587 << exportLIDs[firstBadIndex] <<
", bufSize: " << bufSize <<
", offset: "
588 << firstBadOffset <<
", numBytes: " << firstBadNumBytes <<
".");
589 TEUCHOS_TEST_FOR_EXCEPTION(
590 packErr, std::logic_error,
"First error in packRow() at index i = "
591 << firstBadIndex <<
". exportLIDs[i]: " << exportLIDs[firstBadIndex]
592 <<
", bufSize: " << bufSize <<
", offset: " << firstBadOffset
593 <<
", numBytes: " << firstBadNumBytes <<
".");
605 #define TPETRA_ROWMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
606 template class RowMatrix< SCALAR , LO , GO , NODE >;
609 #endif // TPETRA_ROWMATRIX_DEF_HPP
KOKKOS_INLINE_FUNCTION bool outOfBounds(const IntegerType x, const IntegerType exclusiveUpperBound)
Is x out of bounds? That is, is x less than zero, or greater than or equal to the given exclusive upp...
Sparse matrix that presents a row-oriented interface that lets users read or modify entries...
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const =0
The current number of entries on the calling process in the specified local row.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const =0
The Map associated with the range of this operator, which must be compatible with Y...
virtual ~RowMatrix()
Destructor (virtual for memory safety of derived classes).
virtual void getGlobalRowCopy(GlobalOrdinal GlobalRow, nonconst_global_inds_host_view_type &Indices, nonconst_values_host_view_type &Values, size_t &NumEntries) const =0
Get a copy of the given global row's entries.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const =0
The Map associated with the domain of this operator, which must be compatible with X...
virtual void pack(const Teuchos::ArrayView< const LocalOrdinal > &exportLIDs, Teuchos::Array< char > &exports, const Teuchos::ArrayView< size_t > &numPacketsPerLID, size_t &constantNumPackets) const
Pack this object's data for an Import or Export.
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.
A parallel distribution of indices over processes.
A read-only, row-oriented interface to a sparse matrix.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const =0
The Map that describes the distribution of rows over processes.