157 "Map, then you must supply a nonnull domain and range Map to this "
168 if (!
params.is_null ()) {
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>
303 Teuchos::Array<char>& exports,
307#ifdef HAVE_TPETRA_DEBUG
310 using Teuchos::reduceAll;
311 std::ostringstream
msg;
316 }
catch (std::exception&
e) {
321 const Teuchos::Comm<int>& comm = * (this->getComm ());
325 const int myRank = comm.getRank ();
326 const int numProcs = comm.getSize ();
329 std::ostringstream
os;
330 os <<
"Proc " <<
myRank <<
": " <<
msg.str () << std::endl;
331 std::cerr <<
os.str ();
338 true, std::logic_error,
"packImpl() threw an exception on one or "
339 "more participating processes.");
348 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
352 size_t& totalNumEntries,
353 const Teuchos::ArrayView<const LocalOrdinal>&
exportLIDs)
const
357 typedef typename Teuchos::ArrayView<const LO>::size_type size_type;
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;
543 const size_t numEnt = this->getNumEntriesInLocalRow (
lclRow);
555 const size_t numBytes =
sizeof (LO) +
586 outOfBounds, std::logic_error,
"First invalid offset into 'exports' "
587 "pack buffer at index i = " <<
firstBadIndex <<
". exportLIDs[i]: "
591 packErr, std::logic_error,
"First error in packRow() at index i = "
597#ifdef TPETRA_ENABLE_DEPRECATED_CODE
598 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
610 Teuchos::ArrayView<const Scalar>
vals_av;
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 >;