Tpetra parallel linear algebra  Version of the Day
Tpetra_Details_LocalMap.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_DETAILS_LOCALMAP_HPP
43 #define TPETRA_DETAILS_LOCALMAP_HPP
44 
48 
49 #include "Tpetra_Details_FixedHashTable.hpp"
50 // #include "Tpetra_Details_OrdinalTraits.hpp" // comes in above
51 // #include "Kokkos_Core.hpp" // comes in above
53 
54 namespace Tpetra {
55 namespace Details {
56 namespace Classes {
57 
71 template<class LocalOrdinal, class GlobalOrdinal, class DeviceType>
72 class LocalMap {
73 public:
74  typedef LocalOrdinal local_ordinal_type;
75  typedef GlobalOrdinal global_ordinal_type;
76  typedef DeviceType device_type;
77 
78  LocalMap () :
79  indexBase_ (0),
80  myMinGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
81  myMaxGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
82  firstContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
83  lastContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
84  numLocalElements_ (0),
85  contiguous_ (false)
86  {}
87  LocalMap (const ::Tpetra::Details::FixedHashTable<GlobalOrdinal, LocalOrdinal, DeviceType>& glMap,
88  const ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, DeviceType>& lgMap,
89  const GlobalOrdinal indexBase,
90  const GlobalOrdinal myMinGid,
91  const GlobalOrdinal myMaxGid,
92  const GlobalOrdinal firstContiguousGid,
93  const GlobalOrdinal lastContiguousGid,
94  const LocalOrdinal numLocalElements,
95  const bool contiguous) :
96  glMap_ (glMap),
97  lgMap_ (lgMap),
98  indexBase_ (indexBase),
99  myMinGid_ (myMinGid),
100  myMaxGid_ (myMaxGid),
101  firstContiguousGid_ (firstContiguousGid),
102  lastContiguousGid_ (lastContiguousGid),
103  numLocalElements_ (numLocalElements),
104  contiguous_ (contiguous)
105  {}
106 
108  KOKKOS_INLINE_FUNCTION LocalOrdinal getNodeNumElements () const {
109  return numLocalElements_;
110  }
111 
113  KOKKOS_INLINE_FUNCTION GlobalOrdinal getIndexBase () const {
114  return indexBase_;
115  }
116 
121  KOKKOS_INLINE_FUNCTION bool isContiguous () const {
122  return contiguous_;
123  }
124 
126  KOKKOS_INLINE_FUNCTION LocalOrdinal getMinLocalIndex () const {
127  return 0;
128  }
129 
131  KOKKOS_INLINE_FUNCTION LocalOrdinal
133  {
134  if (numLocalElements_ == 0) {
135  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
136  } else { // Local indices are always zero-based.
137  return static_cast<LocalOrdinal> (numLocalElements_ - 1);
138  }
139  }
140 
142  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex () const {
143  return myMinGid_;
144  }
145 
147  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMaxGlobalIndex () const {
148  return myMaxGid_;
149  }
150 
152  KOKKOS_INLINE_FUNCTION LocalOrdinal
153  getLocalElement (const GlobalOrdinal globalIndex) const
154  {
155  if (contiguous_) {
156  if (globalIndex < myMinGid_ || globalIndex > myMaxGid_) {
157  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
158  }
159  return static_cast<LocalOrdinal> (globalIndex - myMinGid_);
160  }
161  else if (globalIndex >= firstContiguousGid_ &&
162  globalIndex <= lastContiguousGid_) {
163  return static_cast<LocalOrdinal> (globalIndex - firstContiguousGid_);
164  }
165  else {
166  // If the given global index is not in the table, this returns
167  // the same value as OrdinalTraits<LocalOrdinal>::invalid().
168  return glMap_.get (globalIndex);
169  }
170  }
171 
173  KOKKOS_INLINE_FUNCTION GlobalOrdinal
174  getGlobalElement (const LocalOrdinal localIndex) const
175  {
176  if (localIndex < getMinLocalIndex () || localIndex > getMaxLocalIndex ()) {
177  return ::Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ();
178  }
179  if (isContiguous ()) {
180  return getMinGlobalIndex () + localIndex;
181  }
182  else {
183  return lgMap_(localIndex);
184  }
185  }
186 
187 private:
204  ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, DeviceType> lgMap_;
205  GlobalOrdinal indexBase_;
206  GlobalOrdinal myMinGid_;
207  GlobalOrdinal myMaxGid_;
208  GlobalOrdinal firstContiguousGid_;
209  GlobalOrdinal lastContiguousGid_;
210  LocalOrdinal numLocalElements_;
211  bool contiguous_;
212 };
213 
214 } // namespace Classes
215 } // namespace Details
216 } // namespace Tpetra
217 
218 #endif // TPETRA_DETAILS_LOCALMAP_HPP
219 
Tpetra::Details::Classes::LocalMap::getMaxGlobalIndex
KOKKOS_INLINE_FUNCTION GlobalOrdinal getMaxGlobalIndex() const
The maximum global index on the calling process.
Definition: Tpetra_Details_LocalMap.hpp:147
Tpetra::Details::Classes::LocalMap::getMaxLocalIndex
KOKKOS_INLINE_FUNCTION LocalOrdinal getMaxLocalIndex() const
The maximum local index.
Definition: Tpetra_Details_LocalMap.hpp:132
Tpetra::Details::Classes::LocalMap::getGlobalElement
KOKKOS_INLINE_FUNCTION GlobalOrdinal getGlobalElement(const LocalOrdinal localIndex) const
Get the global index corresponding to the given local index.
Definition: Tpetra_Details_LocalMap.hpp:174
Tpetra::Details::FixedHashTable::get
KOKKOS_INLINE_FUNCTION ValueType get(const KeyType &key) const
Get the value corresponding to the given key.
Definition: Tpetra_Details_FixedHashTable_decl.hpp:306
Tpetra::Details::Classes::LocalMap::getIndexBase
KOKKOS_INLINE_FUNCTION GlobalOrdinal getIndexBase() const
The (global) index base.
Definition: Tpetra_Details_LocalMap.hpp:113
Details
Implementation details of Tpetra.
Tpetra_Details_LocalMap_fwd.hpp
Forward declaration of Tpetra::Details::LocalMap.
Tpetra::Details::FixedHashTable< GlobalOrdinal, LocalOrdinal, DeviceType >
Tpetra::Details::Classes::LocalMap
"Local" part of Map suitable for Kokkos kernels.
Definition: Tpetra_Details_LocalMap.hpp:72
Tpetra::Details::Classes::LocalMap::getMinLocalIndex
KOKKOS_INLINE_FUNCTION LocalOrdinal getMinLocalIndex() const
The minimum local index.
Definition: Tpetra_Details_LocalMap.hpp:126
Tpetra::Details::Classes::LocalMap::getLocalElement
KOKKOS_INLINE_FUNCTION LocalOrdinal getLocalElement(const GlobalOrdinal globalIndex) const
Get the local index corresponding to the given global index.
Definition: Tpetra_Details_LocalMap.hpp:153
Tpetra::Details::Classes::LocalMap::getNodeNumElements
KOKKOS_INLINE_FUNCTION LocalOrdinal getNodeNumElements() const
The number of indices that live on the calling process.
Definition: Tpetra_Details_LocalMap.hpp:108
Tpetra
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Tpetra::Details::Classes::LocalMap::getMinGlobalIndex
KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex() const
The minimum global index on the calling process.
Definition: Tpetra_Details_LocalMap.hpp:142
Tpetra::Details::Classes::LocalMap::isContiguous
KOKKOS_INLINE_FUNCTION bool isContiguous() const
Whether the Map is (locally) contiguous.
Definition: Tpetra_Details_LocalMap.hpp:121