Tpetra_CrsMatrix_decl.hpp

00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //               Tpetra: Templated Linear Algebra Services Package 
00005 //                 Copyright (2008) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ************************************************************************
00027 //@HEADER
00028 
00029 #ifndef TPETRA_CRSMATRIX_DECL_HPP
00030 #define TPETRA_CRSMATRIX_DECL_HPP
00031 
00032 // TODO: row-wise insertion of entries in globalAssemble() may be more efficient
00033 
00034 #include <Kokkos_DefaultNode.hpp>
00035 #include <Kokkos_CrsMatrix.hpp>
00036 #include <Kokkos_DefaultSparseMultiply.hpp>
00037 #include <Kokkos_DefaultSparseSolve.hpp>
00038 
00039 #include <Teuchos_ScalarTraits.hpp>
00040 #include <Teuchos_OrdinalTraits.hpp>
00041 
00042 #include "Tpetra_RowMatrix.hpp"
00043 #include "Tpetra_CrsGraph.hpp"
00044 #include "Tpetra_Vector.hpp"
00045 #include "Tpetra_CrsMatrixMultiplyOp_decl.hpp"
00046 
00047 namespace Tpetra {
00048   // struct for i,j,v triplets
00049   template <class Ordinal, class Scalar>
00050   struct CrsIJV {
00051     CrsIJV();
00052     CrsIJV(Ordinal row, Ordinal col, const Scalar &val);
00053     Ordinal i,j;
00054     Scalar  v;
00055   };
00056 }
00057 
00058 namespace Teuchos {
00059   // SerializationTraits specialization for CrsIJV, using DirectSerialization
00060   template <typename Ordinal, typename Scalar>
00061   class SerializationTraits<int,Tpetra::CrsIJV<Ordinal,Scalar> >
00062   : public DirectSerializationTraits<int,Tpetra::CrsIJV<Ordinal,Scalar> >
00063   {};
00064 }
00065 
00066 namespace std {
00067   template <class Ordinal, class Scalar>
00068   bool operator<(const Tpetra::CrsIJV<Ordinal,Scalar> &ijv1, const Tpetra::CrsIJV<Ordinal,Scalar> &ijv2);
00069 }
00070 
00071 namespace Tpetra {
00072 
00074 
00090   template<class Scalar, class LocalOrdinal = int, class GlobalOrdinal = LocalOrdinal, class Node = Kokkos::DefaultNode::DefaultNodeType, class LocalMatVec = Kokkos::DefaultSparseMultiply<Scalar,LocalOrdinal,Node>, class LocalMatSolve = Kokkos::DefaultSparseSolve<Scalar,LocalOrdinal,Node> >
00091   class CrsMatrix : public RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> {
00092     public:
00093       typedef Scalar        scalar_type;
00094       typedef LocalOrdinal  local_ordinal_type;
00095       typedef GlobalOrdinal global_ordinal_type;
00096       typedef Node          node_type;
00097       typedef LocalMatVec   mat_vec_type;
00098       typedef LocalMatSolve mat_solve_type;
00099 
00101 
00102 
00104       CrsMatrix(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype = DynamicProfile);
00105 
00107       CrsMatrix(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, const Teuchos::ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = DynamicProfile);
00108 
00110       CrsMatrix(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &colMap, size_t maxNumEntriesPerRow, ProfileType pftype = DynamicProfile);
00111 
00113       CrsMatrix(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &colMap, const Teuchos::ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = DynamicProfile);
00114 
00116       explicit CrsMatrix(const Teuchos::RCP<const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > &graph);
00117 
00118       // !Destructor.
00119       virtual ~CrsMatrix();
00120 
00122 
00124 
00125 
00127       void insertGlobalValues(GlobalOrdinal globalRow, const Teuchos::ArrayView<const GlobalOrdinal> &cols, const Teuchos::ArrayView<const Scalar> &vals);
00128 
00130       void insertLocalValues(LocalOrdinal localRow, const Teuchos::ArrayView<const LocalOrdinal> &cols, const Teuchos::ArrayView<const Scalar> &vals);
00131 
00133 
00137       void replaceGlobalValues(GlobalOrdinal globalRow, 
00138                          const Teuchos::ArrayView<const GlobalOrdinal> &cols,
00139                          const Teuchos::ArrayView<const Scalar>        &vals);
00140 
00142 
00144       void replaceLocalValues(LocalOrdinal localRow, 
00145                          const Teuchos::ArrayView<const LocalOrdinal> &cols,
00146                          const Teuchos::ArrayView<const Scalar>       &vals);
00147 
00149 
00150       void sumIntoGlobalValues(GlobalOrdinal globalRow, 
00151                          const Teuchos::ArrayView<const GlobalOrdinal> &cols,
00152                          const Teuchos::ArrayView<const Scalar>        &vals);
00153 
00154 
00156       void setAllToScalar(const Scalar &alpha);
00157 
00159       void scale(const Scalar &alpha);
00160 
00162 
00164 
00165 
00167       void globalAssemble();
00168 
00173       void fillComplete(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &domainMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rangeMap, OptimizeOption os = DoOptimizeStorage);
00174 
00180       void fillComplete(OptimizeOption os = DoOptimizeStorage);
00181 
00183       void optimizeStorage();
00184 
00186 
00188 
00189 
00191       const Teuchos::RCP<const Teuchos::Comm<int> > & getComm() const;
00192 
00194       Teuchos::RCP<Node> getNode() const;
00195 
00197       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRowMap() const;
00198 
00200       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getColMap() const;
00201 
00203       Teuchos::RCP<const RowGraph<LocalOrdinal,GlobalOrdinal,Node> > getGraph() const;
00204 
00206       Teuchos::RCP<const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > getCrsGraph() const;
00207 
00209       global_size_t getGlobalNumRows() const;
00210 
00212       global_size_t getGlobalNumCols() const;
00213 
00215       size_t getNodeNumRows() const;
00216 
00218       size_t getNodeNumCols() const;
00219 
00221       GlobalOrdinal getIndexBase() const;
00222 
00224       global_size_t getGlobalNumEntries() const;
00225 
00227       size_t getNodeNumEntries() const;
00228 
00230 
00231       size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const;
00232 
00234 
00235       size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const;
00236 
00238       global_size_t getGlobalNumDiags() const;
00239 
00241       size_t getNodeNumDiags() const;
00242 
00244       size_t getGlobalMaxNumRowEntries() const;
00245 
00247       size_t getNodeMaxNumRowEntries() const;
00248 
00250       bool hasColMap() const; 
00251 
00253       bool isLowerTriangular() const;
00254 
00256       bool isUpperTriangular() const;
00257 
00259       bool isLocallyIndexed() const;
00260 
00262       bool isGloballyIndexed() const;
00263 
00265       bool isFillComplete() const;
00266 
00268 
00278       void getGlobalRowCopy(GlobalOrdinal GlobalRow,
00279                             const Teuchos::ArrayView<GlobalOrdinal> &Indices,
00280                             const Teuchos::ArrayView<Scalar> &Values,
00281                             size_t &NumEntries) const;
00282 
00284 
00294       void getLocalRowCopy(LocalOrdinal LocalRow, 
00295                            const Teuchos::ArrayView<LocalOrdinal> &Indices, 
00296                            const Teuchos::ArrayView<Scalar> &Values,
00297                            size_t &NumEntries) const;
00298 
00300 
00309       void getGlobalRowView(GlobalOrdinal GlobalRow, 
00310                             Teuchos::ArrayRCP<const GlobalOrdinal> &indices,
00311                             Teuchos::ArrayRCP<const Scalar>        &values) const;
00312 
00314 
00323       void getLocalRowView(LocalOrdinal LocalRow,
00324                            Teuchos::ArrayRCP<const LocalOrdinal> &indices,
00325                            Teuchos::ArrayRCP<const Scalar>       &values) const;
00326 
00328 
00330       void getLocalDiagCopy(Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &diag) const;
00331 
00333 
00335 
00336 
00338 
00345       template <class DomainScalar, class RangeScalar>
00346       void multiply(const MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> &Y, Teuchos::ETransp trans, RangeScalar alpha, RangeScalar beta) const;
00347 
00349 
00356       template <class DomainScalar, class RangeScalar>
00357       void solve(const MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> & Y, MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> &X, Teuchos::ETransp trans) const;
00358           
00360 
00362 
00363 
00365 
00368       void apply(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y, 
00369                  Teuchos::ETransp mode = Teuchos::NO_TRANS,
00370                  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
00371                  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const;
00372 
00374       bool hasTransposeApply() const;
00375 
00378       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getDomainMap() const;
00379 
00382       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRangeMap() const;
00383 
00385 
00387 
00388 
00390       bool isStorageOptimized() const;
00391 
00393       ProfileType getProfileType() const;
00394 
00396       bool isStaticGraph() const;
00397 
00399 
00401 
00402 
00404       std::string description() const;
00405 
00407       void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
00408 
00410 
00411     private:
00412       // copy constructor disabled
00413       CrsMatrix(const CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatVec,LocalMatSolve> &Source);
00414       // operator= disabled
00415       CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatVec,LocalMatSolve> & operator=(const CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatVec,LocalMatSolve> &rhs);
00416     protected:
00417 
00418       // useful typedefs
00419       typedef Teuchos::OrdinalTraits<LocalOrdinal>    LOT;
00420       typedef Teuchos::OrdinalTraits<GlobalOrdinal>   GOT;
00421       typedef MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> MV;
00422 
00423       enum GraphAllocationStatus {
00424         GraphAlreadyAllocated,
00425         GraphNotYetAllocated
00426       };
00427       void allocateValues( typename CrsGraph<LocalOrdinal,GlobalOrdinal,Node>::AllocateLocalGlobal lg, Scalar alpha, GraphAllocationStatus gas);
00428       void sortEntries();
00429       void mergeRedundantEntries();
00430       void checkInternalState() const;
00431       void updateAllocation(size_t lrow, size_t allocSize);
00432       void fillLocalMatrix();
00433 
00435 
00443       Teuchos::ArrayRCP<const Scalar> getFullView(size_t myRow, RowInfo sizeInfo) const;
00444 
00446 
00454       Teuchos::ArrayRCP<Scalar> getFullViewNonConst(size_t myRow, RowInfo sizeInfo);
00455 
00456       Teuchos::RCP<const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > staticGraph_;
00457       Teuchos::RCP<      CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >     myGraph_;
00458 
00459       Kokkos::CrsMatrix<Scalar,Node> lclMatrix_;
00460       LocalMatVec lclMatVec_;
00461       LocalMatSolve lclMatSolve_;
00462 
00463       bool valuesAreAllocated_,
00464            constructedWithFilledGraph_,
00465            constructedWithOptimizedGraph_,
00466            fillComplete_,
00467            storageOptimized_;
00468 
00469       // matrix values. before allocation, these are Teuchos::null.
00470       // after allocation, one is Teuchos::Null.
00471       // these are parallel compute buffers, not host memory. therefore, in general, they cannot be dereferenced in host cost.
00472       // 1D == StaticAllocation, 2D == DynamicAllocation
00473       // The allocation always matches that of graph_
00474       Teuchos::ArrayRCP<Scalar>                       pbuf_values1D_, view_values1D_;
00475       Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> >   pbuf_values2D_, view_values2D_;
00476 
00477       // a map between a (non-local) row and a list of (col,val)
00478       std::map<GlobalOrdinal, std::list<std::pair<GlobalOrdinal,Scalar> > > nonlocals_;
00479 
00480       // a wrapper around multiply, for use in apply; it contains a non-owning RCP to *this, therefore, it is not allowed 
00481       // to persist past the destruction of *this. therefore, we may not share it.
00482       Teuchos::RCP< const CrsMatrixMultiplyOp<Scalar,Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatVec,LocalMatSolve> > sameScalarMultiplyOp_;
00483   }; // class CrsMatrix
00484 
00485 }
00486 
00487 #endif

Generated on Tue Jul 13 09:39:06 2010 for Tpetra Matrix/Vector Services by  doxygen 1.4.7