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 // TODO: add typeglobs: CrsMatrix<Scalar,typeglob>
00035 // TODO: add template (template) parameter for nonlocal container (this will be part of typeglob)
00036 
00037 #include <Kokkos_DefaultNode.hpp>
00038 #include <Kokkos_DefaultKernels.hpp>
00039 #include <Kokkos_CrsMatrix.hpp>
00040 
00041 #include <Teuchos_ScalarTraits.hpp>
00042 #include <Teuchos_OrdinalTraits.hpp>
00043 #include <Teuchos_SerializationTraits.hpp>
00044 
00045 #include "Tpetra_ConfigDefs.hpp"
00046 #include "Tpetra_RowMatrix.hpp"
00047 #include "Tpetra_DistObject.hpp"
00048 #include "Tpetra_CrsGraph.hpp"
00049 #include "Tpetra_Vector.hpp"
00050 #include "Tpetra_CrsMatrixMultiplyOp_decl.hpp"
00051 
00052 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00053 namespace Tpetra {
00054   // struct for i,j,v triplets
00055   template <class Ordinal, class Scalar>
00056   struct CrsIJV {
00057     CrsIJV();
00058     CrsIJV(Ordinal row, Ordinal col, const Scalar &val);
00059     Ordinal i,j;
00060     Scalar  v;
00061   };
00062 }
00063 
00064 namespace Teuchos {
00065   // SerializationTraits specialization for CrsIJV, using DirectSerialization
00066   template <typename Ordinal, typename Scalar>
00067   class SerializationTraits<int,Tpetra::CrsIJV<Ordinal,Scalar> >
00068   : public DirectSerializationTraits<int,Tpetra::CrsIJV<Ordinal,Scalar> >
00069   {};
00070 }
00071 
00072 namespace std {
00073   template <class Ordinal, class Scalar>
00074   bool operator<(const Tpetra::CrsIJV<Ordinal,Scalar> &ijv1, const Tpetra::CrsIJV<Ordinal,Scalar> &ijv2);
00075 }
00076 #endif
00077 
00078 namespace Tpetra {
00079 
00081 
00097   template <class Scalar, 
00098             class LocalOrdinal  = int, 
00099             class GlobalOrdinal = LocalOrdinal, 
00100             class Node          = Kokkos::DefaultNode::DefaultNodeType, 
00101             class LocalMatOps   = typename Kokkos::DefaultKernels<Scalar,LocalOrdinal,Node>::SparseOps >
00102   class CrsMatrix : public RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>,
00103                     public DistObject<char, LocalOrdinal,GlobalOrdinal,Node> {
00104     public:
00105       typedef Scalar        scalar_type;
00106       typedef LocalOrdinal  local_ordinal_type;
00107       typedef GlobalOrdinal global_ordinal_type;
00108       typedef Node          node_type;
00109       // backwards compatibility defines both of these
00110       typedef LocalMatOps   mat_vec_type;
00111       typedef LocalMatOps   mat_solve_type;
00112 
00114 
00115 
00117       CrsMatrix(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype = DynamicProfile);
00118 
00120       CrsMatrix(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, const Teuchos::ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = DynamicProfile);
00121 
00123       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);
00124 
00126       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);
00127 
00129       explicit CrsMatrix(const Teuchos::RCP<const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > &graph);
00130 
00131       // !Destructor.
00132       virtual ~CrsMatrix();
00133 
00135 
00137 
00138 
00140       void insertGlobalValues(GlobalOrdinal globalRow, const Teuchos::ArrayView<const GlobalOrdinal> &cols, const Teuchos::ArrayView<const Scalar> &vals);
00141 
00143       void insertLocalValues(LocalOrdinal localRow, const Teuchos::ArrayView<const LocalOrdinal> &cols, const Teuchos::ArrayView<const Scalar> &vals);
00144 
00146 
00150       void replaceGlobalValues(GlobalOrdinal globalRow, 
00151                          const Teuchos::ArrayView<const GlobalOrdinal> &cols,
00152                          const Teuchos::ArrayView<const Scalar>        &vals);
00153 
00155 
00157       void replaceLocalValues(LocalOrdinal localRow, 
00158                          const Teuchos::ArrayView<const LocalOrdinal> &cols,
00159                          const Teuchos::ArrayView<const Scalar>       &vals);
00160 
00162 
00163       void sumIntoGlobalValues(GlobalOrdinal globalRow, 
00164                          const Teuchos::ArrayView<const GlobalOrdinal> &cols,
00165                          const Teuchos::ArrayView<const Scalar>        &vals);
00166 
00167 
00169       void setAllToScalar(const Scalar &alpha);
00170 
00172       void scale(const Scalar &alpha);
00173 
00175 
00177 
00178 
00180       void globalAssemble();
00181 
00186       void fillComplete(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &domainMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rangeMap, OptimizeOption os = DoOptimizeStorage);
00187 
00193       void fillComplete(OptimizeOption os = DoOptimizeStorage);
00194 
00196       void optimizeStorage();
00197 
00199 
00202       void resumeFill();
00203 
00205 
00207 
00208 
00210       const Teuchos::RCP<const Teuchos::Comm<int> > & getComm() const;
00211 
00213       Teuchos::RCP<Node> getNode() const;
00214 
00216       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRowMap() const;
00217 
00219       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getColMap() const;
00220 
00222       Teuchos::RCP<const RowGraph<LocalOrdinal,GlobalOrdinal,Node> > getGraph() const;
00223 
00225       Teuchos::RCP<const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > getCrsGraph() const;
00226 
00228       global_size_t getGlobalNumRows() const;
00229 
00231       global_size_t getGlobalNumCols() const;
00232 
00234       size_t getNodeNumRows() const;
00235 
00237       size_t getNodeNumCols() const;
00238 
00240       GlobalOrdinal getIndexBase() const;
00241 
00243       global_size_t getGlobalNumEntries() const;
00244 
00246       size_t getNodeNumEntries() const;
00247 
00249 
00250       size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const;
00251 
00253 
00254       size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const;
00255 
00257       global_size_t getGlobalNumDiags() const;
00258 
00260       size_t getNodeNumDiags() const;
00261 
00263       size_t getGlobalMaxNumRowEntries() const;
00264 
00266       size_t getNodeMaxNumRowEntries() const;
00267 
00269       bool hasColMap() const; 
00270 
00272       bool isLowerTriangular() const;
00273 
00275       bool isUpperTriangular() const;
00276 
00278       bool isLocallyIndexed() const;
00279 
00281       bool isGloballyIndexed() const;
00282 
00284       bool isFillComplete() const;
00285 
00287       bool isFillResumed() const;
00288 
00290 
00300       void getGlobalRowCopy(GlobalOrdinal GlobalRow,
00301                             const Teuchos::ArrayView<GlobalOrdinal> &Indices,
00302                             const Teuchos::ArrayView<Scalar> &Values,
00303                             size_t &NumEntries) const;
00304 
00306 
00316       void getLocalRowCopy(LocalOrdinal LocalRow, 
00317                            const Teuchos::ArrayView<LocalOrdinal> &Indices, 
00318                            const Teuchos::ArrayView<Scalar> &Values,
00319                            size_t &NumEntries) const;
00320 
00322 
00331       void getGlobalRowView(GlobalOrdinal GlobalRow, 
00332                             Teuchos::ArrayRCP<const GlobalOrdinal> &indices,
00333                             Teuchos::ArrayRCP<const Scalar>        &values) const;
00334 
00336 
00345       void getLocalRowView(LocalOrdinal LocalRow,
00346                            Teuchos::ArrayRCP<const LocalOrdinal> &indices,
00347                            Teuchos::ArrayRCP<const Scalar>       &values) const;
00348 
00350 
00352       void getLocalDiagCopy(Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &diag) const;
00353 
00355 
00357 
00358 
00360 
00367       template <class DomainScalar, class RangeScalar>
00368       void multiply(const MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> &Y, Teuchos::ETransp trans, RangeScalar alpha, RangeScalar beta) const;
00369 
00371 
00378       template <class DomainScalar, class RangeScalar>
00379       void solve(const MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> & Y, MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> &X, Teuchos::ETransp trans) const;
00380           
00382 
00384 
00385 
00387 
00390       void apply(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y, 
00391                  Teuchos::ETransp mode = Teuchos::NO_TRANS,
00392                  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
00393                  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const;
00394 
00396       bool hasTransposeApply() const;
00397 
00400       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getDomainMap() const;
00401 
00404       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRangeMap() const;
00405 
00407 
00409 
00410 
00412       bool isStorageOptimized() const;
00413 
00415       ProfileType getProfileType() const;
00416 
00418       bool isStaticGraph() const;
00419 
00421 
00423 
00424 
00426       std::string description() const;
00427 
00429       void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
00430 
00432 
00434 
00435 
00436       bool checkSizes(const DistObject<char, LocalOrdinal,GlobalOrdinal,Node>& source);
00437 
00438       void copyAndPermute(const DistObject<char, LocalOrdinal,GlobalOrdinal,Node>& source,
00439                           size_t numSameIDs,
00440                           const Teuchos::ArrayView<const LocalOrdinal> &permuteToLIDs,
00441                           const Teuchos::ArrayView<const LocalOrdinal> &permuteFromLIDs);
00442 
00443       void packAndPrepare(const DistObject<char, LocalOrdinal,GlobalOrdinal,Node>& source,
00444                           const Teuchos::ArrayView<const LocalOrdinal> &exportLIDs,
00445                           Teuchos::Array<char> &exports,
00446                           const Teuchos::ArrayView<size_t> & numPacketsPerLID,
00447                           size_t& constantNumPackets,
00448                           Distributor &distor);
00449 
00450       void unpackAndCombine(const Teuchos::ArrayView<const LocalOrdinal> &importLIDs,
00451                             const Teuchos::ArrayView<const char> &imports,
00452                             const Teuchos::ArrayView<size_t> &numPacketsPerLID,
00453                             size_t constantNumPackets,
00454                             Distributor &distor,
00455                             CombineMode CM);
00456 
00457       void createViews() const;
00458 
00459       void createViewsNonConst(Kokkos::ReadWriteOption rwo);
00460 
00461       void releaseViews() const;
00462 
00464 
00465     private:
00466       // copy constructor disabled
00467       CrsMatrix(const CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps> &Source);
00468       // operator= disabled
00469       CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps> & operator=(const CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps> &rhs);
00470     protected:
00471 
00472       // useful typedefs
00473       typedef Teuchos::OrdinalTraits<LocalOrdinal>    LOT;
00474       typedef Teuchos::OrdinalTraits<GlobalOrdinal>   GOT;
00475       typedef Teuchos::ScalarTraits<Scalar>            ST;
00476       typedef MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> MV;
00477       typedef Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>       V;
00478 
00479       enum GraphAllocationStatus {
00480         GraphAlreadyAllocated,
00481         GraphNotYetAllocated
00482       };
00483 
00484       void allocateValues( typename CrsGraph<LocalOrdinal,GlobalOrdinal,Node>::AllocateLocalGlobal lg, Scalar alpha, GraphAllocationStatus gas);
00485       void sortEntries();
00486       void mergeRedundantEntries();
00487       void updateAllocation(size_t lrow, size_t allocSize);
00488       void fillLocalMatrix();
00489       void checkInternalState() const;
00490 
00492 
00500       Teuchos::ArrayRCP<const Scalar> getFullView(size_t myRow, RowInfo sizeInfo) const;
00501 
00503 
00511       Teuchos::ArrayRCP<Scalar> getFullViewNonConst(size_t myRow, RowInfo sizeInfo);
00512 
00513       // Two graph pointers to maintain const-correctness:
00514       // staticGraph_ is a graph passed to the constructor. We are not allowed to modify it.
00515       // myGraph_     is a graph created here. We are allowed to modify it.
00516       Teuchos::RCP<const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> > staticGraph_;
00517       Teuchos::RCP<      CrsGraph<LocalOrdinal,GlobalOrdinal,Node> >     myGraph_;
00518 
00519       Kokkos::CrsMatrix<Scalar,Node> lclMatrix_;
00520       LocalMatOps lclMatOps_;
00521 
00522       bool constructedWithOptimizedGraph_,
00523            fillComplete_;
00524 
00525       // matrix values. before allocation, both are Teuchos::null.
00526       // after allocation, one is Teuchos::Null.
00527       // 1D == StaticAllocation, 2D == DynamicAllocation
00528       // The allocation always matches that of graph_
00529       Teuchos::ArrayRCP<Scalar>                       values1D_;
00530       Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> >   values2D_;
00531       // FINISH
00532       // Teuchos::ArrayRCP< typedef Teuchos::ArrayRCP<const Scalar>::iterator > rowPtrs_;
00533       // Teuchos::ArrayRCP< typedef Teuchos::ArrayRCP<Scalar>::iterator > rowPtrsNC_;
00534 
00535       // a map between a (non-local) row and a list of (col,val)
00536       std::map<GlobalOrdinal, Teuchos::Array<std::pair<GlobalOrdinal,Scalar> > > nonlocals_;
00537 
00538       // a wrapper around multiply, for use in apply; it contains a non-owning RCP to *this, therefore, it is not allowed 
00539       // to persist past the destruction of *this. therefore, WE MAY NOT SHARE THIS POINTER.
00540       Teuchos::RCP< const CrsMatrixMultiplyOp<Scalar,Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps> > sameScalarMultiplyOp_;
00541   }; // class CrsMatrix
00542 
00543 }
00544 
00545 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:21:40 2011 for Tpetra Matrix/Vector Services by  doxygen 1.6.3