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 "
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->getNodeNumElements ());
186 Array<size_t> C_maxNumEntriesPerRow (localNumRows, 0);
189 if (alpha != STS::zero ()) {
190 for (LO localRow = 0; localRow < localNumRows; ++localRow) {
191 const size_t A_numEntries = A.getNumEntriesInLocalRow (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 (),
207 C = rcp (
new crs_matrix_type (C_rowMap, C_maxNumEntriesPerRow (),
208 StaticProfile, constructorSublist));
219 TEUCHOS_TEST_FOR_EXCEPTION(
true,
220 std::invalid_argument,
221 "Tpetra::RowMatrix::add: The row maps must be the same for statically "
222 "allocated matrices in order to be sure that there is sufficient space "
223 "to do the addition");
226#ifdef HAVE_TPETRA_DEBUG
227 TEUCHOS_TEST_FOR_EXCEPTION(C.is_null (), std::logic_error,
228 "Tpetra::RowMatrix::add: C should not be null at this point. "
229 "Please report this bug to the Tpetra developers.");
234 using gids_type = nonconst_global_inds_host_view_type;
235 using vals_type = nonconst_values_host_view_type;
239 if (alpha != STS::zero ()) {
240 const LO A_localNumRows =
static_cast<LO
> (A_rowMap->getNodeNumElements ());
241 for (LO localRow = 0; localRow < A_localNumRows; ++localRow) {
242 size_t A_numEntries = A.getNumEntriesInLocalRow (localRow);
243 const GO globalRow = A_rowMap->getGlobalElement (localRow);
244 if (A_numEntries >
static_cast<size_t> (ind.size ())) {
245 Kokkos::resize(ind,A_numEntries);
246 Kokkos::resize(val,A_numEntries);
248 gids_type indView = Kokkos::subview(ind, std::make_pair((
size_t)0, A_numEntries));
249 vals_type valView = Kokkos::subview(val, std::make_pair((
size_t)0, A_numEntries));
250 A.getGlobalRowCopy (globalRow, indView, valView, A_numEntries);
252 if (alpha != STS::one ()) {
253 for (
size_t k = 0; k < A_numEntries; ++k) {
257 C->insertGlobalValues (globalRow, A_numEntries,
258 reinterpret_cast<const Scalar*
>(valView.data()),
263 if (beta != STS::zero ()) {
264 const LO B_localNumRows =
static_cast<LO
> (B_rowMap->getNodeNumElements ());
265 for (LO localRow = 0; localRow < B_localNumRows; ++localRow) {
266 size_t B_numEntries = B.getNumEntriesInLocalRow (localRow);
267 const GO globalRow = B_rowMap->getGlobalElement (localRow);
268 if (B_numEntries >
static_cast<size_t> (ind.size ())) {
269 Kokkos::resize(ind,B_numEntries);
270 Kokkos::resize(val,B_numEntries);
272 gids_type indView = Kokkos::subview(ind, std::make_pair((
size_t)0, B_numEntries));
273 vals_type valView = Kokkos::subview(val, std::make_pair((
size_t)0, B_numEntries));
274 B.getGlobalRowCopy (globalRow, indView, valView, B_numEntries);
276 if (beta != STS::one ()) {
277 for (
size_t k = 0; k < B_numEntries; ++k) {
281 C->insertGlobalValues (globalRow, B_numEntries,
282 reinterpret_cast<const Scalar*
>(valView.data()),
287 if (callFillComplete) {
288 if (fillCompleteSublist.is_null ()) {
289 C->fillComplete (theDomainMap, theRangeMap);
291 C->fillComplete (theDomainMap, theRangeMap, fillCompleteSublist);
295 return rcp_implicit_cast<this_type> (C);
299 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
302 pack (
const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs,
303 Teuchos::Array<char>& exports,
304 const Teuchos::ArrayView<size_t>& numPacketsPerLID,
305 size_t& constantNumPackets)
const
307#ifdef HAVE_TPETRA_DEBUG
308 const char tfecfFuncName[] =
"pack: ";
310 using Teuchos::reduceAll;
311 std::ostringstream msg;
314 this->packImpl (exportLIDs, exports, numPacketsPerLID,
316 }
catch (std::exception& e) {
321 const Teuchos::Comm<int>& comm = * (this->getComm ());
322 reduceAll<int, int> (comm, Teuchos::REDUCE_MAX,
323 lclBad, Teuchos::outArg (gblBad));
325 const int myRank = comm.getRank ();
326 const int numProcs = comm.getSize ();
327 for (
int r = 0; r < numProcs; ++r) {
328 if (r == myRank && lclBad != 0) {
329 std::ostringstream os;
330 os <<
"Proc " << myRank <<
": " << msg.str () << std::endl;
331 std::cerr << os.str ();
337 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
338 true, std::logic_error,
"packImpl() threw an exception on one or "
339 "more participating processes.");
343 this->packImpl (exportLIDs, exports, numPacketsPerLID,
348 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
352 size_t& totalNumEntries,
353 const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs)
const
355 typedef LocalOrdinal LO;
356 typedef GlobalOrdinal GO;
357 typedef typename Teuchos::ArrayView<const LO>::size_type size_type;
359 const size_type numExportLIDs = exportLIDs.size ();
363 for (size_type i = 0; i < numExportLIDs; ++i) {
364 const LO lclRow = exportLIDs[i];
365 size_t curNumEntries = this->getNumEntriesInLocalRow (lclRow);
368 if (curNumEntries == Teuchos::OrdinalTraits<size_t>::invalid ()) {
371 totalNumEntries += curNumEntries;
382 const size_t allocSize =
383 static_cast<size_t> (numExportLIDs) *
sizeof (LO) +
384 totalNumEntries * (
sizeof (Scalar) +
sizeof (GO));
385 if (
static_cast<size_t> (exports.size ()) < allocSize) {
386 exports.resize (allocSize);
390 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
392 RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
393 packRow (
char*
const numEntOut,
397 const LocalOrdinal lclRow)
const
399 using Teuchos::Array;
400 using Teuchos::ArrayView;
401 typedef LocalOrdinal LO;
402 typedef GlobalOrdinal GO;
405 const LO numEntLO =
static_cast<LO
> (numEnt);
406 memcpy (numEntOut, &numEntLO,
sizeof (LO));
408 if (this->supportsRowViews ()) {
409 if (this->isLocallyIndexed ()) {
413 local_inds_host_view_type indIn;
414 values_host_view_type valIn;
415 this->getLocalRowView (lclRow, indIn, valIn);
416 const map_type& colMap = * (this->getColMap ());
419 for (
size_t k = 0; k < numEnt; ++k) {
420 const GO gblIndIn = colMap.getGlobalElement (indIn[k]);
421 memcpy (indOut + k *
sizeof (GO), &gblIndIn,
sizeof (GO));
423 memcpy (valOut, valIn.data (), numEnt * sizeof (Scalar));
425 else if (this->isGloballyIndexed ()) {
431 global_inds_host_view_type indIn;
432 values_host_view_type valIn;
433 const map_type& rowMap = * (this->getRowMap ());
434 const GO gblRow = rowMap.getGlobalElement (lclRow);
435 this->getGlobalRowView (gblRow, indIn, valIn);
436 memcpy (indOut, indIn.data (), numEnt * sizeof (GO));
437 memcpy (valOut, valIn.data (), numEnt * sizeof (Scalar));
448 if (this->isLocallyIndexed ()) {
449 nonconst_local_inds_host_view_type indIn(
"indIn",numEnt);
450 nonconst_values_host_view_type valIn(
"valIn",numEnt);
451 size_t theNumEnt = 0;
452 this->getLocalRowCopy (lclRow, indIn, valIn, theNumEnt);
453 if (theNumEnt != numEnt) {
456 const map_type& colMap = * (this->getColMap ());
459 for (
size_t k = 0; k < numEnt; ++k) {
460 const GO gblIndIn = colMap.getGlobalElement (indIn[k]);
461 memcpy (indOut + k *
sizeof (GO), &gblIndIn,
sizeof (GO));
463 memcpy (valOut, valIn.data(), numEnt * sizeof (Scalar));
465 else if (this->isGloballyIndexed ()) {
466 nonconst_global_inds_host_view_type indIn(
"indIn",numEnt);
467 nonconst_values_host_view_type valIn(
"valIn",numEnt);
468 const map_type& rowMap = * (this->getRowMap ());
469 const GO gblRow = rowMap.getGlobalElement (lclRow);
470 size_t theNumEnt = 0;
471 this->getGlobalRowCopy (gblRow, indIn, valIn, theNumEnt);
472 if (theNumEnt != numEnt) {
475 memcpy (indOut, indIn.data(), numEnt * sizeof (GO));
476 memcpy (valOut, valIn.data(), numEnt * sizeof (Scalar));
487 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
489 RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
490 packImpl (
const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs,
491 Teuchos::Array<char>& exports,
492 const Teuchos::ArrayView<size_t>& numPacketsPerLID,
493 size_t& constantNumPackets)
const
495 using Teuchos::Array;
496 using Teuchos::ArrayView;
498 using Teuchos::av_reinterpret_cast;
500 typedef LocalOrdinal LO;
501 typedef GlobalOrdinal GO;
502 typedef typename ArrayView<const LO>::size_type size_type;
503 const char tfecfFuncName[] =
"packImpl: ";
505 const size_type numExportLIDs = exportLIDs.size ();
506 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
507 numExportLIDs != numPacketsPerLID.size (), std::invalid_argument,
508 "exportLIDs.size() = " << numExportLIDs <<
" != numPacketsPerLID.size()"
509 " = " << numPacketsPerLID.size () <<
".");
514 constantNumPackets = 0;
519 size_t totalNumEntries = 0;
520 allocatePackSpace (exports, totalNumEntries, exportLIDs);
521 const size_t bufSize =
static_cast<size_t> (exports.size ());
533 size_type firstBadIndex = 0;
534 size_t firstBadOffset = 0;
535 size_t firstBadNumBytes = 0;
536 bool outOfBounds =
false;
537 bool packErr =
false;
539 char*
const exportsRawPtr = exports.getRawPtr ();
541 for (size_type i = 0; i < numExportLIDs; ++i) {
542 const LO lclRow = exportLIDs[i];
543 const size_t numEnt = this->getNumEntriesInLocalRow (lclRow);
547 numPacketsPerLID[i] = 0;
550 char*
const numEntBeg = exportsRawPtr + offset;
551 char*
const numEntEnd = numEntBeg +
sizeof (LO);
552 char*
const valBeg = numEntEnd;
553 char*
const valEnd = valBeg + numEnt *
sizeof (Scalar);
554 char*
const indBeg = valEnd;
555 const size_t numBytes =
sizeof (LO) +
556 numEnt * (
sizeof (Scalar) +
sizeof (GO));
557 if (offset > bufSize || offset + numBytes > bufSize) {
559 firstBadOffset = offset;
560 firstBadNumBytes = numBytes;
564 packErr = ! packRow (numEntBeg, valBeg, indBeg, numEnt, lclRow);
567 firstBadOffset = offset;
568 firstBadNumBytes = numBytes;
574 numPacketsPerLID[i] = numBytes;
585 TEUCHOS_TEST_FOR_EXCEPTION(
586 outOfBounds, std::logic_error,
"First invalid offset into 'exports' "
587 "pack buffer at index i = " << firstBadIndex <<
". exportLIDs[i]: "
588 << exportLIDs[firstBadIndex] <<
", bufSize: " << bufSize <<
", offset: "
589 << firstBadOffset <<
", numBytes: " << firstBadNumBytes <<
".");
590 TEUCHOS_TEST_FOR_EXCEPTION(
591 packErr, std::logic_error,
"First error in packRow() at index i = "
592 << firstBadIndex <<
". exportLIDs[i]: " << exportLIDs[firstBadIndex]
593 <<
", bufSize: " << bufSize <<
", offset: " << firstBadOffset
594 <<
", numBytes: " << firstBadNumBytes <<
".");
597#ifdef TPETRA_ENABLE_DEPRECATED_CODE
598 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
602 LocalOrdinal& numEnt,
603 const LocalOrdinal*& lclColInds,
604 const Scalar*& vals)
const
609 Teuchos::ArrayView<const LocalOrdinal> lclColInds_av;
610 Teuchos::ArrayView<const Scalar> vals_av;
612 this->getLocalRowView (lclRow, lclColInds_av, vals_av);
613 numEnt =
static_cast<LocalOrdinal
> (lclColInds_av.size ());
619 lclColInds = lclColInds_av.getRawPtr ();
620 vals = vals_av.getRawPtr ();
623 return static_cast<LocalOrdinal
> (0);
635#define TPETRA_ROWMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
636 template class RowMatrix< SCALAR , LO , GO , NODE >;
Sparse matrix that presents a row-oriented interface that lets users read or modify entries.
A parallel distribution of indices over processes.
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 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....
A read-only, row-oriented interface to a sparse matrix.
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 ~RowMatrix()
Destructor (virtual for memory safety of derived classes).
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.
Namespace Tpetra contains the class and methods constituting the Tpetra library.