Tpetra Matrix/Vector Services Version of the Day
Tpetra_CrsGraph_decl.hpp
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //          Tpetra: Templated Linear Algebra Services Package
00005 //                 Copyright (2008) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00038 // 
00039 // ************************************************************************
00040 // @HEADER
00041 
00042 #ifndef TPETRA_CRSGRAPH_DECL_HPP
00043 #define TPETRA_CRSGRAPH_DECL_HPP
00044 
00045 #include <Teuchos_Describable.hpp>
00046 #include <Teuchos_CompileTimeAssert.hpp>
00047 
00048 #include <Kokkos_DefaultNode.hpp>
00049 #include <Kokkos_DefaultKernels.hpp>
00050 #include <Kokkos_CrsGraph.hpp>
00051 #include <Kokkos_NodeHelpers.hpp>
00052 
00053 #include "Tpetra_ConfigDefs.hpp"
00054 #include "Tpetra_RowGraph.hpp"
00055 #include "Tpetra_DistObject.hpp"
00056 #include "Tpetra_Util.hpp"
00057 
00058 namespace Tpetra {
00059 
00060 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00061   // forward declaration
00062   template <class S, class LO, class GO, class N, class SpMatOps>
00063   class CrsMatrix;
00064 #endif
00065 
00066   struct RowInfo {
00067     size_t localRow;
00068     size_t allocSize;
00069     size_t numEntries;
00070     size_t offset1D;
00071   };
00072 
00073   enum ELocalGlobal {
00074     LocalIndices,
00075     GlobalIndices
00076   };
00077 
00079 
00123   template <class LocalOrdinal, 
00124             class GlobalOrdinal = LocalOrdinal, 
00125             class Node = Kokkos::DefaultNode::DefaultNodeType,
00126             class LocalMatOps = typename Kokkos::DefaultKernels<void,LocalOrdinal,Node>::SparseOps >
00127   class CrsGraph : 
00128     public RowGraph<LocalOrdinal,GlobalOrdinal,Node>,
00129     public DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node>,
00130     public Teuchos::ParameterListAcceptorDefaultBase
00131   {
00132     template <class S, class LO, class GO, class N, class SpMatOps>
00133     friend class CrsMatrix;
00134 
00135   public: 
00136     typedef LocalOrdinal                         local_ordinal_type;
00137     typedef GlobalOrdinal                        global_ordinal_type;
00138     typedef Node                                 node_type;
00139     typedef Map<LocalOrdinal,GlobalOrdinal,Node> map_type;
00140 
00142 
00143 
00161     CrsGraph (const Teuchos::RCP<const map_type>& rowMap, 
00162               size_t maxNumEntriesPerRow, 
00163               ProfileType pftype = DynamicProfile,
00164               const Teuchos::RCP<Teuchos::ParameterList>& plist = Teuchos::null);
00165 
00183     CrsGraph (const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& rowMap, 
00184               const ArrayRCP<const size_t>& NumEntriesPerRowToAlloc, 
00185               ProfileType pftype = DynamicProfile,
00186               const Teuchos::RCP<Teuchos::ParameterList>& plist = Teuchos::null);
00187 
00210     CrsGraph (const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& rowMap, 
00211               const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& colMap, 
00212               size_t maxNumEntriesPerRow, 
00213               ProfileType pftype = DynamicProfile,
00214               const Teuchos::RCP<Teuchos::ParameterList>& plist = Teuchos::null);
00215 
00238     CrsGraph (const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& rowMap, 
00239               const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& colMap, 
00240               const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc, 
00241               ProfileType pftype = DynamicProfile,
00242               const Teuchos::RCP<Teuchos::ParameterList>& plist = Teuchos::null);
00243 
00245     virtual ~CrsGraph();
00246 
00248 
00250 
00251 
00253     void setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist);
00254 
00256     Teuchos::RCP<const Teuchos::ParameterList>
00257     getValidParameters () const;
00258 
00260 
00262 
00263 
00265 
00276       void insertGlobalIndices(GlobalOrdinal globalRow, const ArrayView<const GlobalOrdinal> &indices);
00277 
00279 
00290       void insertLocalIndices(LocalOrdinal localRow, const ArrayView<const LocalOrdinal> &indices);
00291 
00293 
00302       void removeLocalIndices(LocalOrdinal localRow);
00303 
00305 
00307 
00313 
00315       void globalAssemble();
00316 
00325       void resumeFill();
00326 
00338       void fillComplete(const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &domainMap, const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rangeMap, OptimizeOption os = DoOptimizeStorage);
00339 
00353       void fillComplete(OptimizeOption os = DoOptimizeStorage);
00354 
00356 
00358 
00359 
00361       const RCP<const Comm<int> > & getComm() const;
00362 
00364       RCP<Node> getNode() const;
00365 
00367       const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRowMap() const;
00368 
00370       const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getColMap() const;
00371 
00373       const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getDomainMap() const;
00374 
00376       const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & getRangeMap() const;
00377 
00379       RCP<const Import<LocalOrdinal,GlobalOrdinal,Node> > getImporter() const;
00380 
00382       RCP<const Export<LocalOrdinal,GlobalOrdinal,Node> > getExporter() const;
00383 
00385 
00387       global_size_t getGlobalNumRows() const;
00388 
00390 
00393       global_size_t getGlobalNumCols() const;
00394 
00396       size_t getNodeNumRows() const;
00397 
00399 
00401       size_t getNodeNumCols() const;
00402 
00404       GlobalOrdinal getIndexBase() const;
00405 
00407 
00409       global_size_t getGlobalNumEntries() const;
00410 
00412       size_t getNodeNumEntries() const;
00413 
00415 
00416       size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const;
00417 
00419 
00420       size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const;
00421 
00423 
00430       size_t getNodeAllocationSize() const;
00431 
00433 
00434       size_t getNumAllocatedEntriesInGlobalRow(GlobalOrdinal globalRow) const;
00435 
00437 
00438       size_t getNumAllocatedEntriesInLocalRow(LocalOrdinal localRow) const;
00439 
00441 
00443       global_size_t getGlobalNumDiags() const;
00444 
00446 
00448       size_t getNodeNumDiags() const;
00449 
00451 
00453       size_t getGlobalMaxNumRowEntries() const;
00454 
00456 
00458       size_t getNodeMaxNumRowEntries() const;
00459 
00461       bool hasColMap() const; 
00462 
00464 
00466       bool isLowerTriangular() const;
00467 
00469 
00471       bool isUpperTriangular() const;
00472 
00474       bool isLocallyIndexed() const;
00475 
00477       bool isGloballyIndexed() const;
00478 
00480       bool isFillComplete() const;
00481 
00483       bool isFillActive() const;
00484 
00486 
00489       bool isSorted() const;
00490 
00492 
00498       bool isStorageOptimized() const;
00499 
00501       ProfileType getProfileType() const;
00502 
00504 
00513       void getGlobalRowCopy(GlobalOrdinal GlobalRow, 
00514                             const ArrayView<GlobalOrdinal> &Indices, 
00515                             size_t &NumIndices
00516                             ) const;
00517 
00519 
00530       void getLocalRowCopy(LocalOrdinal LocalRow, 
00531                            const ArrayView<LocalOrdinal> &indices, 
00532                            size_t &NumIndices
00533                            ) const;
00534 
00536 
00544       void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView<const GlobalOrdinal> &Indices) const;
00545 
00547 
00555       void getLocalRowView(LocalOrdinal LocalRow, ArrayView<const LocalOrdinal> &indices) const;
00556 
00558 
00560 
00561 
00563       std::string description() const;
00564 
00566       void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
00567 
00569 
00571 
00572 
00573       bool checkSizes(const DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node>& source);
00574 
00575       void copyAndPermute(const DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node> & source,
00576                           size_t numSameIDs,
00577                           const ArrayView<const LocalOrdinal> &permuteToLIDs,
00578                           const ArrayView<const LocalOrdinal> &permuteFromLIDs);
00579 
00580       void packAndPrepare(const DistObject<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node> & source,
00581                           const ArrayView<const LocalOrdinal> &exportLIDs,
00582                           Array<GlobalOrdinal> &exports,
00583                           const ArrayView<size_t> & numPacketsPerLID,
00584                           size_t& constantNumPackets,
00585                           Distributor &distor);
00586 
00587       void unpackAndCombine(const ArrayView<const LocalOrdinal> &importLIDs,
00588                             const ArrayView<const GlobalOrdinal> &imports,
00589                             const ArrayView<size_t> &numPacketsPerLID,
00590                             size_t constantNumPackets,
00591                             Distributor &distor,
00592                             CombineMode CM);
00594 
00596 
00597 
00599 
00602       ArrayRCP<const size_t>       getNodeRowBegs() const;
00603 
00605 
00608       ArrayRCP<const LocalOrdinal> getNodePackedIndices() const;
00609 
00611 
00613 
00614 
00623       TPETRA_DEPRECATED void optimizeStorage();
00624 
00626       TPETRA_DEPRECATED ArrayRCP<const GlobalOrdinal> getGlobalRowView(GlobalOrdinal GlobalRow) const;
00627 
00629       TPETRA_DEPRECATED ArrayRCP<const LocalOrdinal> getLocalRowView(LocalOrdinal LocalRow) const;
00630 
00632 
00633     private:
00634       // We forbid copy construction by declaring this method private
00635       // and not implementing it.
00636       CrsGraph (const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> &Source);
00637 
00638       // We forbid assignment (operator=) by declaring this method
00639       // private and not implementing it.
00640       CrsGraph<LocalOrdinal,GlobalOrdinal,Node>& 
00641       operator= (const CrsGraph<LocalOrdinal,GlobalOrdinal,Node> &rhs);
00642 
00643     protected:
00644       // these structs are conveniences, to cut down on the number of
00645       // arguments to some of the methods below.
00646       struct SLocalGlobalViews {
00647         ArrayView<const GlobalOrdinal> ginds;
00648         ArrayView<const LocalOrdinal>  linds;
00649       };
00650       struct SLocalGlobalNCViews {
00651         ArrayView<GlobalOrdinal>       ginds;
00652         ArrayView<LocalOrdinal>        linds;
00653       };
00654       //
00655       // Allocation
00656       //
00657       bool indicesAreAllocated() const;
00658 
00659       void allocateIndices (ELocalGlobal lg);
00660 
00661       template<class T>
00662       void 
00663       allocateValues (ArrayRCP<T> &values1D, ArrayRCP<ArrayRCP<T> > &values2D) const;
00664 
00665       template<ELocalGlobal lg>
00666       RowInfo 
00667       updateAlloc (RowInfo rowinfo, size_t allocSize);
00668       template<ELocalGlobal lg, class T> 
00669       RowInfo 
00670       updateAllocAndValues (RowInfo rowinfo, size_t allocSize, ArrayRCP<T> &rowVals);
00671       //
00672       // Local versus global indices
00673       //
00674       void computeIndexState();
00675       void makeColMap();
00676       void makeIndicesLocal();
00677       void makeImportExport();
00678       //
00679       // insert/suminto/replace
00680       //
00681       template<ELocalGlobal lg>
00682       size_t 
00683       filterIndices (const SLocalGlobalNCViews &inds) const;
00684 
00685       template<ELocalGlobal lg, class T>
00686       size_t 
00687       filterIndicesAndValues (const SLocalGlobalNCViews &inds, const ArrayView<T> &vals) const;
00688 
00689       template<ELocalGlobal lg, ELocalGlobal I> 
00690       size_t
00691       insertIndices (RowInfo rowInfo, const SLocalGlobalViews &newInds);
00692 
00693       template<ELocalGlobal lg, ELocalGlobal I, class IterO, class IterN> 
00694       void
00695       insertIndicesAndValues (RowInfo rowInfo, const SLocalGlobalViews &newInds, IterO rowVals, IterN newVals);
00696 
00697       template<ELocalGlobal lg, class IterO, class IterN, class BinaryFunction> 
00698       void 
00699       transformValues (RowInfo rowInfo, const SLocalGlobalViews &inds, IterO rowVals, IterN newVals, BinaryFunction f) const;
00700       //
00701       // Sorting and merging
00702       //
00703       bool                       isMerged() const;
00704       void                       setSorted(bool sorted);
00705       void                       setMerged(bool merged);
00706       void                       sortAllIndices();
00707       void                       sortRowIndices(RowInfo rowinfo);
00708       template <class Scalar> void sortRowIndicesAndValues(RowInfo rowinfo, ArrayView<Scalar> values);
00709       void                                             mergeAllIndices();
00710       void                                             mergeRowIndices(RowInfo rowinfo);
00711       template <class Iter, class BinaryFunction> void mergeRowIndicesAndValues(RowInfo rowinfo, Iter rowValueIter, BinaryFunction f);
00712       // 
00713       void setDomainRangeMaps(const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &domainMap, const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &rangeMap);
00714       void staticAssertions() const;
00715       // global consts
00716       void clearGlobalConstants();
00717       void computeGlobalConstants();
00718       // graph data accessors
00719       RowInfo                         getRowInfo(size_t myRow) const;
00720       ArrayView<const LocalOrdinal>   getLocalView(RowInfo rowinfo) const;
00721       ArrayView<LocalOrdinal>         getLocalViewNonConst(RowInfo rowinfo);
00722       ArrayView<const GlobalOrdinal>  getGlobalView(RowInfo rowinfo) const;
00723       ArrayView<GlobalOrdinal>        getGlobalViewNonConst(RowInfo rowinfo);
00724       size_t                          findLocalIndex(RowInfo rowinfo, LocalOrdinal ind) const;
00725       size_t                          findGlobalIndex(RowInfo rowinfo, GlobalOrdinal ind) const;
00726       // local Kokkos objects
00727       void pushToLocalGraph();
00728       void pullFromLocalGraph();
00729       void fillLocalGraph(OptimizeOption os);
00730       const Kokkos::CrsGraph<LocalOrdinal,Node,LocalMatOps> & getLocalGraph() const;
00731       Kokkos::CrsGraph<LocalOrdinal,Node,LocalMatOps>       & getLocalGraphNonConst();
00732       // debugging
00733       void checkInternalState() const;
00734 
00735       // Tpetra support objects
00736       RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > rowMap_, colMap_, rangeMap_, domainMap_;
00737       RCP<Import<LocalOrdinal,GlobalOrdinal,Node> > importer_;
00738       RCP<Export<LocalOrdinal,GlobalOrdinal,Node> > exporter_;
00739 
00740       // local data, stored in a Kokkos::CrsGraph. only initialized after fillComplete()
00741       Kokkos::CrsGraph<LocalOrdinal,Node,LocalMatOps> lclGraph_;
00742 
00743       // Local and Global Counts
00744       // nodeNumEntries_ and nodeNumAllocated_ are required to be always consistent
00745       // nodeMaxNumEntries_, nodeNumDiags_ and the global quantities are computed during fillComplete() and only valid when isFillComplete()
00746       global_size_t globalNumEntries_, globalNumDiags_, globalMaxNumRowEntries_;
00747       size_t          nodeNumEntries_,   nodeNumDiags_,   nodeMaxNumRowEntries_, nodeNumAllocated_;
00748 
00749       // allocate static or dynamic?
00750       ProfileType pftype_;
00751       // requested allocation sizes; we have to preserve these, because we perform late-allocation
00752       // number of non-zeros to allocate per row; set to null after they are allocated.
00753       ArrayRCP<const size_t> numAllocPerRow_;
00754       // number of non-zeros to allocate for all row; either this or numAllocPerRow_ is used, but not both.
00755       size_t numAllocForAllRows_;
00756 
00757       // graph indices. before allocation, all are null. 
00758       // after allocation, except during makeIndicesLocal(), one of local or global is null.
00759       // we will never have 1D and 2D structures being non-null
00760       // this is host memory
00761       // 1D == StaticAllocation, 2D == DynamicAllocation
00763       //
00764       // 1D/Static structures
00765       //
00768       ArrayRCP< LocalOrdinal>                     lclInds1D_;
00770       ArrayRCP<GlobalOrdinal>                     gblInds1D_;
00771       // offset to the beg and end entries of each row. only used for 1D (Static) allocation.
00772       // i.e., indices for row R are lclInds1D_[i] for i in [b,e) where b = rowBegs_[R] and e = rowEnds_[R]
00773       // for a packed (optimized) allocation, we will typically have &rowBegs_[R]+1 == &rowEnds_[R]
00774       // these are null for 2D (Dynamic) allocations
00775       // the allocation size is computed by looking at the difference between rowBegs_[r+1] and rowBegs_[r]
00776       // rowBegs_ therefore has length N+1, while rowEnds_ has length N
00777       ArrayRCP<size_t> rowBegs_, rowEnds_;
00779       //
00780       // 2D/Dynamic structures. 
00781       //
00784       ArrayRCP<ArrayRCP< LocalOrdinal> > lclInds2D_;
00786       ArrayRCP<ArrayRCP<GlobalOrdinal> > gblInds2D_;
00788       ArrayRCP<size_t>       numEntriesPerRow_;
00789 
00790       // TODO: these might be useful in the future
00791       // ArrayRCP< typedef ArrayRCP<const GlobalOrdinal>::iterator > gRowPtrs_;
00792       // ArrayRCP< typedef ArrayRCP<GlobalOrdinal>::iterator > gRowPtrsNC_;
00793       // ArrayRCP< typedef ArrayRCP<const LocalOrdinal>::iterator > lRowPtrs_;
00794       // ArrayRCP< typedef ArrayRCP<LocalOrdinal>::iterator > lRowPtrsNC_;
00795 
00796       bool indicesAreAllocated_,
00797            indicesAreLocal_,
00798            indicesAreGlobal_,
00799            fillComplete_, 
00800            lowerTriangular_,
00801            upperTriangular_,
00802            indicesAreSorted_,
00803            noRedundancies_,
00804            haveGlobalConstants_;
00805 
00806       // non-local data
00807       std::map<GlobalOrdinal, std::deque<GlobalOrdinal> > nonlocals_;
00808 
00809   }; // class CrsGraph
00810 
00811 } // namespace Tpetra
00812 
00813 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines