Tpetra_CrsGraph_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_CRSGRAPH_DECL_HPP
00030 #define TPETRA_CRSGRAPH_DECL_HPP
00031 
00032 // TODO: filter column indices first in insertLocalIndices()
00033 // TODO: filter column indices first in insertGlobalIndices()
00034 
00035 #include <Teuchos_Describable.hpp>
00036 #include <Teuchos_SerialDenseMatrix.hpp>
00037 #include <Teuchos_Array.hpp>
00038 #include <Teuchos_NullIteratorTraits.hpp>
00039 #include <Teuchos_CompileTimeAssert.hpp>
00040 
00041 #include <Kokkos_DefaultNode.hpp>
00042 #include <Kokkos_CrsGraph.hpp>
00043 #include <Kokkos_NodeHelpers.hpp>
00044 
00045 #include "Tpetra_ConfigDefs.hpp"
00046 #include "Tpetra_RowGraph.hpp"
00047 #include "Tpetra_DistObject.hpp"
00048 #include "Tpetra_Util.hpp"
00049 
00050 namespace Tpetra {
00051 
00052 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00053   // forward declaration
00054   template <class S, class LO, class GO, class N, class SpMatVec, class SpMatSlv>
00055   class CrsMatrix;
00056 #endif
00057 
00058   struct RowInfo {
00059     size_t allocSize;
00060     size_t numEntries;
00061     size_t offset1D;
00062   };
00063 
00065 
00068   template <class LocalOrdinal, class GlobalOrdinal = LocalOrdinal, class Node = Kokkos::DefaultNode::DefaultNodeType>
00069   class CrsGraph : public RowGraph<LocalOrdinal,GlobalOrdinal,Node>,
00070                    public DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node> {
00071     template <class S, class LO, class GO, class N, class SpMatVec, class SpMatSlv>
00072     friend class CrsMatrix;
00073 
00074     public: 
00075       typedef LocalOrdinal  local_ordinal_type;
00076       typedef GlobalOrdinal global_ordinal_type;
00077       typedef Node          node_type;
00078 
00080 
00081 
00083       CrsGraph(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype = DynamicProfile);
00084 
00086       CrsGraph(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, const Teuchos::ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, ProfileType pftype = DynamicProfile);
00087 
00089       CrsGraph(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rowMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &colMap, size_t maxNumEntriesPerRow, ProfileType pftype = DynamicProfile);
00090 
00092       CrsGraph(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);
00093 
00094       // !Destructor.
00095       virtual ~CrsGraph();
00096 
00098 
00100 
00101 
00103       void insertGlobalIndices(GlobalOrdinal row, const Teuchos::ArrayView<const GlobalOrdinal> &indices);
00104 
00106       void insertLocalIndices(LocalOrdinal row, const Teuchos::ArrayView<const LocalOrdinal> &indices);
00107 
00109       void removeLocalIndices(LocalOrdinal row);
00110 
00112 
00114 
00115 
00117       void globalAssemble();
00118 
00123       void fillComplete(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &domainMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rangeMap, OptimizeOption os = DoOptimizeStorage);
00124 
00130       void fillComplete(OptimizeOption os = DoOptimizeStorage);
00131 
00133       void optimizeStorage();
00134 
00136 
00138 
00139 
00141       const Teuchos::RCP<const Teuchos::Comm<int> > & getComm() const;
00142 
00144       Teuchos::RCP<Node> getNode() const;
00145 
00147       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRowMap() const;
00148 
00150       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getColMap() const;
00151 
00153       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getDomainMap() const;
00154 
00156       const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRangeMap() const;
00157 
00159       Teuchos::RCP<const Import<LocalOrdinal,GlobalOrdinal,Node> > getImporter() const;
00160 
00162       Teuchos::RCP<const Export<LocalOrdinal,GlobalOrdinal,Node> > getExporter() const;
00163 
00165       global_size_t getGlobalNumRows() const;
00166 
00168       global_size_t getGlobalNumCols() const;
00169 
00171       size_t getNodeNumRows() const;
00172 
00174       size_t getNodeNumCols() const;
00175 
00177       GlobalOrdinal getIndexBase() const;
00178 
00180       global_size_t getGlobalNumEntries() const;
00181 
00183       size_t getNodeNumEntries() const;
00184 
00186 
00187       size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const;
00188 
00190 
00191       size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const;
00192 
00194 
00201       size_t getNodeAllocationSize() const;
00202 
00204 
00205       size_t getNumAllocatedEntriesInGlobalRow(GlobalOrdinal globalRow) const;
00206 
00208 
00209       size_t getNumAllocatedEntriesInLocalRow(LocalOrdinal localRow) const;
00210 
00212       global_size_t getGlobalNumDiags() const;
00213 
00215       size_t getNodeNumDiags() const;
00216 
00218       size_t getGlobalMaxNumRowEntries() const;
00219 
00221       size_t getNodeMaxNumRowEntries() const;
00222 
00224       bool hasColMap() const; 
00225 
00227       bool isLowerTriangular() const;
00228 
00230       bool isUpperTriangular() const;
00231 
00233       bool isLocallyIndexed() const;
00234 
00236       bool isGloballyIndexed() const;
00237 
00239       bool isFillComplete() const;
00240 
00242 
00251       void getGlobalRowCopy(GlobalOrdinal GlobalRow, 
00252                             const Teuchos::ArrayView<GlobalOrdinal> &Indices, 
00253                             size_t &NumIndices) const;
00254 
00256 
00267       void getLocalRowCopy(LocalOrdinal LocalRow, const Teuchos::ArrayView<LocalOrdinal> &indices, size_t &NumIndices) const;
00268 
00270 
00277       Teuchos::ArrayRCP<const GlobalOrdinal> getGlobalRowView(GlobalOrdinal GlobalRow) const;
00278 
00280 
00287       Teuchos::ArrayRCP<const LocalOrdinal> getLocalRowView(LocalOrdinal LocalRow) const;
00288 
00290 
00292 
00293 
00295       bool isStorageOptimized() const;
00296 
00298       ProfileType getProfileType() const;
00299 
00301 
00303 
00304 
00306       std::string description() const;
00307 
00309       void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
00310 
00312 
00314 
00315 
00316       bool checkSizes(const DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node>& source);
00317 
00318       void copyAndPermute(const DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node> & source,
00319                           size_t numSameIDs,
00320                           const Teuchos::ArrayView<const LocalOrdinal> &permuteToLIDs,
00321                           const Teuchos::ArrayView<const LocalOrdinal> &permuteFromLIDs);
00322 
00323       void packAndPrepare(const DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node> & source,
00324                           const Teuchos::ArrayView<const LocalOrdinal> &exportLIDs,
00325                           Teuchos::Array<GlobalOrdinal> &exports,
00326                           const Teuchos::ArrayView<size_t> & numPacketsPerLID,
00327                           size_t& constantNumPackets,
00328                           Distributor &distor);
00329 
00330       void unpackAndCombine(const Teuchos::ArrayView<const LocalOrdinal> &importLIDs,
00331                             const Teuchos::ArrayView<const GlobalOrdinal> &imports,
00332                             const Teuchos::ArrayView<size_t> &numPacketsPerLID,
00333                             size_t constantNumPackets,
00334                             Distributor &distor,
00335                             CombineMode CM);
00337 
00338     private:
00339       // copy constructor disabled
00340       CrsGraph(const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> &Source);
00341       // operator= disabled
00342       CrsGraph<LocalOrdinal,GlobalOrdinal,Node> & operator=(const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> &rhs);
00343     protected:
00344       enum AllocateLocalGlobal {
00345         AllocateLocal,
00346         AllocateGlobal
00347       };
00348       void allocateIndices(AllocateLocalGlobal lg);
00349       void makeIndicesLocal(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &domainMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rangeMap);
00350       void makeColMap(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &domainMap, const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rangeMap);
00351       void computeIndexState();
00352       void sortIndices();
00353       void removeRedundantIndices();
00354       void makeImportExport();
00355       bool notRedundant() const;
00356       bool isSorted() const;
00357       void setSorted(bool sorted);
00358       bool indicesAreAllocated() const;
00359       void staticAssertions();
00360       size_t findLocalIndex(size_t row, LocalOrdinal ind, const Teuchos::ArrayRCP<const LocalOrdinal> &alreadyHaveAView) const;
00361       size_t findGlobalIndex(size_t row, GlobalOrdinal ind, const Teuchos::ArrayRCP<const GlobalOrdinal> &alreadyHaveAView) const;
00362       inline size_t RNNZ(size_t row) const;
00363       inline size_t RNumAlloc(size_t row) const;
00364       void checkInternalState() const;
00365       void clearGlobalConstants();
00366       void updateLocalAllocation(size_t lrow, size_t allocSize);
00367       void updateGlobalAllocation(size_t lrow, size_t allocSize);
00368       void fillLocalGraph();
00369 
00371 
00378       RowInfo getRowInfo(size_t myRow) const;
00379 
00381 
00391       RowInfo getFullLocalView(size_t myRow, Teuchos::ArrayRCP<const LocalOrdinal> &indices) const;
00392 
00394 
00404       RowInfo getFullLocalViewNonConst(size_t myRow, Teuchos::ArrayRCP<LocalOrdinal> &indices);
00405 
00407 
00417       RowInfo getFullGlobalView(size_t myRow, Teuchos::ArrayRCP<const GlobalOrdinal> &indices) const;
00418 
00420 
00430       RowInfo getFullGlobalViewNonConst(size_t myRow, Teuchos::ArrayRCP<GlobalOrdinal> &indices);
00431 
00432       void insertLocalIndicesViaView(size_t myRow, const Teuchos::ArrayView<const LocalOrdinal> &indices, const Teuchos::ArrayRCP<LocalOrdinal> &inds_view);
00433       void insertGlobalIndicesViaView(size_t myRow, const Teuchos::ArrayView<const GlobalOrdinal> &indices, const Teuchos::ArrayRCP<GlobalOrdinal> &inds_view);
00434 
00435       // Tpetra support objects
00436       Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > rowMap_, colMap_, rangeMap_, domainMap_;
00437       Teuchos::RCP<Import<LocalOrdinal,GlobalOrdinal,Node> > importer_;
00438       Teuchos::RCP<Export<LocalOrdinal,GlobalOrdinal,Node> > exporter_;
00439 
00440       // local data, stored in a Kokkos::CrsGraph. only initialized after fillComplete()
00441       Kokkos::CrsGraph<LocalOrdinal,Node> lclGraph_;
00442 
00443       // Local and Global Counts
00444       global_size_t globalNumEntries_, globalNumDiags_, globalMaxNumRowEntries_;
00445       size_t          nodeNumEntries_,   nodeNumDiags_,   nodeMaxNumRowEntries_, nodeNumAllocated_;
00446 
00447       // allocate static or dynamic?
00448       ProfileType pftype_;
00449       // number of non-zeros to allocate per row; set to Teuchos::null after they are allocated.
00450       Teuchos::ArrayRCP<const size_t> numAllocPerRow_;
00451       // number of non-zeros to allocate for all row; either this or numAllocPerRow_ is used, but not both.
00452       size_t numAllocForAllRows_;
00453 
00454       // number of valid entries in a row; this may be less than the number of allocated per row. Used for static and dynamic allocations.
00455       // set to Teuchos::null after optimizeStorage(), because is no longer needed (all entries are valid)
00456       // set to null if nodeNumEntries_ == 0
00457       Teuchos::ArrayRCP<size_t>       numEntriesPerRow_;
00458 
00459       // graph indices. before allocation, both are Teuchos::null. 
00460       // after allocation, except during makeIndicesLocal(), one of these is Teuchos::null.
00461       // this is a parallel compute buffer, not host memory
00462       // 1D == StaticAllocation, 2D == DynamicAllocation
00463       Teuchos::ArrayRCP< LocalOrdinal>                     pbuf_lclInds1D_, view_lclInds1D_;
00464       Teuchos::ArrayRCP<GlobalOrdinal>                     pbuf_gblInds1D_, view_gblInds1D_;
00465       Teuchos::ArrayRCP<Teuchos::ArrayRCP< LocalOrdinal> > pbuf_lclInds2D_, view_lclInds2D_;
00466       Teuchos::ArrayRCP<Teuchos::ArrayRCP<GlobalOrdinal> > pbuf_gblInds2D_, view_gblInds2D_;
00467       // offset to the first entry of each row. length numRows + 1. only used if pftype_ == StaticAllocation, otherwise set to Teuchos::null.
00468       // also, set to null if nodeNumAllocated_ == 0
00469       Teuchos::ArrayRCP<      size_t>                      pbuf_rowOffsets_, view_rowOffsets_;
00470 
00471       bool indicesAreAllocated_,
00472            indicesAreLocal_,
00473            indicesAreGlobal_,
00474            fillComplete_, 
00475            storageOptimized_,
00476            lowerTriangular_,
00477            upperTriangular_,
00478            indicesAreSorted_,
00479            haveGlobalConstants_, 
00480            noRedundancies_;
00481 
00482       // non-local data
00483       std::map<GlobalOrdinal, std::deque<GlobalOrdinal> > nonlocals_;
00484 
00485   }; // class CrsGrap
00486 
00487 } // namespace Tpetra
00488 
00489 #endif

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