42 #ifndef TPETRA_MATRIXMATRIX_DEF_HPP 43 #define TPETRA_MATRIXMATRIX_DEF_HPP 45 #include "TpetraExt_MatrixMatrix_decl.hpp" 46 #include "Teuchos_VerboseObject.hpp" 49 #include "Tpetra_ConfigDefs.hpp" 50 #include "Tpetra_CrsMatrix.hpp" 51 #include "TpetraExt_MMHelpers_def.hpp" 52 #include "Tpetra_RowMatrixTransposer.hpp" 53 #include "Tpetra_ConfigDefs.hpp" 54 #include "Tpetra_Map.hpp" 56 #include "Teuchos_FancyOStream.hpp" 69 template <
class Scalar,
75 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& A,
77 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& B,
79 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& C,
80 bool call_FillComplete_on_result)
83 typedef CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> Matrix_t;
95 "Uh oh. Looks like there's a bit of a problem here. No worries though. We'll help you figure it out. You're " 96 "a fantastic programer and this just a minor bump in the road! Maybe the information below can help you out a bit." 97 "\n\n MatrixMatrix::Multiply(): Matrix A is not fill complete.");
99 "Uh oh. Looks like there's a bit of a problem here. No worries though. We'll help you figure it out. You're " 100 "a fantastic programer and this just a minor bump in the road! Maybe the information below can help you out a bit." 101 "\n\n MatrixMatrix::Multiply(): Matrix B is not fill complete.");
103 "MatrixMatrix::Add ERROR, input matrix C must not be locally indexed!");
106 typedef CrsMatrixStruct<
111 SpMatOps> CrsMatrixStruct_t;
112 typedef Map<LocalOrdinal, GlobalOrdinal, Node> Map_t;
114 RCP<const Matrix_t > Aprime = null;
115 RCP<const Matrix_t > Bprime = null;
117 RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> at (Teuchos::rcpFromRef (A));
118 Aprime = at.createTranspose();
121 Aprime = rcpFromRef(A);
124 RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> bt (Teuchos::rcpFromRef (B));
125 Bprime=bt.createTranspose();
128 Bprime = rcpFromRef(B);
133 global_size_t numACols = A.getDomainMap()->getGlobalNumElements();
134 global_size_t numBCols = B.getDomainMap()->getGlobalNumElements();
135 global_size_t Aouter = transposeA ? numACols : A.getGlobalNumRows();
136 global_size_t Bouter = transposeB ? B.getGlobalNumRows() : numBCols;
137 global_size_t Ainner = transposeA ? A.getGlobalNumRows() : numACols;
138 global_size_t Binner = transposeB ? numBCols : B.getGlobalNumRows();
140 "MatrixMatrix::Multiply: ERROR, inner dimensions of op(A) and op(B) " 141 "must match for matrix-matrix product. op(A) is " 142 <<Aouter<<
"x"<<Ainner <<
", op(B) is "<<Binner<<
"x"<<Bouter<<std::endl);
149 "MatrixMatrix::Multiply: ERROR, dimensions of result C must " 150 "match dimensions of op(A) * op(B). C has "<<C.getGlobalNumRows()
151 <<
" rows, should have at least "<<Aouter << std::endl);
163 int numProcs = A.getComm()->getSize();
167 CrsMatrixStruct_t Aview;
168 CrsMatrixStruct_t Bview;
170 RCP<const Map_t > targetMap_A = Aprime->getRowMap();
171 RCP<const Map_t > targetMap_B = Bprime->getRowMap();
174 MMdetails::import_and_extract_views(*Aprime, targetMap_A, Aview);
180 targetMap_B = Aprime->getColMap();
184 MMdetails::import_and_extract_views(*Bprime, targetMap_B, Bview);
210 CrsWrapper_CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crsmat(C);
212 MMdetails::mult_A_B(Aview, Bview, crsmat);
214 if (call_FillComplete_on_result) {
221 if (!C.isFillComplete()) {
222 C.fillComplete(Bprime->getDomainMap(), Aprime->getRangeMap());
229 template <
class Scalar,
235 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& A,
238 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& B,
242 "MatrixMatrix::Add ERROR, input matrix A.isFillComplete() is false; it is required to be true. (Result matrix B is not required to be isFillComplete()).");
244 "MatrixMatrix::Add ERROR, input matrix B must not be fill complete!");
246 "MatrixMatrix::Add ERROR, input matrix B must not have static graph!");
248 "MatrixMatrix::Add ERROR, input matrix B must not be locally indexed!");
250 "MatrixMatrix::Add ERROR, input matrix B must have a dynamic profile!");
257 SpMatOps> CrsMatrix_t;
258 RCP<const CrsMatrix_t> Aprime = null;
260 RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> theTransposer(Teuchos::rcpFromRef (A));
261 Aprime = theTransposer.createTranspose();
264 Aprime = rcpFromRef(A);
267 Array<GlobalOrdinal> a_inds(A.getNodeMaxNumRowEntries());
268 Array<Scalar> a_vals(A.getNodeMaxNumRowEntries());
275 bool bFilled = B.isFillComplete();
276 size_t numMyRows = B.getNodeNumRows();
278 for(LocalOrdinal i = 0; (size_t)i < numMyRows; ++i){
279 row = B.getRowMap()->getGlobalElement(i);
280 Aprime->getGlobalRowCopy(row, a_inds(), a_vals(), a_numEntries);
282 for(
size_t j =0; j<a_numEntries; ++j){
283 a_vals[j] *= scalarA;
287 B.sumIntoGlobalValues(row, a_inds(0,a_numEntries), a_vals(0,a_numEntries));
290 B.insertGlobalValues(row, a_inds(0,a_numEntries), a_vals(0,a_numEntries));
298 template <
class Scalar,
304 add (
const Scalar& alpha,
305 const bool transposeA,
306 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& A,
308 const bool transposeB,
309 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& B,
310 const Teuchos::RCP<
const Map<LocalOrdinal, GlobalOrdinal, Node> >& domainMap,
311 const Teuchos::RCP<
const Map<LocalOrdinal, GlobalOrdinal, Node> >& rangeMap,
316 using Teuchos::rcpFromRef;
317 using Teuchos::rcp_dynamic_cast;
318 using Teuchos::rcp_implicit_cast;
319 typedef RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> row_matrix_type;
320 typedef CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> crs_matrix_type;
321 typedef RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> transposer_type;
324 ! A.isFillComplete () || ! B.isFillComplete (), std::invalid_argument,
325 "Tpetra::MatrixMatrix::add: A and B must both be fill complete.");
327 #ifdef HAVE_TPETRA_DEBUG 329 if (A.isFillComplete () && B.isFillComplete ()) {
330 const bool domainMapsSame =
331 (! transposeA && ! transposeB && ! A.getDomainMap ()->isSameAs (* (B.getDomainMap ()))) ||
332 (! transposeA && transposeB && ! A.getDomainMap ()->isSameAs (* (B.getRangeMap ()))) ||
333 (transposeA && ! transposeB && ! A.getRangeMap ()->isSameAs (* (B.getDomainMap ())));
335 domainMapsSame, std::invalid_argument,
336 "Tpetra::MatrixMatrix::add: The domain Maps of Op(A) and Op(B) are not the same.");
338 const bool rangeMapsSame =
339 (! transposeA && ! transposeB && ! A.getRangeMap ()->isSameAs (* (B.getRangeMap ()))) ||
340 (! transposeA && transposeB && ! A.getRangeMap ()->isSameAs (* (B.getDomainMap ()))) ||
341 (transposeA && ! transposeB && ! A.getDomainMap ()->isSameAs (* (B.getRangeMap ())));
343 rangeMapsSame, std::invalid_argument,
344 "Tpetra::MatrixMatrix::add: The range Maps of Op(A) and Op(B) are not the same.");
346 #endif // HAVE_TPETRA_DEBUG 349 RCP<const crs_matrix_type> Aprime;
351 transposer_type theTransposer (rcpFromRef (A));
352 Aprime = theTransposer.createTranspose ();
354 Aprime = rcpFromRef (A);
357 #ifdef HAVE_TPETRA_DEBUG 359 "Tpetra::MatrixMatrix::Add: Failed to compute Op(A). " 360 "Please report this bug to the Tpetra developers.");
361 #endif // HAVE_TPETRA_DEBUG 364 RCP<const crs_matrix_type> Bprime;
366 transposer_type theTransposer (rcpFromRef (B));
367 Bprime = theTransposer.createTranspose ();
369 Bprime = rcpFromRef (B);
372 #ifdef HAVE_TPETRA_DEBUG 374 "Tpetra::MatrixMatrix::Add: Failed to compute Op(B). " 375 "Please report this bug to the Tpetra developers.");
378 ! Aprime->isFillComplete () || ! Bprime->isFillComplete (), std::invalid_argument,
379 "Tpetra::MatrixMatrix::add: Aprime and Bprime must both be fill complete. " 380 "Please report this bug to the Tpetra developers.");
381 #endif // HAVE_TPETRA_DEBUG 384 RCP<row_matrix_type> C =
385 Bprime->add (alpha, *rcp_implicit_cast<const row_matrix_type> (Aprime),
386 beta, domainMap, rangeMap, params);
388 return rcp_dynamic_cast<crs_matrix_type> (C);
394 template <
class Scalar,
400 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& A,
403 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& B,
406 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> > C)
414 using Teuchos::rcp_dynamic_cast;
415 using Teuchos::rcpFromRef;
416 using Teuchos::tuple;
420 typedef Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
421 typedef Import<LocalOrdinal, GlobalOrdinal, Node> import_type;
422 typedef RowGraph<LocalOrdinal, GlobalOrdinal, Node> row_graph_type;
423 typedef CrsGraph<LocalOrdinal, GlobalOrdinal, Node, SpMatOps> crs_graph_type;
424 typedef CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> crs_matrix_type;
425 typedef RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps> transposer_type;
428 "Tpetra::MatrixMatrix::Add: The case C == null does not actually work. " 429 "Fixing this will require an interface change.");
432 ! A.isFillComplete () || ! B.isFillComplete (), std::invalid_argument,
433 "Tpetra::MatrixMatrix::Add: Both input matrices must be fill complete " 434 "before calling this function.");
436 #ifdef HAVE_TPETRA_DEBUG 438 const bool domainMapsSame =
439 (! transposeA && ! transposeB && ! A.getDomainMap ()->isSameAs (* (B.getDomainMap ()))) ||
440 (! transposeA && transposeB && ! A.getDomainMap ()->isSameAs (* (B.getRangeMap ()))) ||
441 (transposeA && ! transposeB && ! A.getRangeMap ()->isSameAs (* (B.getDomainMap ())));
443 domainMapsSame, std::invalid_argument,
444 "Tpetra::MatrixMatrix::Add: The domain Maps of Op(A) and Op(B) are not the same.");
446 const bool rangeMapsSame =
447 (! transposeA && ! transposeB && ! A.getRangeMap ()->isSameAs (* (B.getRangeMap ()))) ||
448 (! transposeA && transposeB && ! A.getRangeMap ()->isSameAs (* (B.getDomainMap ()))) ||
449 (transposeA && ! transposeB && ! A.getDomainMap ()->isSameAs (* (B.getRangeMap ())));
451 rangeMapsSame, std::invalid_argument,
452 "Tpetra::MatrixMatrix::Add: The range Maps of Op(A) and Op(B) are not the same.");
454 #endif // HAVE_TPETRA_DEBUG 457 RCP<const crs_matrix_type> Aprime;
459 transposer_type theTransposer (rcpFromRef (A));
460 Aprime = theTransposer.createTranspose ();
462 Aprime = rcpFromRef (A);
465 #ifdef HAVE_TPETRA_DEBUG 467 "Tpetra::MatrixMatrix::Add: Failed to compute Op(A). " 468 "Please report this bug to the Tpetra developers.");
469 #endif // HAVE_TPETRA_DEBUG 472 RCP<const crs_matrix_type> Bprime;
474 transposer_type theTransposer (rcpFromRef (B));
475 Bprime = theTransposer.createTranspose ();
477 Bprime = rcpFromRef (B);
480 #ifdef HAVE_TPETRA_DEBUG 482 "Tpetra::MatrixMatrix::Add: Failed to compute Op(B). " 483 "Please report this bug to the Tpetra developers.");
484 #endif // HAVE_TPETRA_DEBUG 487 if (! C.is_null ()) {
488 C->setAllToScalar (STS::zero ());
496 if (Aprime->getRowMap ()->isSameAs (* (Bprime->getRowMap ())) {
497 RCP<const map_type> rowMap = Aprime->getRowMap ();
499 RCP<const crs_graph_type> A_graph =
500 rcp_dynamic_cast<
const crs_graph_type> (Aprime->getGraph (),
true);
501 #ifdef HAVE_TPETRA_DEBUG 503 "Tpetra::MatrixMatrix::Add: Graph of Op(A) is null. " 504 "Please report this bug to the Tpetra developers.");
505 #endif // HAVE_TPETRA_DEBUG 506 RCP<const crs_graph_type> B_graph =
507 rcp_dynamic_cast<
const crs_graph_type> (Bprime->getGraph (),
true);
508 #ifdef HAVE_TPETRA_DEBUG 510 "Tpetra::MatrixMatrix::Add: Graph of Op(B) is null. " 511 "Please report this bug to the Tpetra developers.");
512 #endif // HAVE_TPETRA_DEBUG 513 RCP<const map_type> A_colMap = A_graph->getColMap ();
514 #ifdef HAVE_TPETRA_DEBUG 516 "Tpetra::MatrixMatrix::Add: Column Map of Op(A) is null. " 517 "Please report this bug to the Tpetra developers.");
518 #endif // HAVE_TPETRA_DEBUG 519 RCP<const map_type> B_colMap = B_graph->getColMap ();
520 #ifdef HAVE_TPETRA_DEBUG 522 "Tpetra::MatrixMatrix::Add: Column Map of Op(B) is null. " 523 "Please report this bug to the Tpetra developers.");
526 "Tpetra::MatrixMatrix::Add: Op(A)'s Import is null. " 527 "Please report this bug to the Tpetra developers.");
530 "Tpetra::MatrixMatrix::Add: Op(B)'s Import is null. " 531 "Please report this bug to the Tpetra developers.");
532 #endif // HAVE_TPETRA_DEBUG 535 RCP<const import_type> sumImport =
536 A_graph->getImporter ()->setUnion (* (B_graph->getImporter ()));
537 RCP<const map_type> C_colMap = sumImport->getTargetMap ();
545 ArrayView<const LocalOrdinal> A_local_ind;
546 Array<GlobalOrdinal> A_global_ind;
547 ArrayView<const LocalOrdinal> B_local_ind;
548 Array<GlobalOrdinal> B_global_ind;
550 const size_t localNumRows = rowMap->getNodeNumElements ();
551 ArrayRCP<size_t> numEntriesPerRow (localNumRows);
554 size_t maxNumEntriesPerRow = 0;
555 for (
size_t localRow = 0; localRow < localNumRows; ++localRow) {
557 A_graph->getLocalRowView (as<LocalOrdinal> (localRow), A_local_ind);
558 const size_type A_numEnt = A_local_ind.size ();
559 if (A_numEnt > A_global_ind.size ()) {
560 A_global_ind.resize (A_numEnt);
563 for (size_type k = 0; k < A_numEnt; ++k) {
564 A_global_ind[k] = A_colMap->getGlobalElement (A_local_ind[k]);
568 B_graph->getLocalRowView (as<LocalOrdinal> (localRow), B_local_ind);
569 const size_type B_numEnt = B_local_ind.size ();
570 if (B_numEnt > B_global_ind.size ()) {
571 B_global_ind.resize (B_numEnt);
574 for (size_type k = 0; k < B_numEnt; ++k) {
575 B_global_ind[k] = B_colMap->getGlobalElement (B_local_ind[k]);
579 const size_t curNumEntriesPerRow =
580 keyMergeCount (A_global_ind.begin (), A_global_ind.end (),
581 B_global_ind.begin (), B_global_ind.end ());
582 numEntriesPerRow[localRow] = curNumEntriesPerRow;
583 maxNumEntriesPerRow = std::max (maxNumEntriesPerRow, curNumEntriesPerRow);
590 C =
rcp (
new crs_matrix_type (rowMap, C_colMap, numEntriesPerRow,
StaticProfile));
595 ArrayView<const Scalar> A_val;
596 ArrayView<const Scalar> B_val;
598 Array<LocalOrdinal> AplusB_local_ind (maxNumEntriesPerRow);
599 Array<GlobalOrdinal> AplusB_global_ind (maxNumEntriesPerRow);
600 Array<Scalar> AplusB_val (maxNumEntriesPerRow);
602 for (
size_t localRow = 0; localRow < localNumRows; ++localRow) {
604 Aprime->getLocalRowView (as<LocalOrdinal> (localRow), A_local_ind, A_val);
606 for (size_type k = 0; k < A_local_ind.size (); ++k) {
607 A_global_ind[k] = A_colMap->getGlobalElement (A_local_ind[k]);
611 Bprime->getLocalRowView (as<LocalOrdinal> (localRow), B_local_ind, B_val);
613 for (size_type k = 0; k < B_local_ind.size (); ++k) {
614 B_global_ind[k] = B_colMap->getGlobalElement (B_local_ind[k]);
617 const size_t curNumEntries = numEntriesPerRow[localRow];
618 ArrayView<LocalOrdinal> C_local_ind = AplusB_local_ind (0, curNumEntries);
619 ArrayView<GlobalOrdinal> C_global_ind = AplusB_global_ind (0, curNumEntries);
620 ArrayView<Scalar> C_val = AplusB_val (0, curNumEntries);
624 A_val.begin (), A_val.end (),
625 B_global_ind.begin (), B_global_ind.end (),
626 B_val.begin (), B_val.end (),
627 C_global_ind.begin (), C_val.begin (),
628 std::plus<Scalar> ());
630 for (size_type k = 0; k < as<size_type> (numEntriesPerRow[localRow]); ++k) {
631 C_local_ind[k] = C_colMap->getLocalElement (C_global_ind[k]);
634 C->replaceLocalValues (localRow, C_local_ind, C_val);
639 RCP<const map_type> domainMap = A_graph->getDomainMap ();
640 RCP<const map_type> rangeMap = A_graph->getRangeMap ();
641 C->expertStaticFillComplete (domainMap, rangeMap, sumImport, A_graph->getExporter ());
649 C =
rcp (
new crs_matrix_type (Aprime->getRowMap (), null));
656 C =
rcp (
new crs_matrix_type (Aprime->getRowMap (), 0));
660 #ifdef HAVE_TPETRA_DEBUG 662 "Tpetra::MatrixMatrix::Add: At this point, Aprime is null. " 663 "Please report this bug to the Tpetra developers.");
665 "Tpetra::MatrixMatrix::Add: At this point, Bprime is null. " 666 "Please report this bug to the Tpetra developers.");
668 "Tpetra::MatrixMatrix::Add: At this point, C is null. " 669 "Please report this bug to the Tpetra developers.");
670 #endif // HAVE_TPETRA_DEBUG 672 Array<RCP<const crs_matrix_type> > Mat =
673 tuple<RCP<const crs_matrix_type> > (Aprime, Bprime);
674 Array<Scalar> scalar = tuple<Scalar> (scalarA, scalarB);
677 for (
int k = 0; k < 2; ++k) {
678 Array<GlobalOrdinal> Indices;
679 Array<Scalar> Values;
687 #ifdef HAVE_TPETRA_DEBUG 689 "Tpetra::MatrixMatrix::Add: At this point, curRowMap is null. " 690 "Please report this bug to the Tpetra developers.");
691 #endif // HAVE_TPETRA_DEBUG 692 RCP<const map_type> curRowMap = Mat[k]->getRowMap ();
693 #ifdef HAVE_TPETRA_DEBUG 695 "Tpetra::MatrixMatrix::Add: At this point, curRowMap is null. " 696 "Please report this bug to the Tpetra developers.");
697 #endif // HAVE_TPETRA_DEBUG 699 const size_t localNumRows = Mat[k]->getNodeNumRows ();
700 for (
size_t i = 0; i < localNumRows; ++i) {
701 const GlobalOrdinal globalRow = curRowMap->getGlobalElement (i);
702 size_t numEntries = Mat[k]->getNumEntriesInGlobalRow (globalRow);
703 if (numEntries > 0) {
704 Indices.resize (numEntries);
705 Values.resize (numEntries);
706 Mat[k]->getGlobalRowCopy (globalRow, Indices (), Values (), numEntries);
708 if (scalar[k] != STS::one ()) {
709 for (
size_t j = 0; j < numEntries; ++j) {
710 Values[j] *= scalar[k];
714 if (C->isFillComplete ()) {
715 C->sumIntoGlobalValues (globalRow, Indices, Values);
717 C->insertGlobalValues (globalRow, Indices, Values);
730 template<
class Scalar,
736 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& Aview,
737 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& Bview,
738 CrsWrapper<Scalar, LocalOrdinal, GlobalOrdinal, Node>& C,
739 bool onlyCalculateStructure)
742 LocalOrdinal C_firstCol = Bview.colMap->getMinLocalIndex();
743 LocalOrdinal C_lastCol = Bview.colMap->getMaxLocalIndex();
748 ArrayView<const GlobalOrdinal> bcols = Bview.colMap->getNodeElementList();
749 ArrayView<const GlobalOrdinal> bcols_import = null;
750 if (Bview.importColMap != null) {
751 C_firstCol_import = Bview.importColMap->getMinLocalIndex();
752 C_lastCol_import = Bview.importColMap->getMaxLocalIndex();
754 bcols_import = Bview.importColMap->getNodeElementList();
757 size_t C_numCols = C_lastCol - C_firstCol +
759 size_t C_numCols_import = C_lastCol_import - C_firstCol_import +
762 if (C_numCols_import > C_numCols) C_numCols = C_numCols_import;
764 Array<Scalar> dwork = onlyCalculateStructure ? Array<Scalar>() : Array<Scalar>(C_numCols);
765 Array<GlobalOrdinal> iwork = Array<GlobalOrdinal>(C_numCols);
766 Array<size_t> iwork2 = Array<size_t>(C_numCols);
768 Array<Scalar> C_row_i = dwork;
769 Array<GlobalOrdinal> C_cols = iwork;
770 Array<size_t> c_index = iwork2;
771 Array<GlobalOrdinal> combined_index = Array<GlobalOrdinal>(2*C_numCols);
772 Array<Scalar> combined_values = onlyCalculateStructure ? Array<Scalar>() : Array<Scalar>(2*C_numCols);
774 size_t C_row_i_length, j, k, lastj, last_index;
777 Array<LocalOrdinal> Acol2Brow(Aview.colMap->getNodeNumElements());
778 if(Aview.colMap->isSameAs(*Bview.rowMap)){
780 for(LocalOrdinal i=Aview.colMap->getMinLocalIndex(); i <=
781 Aview.colMap->getMaxLocalIndex(); i++)
786 for(LocalOrdinal i=Aview.colMap->getMinLocalIndex(); i <=
787 Aview.colMap->getMaxLocalIndex(); i++)
788 Acol2Brow[i]=Bview.rowMap->getLocalElement(
789 Aview.colMap->getGlobalElement(i));
799 bool C_filled = C.isFillComplete();
801 for (
size_t i = 0; i < C_numCols; i++)
805 for(
size_t i=0; i<Aview.numRows; ++i) {
810 if (Aview.remote[i]) {
815 ArrayView<const LocalOrdinal> Aindices_i = Aview.indices[i];
816 ArrayView<const Scalar> Aval_i = onlyCalculateStructure ? null : Aview.values[i];
818 GlobalOrdinal global_row = Aview.rowMap->getGlobalElement(i);
831 LocalOrdinal Ak = Acol2Brow[Aindices_i[k]];
832 Scalar Aval = onlyCalculateStructure ? Teuchos::as<Scalar>(0) : Aval_i[k];
834 if (Bview.remote[Ak])
continue;
836 const LocalOrdinal *Bcol_inds = Bview.indices[Ak].getRawPtr();
837 const Scalar *Bvals_k = onlyCalculateStructure ? NULL :
838 Bview.values[Ak].getRawPtr();
840 lastj = Bview.numEntriesPerRow[Ak];
843 LocalOrdinal col = Bcol_inds[j];
847 if(!onlyCalculateStructure){
849 C_row_i[C_row_i_length] = Aval*Bvals_k[j];
851 C_cols[C_row_i_length] = col;
852 c_index[col] = C_row_i_length;
856 if(!onlyCalculateStructure){
857 C_row_i[c_index[col]] += Aval*Bvals_k[j];
863 for (
size_t ii = 0; ii < C_row_i_length; ii++) {
865 C_cols[ii] = bcols[C_cols[ii]];
866 combined_index[ii] = C_cols[ii];
867 if (!onlyCalculateStructure)
868 combined_values[ii] = C_row_i[ii];
870 last_index = C_row_i_length;
895 LocalOrdinal Ak = Acol2Brow[Aindices_i[k]];
896 Scalar Aval = onlyCalculateStructure ? Teuchos::as<Scalar>(0) : Aval_i[k];
898 if (!Bview.remote[Ak])
continue;
900 const LocalOrdinal *Bcol_inds = Bview.indices[Ak].getRawPtr();
901 const Scalar *Bvals_k = onlyCalculateStructure ? NULL :
902 Bview.values[Ak].getRawPtr();
904 lastj = Bview.numEntriesPerRow[Ak];
906 LocalOrdinal col = Bcol_inds[j];
910 if(!onlyCalculateStructure){
912 C_row_i[C_row_i_length] = Aval*Bvals_k[j];
914 C_cols[C_row_i_length] = col;
915 c_index[col] = C_row_i_length;
919 if(!onlyCalculateStructure){
921 C_row_i[c_index[col]] += Aval*Bvals_k[j];
927 for (
size_t ii = 0; ii < C_row_i_length; ii++) {
929 C_cols[ii] = bcols_import[C_cols[ii]];
930 combined_index[last_index] = C_cols[ii];
931 if (!onlyCalculateStructure)
932 combined_values[last_index] = C_row_i[ii];
940 if (!onlyCalculateStructure)
943 C.sumIntoGlobalValues(
946 onlyCalculateStructure ? null : combined_values.view(
949 C.insertGlobalValues(
952 onlyCalculateStructure ? null : combined_values.view(
960 template<
class Scalar,
965 void setMaxNumEntriesPerRow(
966 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& Mview)
968 typedef typename Array<ArrayView<const LocalOrdinal> >::size_type local_length_size;
971 Mview.maxNumRowEntries = Mview.indices[0].size();
972 for(local_length_size i = 1; i<Mview.indices.size(); ++i){
973 if(Mview.indices[i].size() > Mview.maxNumRowEntries){
974 Mview.maxNumRowEntries = Mview.indices[i].size();
980 template<
class Scalar,
985 void import_and_extract_views(
986 const CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& M,
987 RCP<
const Map<LocalOrdinal, GlobalOrdinal, Node> > targetMap,
988 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node, SpMatOps>& Mview)
991 typedef Map<LocalOrdinal, GlobalOrdinal, Node> Map_t;
998 Mview.deleteContents();
1000 RCP<const Map_t> Mrowmap = M.getRowMap();
1002 const int numProcs = Mrowmap->getComm()->getSize();
1004 ArrayView<const GlobalOrdinal> Mrows = targetMap->getNodeElementList();
1006 Mview.numRemote = 0;
1007 Mview.numRows = targetMap->getNodeNumElements();
1008 Mview.numEntriesPerRow.resize(Mview.numRows);
1009 Mview.indices.resize( Mview.numRows);
1010 Mview.values.resize( Mview.numRows);
1011 Mview.remote.resize( Mview.numRows);
1014 Mview.origRowMap = M.getRowMap();
1015 Mview.rowMap = targetMap;
1016 Mview.colMap = M.getColMap();
1017 Mview.domainMap = M.getDomainMap();
1018 Mview.importColMap = null;
1023 for(
size_t i=0; i < Mview.numRows; ++i)
1025 const LocalOrdinal mlid = Mrowmap->getLocalElement(Mrows[i]);
1028 Mview.remote[i] =
true;
1032 Mview.remote[i] =
false;
1033 M.getLocalRowView(mlid, Mview.indices[i], Mview.values[i]);
1034 Mview.numEntriesPerRow[i] = Mview.indices[i].size();
1040 "MatrixMatrix::import_and_extract_views ERROR, numProcs < 2 but attempting to import remote matrix rows." <<std::endl);
1041 setMaxNumEntriesPerRow(Mview);
1052 Teuchos::reduceAll(*(Mrowmap->getComm()) , Teuchos::REDUCE_MAX, Mview.numRemote, Teuchos::outArg(globalMaxNumRemote) );
1054 if (globalMaxNumRemote > 0) {
1057 Array<GlobalOrdinal> MremoteRows(Mview.numRemote);
1061 for(
size_t i=0; i < Mview.numRows; ++i) {
1062 if (Mview.remote[i]) {
1063 MremoteRows[offset++] = Mrows[i];
1067 RCP<const Map_t> MremoteRowMap =
rcp(
new Map_t(
1070 Mrowmap->getIndexBase(),
1072 Mrowmap->getNode()));
1075 Import<LocalOrdinal, GlobalOrdinal, Node> importer(Mrowmap, MremoteRowMap);
1078 Mview.importMatrix =
rcp(
new CrsMatrix<Scalar,LocalOrdinal, GlobalOrdinal, Node, SpMatOps>( MremoteRowMap, 1 ));
1079 Mview.importMatrix->doImport(M, importer,
INSERT);
1080 Mview.importMatrix->fillComplete(M.getDomainMap(), M.getRangeMap());
1083 Mview.importColMap = Mview.importMatrix->getColMap();
1086 for(
size_t i=0; i < Mview.numRows; ++i)
1088 if (Mview.remote[i]) {
1089 const LocalOrdinal importLID = MremoteRowMap->getLocalElement(Mrows[i]);
1090 Mview.importMatrix->getLocalRowView(importLID,
1093 Mview.numEntriesPerRow[i] = Mview.indices[i].size();
1097 setMaxNumEntriesPerRow(Mview);
1109 #define TPETRA_MATRIXMATRIX_INSTANT(SCALAR,LO,GO,NODE) \ 1112 void MatrixMatrix::Multiply( \ 1113 const CrsMatrix< SCALAR , LO , GO , NODE >& A, \ 1115 const CrsMatrix< SCALAR , LO , GO , NODE >& B, \ 1117 CrsMatrix< SCALAR , LO , GO , NODE >& C, \ 1118 bool call_FillComplete_on_result); \ 1121 void MatrixMatrix::Add( \ 1122 const CrsMatrix< SCALAR , LO , GO , NODE >& A, \ 1125 const CrsMatrix< SCALAR , LO , GO , NODE >& B, \ 1128 RCP<CrsMatrix< SCALAR , LO , GO , NODE > > C); \ 1131 void MatrixMatrix::Add( \ 1132 const CrsMatrix<SCALAR, LO, GO, NODE>& A, \ 1135 CrsMatrix<SCALAR, LO, GO, NODE>& B, \ 1139 Teuchos::RCP<CrsMatrix< SCALAR , LO , GO , NODE > > \ 1140 MatrixMatrix::add (const SCALAR & alpha, \ 1141 const bool transposeA, \ 1142 const CrsMatrix< SCALAR , LO , GO , NODE >& A, \ 1143 const SCALAR & beta, \ 1144 const bool transposeB, \ 1145 const CrsMatrix< SCALAR , LO , GO , NODE >& B, \ 1146 const Teuchos::RCP<const Map< LO , GO , NODE > >& domainMap, \ 1147 const Teuchos::RCP<const Map< LO , GO , NODE > >& rangeMap, \ 1148 const Teuchos::RCP<Teuchos::ParameterList>& params); \ 1152 #endif // TPETRA_MATRIXMATRIX_DEF_HPP Namespace Tpetra contains the class and methods constituting the Tpetra library.
bool is_null(const boost::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_t global_size_t
Global size_t object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void keyValueMerge(KeyInputIterType keyBeg1, KeyInputIterType keyEnd1, ValueInputIterType valBeg1, ValueInputIterType valEnd1, KeyInputIterType keyBeg2, KeyInputIterType keyEnd2, ValueInputIterType valBeg2, ValueInputIterType valEnd2, KeyOutputIterType keyOut, ValueOutputIterType valOut, BinaryFunction f)
Merge two sorted (by keys) sequences of unique (key,value) pairs by combining pairs with equal keys...
Distributed sparse matrix-matrix multiply and add.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
TypeTo as(const TypeFrom &t)
Stand-alone utility functions and macros.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)