All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Xpetra_CrsMatrixWrap.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Xpetra: A linear algebra interface package
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jeremie Gaidamour (jngaida@sandia.gov)
40 // Jonathan Hu (jhu@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 
47 // WARNING: This code is experimental. Backwards compatibility should not be expected.
48 
49 #ifndef XPETRA_CRSMATRIXWRAP_HPP
50 #define XPETRA_CRSMATRIXWRAP_HPP
51 
52 #include <Kokkos_DefaultNode.hpp>
54 
55 #include "Xpetra_ConfigDefs.hpp"
56 #include "Xpetra_Exceptions.hpp"
57 
58 #include "Xpetra_MultiVector.hpp"
59 #include "Xpetra_CrsGraph.hpp"
60 #include "Xpetra_CrsMatrix.hpp"
62 
63 #include "Xpetra_Matrix.hpp"
64 
66 #include <Teuchos_Hashtable.hpp>
67 
72 namespace Xpetra {
73 
74  typedef std::string viewLabel_t;
75 
80 template <class Scalar,
81  class LocalOrdinal = int,
82  class GlobalOrdinal = LocalOrdinal,
84  class LocalMatOps = typename Kokkos::DefaultKernels<Scalar,LocalOrdinal,Node>::SparseOps > //TODO: or BlockSparseOp ?
85 class CrsMatrixWrap : public Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps> {
86 
91 #ifdef HAVE_XPETRA_TPETRA
93 #endif
96 
97 public:
98 
100 
101 
103  CrsMatrixWrap(const RCP<const Map> &rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile)
104  : finalDefaultView_(false)
105  {
106  // Set matrix data
107  matrixData_ = CrsMatrixFactory::Build(rowMap, maxNumEntriesPerRow, pftype);
108 
109  // Default view
111  }
112 
114  CrsMatrixWrap(const RCP<const Map> &rowMap, const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = Xpetra::DynamicProfile)
115  : finalDefaultView_(false)
116  {
117  // Set matrix data
118  matrixData_ = CrsMatrixFactory::Build(rowMap, NumEntriesPerRowToAlloc, pftype);
119 
120  // Default view
122  }
123 
125  CrsMatrixWrap(const RCP<const Map> &rowMap, const RCP<const Map>& colMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype = Xpetra::DynamicProfile)
126  : finalDefaultView_(false)
127  {
128  // Set matrix data
129  matrixData_ = CrsMatrixFactory::Build(rowMap, colMap, maxNumEntriesPerRow, pftype);
130 
131  // Default view
133  }
134 
136  CrsMatrixWrap(const RCP<const Map> &rowMap, const RCP<const Map>& colMap, const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, Xpetra::ProfileType pftype = Xpetra::DynamicProfile)
137  : finalDefaultView_(false)
138  {
139  // Set matrix data
140  matrixData_ = CrsMatrixFactory::Build(rowMap, colMap, NumEntriesPerRowToAlloc, pftype);
141 
142  // Default view
144  }
145 
147  : finalDefaultView_(matrix->isFillComplete())
148  {
149  // Set matrix data
150  matrixData_ = matrix;
151 
152  // Default view
154  }
155 
157  virtual ~CrsMatrixWrap() {}
158 
160 
161 
163 
164 
166 
177  void insertGlobalValues(GlobalOrdinal globalRow, const ArrayView<const GlobalOrdinal> &cols, const ArrayView<const Scalar> &vals) {
178  matrixData_->insertGlobalValues(globalRow, cols, vals);
179  }
180 
182 
189  void insertLocalValues(LocalOrdinal localRow, const ArrayView<const LocalOrdinal> &cols, const ArrayView<const Scalar> &vals) {
190  matrixData_->insertLocalValues(localRow, cols, vals);
191  }
192 
194 
199  void replaceGlobalValues(GlobalOrdinal globalRow,
200  const ArrayView<const GlobalOrdinal> &cols,
201  const ArrayView<const Scalar> &vals) { matrixData_->replaceGlobalValues(globalRow, cols, vals); }
202 
204 
207  void replaceLocalValues(LocalOrdinal localRow,
208  const ArrayView<const LocalOrdinal> &cols,
209  const ArrayView<const Scalar> &vals) { matrixData_->replaceLocalValues(localRow, cols, vals); }
210 
212  virtual void setAllToScalar(const Scalar &alpha) { matrixData_->setAllToScalar(alpha); }
213 
215  void scale(const Scalar &alpha) {
216  matrixData_->scale(alpha);
217  }
218 
220 
222 
223 
232  void resumeFill(const RCP< ParameterList > &params=null) {
233  matrixData_->resumeFill(params);
234  }
235 
247  void fillComplete(const RCP<const Map> &domainMap, const RCP<const Map> &rangeMap, const RCP<Teuchos::ParameterList> &params = null) {
248  matrixData_->fillComplete(domainMap, rangeMap, params);
249 
250  // Update default view with the colMap because colMap can be <tt>null</tt> until fillComplete() is called.
252  }
253 
267  //TODO : Get ride of "Tpetra"::OptimizeOption
268  void fillComplete(const RCP<ParameterList> &params = null) {
269  matrixData_->fillComplete(params);
270 
271  // Update default view with the colMap because colMap can be <tt>null</tt> until fillComplete() is called.
273  }
274 
276 
278 
281  return matrixData_->getGlobalNumRows();
282  }
283 
285 
288  return matrixData_->getGlobalNumCols();
289  }
290 
292  size_t getNodeNumRows() const {
293  return matrixData_->getNodeNumRows();
294  }
295 
298  return matrixData_->getGlobalNumEntries();
299  }
300 
302  size_t getNodeNumEntries() const {
303  return matrixData_->getNodeNumEntries();
304  }
305 
307 
308  size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const {
309  return matrixData_->getNumEntriesInLocalRow(localRow);
310  }
311 
313 
316  return matrixData_->getGlobalNumDiags();
317  }
318 
320 
322  size_t getNodeNumDiags() const {
323  return matrixData_->getNodeNumDiags();
324  }
325 
327 
329  size_t getGlobalMaxNumRowEntries() const {
330  return matrixData_->getGlobalMaxNumRowEntries();
331  }
332 
334 
336  size_t getNodeMaxNumRowEntries() const {
337  return matrixData_->getNodeMaxNumRowEntries();
338  }
339 
341  bool isLocallyIndexed() const {
342  return matrixData_->isLocallyIndexed();
343  }
344 
346  bool isGloballyIndexed() const {
347  return matrixData_->isGloballyIndexed();
348  }
349 
351  bool isFillComplete() const {
352  return matrixData_->isFillComplete();
353  }
354 
356 
368  void getLocalRowCopy(LocalOrdinal LocalRow,
369  const ArrayView<LocalOrdinal> &Indices,
370  const ArrayView<Scalar> &Values,
371  size_t &NumEntries
372  ) const {
373  matrixData_->getLocalRowCopy(LocalRow, Indices, Values, NumEntries);
374  }
375 
377 
386  void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView<const GlobalOrdinal> &indices, ArrayView<const Scalar> &values) const {
387  matrixData_->getGlobalRowView(GlobalRow, indices, values);
388  }
389 
391 
400  void getLocalRowView(LocalOrdinal LocalRow, ArrayView<const LocalOrdinal> &indices, ArrayView<const Scalar> &values) const {
401  matrixData_->getLocalRowView(LocalRow, indices, values);
402  }
403 
405 
408  matrixData_->getLocalDiagCopy(diag);
409  }
410 
413  return matrixData_->getFrobeniusNorm();
414  }
415 
417 
419 
420 
422 
431  //TODO virtual=0 // TODO: Add default parameters ?
432 // void multiply(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y, Teuchos::ETransp trans, Scalar alpha, Scalar beta) const {
433 // matrixData_->multiply(X, Y, trans, alpha, beta);
434 // }
435 
437 
439 
440 
442 
447  Scalar alpha = ScalarTraits<Scalar>::one(),
448  Scalar beta = ScalarTraits<Scalar>::zero()) const {
449 
450  matrixData_->apply(X,Y,mode,alpha,beta);
451  }
452 
456  return matrixData_->getDomainMap();
457  }
458 
462  return matrixData_->getRangeMap();
463  }
464 
468 
470  const RCP<const Map> & getColMap(viewLabel_t viewLabel) const {
471  TEUCHOS_TEST_FOR_EXCEPTION(Matrix::operatorViewTable_.containsKey(viewLabel) == false, Xpetra::Exceptions::RuntimeError, "Xpetra::Matrix.GetColMap(): view '" + viewLabel + "' does not exist.");
472  updateDefaultView(); // If CrsMatrix::fillComplete() have been used instead of CrsMatrixWrap::fillComplete(), the default view is updated.
473  return Matrix::operatorViewTable_.get(viewLabel)->GetColMap();
474  }
475 
477  matrixData_->removeEmptyProcessesInPlace(newMap);
478  this->operatorViewTable_.get(this->GetCurrentViewLabel())->SetRowMap(matrixData_->getRowMap());
479  this->operatorViewTable_.get(this->GetCurrentViewLabel())->SetColMap(matrixData_->getColMap());
480  }
481 
483 
485  //{@
486 
489  return matrixData_->getMap();
490  }
491 
493  void doImport(const Matrix &source,
495  const CrsMatrixWrap & sourceWrp = dynamic_cast<const CrsMatrixWrap &>(source);
496  matrixData_->doImport(*sourceWrp.getCrsMatrix(), importer, CM);
497  }
498 
500  void doExport(const Matrix &dest,
502  const CrsMatrixWrap & destWrp = dynamic_cast<const CrsMatrixWrap &>(dest);
503  matrixData_->doExport(*destWrp.getCrsMatrix(), importer, CM);
504  }
505 
507  void doImport(const Matrix &source,
509  const CrsMatrixWrap & sourceWrp = dynamic_cast<const CrsMatrixWrap &>(source);
510  matrixData_->doImport(*sourceWrp.getCrsMatrix(), exporter, CM);
511  }
512 
514  void doExport(const Matrix &dest,
516  const CrsMatrixWrap & destWrp = dynamic_cast<const CrsMatrixWrap &>(dest);
517  matrixData_->doExport(*destWrp.getCrsMatrix(), exporter, CM);
518  }
519 
520  // @}
521 
523 
524 
526  std::string description() const {
527  return "Xpetra::CrsMatrixWrap";
528  }
529 
532  // Teuchos::EVerbosityLevel vl = verbLevel;
533  // if (vl == VERB_DEFAULT) vl = VERB_LOW;
534  // RCP<const Comm<int> > comm = this->getComm();
535  // const int myImageID = comm->getRank(),
536  // numImages = comm->getSize();
537 
538  // if (myImageID == 0) out << this->description() << std::endl;
539 
540  matrixData_->describe(out,verbLevel);
541 
542  // Teuchos::OSTab tab(out);
543  }
544 
545  // JG: Added:
546 
548  RCP<const CrsGraph> getCrsGraph() const { return matrixData_->getCrsGraph(); }
549 
551 
553 
554 
555 private:
556 
557  // Default view is created after fillComplete()
558  // Because ColMap might not be available before fillComplete().
560 
561  // Create default view
562  this->defaultViewLabel_ = "point";
563  this->CreateView(this->GetDefaultViewLabel(), matrixData_->getRowMap(), matrixData_->getColMap());
564 
565  // Set current view
566  this->currentViewLabel_ = this->GetDefaultViewLabel();
567  }
568 
569 private:
570 
571  // The colMap can be <tt>null</tt> until fillComplete() is called. The default view of the Matrix have to be updated when fillComplete() is called.
572  // If CrsMatrix::fillComplete() have been used instead of CrsMatrixWrap::fillComplete(), the default view is updated when getColMap() is called.
573  void updateDefaultView() const {
574  if ((finalDefaultView_ == false) && matrixData_->isFillComplete() ) {
575  // Update default view with the colMap
577  finalDefaultView_ = true;
578  }
579  }
580  // The boolean finalDefaultView_ keep track of the status of the default view (= already updated or not)
581  // See also CrsMatrixWrap::updateDefaultView()
582  mutable bool finalDefaultView_;
583 
584 
586 
587 }; //class Matrix
588 
589 } //namespace Xpetra
590 
591 #define XPETRA_CRSMATRIXWRAP_SHORT
592 #endif //XPETRA_CRSMATRIXWRAP_DECL_HPP
593 
594 //NOTE: if CrsMatrix and VbrMatrix share a common interface for fillComplete() etc, I can move some stuff in Xpetra_Matrix.hpp
595 //TODO: getUnderlyingMatrix() method
void doImport(const Matrix &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Import.
Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > Map
void CreateView(viewLabel_t viewLabel, const RCP< const Map > &rowMap, const RCP< const Map > &colMap)
void fillComplete(const RCP< const Map > &domainMap, const RCP< const Map > &rangeMap, const RCP< Teuchos::ParameterList > &params=null)
Signal that data entry is complete, specifying domain and range maps.
const Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const
Implements DistObject interface.
void fillComplete(const RCP< ParameterList > &params=null)
Signal that data entry is complete.
size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Xpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > CrsMatrix
CrsMatrixWrap(const RCP< const Map > &rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying fixed number of entries for each row.
Exception throws to report errors in the internal logical of the program.
void getLocalRowCopy(LocalOrdinal LocalRow, const ArrayView< LocalOrdinal > &Indices, const ArrayView< Scalar > &Values, size_t &NumEntries) const
Extract a list of entries in a specified local row of the matrix. Put into storage allocated by calli...
void insertGlobalValues(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Insert matrix entries, using global IDs.
Xpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > CrsGraph
global_size_t getGlobalNumCols() const
Returns the number of global columns in the matrix.
size_t getNodeMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
void doExport(const Matrix &dest, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)
Export (using an Importer).
Xpetra::MatrixView< LocalOrdinal, GlobalOrdinal, Node > MatrixView
void scale(const Scalar &alpha)
Scale the current values of a matrix, this = alpha*this.
Xpetra::CrsMatrixFactory< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > CrsMatrixFactory
void insertLocalValues(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Insert matrix entries, using local IDs.
const RCP< const Map > & getColMap(viewLabel_t viewLabel) const
Returns the Map that describes the column distribution in this matrix.
global_size_t getGlobalNumDiags() const
Returns the number of global diagonal entries, based on global row/column index comparisons.
Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > Matrix
const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Returns the Map associated with the domain of this operator. This will be null until fillComplete() i...
RCP< CrsMatrix > getCrsMatrix() const
CrsMatrixWrap(const RCP< const Map > &rowMap, const RCP< const Map > &colMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying fixed number of entries for each row and column map.
void resumeFill(const RCP< ParameterList > &params=null)
const viewLabel_t & GetCurrentViewLabel() const
RCP< const CrsGraph > getCrsGraph() const
Returns the CrsGraph associated with this matrix.
CrsMatrixWrap(const RCP< const Map > &rowMap, const RCP< const Map > &colMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying fixed number of entries for each row and column map.
viewLabel_t currentViewLabel_
ScalarTraits< Scalar >::magnitudeType getFrobeniusNorm() const
Get Frobenius norm of the matrix.
global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Teuchos::Hashtable< viewLabel_t, RCP< MatrixView > > operatorViewTable_
Xpetra::TpetraCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > TpetraCrsMatrix
void replaceGlobalValues(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Replace matrix entries, using global IDs.
std::string viewLabel_t
size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Returns the current number of entries on this node in the specified local row.
size_t global_size_t
Global size_t object.
virtual void setAllToScalar(const Scalar &alpha)
Set all matrix entries equal to scalar.
const RCP< const Map > & getColMap() const
Returns the Map that describes the column distribution in this matrix. This might be null until fillC...
static const EVerbosityLevel verbLevel_default
CrsMatrixWrap(RCP< CrsMatrix > matrix)
void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView< const GlobalOrdinal > &indices, ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of global indices in a specified row of the matrix.
bool isLocallyIndexed() const
If matrix indices are in the local range, this function returns true. Otherwise, this function return...
viewLabel_t defaultViewLabel_
Concrete implementation of Xpetra::Matrix.
void replaceLocalValues(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Replace matrix entries, using local IDs.
void doExport(const Matrix &dest, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Export.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
CombineMode
Xpetra::Combine Mode enumerable type.
global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
virtual ~CrsMatrixWrap()
Destructor.
static RCP< CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile, const Teuchos::RCP< Teuchos::ParameterList > &plist=Teuchos::null)
Constructor specifying fixed number of entries for each row.
bool isFillComplete() const
Returns true if fillComplete() has been called and the matrix is in compute mode. ...
size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
const viewLabel_t & GetDefaultViewLabel() const
void removeEmptyProcessesInPlace(const Teuchos::RCP< const Map > &newMap)
CrsMatrixWrap(const RCP< const Map > &rowMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying (possibly different) number of entries in each row.
void doImport(const Matrix &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)
Import (using an Exporter).
const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
std::string description() const
Return a simple one-line description of this object.
void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=ScalarTraits< Scalar >::one(), Scalar beta=ScalarTraits< Scalar >::zero()) const
Computes the sparse matrix-multivector multiplication.
Xpetra-specific matrix class.
void getLocalDiagCopy(Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const
Get a copy of the diagonal entries owned by this node, with local row idices.
void getLocalRowView(LocalOrdinal LocalRow, ArrayView< const LocalOrdinal > &indices, ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of local indices in a specified row of the matrix.
size_t getNodeNumDiags() const
Returns the number of local diagonal entries, based on global row/column index comparisons.
size_t getNodeNumRows() const
Returns the number of matrix rows owned on the calling node.