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>
55 Teuchos::RCP<RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
57 add (
const Scalar& alpha,
62 const Teuchos::RCP<Teuchos::ParameterList>& params)
const
65 using Teuchos::ArrayRCP;
67 using Teuchos::ParameterList;
70 using Teuchos::rcp_implicit_cast;
71 using Teuchos::sublist;
72 typedef LocalOrdinal LO;
73 typedef GlobalOrdinal GO;
74 typedef Teuchos::ScalarTraits<Scalar> STS;
79 const this_type& B = *
this;
87 RCP<const map_type> B_domainMap = B.getDomainMap ();
88 RCP<const map_type> B_rangeMap = B.getRangeMap ();
90 RCP<const map_type> theDomainMap = domainMap;
91 RCP<const map_type> theRangeMap = rangeMap;
93 if (domainMap.is_null ()) {
94 if (B_domainMap.is_null ()) {
95 TEUCHOS_TEST_FOR_EXCEPTION(
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 ()) {
105 if (B_rangeMap.is_null ()) {
106 TEUCHOS_TEST_FOR_EXCEPTION(
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
120 if (! A_domainMap.is_null () && ! A_rangeMap.is_null ()) {
121 if (! B_domainMap.is_null () && ! B_rangeMap.is_null ()) {
122 TEUCHOS_TEST_FOR_EXCEPTION(
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.");
126 TEUCHOS_TEST_FOR_EXCEPTION(
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.");
130 TEUCHOS_TEST_FOR_EXCEPTION(
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.");
135 TEUCHOS_TEST_FOR_EXCEPTION(
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.");
142 else if (! B_domainMap.is_null () && ! B_rangeMap.is_null ()) {
143 TEUCHOS_TEST_FOR_EXCEPTION(
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.");
148 TEUCHOS_TEST_FOR_EXCEPTION(
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.");
155 TEUCHOS_TEST_FOR_EXCEPTION(
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;
167 RCP<ParameterList> constructorSublist;
168 RCP<ParameterList> fillCompleteSublist;
169 if (! params.is_null ()) {
170 callFillComplete = params->get (
"Call fillComplete", callFillComplete);
171 constructorSublist = sublist (params,
"Constructor parameters");
172 fillCompleteSublist = sublist (params,
"fillComplete parameters");
175 RCP<const map_type> A_rowMap = A.
getRowMap ();
176 RCP<const map_type> B_rowMap = B.getRowMap ();
177 RCP<const map_type> C_rowMap = B_rowMap;
178 RCP<crs_matrix_type> C;
185 if (A_rowMap->isSameAs (*B_rowMap)) {
186 const LO localNumRows = as<LO> (A_rowMap->getNodeNumElements ());
187 ArrayRCP<size_t> C_maxNumEntriesPerRow (localNumRows, 0);
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
228 TEUCHOS_TEST_FOR_EXCEPTION(C.is_null (), std::logic_error,
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);
247 ArrayView<GO> indView = ind (0, A_numEntries);
248 ArrayView<Scalar> valView = val (0, 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);
269 ArrayView<GO> indView = ind (0, B_numEntries);
270 ArrayView<Scalar> valView = val (0, 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>
297 pack (
const Teuchos::ArrayView<const LocalOrdinal>& exportLIDs,
298 Teuchos::Array<char>& exports,
299 const Teuchos::ArrayView<size_t>& numPacketsPerLID,
300 size_t& constantNumPackets,
303 using Teuchos::Array;
304 using Teuchos::ArrayView;
306 using Teuchos::av_reinterpret_cast;
308 typedef LocalOrdinal LO;
309 typedef GlobalOrdinal GO;
310 typedef typename ArrayView<const LO>::size_type size_type;
312 const char tfecfFuncName[] =
"pack";
314 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
315 exportLIDs.size() != numPacketsPerLID.size(),
316 std::invalid_argument,
"exportLIDs.size() = " << exportLIDs.size()
317 <<
"!= numPacketsPerLID.size() = " << numPacketsPerLID.size() <<
".");
320 RCP<const map_type> rowMapPtr = this->getRowMap ();
321 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
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;
332 Array<GO> exportGIDs (exportLIDs.size ());
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));
380 Array<Scalar> vals (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);
401 ArrayView<const GO> curInds = inds (0, as<size_type> (curNumEntries));
402 ArrayView<const Scalar> curVals = vals (0, as<size_type> (curNumEntries));
406 ArrayView<char> outIndsChar =
407 exports (curOffsetInBytes, curNumEntries *
sizeof (GO));
408 ArrayView<char> outValsChar =
409 exports (curOffsetInBytes + curNumEntries *
sizeof (GO),
410 curNumEntries *
sizeof (Scalar));
413 ArrayView<GO> outInds = av_reinterpret_cast<GO> (outIndsChar);
414 ArrayView<Scalar> outVals = av_reinterpret_cast<Scalar> (outValsChar);
418 std::copy (curInds.begin(), curInds.end(), outInds.begin());
419 std::copy (curVals.begin(), curVals.end(), outVals.begin());
420 curOffsetInBytes += sizeOfOrdValPair * curNumEntries;
423 #ifdef HAVE_TPETRA_DEBUG
424 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(curOffsetInBytes != totalNumBytes,
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
Sparse matrix that presents a compressed sparse row interface.
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const =0
The current number of entries on the calling process in the specified local row.
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...
virtual ~RowMatrix()
Destructor (virtual for memory safety of derived classes).
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 const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getRowMap() const =0
The Map that describes the distribution of rows over processes.
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.
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.
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.