Tpetra Matrix/Vector Services Version of the Day
Tpetra_Map_decl.hpp
Go to the documentation of this file.
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_MAP_DECL_HPP
00043 #define TPETRA_MAP_DECL_HPP
00044 
00045 #include <Kokkos_DefaultNode.hpp>
00046 #include <Teuchos_Describable.hpp>
00047 
00048 // enums and defines
00049 #include "Tpetra_ConfigDefs.hpp"
00050 
00051 // mfh 27 Apr 2013: If HAVE_TPETRA_FIXED_HASH_TABLE is defined (which
00052 // it is by default), then Map will used the fixed-structure hash
00053 // table variant for global-to-local index lookups.  Otherwise, it
00054 // will use the dynamic-structure hash table variant.
00055 //
00056 // mfh 23 Mar 2014: I've removed all code in Map that uses the
00057 // dynamic-structure hash table variant, since it has not been used
00058 // for at least a year.  However, I am retaining the #define, in case
00059 // downstream code depends on it.
00060 
00061 #ifndef HAVE_TPETRA_FIXED_HASH_TABLE
00062 #  define HAVE_TPETRA_FIXED_HASH_TABLE 1
00063 #endif // HAVE_TPETRA_FIXED_HASH_TABLE
00064 
00068 namespace Tpetra {
00069 
00070 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00071   // Forward declaration of Directory.
00072   template <class LO, class GO, class N> class Directory;
00073 #endif // DOXYGEN_SHOULD_SKIP_THIS
00074 
00075   namespace Details {
00076 
00077 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00078     // Forward declaration of TieBreak
00079     template <class LO, class GO> class TieBreak;
00080 
00081     template<class GlobalOrdinal, class LocalOrdinal>
00082     class FixedHashTable;
00083 #endif // DOXYGEN_SHOULD_SKIP_THIS
00084 
00087     template<class OutMapType, class InMapType>
00088     struct MapCloner {
00089       typedef typename OutMapType::node_type out_node_type;
00090       typedef typename InMapType::node_type in_node_type;
00091 
00092       static OutMapType
00093       clone (const InMapType& mapIn,
00094              const Teuchos::RCP<out_node_type>& node2);
00095     };
00096   } // namespace Details
00097 
00231   template <class LocalOrdinal = int,
00232             class GlobalOrdinal = LocalOrdinal,
00233             class Node = KokkosClassic::DefaultNode::DefaultNodeType>
00234   class Map : public Teuchos::Describable {
00235   public:
00237 
00238 
00240     typedef LocalOrdinal local_ordinal_type;
00242     typedef GlobalOrdinal global_ordinal_type;
00244     typedef Node node_type;
00245 
00247 
00248 
00249 
00291     Map (global_size_t numGlobalElements,
00292          GlobalOrdinal indexBase,
00293          const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
00294          LocalGlobal lg=GloballyDistributed,
00295          const Teuchos::RCP<Node> &node = defaultArgNode());
00296 
00336     Map (global_size_t numGlobalElements,
00337          size_t numLocalElements,
00338          GlobalOrdinal indexBase,
00339          const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
00340          const Teuchos::RCP<Node> &node = defaultArgNode());
00341 
00376     Map (global_size_t numGlobalElements,
00377          const Teuchos::ArrayView<const GlobalOrdinal> &elementList,
00378          GlobalOrdinal indexBase,
00379          const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
00380          const Teuchos::RCP<Node> &node = defaultArgNode());
00381 
00382 
00393     Map ();
00394 
00396     ~Map ();
00397 
00399 
00400 
00401 
00406     bool isOneToOne () const;
00407 
00409     inline global_size_t getGlobalNumElements() const { return numGlobalElements_; }
00410 
00412     inline size_t getNodeNumElements() const { return numLocalElements_; }
00413 
00415     inline GlobalOrdinal getIndexBase() const { return indexBase_; }
00416 
00418     inline LocalOrdinal getMinLocalIndex() const {
00419       return Teuchos::OrdinalTraits<LocalOrdinal>::zero();
00420     }
00421 
00427     inline LocalOrdinal getMaxLocalIndex() const {
00428       if (getNodeNumElements () == 0) {
00429         return Teuchos::OrdinalTraits<LocalOrdinal>::invalid ();
00430       }
00431       else { // Local indices are always zero-based.
00432         return Teuchos::as<LocalOrdinal> (getNodeNumElements () - 1);
00433       }
00434     }
00435 
00437     inline GlobalOrdinal getMinGlobalIndex() const { return minMyGID_; }
00438 
00440     inline GlobalOrdinal getMaxGlobalIndex() const { return maxMyGID_; }
00441 
00443     inline GlobalOrdinal getMinAllGlobalIndex() const { return minAllGID_; }
00444 
00446     inline GlobalOrdinal getMaxAllGlobalIndex() const { return maxAllGID_; }
00447 
00452     LocalOrdinal getLocalElement (GlobalOrdinal globalIndex) const;
00453 
00458     GlobalOrdinal getGlobalElement (LocalOrdinal localIndex) const;
00459 
00489     LookupStatus
00490     getRemoteIndexList (const Teuchos::ArrayView<const GlobalOrdinal>& GIDList,
00491                         const Teuchos::ArrayView<                int>& nodeIDList,
00492                         const Teuchos::ArrayView<       LocalOrdinal>& LIDList) const;
00493 
00517     LookupStatus
00518     getRemoteIndexList (const Teuchos::ArrayView<const GlobalOrdinal> & GIDList,
00519                         const Teuchos::ArrayView<                int> & nodeIDList) const;
00520 
00527     Teuchos::ArrayView<const GlobalOrdinal> getNodeElementList() const;
00528 
00530 
00531 
00532 
00534     bool isNodeLocalElement (LocalOrdinal localIndex) const;
00535 
00537     bool isNodeGlobalElement (GlobalOrdinal globalIndex) const;
00538 
00545     bool isUniform () const;
00546 
00558     bool isContiguous () const;
00559 
00579     bool isDistributed () const;
00580 
00605     bool isCompatible (const Map<LocalOrdinal,GlobalOrdinal,Node> &map) const;
00606 
00637     bool isSameAs (const Map<LocalOrdinal,GlobalOrdinal,Node> &map) const;
00638 
00643     bool locallySameAs (const Map<LocalOrdinal, GlobalOrdinal, node_type>& map) const;
00644 
00646 
00647 
00648 
00650     Teuchos::RCP<const Teuchos::Comm<int> > getComm () const;
00651 
00653     Teuchos::RCP<Node> getNode () const;
00654 
00656 
00657 
00658 
00660     std::string description() const;
00661 
00663     void
00664     describe (Teuchos::FancyOStream &out,
00665               const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
00666 
00668 
00669 
00670 
00671     static Teuchos::RCP<Node> defaultArgNode() {
00672         // Workaround function for a deferred visual studio bug
00673         // http://connect.microsoft.com/VisualStudio/feedback/details/719847/erroneous-error-c2783-could-not-deduce-template-argument
00674         // Use this function for default arguments rather than calling
00675         // what is the return value below.  Also helps in reducing
00676         // duplication in various constructors.
00677         return KokkosClassic::Details::getNode<Node>();
00678     }
00679 
00681     template <class NodeOut>
00682     Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, NodeOut> >
00683     clone (const RCP<NodeOut>& nodeOut) const;
00684 
00732     RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
00733     removeEmptyProcesses () const;
00734 
00762     RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
00763     replaceCommWithSubset (const Teuchos::RCP<const Teuchos::Comm<int> >& newComm) const;
00765 
00766   protected:
00767     // This lets other specializations of Map access all of this
00768     // specialization's internal methods and data, so that we can
00769     // implement clone() without exposing the details of Map to users.
00770     template <class LO, class GO, class N> friend class Map;
00771 
00772   private:
00773     template<class OutMapType, class InMapType>
00774     friend struct Details::MapCloner;
00775 
00783     void setupDirectory () const;
00784 
00799     bool checkIsDist() const;
00800 
00802     Teuchos::RCP<const Teuchos::Comm<int> > comm_;
00803 
00805     Teuchos::RCP<Node> node_;
00806 
00808     GlobalOrdinal indexBase_;
00809 
00811     global_size_t numGlobalElements_;
00812 
00814     size_t numLocalElements_;
00815 
00817     GlobalOrdinal minMyGID_;
00818 
00820     GlobalOrdinal maxMyGID_;
00821 
00823     GlobalOrdinal minAllGID_;
00824 
00826     GlobalOrdinal maxAllGID_;
00827 
00834     GlobalOrdinal firstContiguousGID_;
00835 
00849     GlobalOrdinal lastContiguousGID_;
00850 
00856     bool uniform_;
00857 
00859     bool contiguous_;
00860 
00867     bool distributed_;
00868 
00885     mutable Teuchos::ArrayRCP<GlobalOrdinal> lgMap_;
00886 
00888     typedef Details::FixedHashTable<GlobalOrdinal, LocalOrdinal> global_to_local_table_type;
00889 
00902     RCP<global_to_local_table_type> glMap_;
00903 
00940     mutable Teuchos::RCP<Directory<LocalOrdinal,GlobalOrdinal,Node> > directory_;
00941 
00942   }; // Map class
00943 
00958   template <class LocalOrdinal, class GlobalOrdinal>
00959   Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal> >
00960   createLocalMap (size_t numElements, const Teuchos::RCP<const Teuchos::Comm<int> >& comm);
00961 
00978   template <class LocalOrdinal, class GlobalOrdinal, class Node>
00979   Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >
00980   createLocalMapWithNode (size_t numElements,
00981                           const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
00982                           const Teuchos::RCP<Node>& node = KokkosClassic::Details::getNode<Node> ());
00983 
00992   template <class LocalOrdinal, class GlobalOrdinal>
00993   Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal> >
00994   createUniformContigMap (global_size_t numElements,
00995                           const Teuchos::RCP<const Teuchos::Comm<int> >& comm);
00996 
01003   template <class LocalOrdinal, class GlobalOrdinal, class Node>
01004   Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >
01005   createUniformContigMapWithNode (global_size_t numElements,
01006                                   const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
01007                                   const Teuchos::RCP<Node>& node =
01008                                   KokkosClassic::Details::getNode<Node> ());
01009 
01018   template <class LocalOrdinal, class GlobalOrdinal>
01019   Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,KokkosClassic::DefaultNode::DefaultNodeType> >
01020   createContigMap (global_size_t numElements,
01021                    size_t localNumElements,
01022                    const Teuchos::RCP<const Teuchos::Comm<int> > &comm);
01023 
01030   template <class LocalOrdinal, class GlobalOrdinal, class Node>
01031   Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >
01032   createContigMapWithNode (global_size_t numElements,
01033                            size_t localNumElements,
01034                            const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
01035                            const Teuchos::RCP<Node> &node);
01036 
01045   template <class LocalOrdinal, class GlobalOrdinal>
01046   Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,KokkosClassic::DefaultNode::DefaultNodeType> >
01047   createNonContigMap (const ArrayView<const GlobalOrdinal> &elementList,
01048                       const RCP<const Teuchos::Comm<int> > &comm);
01049 
01056   template <class LocalOrdinal, class GlobalOrdinal, class Node>
01057   Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
01058   createNonContigMapWithNode (const ArrayView<const GlobalOrdinal> &elementList,
01059                               const RCP<const Teuchos::Comm<int> > &comm,
01060                               const RCP<Node> &node);
01061 
01068   template <class LocalOrdinal, class GlobalOrdinal, class Node>
01069   Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
01070   createWeightedContigMapWithNode (int thisNodeWeight,
01071                                    global_size_t numElements,
01072                                    const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
01073                                    const Teuchos::RCP<Node> &node);
01074 
01081   template<class LocalOrdinal, class GlobalOrdinal, class Node>
01082   Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
01083   createOneToOne (const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &M);
01084 
01092   template<class LocalOrdinal, class GlobalOrdinal, class Node>
01093   Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
01094   createOneToOne(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &M,
01095                  const Tpetra::Details::TieBreak<LocalOrdinal,GlobalOrdinal> & tie_break);
01096 
01097 } // Tpetra namespace
01098 
01099 #include "Tpetra_Directory_decl.hpp"
01100 
01101 namespace Tpetra {
01102   namespace Details {
01103 
01104     template<class OutMapType, class InMapType>
01105     OutMapType
01106     MapCloner<OutMapType, InMapType>::
01107     clone (const InMapType& mapIn,
01108            const Teuchos::RCP<out_node_type>& nodeOut)
01109     {
01110       typedef ::Tpetra::Directory<typename OutMapType::local_ordinal_type,
01111                                   typename OutMapType::global_ordinal_type,
01112                                   typename OutMapType::node_type> out_dir_type;
01113 
01114       OutMapType mapOut; // Make an empty Map.
01115 
01116       // Fill the new Map with shallow copies of all of the original
01117       // Map's data.  This is safe because Map is immutable, so
01118       // users can't change the original Map.
01119       mapOut.comm_              = mapIn.comm_;
01120       mapOut.indexBase_         = mapIn.indexBase_;
01121       mapOut.numGlobalElements_ = mapIn.numGlobalElements_;
01122       mapOut.numLocalElements_  = mapIn.numLocalElements_;
01123       mapOut.minMyGID_          = mapIn.minMyGID_;
01124       mapOut.maxMyGID_          = mapIn.maxMyGID_;
01125       mapOut.minAllGID_         = mapIn.minAllGID_;
01126       mapOut.maxAllGID_         = mapIn.maxAllGID_;
01127       mapOut.firstContiguousGID_= mapIn.firstContiguousGID_;
01128       mapOut.lastContiguousGID_ = mapIn.lastContiguousGID_;
01129       mapOut.uniform_           = mapIn.uniform_;
01130       mapOut.contiguous_        = mapIn.contiguous_;
01131       mapOut.distributed_       = mapIn.distributed_;
01132       mapOut.lgMap_             = mapIn.lgMap_;
01133       mapOut.glMap_             = mapIn.glMap_;
01134       // New Map gets the new Node instance.
01135       mapOut.node_              = nodeOut;
01136 
01137       // We could cleverly clone the Directory here if it is
01138       // initialized, but there is no harm in simply creating it
01139       // uninitialized.
01140       mapOut.directory_ = Teuchos::rcp (new out_dir_type ());
01141 
01142       return mapOut;
01143     }
01144   } // namespace Details
01145 
01146 
01147   template <class LocalOrdinal, class GlobalOrdinal, class Node>
01148   template <class NodeOut>
01149   RCP<const Map<LocalOrdinal, GlobalOrdinal, NodeOut> >
01150   Map<LocalOrdinal,GlobalOrdinal,Node>::
01151   clone (const Teuchos::RCP<NodeOut>& nodeOut) const
01152   {
01153     typedef Map<LocalOrdinal, GlobalOrdinal, Node> in_map_type;
01154     typedef Map<LocalOrdinal, GlobalOrdinal, NodeOut> out_map_type;
01155     typedef Details::MapCloner<out_map_type, in_map_type> cloner_type;
01156     // Copy constructor does a shallow copy.
01157     return Teuchos::rcp (new out_map_type (cloner_type::clone (*this, nodeOut)));
01158   }
01159 
01160 } // namespace Tpetra
01161 
01164 template <class LocalOrdinal, class GlobalOrdinal, class Node>
01165 bool operator== (const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> &map1,
01166                  const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> &map2)
01167 { return map1.isSameAs (map2); }
01168 
01171 template <class LocalOrdinal, class GlobalOrdinal, class Node>
01172 bool operator!= (const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> &map1,
01173                  const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> &map2)
01174 { return ! map1.isSameAs (map2); }
01175 
01176 
01177 #if defined(TPETRA_USE_KOKKOS_REFACTOR_MAP)
01178 // Include KokkosRefactor partial specialization if enabled
01179 #  if defined(TPETRA_HAVE_KOKKOS_REFACTOR)
01180 #    include "Tpetra_KokkosRefactor_Map_decl.hpp"
01181 #  endif // defined(TPETRA_HAVE_KOKKOS_REFACTOR)
01182 #endif // defined(TPETRA_USE_KOKKOS_REFACTOR_MAP)
01183 
01184 
01185 #endif // TPETRA_MAP_DECL_HPP
01186 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines