Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_OverlappingRowMatrix_def.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #ifndef IFPACK2_OVERLAPPINGROWMATRIX_DEF_HPP
44 #define IFPACK2_OVERLAPPINGROWMATRIX_DEF_HPP
45 #include "Ifpack2_OverlappingRowMatrix_decl.hpp"
46 #include "Tpetra_CrsMatrix.hpp"
47 
48 #include "Teuchos_CommHelpers.hpp"
49 
50 namespace Ifpack2 {
51 //==========================================================================
52 // Standard constructor
53 template<class MatrixType>
54 OverlappingRowMatrix<MatrixType>::OverlappingRowMatrix(const Teuchos::RCP<const Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& Matrix_in,
55  int OverlapLevel_in) :
56  A_(Matrix_in),
57  OverlapLevel_(OverlapLevel_in),
58  UseSubComm_(false)
59 {
60  using Teuchos::RCP;
61  using Teuchos::rcp;
62  using Teuchos::Array;
63 
64  // Short form names
65  typedef typename Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> MapType;
66  typedef typename Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> ImportType;
67  typedef typename Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> CrsMatrixType;
68  typedef typename Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> RowMatrixType;
69 
70  TEUCHOS_TEST_FOR_EXCEPTION(OverlapLevel_ <= 0, std::runtime_error, "Ifpack2::OverlappingRowMatrix: OverlapLevel must be > 0.");
71  TEUCHOS_TEST_FOR_EXCEPTION(A_->getComm()->getSize() == 1, std::runtime_error, "Ifpack2::OverlappingRowMatrix: Matrix must be parallel.");
72 
73  RCP<const CrsMatrixType> ACRS = Teuchos::rcp_dynamic_cast<const CrsMatrixType,const RowMatrixType>(A_);
74  TEUCHOS_TEST_FOR_EXCEPTION(ACRS == Teuchos::null, std::runtime_error, "Ifpack2::OverlappingRowMatrix: Matrix must be Tpetra::CrsMatrix.");
75 
76  NumMyRowsA_ = A_->getNodeNumRows();
77 
78  GlobalOrdinal global_invalid = Teuchos::OrdinalTraits<global_size_t>::invalid();
79 
80  // Temp arrays
81  Array<GlobalOrdinal> ExtElements;
82  RCP<MapType> TmpMap;
83  RCP<CrsMatrixType> TmpMatrix;
84  RCP<ImportType> TmpImporter;
85  RCP<const MapType> RowMap, ColMap;
86 
87  // The big import loop
88  for (int overlap = 0 ; overlap < OverlapLevel_ ; ++overlap) {
89  // Get the current maps
90  if(overlap==0){
91  RowMap = A_->getRowMap();
92  ColMap = A_->getColMap();
93  }
94  else {
95  RowMap = TmpMatrix->getRowMap();
96  ColMap = TmpMatrix->getColMap();
97  }
98 
99  size_t size = ColMap->getNodeNumElements() - RowMap->getNodeNumElements();
100  Array<GlobalOrdinal> mylist(size);
101  size_t count = 0;
102 
103  // define the set of rows that are in ColMap but not in RowMap
104  for (LocalOrdinal i = 0 ; (size_t) i < ColMap->getNodeNumElements() ; ++i) {
105  GlobalOrdinal GID = ColMap->getGlobalElement(i);
106  if (A_->getRowMap()->getLocalElement(GID) == global_invalid) {
107  typename Array<GlobalOrdinal>::iterator pos
108  = std::find(ExtElements.begin(),ExtElements.end(),GID);
109  if (pos == ExtElements.end()) {
110  ExtElements.push_back(GID);
111  mylist[count] = GID;
112  ++count;
113  }
114  }
115  }
116 
117  // Allocate & import new matrices, maps, etc.
118  TmpMap = rcp(new MapType(global_invalid,mylist(0,count),Teuchos::OrdinalTraits<GlobalOrdinal>::zero(),A_->getComm(),A_->getNode()));
119  TmpMatrix = rcp(new CrsMatrixType(TmpMap,0));
120  TmpImporter = rcp(new ImportType(A_->getRowMap(),TmpMap));
121 
122  TmpMatrix->doImport(*ACRS,*TmpImporter,Tpetra::INSERT);
123  TmpMatrix->fillComplete(A_->getDomainMap(),TmpMap);
124  }
125 
126  // build the map containing all the nodes (original
127  // matrix + extended matrix)
128  Array<GlobalOrdinal> mylist(NumMyRowsA_ + ExtElements.size());
129  for (LocalOrdinal i = 0 ; (size_t)i < NumMyRowsA_ ; ++i)
130  mylist[i] = A_->getRowMap()->getGlobalElement(i);
131  for (LocalOrdinal i = 0 ; i < ExtElements.size() ; ++i)
132  mylist[i + NumMyRowsA_] = ExtElements[i];
133 
134  RowMap_= rcp(new MapType(global_invalid,mylist(),Teuchos::OrdinalTraits<GlobalOrdinal>::zero(),A_->getComm(),A_->getNode()));
135  ColMap_= RowMap_;
136 
137  // now build the map corresponding to all the external nodes
138  // (with respect to A().RowMatrixRowMap().
139  ExtMap_ = rcp(new MapType(global_invalid,ExtElements(),Teuchos::OrdinalTraits<GlobalOrdinal>::zero(),A_->getComm(),A_->getNode()));
140  ExtMatrix_ = rcp(new CrsMatrixType(ExtMap_,ColMap_,0));
141  ExtImporter_ = rcp(new ImportType(A_->getRowMap(),ExtMap_));
142 
143  RCP<CrsMatrixType> ExtMatrixCRS = Teuchos::rcp_dynamic_cast<CrsMatrixType,RowMatrixType>(ExtMatrix_);
144  ExtMatrixCRS->doImport(*ACRS,*ExtImporter_,Tpetra::INSERT);
145  ExtMatrixCRS->fillComplete(A_->getDomainMap(),RowMap_);
146 
147  Importer_ = rcp(new ImportType(A_->getRowMap(),RowMap_));
148 
149  // fix indices for overlapping matrix
150  NumMyRowsB_ = ExtMatrix_->getNodeNumRows();
151  NumMyRows_ = NumMyRowsA_ + NumMyRowsB_;
152  NumMyCols_ = NumMyRows_;
153 
154  NumMyDiagonals_ = A_->getNodeNumDiags() + ExtMatrix_->getNodeNumDiags();
155  NumMyNonzeros_ = A_->getNodeNumEntries() + ExtMatrix_->getNodeNumEntries();
156 
157  // FIXME: Fix this later when Teuchos::Comm gets redone
158  Tpetra::global_size_t NumMyNonzeros_tmp = NumMyNonzeros_;
159  Teuchos::reduceAll<int,Tpetra::global_size_t>(*A_->getComm(),Teuchos::REDUCE_SUM,NumMyNonzeros_tmp,Teuchos::outArg(NumGlobalNonzeros_));
160  Tpetra::global_size_t NumMyRows_tmp = NumMyRows_;
161  Teuchos::reduceAll<int,Tpetra::global_size_t>(*A_->getComm(),Teuchos::REDUCE_SUM,NumMyRows_tmp,Teuchos::outArg(NumGlobalRows_));
162 
163  MaxNumEntries_ = A_->getNodeMaxNumRowEntries();
164  if (MaxNumEntries_ < ExtMatrix_->getNodeMaxNumRowEntries())
165  MaxNumEntries_ = ExtMatrix_->getNodeMaxNumRowEntries();
166 
167  // Resize temp arrays
168  Indices_.resize(MaxNumEntries_);
169  Values_.resize(MaxNumEntries_);
170 }
171 
172 
173 
174 //==========================================================================
175 // Sub-communicator constructor
176 template<class MatrixType>
177 OverlappingRowMatrix<MatrixType>::OverlappingRowMatrix(const Teuchos::RCP<const Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& Matrix_in,
178  int OverlapLevel_in, int subdomainID)
179 {
180  //FIXME
181  throw std::runtime_error("Ifpack2::OverlappingRowMatrix - subdomain code not implemented yet.");
182 }
183 
184 //==========================================================================
185 // Destructor
186 template<class MatrixType>
187 OverlappingRowMatrix<MatrixType>::~OverlappingRowMatrix()
188 {
189 
190 }
191 //==========================================================================
192 // Returns the communicator.
193 template<class MatrixType>
194 const Teuchos::RCP<const Teuchos::Comm<int> > & OverlappingRowMatrix<MatrixType>::getComm() const
195 {
196  return A_->getComm();
197 }
198 
199 //==========================================================================
200 // Returns the underlying node.
201 template<class MatrixType>
202 Teuchos::RCP<typename MatrixType::node_type> OverlappingRowMatrix<MatrixType>::getNode() const
203 {
204  return A_->getNode();
205 }
206 
207 //==========================================================================
208 // Returns the Map that describes the row distribution in this matrix.
209 template<class MatrixType>
210 const Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type> > & OverlappingRowMatrix<MatrixType>::getRowMap() const
211 {
212  return RowMap_;
213 }
214 
215 //==========================================================================
216 // Returns the Map that describes the column distribution in this matrix.
217 template<class MatrixType>
218 const Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type> > & OverlappingRowMatrix<MatrixType>::getColMap() const
219 {
220  return ColMap_;
221 }
222 
223 //==========================================================================
224 // Returns the Map that describes the column distribution in this matrix.
225 template<class MatrixType>
226 const Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type> > & OverlappingRowMatrix<MatrixType>::getDomainMap() const
227 {
228  return A_->getDomainMap();
229 }
230 
231 //==========================================================================
232 // Returns the Map that describes the column distribution in this matrix.
233 template<class MatrixType>
234 const Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type> > & OverlappingRowMatrix<MatrixType>::getRangeMap() const
235 {
236  return A_->getRangeMap();
237 }
238 
239 //==========================================================================
240  // Returns the RowGraph associated with this matrix.
241 template<class MatrixType>
242 Teuchos::RCP<const Tpetra::RowGraph<typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type> > OverlappingRowMatrix<MatrixType>::getGraph() const
243 {
244  throw std::runtime_error("Ifpack2::OverlappingRowMatrix - ERROR getGraph() is not implemented.");
245 }
246 
247 //==========================================================================
248 // Returns the number of global rows in this matrix.
249 template<class MatrixType>
250 global_size_t OverlappingRowMatrix<MatrixType>::getGlobalNumRows() const
251 {
252  return NumGlobalRows_;
253 }
254 
255 //==========================================================================
256 // Returns the number of global columns in this matrix.
257 template<class MatrixType>
258 global_size_t OverlappingRowMatrix<MatrixType>::getGlobalNumCols() const
259 {
260  return NumGlobalRows_;
261 }
262 
263 //==========================================================================
264 // Returns the number of rows owned on the calling node.
265 template<class MatrixType>
266 size_t OverlappingRowMatrix<MatrixType>::getNodeNumRows() const
267 {
268  return NumMyRows_;
269 }
270 
271 //==========================================================================
272 // Returns the number of columns needed to apply the forward operator on this node, i.e., the number of elements listed in the column map.
273 template<class MatrixType>
274 size_t OverlappingRowMatrix<MatrixType>::getNodeNumCols() const
275 {
276  return NumMyCols_;
277 }
278 
279 //==========================================================================
280 // Returns the index base for global indices for this matrix.
281 template<class MatrixType>
282 typename MatrixType::global_ordinal_type OverlappingRowMatrix<MatrixType>::getIndexBase() const
283 {
284  return A_->getIndexBase();
285 }
286 
287 //==========================================================================
288 // Returns the global number of entries in this matrix.
289 template<class MatrixType>
290 Tpetra::global_size_t OverlappingRowMatrix<MatrixType>::getGlobalNumEntries() const
291 {
292  return NumGlobalNonzeros_;
293 }
294 
295 //==========================================================================
296 // Returns the local number of entries in this matrix.
297 template<class MatrixType>
298 size_t OverlappingRowMatrix<MatrixType>::getNodeNumEntries() const
299 {
300  return NumMyNonzeros_;
301 }
302 
303 //==========================================================================
304 // Returns the current number of entries on this node in the specified global row.
305 /* Returns Teuchos::OrdinalTraits<size_t>::invalid() if the specified global row is not valid for this graph. */
306 template<class MatrixType>
307 size_t OverlappingRowMatrix<MatrixType>::getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
308 {
309  LocalOrdinal localRow = RowMap_->getLocalElement(globalRow);
310  if(localRow == Teuchos::OrdinalTraits<LocalOrdinal>::invalid()) return Teuchos::OrdinalTraits<size_t>::invalid();
311  else return getNumEntriesInLocalRow(localRow);
312 }
313 
314 
315 //==========================================================================
316 // Returns the current number of entries on this node in the specified local row.
317 /* Returns Teuchos::OrdinalTraits<size_t>::invalid() if the specified local row is not valid for this graph. */
318 template<class MatrixType>
319 size_t OverlappingRowMatrix<MatrixType>::getNumEntriesInLocalRow(LocalOrdinal localRow) const
320 {
321  if ((size_t)localRow < NumMyRowsA_)
322  return A_->getNumEntriesInLocalRow(localRow);
323  else
324  return ExtMatrix_->getNumEntriesInLocalRow((LocalOrdinal)(localRow-NumMyRowsA_));
325 }
326 
327 //==========================================================================
328 // Returns the number of global diagonal entries, based on global row/column index comparisons.
329 template<class MatrixType>
330 global_size_t OverlappingRowMatrix<MatrixType>::getGlobalNumDiags() const
331 {
332  throw std::runtime_error("Ifpack2::OverlappingRowMatrix::getGlobalNumDiags() not supported.");
333 }
334 
335 //==========================================================================
336 // Returns the number of local diagonal entries, based on global row/column index comparisons.
337 template<class MatrixType>
338 size_t OverlappingRowMatrix<MatrixType>::getNodeNumDiags() const
339 {
340  return A_->getNodeNumDiags();
341 }
342 
343 //==========================================================================
344 // Returns the maximum number of entries across all rows/columns on all nodes.
345 template<class MatrixType>
346 size_t OverlappingRowMatrix<MatrixType>::getGlobalMaxNumRowEntries() const
347 {
348  throw std::runtime_error("Ifpack2::OverlappingRowMatrix::getGlobalMaxNumRowEntries() not supported.");
349 }
350 
351 //==========================================================================
352 template<class MatrixType>
353 // Returns the maximum number of entries across all rows/columns on this node.
354 size_t OverlappingRowMatrix<MatrixType>::getNodeMaxNumRowEntries() const
355 {
356  return MaxNumEntries_;
357 }
358 
359 //==========================================================================
360 // Indicates whether this matrix has a well-defined column map
361 template<class MatrixType>
362 bool OverlappingRowMatrix<MatrixType>::hasColMap() const
363 {
364  return true;
365 }
366 
367 //==========================================================================
368 // Indicates whether this matrix is lower triangular.
369 template<class MatrixType>
370 bool OverlappingRowMatrix<MatrixType>::isLowerTriangular() const
371 {
372  return A_->isLowerTriangular();
373 }
374 
375 //==========================================================================
376 // Indicates whether this matrix is upper triangular.
377 template<class MatrixType>
378 bool OverlappingRowMatrix<MatrixType>::isUpperTriangular() const
379 {
380  return A_->isUpperTriangular();
381 }
382 //==========================================================================
383 // If matrix indices are in the local range, this function returns true. Otherwise, this function returns false. */
384 template<class MatrixType>
385 bool OverlappingRowMatrix<MatrixType>::isLocallyIndexed() const
386 {
387  return true;
388 }
389 
390 //==========================================================================
391 // If matrix indices are in the global range, this function returns true. Otherwise, this function returns false. */
392 template<class MatrixType>
393 bool OverlappingRowMatrix<MatrixType>::isGloballyIndexed() const
394 {
395  return false;
396 }
397 
398 //==========================================================================
399 // Returns \c true if fillComplete() has been called.
400 template<class MatrixType>
401 bool OverlappingRowMatrix<MatrixType>::isFillComplete() const
402 {
403  return true;
404 }
405 
406 //==========================================================================
407 // Extract a list of entries in a specified global row of this matrix. Put into pre-allocated storage.
408  /*
409  \param LocalRow - (In) Global row number for which indices are desired.
410  \param Indices - (Out) Global column indices corresponding to values.
411  \param Values - (Out) Matrix values.
412  \param NumEntries - (Out) Number of indices.
413 
414  Note: A std::runtime_error exception is thrown if either \c Indices or \c Values is not large enough to hold the data associated
415  with row \c GlobalRow. If \c GlobalRow does not belong to this node, then \c Indices and \c Values are unchanged and \c NumIndices is
416  returned as Teuchos::OrdinalTraits<size_t>::invalid().
417  */
418 template<class MatrixType>
419 void OverlappingRowMatrix<MatrixType>::getGlobalRowCopy(GlobalOrdinal GlobalRow,
420  const Teuchos::ArrayView<GlobalOrdinal> &Indices,
421  const Teuchos::ArrayView<Scalar> &Values,
422  size_t &NumEntries) const
423 {
424  LocalOrdinal LocalRow=RowMap_->getLocalElement(GlobalRow);
425  if(LocalRow == Teuchos::OrdinalTraits<LocalOrdinal>::invalid()) NumEntries = Teuchos::OrdinalTraits<size_t>::invalid();
426  else {
427  if ((size_t)LocalRow < NumMyRowsA_)
428  A_->getGlobalRowCopy(GlobalRow,Indices,Values,NumEntries);
429  else
430  ExtMatrix_->getGlobalRowCopy(GlobalRow,Indices,Values,NumEntries);
431  }
432 
433 }
434 
435 //==========================================================================
436 // Extract a list of entries in a specified local row of the graph. Put into storage allocated by calling routine.
437  /*
438  \param LocalRow - (In) Local row number for which indices are desired.
439  \param Indices - (Out) Local column indices corresponding to values.
440  \param Values - (Out) Matrix values.
441  \param NumIndices - (Out) Number of indices.
442 
443  Note: A std::runtime_error exception is thrown if either \c Indices or \c Values is not large enough to hold the data associated
444  with row \c LocalRow. If \c LocalRow is not valid for this node, then \c Indices and \c Values are unchanged and \c NumIndices is
445  returned as Teuchos::OrdinalTraits<size_t>::invalid().
446  */
447 template<class MatrixType>
448 void OverlappingRowMatrix<MatrixType>::getLocalRowCopy(LocalOrdinal LocalRow,
449  const Teuchos::ArrayView<LocalOrdinal> &Indices,
450  const Teuchos::ArrayView<Scalar> &Values,
451  size_t &NumEntries) const
452 {
453  if ((size_t)LocalRow < NumMyRowsA_)
454  A_->getLocalRowCopy(LocalRow,Indices,Values,NumEntries);
455  else
456  ExtMatrix_->getLocalRowCopy(LocalRow-(LocalOrdinal)(NumMyRowsA_),Indices,Values,NumEntries);
457 }
458 
459 //==========================================================================
460 // Extract a const, non-persisting view of global indices in a specified row of the matrix.
461  /*
462  \param GlobalRow - (In) Global row number for which indices are desired.
463  \param Indices - (Out) Global column indices corresponding to values.
464  \param Values - (Out) Row values
465  \pre <tt>isLocallyIndexed() == false</tt>
466  \post <tt>indices.size() == getNumEntriesInGlobalRow(GlobalRow)</tt>
467 
468  Note: If \c GlobalRow does not belong to this node, then \c indices is set to null.
469  */
470 template<class MatrixType>
471 void OverlappingRowMatrix<MatrixType>::getGlobalRowView(GlobalOrdinal GlobalRow,
472  Teuchos::ArrayView<const GlobalOrdinal> &indices,
473  Teuchos::ArrayView<const Scalar> &values) const
474 {
475  LocalOrdinal LocalRow=RowMap_->getLocalElement(GlobalRow);
476  if(LocalRow == Teuchos::OrdinalTraits<LocalOrdinal>::invalid()) {
477  indices=Teuchos::null;
478  values =Teuchos::null;
479  }
480  else {
481  if ((size_t)LocalRow < NumMyRowsA_)
482  A_->getGlobalRowView(GlobalRow,indices,values);
483  else
484  ExtMatrix_->getGlobalRowView(GlobalRow,indices,values);
485  }
486 }
487 
488 //==========================================================================
489 // Extract a const, non-persisting view of local indices in a specified row of the matrix.
490  /*
491  \param LocalRow - (In) Local row number for which indices are desired.
492  \param Indices - (Out) Global column indices corresponding to values.
493  \param Values - (Out) Row values
494  \pre <tt>isGloballyIndexed() == false</tt>
495  \post <tt>indices.size() == getNumEntriesInLocalRow(LocalRow)</tt>
496 
497  Note: If \c LocalRow does not belong to this node, then \c indices is set to null.
498  */
499 template<class MatrixType>
500 void OverlappingRowMatrix<MatrixType>::getLocalRowView(LocalOrdinal LocalRow,
501  Teuchos::ArrayView<const LocalOrdinal> &indices,
502  Teuchos::ArrayView<const Scalar> &values) const
503 {
504  if ((size_t)LocalRow < NumMyRowsA_)
505  A_->getLocalRowView(LocalRow,indices,values);
506  else
507  ExtMatrix_->getLocalRowView(LocalRow-(LocalOrdinal)(NumMyRowsA_),indices,values);
508 }
509 
510 //==========================================================================
511 // Get a copy of the diagonal entries owned by this node, with local row indices.
512  /* Returns a distributed Vector object partitioned according to this matrix's row map, containing the
513  the zero and non-zero diagonals owned by this node. */
514 template<class MatrixType>
515 void OverlappingRowMatrix<MatrixType>::getLocalDiagCopy(Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &diag) const
516 {
517  throw std::runtime_error("Ifpack2::OverlappingRowMatrix::getLocalDiagCopy not supported.");
518 }
519 
520 //==========================================================================
521 template<class MatrixType>
522 void OverlappingRowMatrix<MatrixType>::leftScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x)
523 {
524  throw std::runtime_error("Ifpack2::OverlappingRowMatrix does not support leftScale.");
525 }
526 
527 //==========================================================================
528 template<class MatrixType>
529 void OverlappingRowMatrix<MatrixType>::rightScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x)
530 {
531  throw std::runtime_error("Ifpack2::OverlappingRowMatrix does not support leftScale.");
532 }
533 
534 //==========================================================================
535 // Returns the Frobenius norm of the matrix.
536 template<class MatrixType>
537 typename Teuchos::ScalarTraits<typename MatrixType::scalar_type>::magnitudeType OverlappingRowMatrix<MatrixType>::getFrobeniusNorm() const
538 {
539  throw std::runtime_error("Ifpack2::OverlappingRowMatrix does not support getFrobeniusNorm.");
540 }
541 
542 
543 
544 //==========================================================================
545  // \brief Computes the operator-multivector application.
546  /* Loosely, performs \f$Y = \alpha \cdot A^{\textrm{mode}} \cdot X + \beta \cdot Y\f$. However, the details of operation
547  vary according to the values of \c alpha and \c beta. Specifically
548  - if <tt>beta == 0</tt>, apply() <b>must</b> overwrite \c Y, so that any values in \c Y (including NaNs) are ignored.
549  - if <tt>alpha == 0</tt>, apply() <b>may</b> short-circuit the operator, so that any values in \c X (including NaNs) are ignored.
550 
551  This is analagous to the *Multiply* function in Ifpack, not the *Apply*
552  */
553 template<class MatrixType>
554 void OverlappingRowMatrix<MatrixType>::apply(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X,
555  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
556  Teuchos::ETransp mode,
557  Scalar alpha,
558  Scalar beta) const
559 {
560  this->template applyTempl<Scalar,Scalar>(X, Y, mode, alpha, beta);
561 }
562 
563 
564 //==========================================================================
565  // \brief Computes the operator-multivector application.
566  /* Loosely, performs \f$Y = \alpha \cdot A^{\textrm{mode}} \cdot X + \beta \cdot Y\f$. However, the details of operation
567  vary according to the values of \c alpha and \c beta. Specifically
568  - if <tt>beta == 0</tt>, apply() <b>must</b> overwrite \c Y, so that any values in \c Y (including NaNs) are ignored.
569  - if <tt>alpha == 0</tt>, apply() <b>may</b> short-circuit the operator, so that any values in \c X (including NaNs) are ignored.
570 
571  This is analagous to the *Multiply* function in Ifpack, not the *Apply*
572  */
573 template<class MatrixType>
574 template<class DomainScalar, class RangeScalar>
575 void OverlappingRowMatrix<MatrixType>::applyTempl(const Tpetra::MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> &X,
576  Tpetra::MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
577  Teuchos::ETransp mode,
578  RangeScalar alpha,
579  RangeScalar beta) const
580 {
581  // Note: This isn't AztecOO compliant. But neither was Ifpack's version.
582  TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
583  "Ifpack2::OverlappingRowMatrix::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
584 
585  RangeScalar zero = Teuchos::ScalarTraits<RangeScalar>::zero();
586  Teuchos::ArrayRCP<Teuchos::ArrayRCP<const DomainScalar> > x_ptr = X.get2dView();
587  Teuchos::ArrayRCP<Teuchos::ArrayRCP<RangeScalar> > y_ptr = Y.get2dViewNonConst();
588  Y.putScalar(zero);
589  size_t NumVectors = Y.getNumVectors();
590 
591  for (size_t i = 0 ; i < NumMyRowsA_ ; ++i) {
592  size_t Nnz;
593  // Use this class's getrow to make the below code simpler
594  A_->getLocalRowCopy(i,Indices_(),Values_(),Nnz);
595  if (mode==Teuchos::NO_TRANS){
596  for (size_t j = 0 ; j < Nnz ; ++j)
597  for (size_t k = 0 ; k < NumVectors ; ++k)
598  y_ptr[k][i] += (RangeScalar)Values_[j] * (RangeScalar)x_ptr[k][Indices_[j]];
599  }
600  else if (mode==Teuchos::TRANS){
601  for (size_t j = 0 ; j < Nnz ; ++j)
602  for (size_t k = 0 ; k < NumVectors ; ++k)
603  y_ptr[k][Indices_[j]] += (RangeScalar)Values_[j] * (RangeScalar)x_ptr[k][i];
604  }
605  else { //mode==Teuchos::CONJ_TRANS
606  for (size_t j = 0 ; j < Nnz ; ++j)
607  for (size_t k = 0 ; k < NumVectors ; ++k)
608  y_ptr[k][Indices_[j]] += Teuchos::ScalarTraits<RangeScalar>::conjugate((RangeScalar)Values_[j]) * (RangeScalar)x_ptr[k][i];
609  }
610  }
611 
612  for (size_t i = 0 ; i < NumMyRowsB_ ; ++i) {
613  size_t Nnz;
614  // Use this class's getrow to make the below code simpler
615  ExtMatrix_->getLocalRowCopy(i,Indices_(),Values_(),Nnz);
616  if (mode==Teuchos::NO_TRANS){
617  for (size_t j = 0 ; j < Nnz ; ++j)
618  for (size_t k = 0 ; k < NumVectors ; ++k)
619  y_ptr[k][NumMyRowsA_+i] += (RangeScalar)Values_[j] * (RangeScalar)x_ptr[k][Indices_[j]];
620  }
621  else if (mode==Teuchos::TRANS){
622  for (size_t j = 0 ; j < Nnz ; ++j)
623  for (size_t k = 0 ; k < NumVectors ; ++k)
624  y_ptr[k][NumMyRowsA_+Indices_[j]] += (RangeScalar)Values_[j] * (RangeScalar)x_ptr[k][i];
625  }
626  else { //mode==Teuchos::CONJ_TRANS
627  for (size_t j = 0 ; j < Nnz ; ++j)
628  for (size_t k = 0 ; k < NumVectors ; ++k)
629  y_ptr[k][NumMyRowsA_+Indices_[j]] += Teuchos::ScalarTraits<RangeScalar>::conjugate((RangeScalar)Values_[j]) * (RangeScalar)x_ptr[k][i];
630  }
631  }
632 }
633 
634 
635 
636 // ======================================================================
637 template<class MatrixType>
638 void OverlappingRowMatrix<MatrixType>::importMultiVector(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X,
639  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &OvX,
640  Tpetra::CombineMode CM)
641 {
642  this->template importMultiVectorTempl<Scalar>(X, OvX, CM);
643 }
644 
645 // ======================================================================
646 template<class MatrixType>
647 template<class OpScalar>
648 void OverlappingRowMatrix<MatrixType>::importMultiVectorTempl(const Tpetra::MultiVector<OpScalar,LocalOrdinal,GlobalOrdinal,Node> &X,
649  Tpetra::MultiVector<OpScalar,LocalOrdinal,GlobalOrdinal,Node> &OvX,
650  Tpetra::CombineMode CM)
651 {
652  OvX.doImport(X,*Importer_,CM);
653 }
654 
655 
656 // ======================================================================
657 template<class MatrixType>
658 void OverlappingRowMatrix<MatrixType>::exportMultiVector(const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &OvX,
659  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X,
660  Tpetra::CombineMode CM)
661 {
662  this->template exportMultiVectorTempl<Scalar>(OvX, X, CM);
663 }
664 
665 // ======================================================================
666 template<class MatrixType>
667 template<class OpScalar>
668 void OverlappingRowMatrix<MatrixType>::exportMultiVectorTempl(const Tpetra::MultiVector<OpScalar,LocalOrdinal,GlobalOrdinal,Node> &OvX,
669  Tpetra::MultiVector<OpScalar,LocalOrdinal,GlobalOrdinal,Node> &X,
670  Tpetra::CombineMode CM)
671 {
672  X.doExport(OvX,*Importer_,CM);
673 }
674 
675 //==========================================================================
676 // Indicates whether this operator supports applying the adjoint operator.
677 template<class MatrixType>
678 bool OverlappingRowMatrix<MatrixType>::hasTransposeApply() const
679 {
680  return true;
681 }
682 
683 //==========================================================================
684 template<class MatrixType>
685 bool OverlappingRowMatrix<MatrixType>::supportsRowViews() const
686 {
687  return false;
688 }
689 
690 
691 } // namespace Ifpack2
692 
693 #endif // IFPACK2_OVERLAPPINGROWMATRIX_DEF_HPP
virtual void leftScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the left with the Vector x.
Definition: Ifpack2_OverlappingRowMatrix_def.hpp:522
virtual Teuchos::ScalarTraits< Scalar >::magnitudeType getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_OverlappingRowMatrix_def.hpp:537
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_OverlappingRowMatrix_def.hpp:242
Ifpack2::OverlappingRowMatrix: matrix with ghost rows, based on Tpetra::RowMatrix.
Definition: Ifpack2_OverlappingRowMatrix_decl.hpp:60
OverlappingRowMatrix(const Teuchos::RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &Matrix_in, int OverlapLevel_in)
Standard constructor.
Definition: Ifpack2_OverlappingRowMatrix_def.hpp:54
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67