Tpetra parallel linear algebra  Version of the Day
Tpetra_Map_decl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_MAP_DECL_HPP
43 #define TPETRA_MAP_DECL_HPP
44 
48 
49 #include "Tpetra_ConfigDefs.hpp"
50 #include "Kokkos_DefaultNode.hpp"
51 #include "Kokkos_DualView.hpp"
52 #include "Teuchos_Describable.hpp"
53 #include "Tpetra_Details_FixedHashTable_decl.hpp"
55 
56 // mfh 27 Apr 2013: If HAVE_TPETRA_FIXED_HASH_TABLE is defined (which
57 // it is by default), then Map will used the fixed-structure hash
58 // table variant for global-to-local index lookups. Otherwise, it
59 // will use the dynamic-structure hash table variant.
60 //
61 // mfh 23 Mar 2014: I've removed all code in Map that uses the
62 // dynamic-structure hash table variant, since it has not been used
63 // for at least a year. However, I am retaining the #define, in case
64 // downstream code depends on it.
65 
66 #ifndef HAVE_TPETRA_FIXED_HASH_TABLE
67 # define HAVE_TPETRA_FIXED_HASH_TABLE 1
68 #endif // HAVE_TPETRA_FIXED_HASH_TABLE
69 
70 namespace Tpetra {
71 
72 #ifndef DOXYGEN_SHOULD_SKIP_THIS
73  // Forward declaration of Directory.
74  template <class LO, class GO, class N> class Directory;
75 #endif // DOXYGEN_SHOULD_SKIP_THIS
76 
77  namespace Details {
78 
79 #ifndef DOXYGEN_SHOULD_SKIP_THIS
80  // Forward declaration of TieBreak
81  template <class LO, class GO> class TieBreak;
82 #endif // DOXYGEN_SHOULD_SKIP_THIS
83 
86  template<class OutMapType, class InMapType>
87  struct MapCloner {
88  typedef typename OutMapType::node_type out_node_type;
89  typedef typename InMapType::node_type in_node_type;
90 
91  static OutMapType
92  clone (const InMapType& mapIn,
93  const Teuchos::RCP<out_node_type>& node2);
94  };
95  } // namespace Details
96 
97  template<class Node>
98  Teuchos::RCP<Node> defaultArgNode() {
99  // Workaround function for a deferred visual studio bug
100  // http://connect.microsoft.com/VisualStudio/feedback/details/719847/erroneous-error-c2783-could-not-deduce-template-argument
101  // Use this function for default arguments rather than calling
102  // what is the return value below. Also helps in reducing
103  // duplication in various constructors.
104  return KokkosClassic::Details::getNode<Node> ();
105  }
106 
256  template <class LocalOrdinal = Details::DefaultTypes::local_ordinal_type,
257  class GlobalOrdinal = Details::DefaultTypes::global_ordinal_type,
259  class Map : public Teuchos::Describable {
260  public:
262 
263 
265  typedef LocalOrdinal local_ordinal_type;
267  typedef GlobalOrdinal global_ordinal_type;
269  typedef Node node_type;
270 
272 
274 
316  Map (global_size_t numGlobalElements,
317  GlobalOrdinal indexBase,
318  const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
319  LocalGlobal lg=GloballyDistributed,
320  const Teuchos::RCP<Node> &node = defaultArgNode<Node>());
321 
361  Map (global_size_t numGlobalElements,
362  size_t numLocalElements,
363  GlobalOrdinal indexBase,
364  const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
365  const Teuchos::RCP<Node> &node = defaultArgNode<Node>());
366 
401  Map (global_size_t numGlobalElements,
402  const Teuchos::ArrayView<const GlobalOrdinal> &elementList,
403  GlobalOrdinal indexBase,
404  const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
405  const Teuchos::RCP<Node> &node = defaultArgNode<Node>());
406 
407 
418  Map ();
419 
421  ~Map ();
422 
424 
426 
431  bool isOneToOne () const;
432 
439  return numGlobalElements_;
440  }
441 
447  size_t getNodeNumElements () const {
448  return numLocalElements_;
449  }
450 
456  GlobalOrdinal getIndexBase () const {
457  return indexBase_;
458  }
459 
465  LocalOrdinal getMinLocalIndex () const {
466  return static_cast<LocalOrdinal> (0);
467  }
468 
479  LocalOrdinal getMaxLocalIndex () const {
480  if (this->getNodeNumElements () == 0) {
482  } else { // Local indices are always zero-based.
483  return static_cast<LocalOrdinal> (this->getNodeNumElements () - 1);
484  }
485  }
486 
492  GlobalOrdinal getMinGlobalIndex () const {
493  return minMyGID_;
494  }
495 
501  GlobalOrdinal getMaxGlobalIndex () const {
502  return maxMyGID_;
503  }
504 
510  GlobalOrdinal getMinAllGlobalIndex () const {
511  return minAllGID_;
512  }
513 
519  GlobalOrdinal getMaxAllGlobalIndex () const {
520  return maxAllGID_;
521  }
522 
535  LocalOrdinal getLocalElement (GlobalOrdinal globalIndex) const;
536 
545  GlobalOrdinal getGlobalElement (LocalOrdinal localIndex) const;
546 
577  getRemoteIndexList (const Teuchos::ArrayView<const GlobalOrdinal>& GIDList,
578  const Teuchos::ArrayView< int>& nodeIDList,
579  const Teuchos::ArrayView< LocalOrdinal>& LIDList) const;
580 
605  getRemoteIndexList (const Teuchos::ArrayView<const GlobalOrdinal> & GIDList,
606  const Teuchos::ArrayView< int> & nodeIDList) const;
607 
614  Teuchos::ArrayView<const GlobalOrdinal> getNodeElementList() const;
615 
617 
619 
626  bool isNodeLocalElement (LocalOrdinal localIndex) const;
627 
634  bool isNodeGlobalElement (GlobalOrdinal globalIndex) const;
635 
642  bool isUniform () const;
643 
655  bool isContiguous () const;
656 
677  bool isDistributed () const;
678 
703  bool isCompatible (const Map<LocalOrdinal,GlobalOrdinal,Node> &map) const;
704 
735  bool isSameAs (const Map<LocalOrdinal,GlobalOrdinal,Node> &map) const;
736 
741  bool locallySameAs (const Map<LocalOrdinal, GlobalOrdinal, node_type>& map) const;
742 
744 
746 
748  Teuchos::RCP<const Teuchos::Comm<int> > getComm () const;
749 
751  Teuchos::RCP<Node> getNode () const;
752 
754 
756 
758  std::string description () const;
759 
761  void
762  describe (Teuchos::FancyOStream &out,
763  const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const;
764 
766 
768 
770  template <class NodeOut>
771  Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, NodeOut> >
772  clone (const RCP<NodeOut>& nodeOut) const;
773 
821  RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
822  removeEmptyProcesses () const;
823 
851  RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >
852  replaceCommWithSubset (const Teuchos::RCP<const Teuchos::Comm<int> >& newComm) const;
854 
855  protected:
856  // This lets other specializations of Map access all of this
857  // specialization's internal methods and data, so that we can
858  // implement clone() without exposing the details of Map to users.
859  template <class LO, class GO, class N> friend class Map;
860 
861  private:
862  template<class OutMapType, class InMapType>
863  friend struct Details::MapCloner;
864 
872  void setupDirectory () const;
873 
888  bool checkIsDist() const;
889 
891  Teuchos::RCP<const Teuchos::Comm<int> > comm_;
892 
894  Teuchos::RCP<Node> node_;
895 
897  GlobalOrdinal indexBase_;
898 
900  global_size_t numGlobalElements_;
901 
903  size_t numLocalElements_;
904 
906  GlobalOrdinal minMyGID_;
907 
909  GlobalOrdinal maxMyGID_;
910 
912  GlobalOrdinal minAllGID_;
913 
915  GlobalOrdinal maxAllGID_;
916 
923  GlobalOrdinal firstContiguousGID_;
924 
938  GlobalOrdinal lastContiguousGID_;
939 
945  bool uniform_;
946 
948  bool contiguous_;
949 
956  bool distributed_;
957 
959  typedef typename Kokkos::Device<typename Node::execution_space,
960  typename Node::memory_space> device_type;
961 
978  mutable Kokkos::DualView<GlobalOrdinal*, device_type> lgMap_;
979 
981  typedef Details::FixedHashTable<GlobalOrdinal, LocalOrdinal,
982  device_type> global_to_local_table_type;
983 
996  global_to_local_table_type glMap_;
997 
1034  mutable Teuchos::RCP<Directory<LocalOrdinal,GlobalOrdinal,Node> > directory_;
1035 
1036  }; // Map class
1037 
1051  template <class LocalOrdinal, class GlobalOrdinal>
1052  Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal> >
1053  createLocalMap (size_t numElements, const Teuchos::RCP<const Teuchos::Comm<int> >& comm);
1054 
1071  template <class LocalOrdinal, class GlobalOrdinal, class Node>
1072  Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >
1073  createLocalMapWithNode (size_t numElements,
1074  const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
1075  const Teuchos::RCP<Node>& node = defaultArgNode<Node> ());
1076 
1084  template <class LocalOrdinal, class GlobalOrdinal>
1085  Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal> >
1087  const Teuchos::RCP<const Teuchos::Comm<int> >& comm);
1088 
1095  template <class LocalOrdinal, class GlobalOrdinal, class Node>
1096  Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >
1098  const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
1099  const Teuchos::RCP<Node>& node =
1100  defaultArgNode<Node> ());
1101 
1110  template <class LocalOrdinal, class GlobalOrdinal>
1111  Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal> >
1112  createContigMap (global_size_t numElements,
1113  size_t localNumElements,
1114  const Teuchos::RCP<const Teuchos::Comm<int> > &comm);
1115 
1122  template <class LocalOrdinal, class GlobalOrdinal, class Node>
1123  Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >
1125  size_t localNumElements,
1126  const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
1127  const Teuchos::RCP<Node>& node =
1128  defaultArgNode<Node> ());
1129 
1138  template <class LocalOrdinal, class GlobalOrdinal>
1139  Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal> >
1140  createNonContigMap (const ArrayView<const GlobalOrdinal> &elementList,
1141  const RCP<const Teuchos::Comm<int> > &comm);
1142 
1149  template <class LocalOrdinal, class GlobalOrdinal, class Node>
1150  Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
1151  createNonContigMapWithNode (const ArrayView<const GlobalOrdinal> &elementList,
1152  const RCP<const Teuchos::Comm<int> > &comm,
1153  const RCP<Node> &node);
1154 
1161  template <class LocalOrdinal, class GlobalOrdinal, class Node>
1162  Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
1163  createWeightedContigMapWithNode (int thisNodeWeight,
1164  global_size_t numElements,
1165  const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
1166  const Teuchos::RCP<Node> &node);
1167 
1174  template<class LocalOrdinal, class GlobalOrdinal, class Node>
1175  Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
1176  createOneToOne (const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &M);
1177 
1185  template<class LocalOrdinal, class GlobalOrdinal, class Node>
1186  Teuchos::RCP< const Map<LocalOrdinal,GlobalOrdinal,Node> >
1187  createOneToOne(const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &M,
1189 
1190 } // Tpetra namespace
1191 
1192 #include "Tpetra_Directory_decl.hpp"
1193 
1194 namespace Tpetra {
1195  namespace Details {
1196 
1197  template<class OutMapType, class InMapType>
1198  OutMapType
1199  MapCloner<OutMapType, InMapType>::
1200  clone (const InMapType& mapIn,
1201  const Teuchos::RCP<out_node_type>& nodeOut)
1202  {
1203  typedef ::Tpetra::Directory<typename OutMapType::local_ordinal_type,
1204  typename OutMapType::global_ordinal_type,
1205  typename OutMapType::node_type> out_dir_type;
1206  typedef typename OutMapType::global_to_local_table_type out_table_type;
1207 
1208  OutMapType mapOut; // Make an empty Map.
1209 
1210  // Fill the new Map with (possibly) shallow copies of all of the
1211  // original Map's data. This is safe because Map is immutable,
1212  // so users can't change the original Map.
1213  mapOut.comm_ = mapIn.comm_;
1214  mapOut.indexBase_ = mapIn.indexBase_;
1215  mapOut.numGlobalElements_ = mapIn.numGlobalElements_;
1216  mapOut.numLocalElements_ = mapIn.numLocalElements_;
1217  mapOut.minMyGID_ = mapIn.minMyGID_;
1218  mapOut.maxMyGID_ = mapIn.maxMyGID_;
1219  mapOut.minAllGID_ = mapIn.minAllGID_;
1220  mapOut.maxAllGID_ = mapIn.maxAllGID_;
1221  mapOut.firstContiguousGID_= mapIn.firstContiguousGID_;
1222  mapOut.lastContiguousGID_ = mapIn.lastContiguousGID_;
1223  mapOut.uniform_ = mapIn.uniform_;
1224  mapOut.contiguous_ = mapIn.contiguous_;
1225  mapOut.distributed_ = mapIn.distributed_;
1226  mapOut.lgMap_ = mapIn.lgMap_;
1227  // This makes a deep copy only if necessary. We could have
1228  // defined operator= to do this, but that would violate
1229  // expectations. (Kokkos::View::operator= only does a shallow
1230  // copy, EVER.)
1231  mapOut.glMap_ = out_table_type (mapIn.glMap_);
1232  // New Map gets the new Node instance.
1233  mapOut.node_ = nodeOut;
1234 
1235  // We could cleverly clone the Directory here if it is
1236  // initialized, but there is no harm in simply creating it
1237  // uninitialized.
1238  mapOut.directory_ = Teuchos::rcp (new out_dir_type ());
1239 
1240  return mapOut;
1241  }
1242  } // namespace Details
1243 
1244 
1245  template <class LocalOrdinal, class GlobalOrdinal, class Node>
1246  template <class NodeOut>
1247  RCP<const Map<LocalOrdinal, GlobalOrdinal, NodeOut> >
1249  clone (const Teuchos::RCP<NodeOut>& nodeOut) const
1250  {
1251  typedef Map<LocalOrdinal, GlobalOrdinal, Node> in_map_type;
1252  typedef Map<LocalOrdinal, GlobalOrdinal, NodeOut> out_map_type;
1254  // Copy constructor does a shallow copy.
1255  return Teuchos::rcp (new out_map_type (cloner_type::clone (*this, nodeOut)));
1256  }
1257 
1258 } // namespace Tpetra
1259 
1262 template <class LocalOrdinal, class GlobalOrdinal, class Node>
1265 { return map1.isSameAs (map2); }
1266 
1269 template <class LocalOrdinal, class GlobalOrdinal, class Node>
1272 { return ! map1.isSameAs (map2); }
1273 
1274 
1275 #endif // TPETRA_MAP_DECL_HPP
1276 
Interface for breaking ties in ownership.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Traits class for "invalid" (flag) values of integer types that Tpetra uses as local ordinals or globa...
GlobalOrdinal getMinGlobalIndex() const
The minimum global index owned by the calling process.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, NodeOut > > clone(const RCP< NodeOut > &nodeOut) const
Advanced methods.
GlobalOrdinal global_ordinal_type
The type of global indices.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal > > createNonContigMap(const ArrayView< const GlobalOrdinal > &elementList, const RCP< const Teuchos::Comm< int > > &comm)
Non-member constructor for a non-contiguous Map with the default Kokkos Node.
KokkosClassic::DefaultNode::DefaultNodeType node_type
Default value of Node template parameter.
LookupStatus
Return status of Map remote index lookup (getRemoteIndexList()).
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createLocalMapWithNode(size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const Teuchos::RCP< Node > &node=defaultArgNode< Node >())
Nonmember constructor for a locally replicated Map with a specified Kokkos Node.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createContigMapWithNode(global_size_t numElements, size_t localNumElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const Teuchos::RCP< Node > &node=defaultArgNode< Node >())
Non-member constructor for a (potentially) non-uniformly distributed, contiguous Map with a user-spec...
GlobalOrdinal getIndexBase() const
The index base for this Map.
LocalOrdinal getMinLocalIndex() const
The minimum local index.
GlobalOrdinal getMinAllGlobalIndex() const
The minimum global index over all processes in the communicator.
int local_ordinal_type
Default value of LocalOrdinal template parameter.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal > > createLocalMap(size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Nonmember constructor for a locally replicated Map with the default Kokkos Node.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createWeightedContigMapWithNode(int thisNodeWeight, global_size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const Teuchos::RCP< Node > &node)
Non-member constructor for a contiguous Map with user-defined weights and a user-specified Kokkos Nod...
Implementation details of Tpetra.
GlobalOrdinal getMaxAllGlobalIndex() const
The maximum global index over all processes in the communicator.
size_t global_size_t
Global size_t object.
Traits class for "invalid" (flag) values of integer types that Tpetra uses as local ordinals or globa...
bool operator!=(const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map1, const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map2)
True if map1 is not the same as (in the sense of isSameAs()) map2, else false.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal > > createUniformContigMap(global_size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Non-member constructor for a uniformly distributed, contiguous Map with the default Kokkos Node...
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createOneToOne(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &M)
Creates a one-to-one version of the given Map where each GID is owned by only one process...
GlobalOrdinal getMaxGlobalIndex() const
The maximum global index owned by the calling process.
Node node_type
The type of the Kokkos Node.
Implementation detail of Map::clone().
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal > > createContigMap(global_size_t numElements, size_t localNumElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Non-member constructor for a (potentially) non-uniformly distributed, contiguous Map with the default...
size_t getNodeNumElements() const
The number of elements belonging to the calling process.
Describes a parallel distribution of objects over processes.
bool operator==(const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map1, const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map2)
True if map1 is the same as (in the sense of isSameAs()) map2, else false.
bool isSameAs(const Map< LocalOrdinal, GlobalOrdinal, Node > &map) const
True if and only if map is identical to this Map.
LocalOrdinal local_ordinal_type
The type of local indices.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createNonContigMapWithNode(const ArrayView< const GlobalOrdinal > &elementList, const RCP< const Teuchos::Comm< int > > &comm, const RCP< Node > &node)
Non-member constructor for a non-contiguous Map with a user-specified Kokkos Node.
LocalGlobal
Enum for local versus global allocation of Map entries.
LocalOrdinal getMaxLocalIndex() const
The maximum local index on the calling process.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createUniformContigMapWithNode(global_size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const Teuchos::RCP< Node > &node=defaultArgNode< Node >())
Non-member constructor for a uniformly distributed, contiguous Map with a user-specified Kokkos Node...
global_size_t getGlobalNumElements() const
The number of elements in this Map.