Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
70 template<class LocalOrdinal, class GlobalOrdinal, class DeviceType>
71 class LocalMap {
72 public:
74  using local_ordinal_type = LocalOrdinal;
75 
77  using global_ordinal_type = GlobalOrdinal;
78 
80  using device_type = DeviceType;
81 
83  using execution_space = typename device_type::execution_space;
84 
86  using memory_space = typename device_type::memory_space;
87 
89 #ifdef KOKKOS_ENABLE_CUDA
90  using no_uvm_memory_space = typename std::conditional<std::is_same<memory_space, Kokkos::CudaUVMSpace>::value,
91  Kokkos::CudaSpace, memory_space>::type;
92  using no_uvm_device_type = Kokkos::Device<execution_space, no_uvm_memory_space>;
93 #else
95 #endif
96 
97  LocalMap () :
98  indexBase_ (0),
99  myMinGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
100  myMaxGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
101  firstContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
102  lastContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
103  numLocalElements_ (0),
104  contiguous_ (false)
105  {}
106  LocalMap (const ::Tpetra::Details::FixedHashTable<GlobalOrdinal, LocalOrdinal, no_uvm_device_type>& glMap,
107  const ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, no_uvm_device_type>& lgMap,
108  const GlobalOrdinal indexBase,
109  const GlobalOrdinal myMinGid,
110  const GlobalOrdinal myMaxGid,
111  const GlobalOrdinal firstContiguousGid,
112  const GlobalOrdinal lastContiguousGid,
113  const LocalOrdinal numLocalElements,
114  const bool contiguous) :
115  glMap_ (glMap),
116  lgMap_ (lgMap),
117  indexBase_ (indexBase),
118  myMinGid_ (myMinGid),
119  myMaxGid_ (myMaxGid),
120  firstContiguousGid_ (firstContiguousGid),
121  lastContiguousGid_ (lastContiguousGid),
122  numLocalElements_ (numLocalElements),
123  contiguous_ (contiguous)
124  {}
125 
127  KOKKOS_INLINE_FUNCTION LocalOrdinal getLocalNumElements () const {
128  return numLocalElements_;
129  }
130 
132  KOKKOS_INLINE_FUNCTION GlobalOrdinal getIndexBase () const {
133  return indexBase_;
134  }
135 
140  KOKKOS_INLINE_FUNCTION bool isContiguous () const {
141  return contiguous_;
142  }
143 
145  KOKKOS_INLINE_FUNCTION LocalOrdinal getMinLocalIndex () const {
146  return 0;
147  }
148 
150  KOKKOS_INLINE_FUNCTION LocalOrdinal
152  {
153  if (numLocalElements_ == 0) {
154  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
155  } else { // Local indices are always zero-based.
156  return static_cast<LocalOrdinal> (numLocalElements_ - 1);
157  }
158  }
159 
161  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex () const {
162  return myMinGid_;
163  }
164 
166  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMaxGlobalIndex () const {
167  return myMaxGid_;
168  }
169 
171  KOKKOS_INLINE_FUNCTION LocalOrdinal
172  getLocalElement (const GlobalOrdinal globalIndex) const
173  {
174  if (contiguous_) {
175  if (globalIndex < myMinGid_ || globalIndex > myMaxGid_) {
176  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
177  }
178  return static_cast<LocalOrdinal> (globalIndex - myMinGid_);
179  }
180  else if (globalIndex >= firstContiguousGid_ &&
181  globalIndex <= lastContiguousGid_) {
182  return static_cast<LocalOrdinal> (globalIndex - firstContiguousGid_);
183  }
184  else {
185  // If the given global index is not in the table, this returns
186  // the same value as OrdinalTraits<LocalOrdinal>::invalid().
187  return glMap_.get (globalIndex);
188  }
189  }
190 
192  KOKKOS_INLINE_FUNCTION GlobalOrdinal
193  getGlobalElement (const LocalOrdinal localIndex) const
194  {
195  if (localIndex < getMinLocalIndex () || localIndex > getMaxLocalIndex ()) {
196  return ::Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ();
197  }
198  if (isContiguous ()) {
199  return getMinGlobalIndex () + localIndex;
200  }
201  else {
202  return lgMap_(localIndex);
203  }
204  }
205 
206 private:
223  ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, no_uvm_device_type> lgMap_;
224 
225  GlobalOrdinal indexBase_;
226  GlobalOrdinal myMinGid_;
227  GlobalOrdinal myMaxGid_;
228  GlobalOrdinal firstContiguousGid_;
229  GlobalOrdinal lastContiguousGid_;
230  LocalOrdinal numLocalElements_;
231  bool contiguous_;
232 };
233 
234 } // namespace Details
235 } // namespace Tpetra
236 
237 #endif // TPETRA_DETAILS_LOCALMAP_HPP
238 
KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex() const
The minimum global index on the calling process.
GO global_ordinal_type
The type of global indices.
KOKKOS_INLINE_FUNCTION ValueType get(const KeyType &key) const
Get the value corresponding to the given key.
KOKKOS_INLINE_FUNCTION LocalOrdinal getLocalElement(const GlobalOrdinal globalIndex) const
Get the local index corresponding to the given global index. (device only)
typename device_type::execution_space execution_space
The Kokkos execution space.
&quot;Local&quot; part of Map suitable for Kokkos kernels.
KOKKOS_INLINE_FUNCTION GlobalOrdinal getGlobalElement(const LocalOrdinal localIndex) const
Get the global index corresponding to the given local index. (device only)
KOKKOS_INLINE_FUNCTION bool isContiguous() const
Whether the Map is (locally) contiguous.
KOKKOS_INLINE_FUNCTION LocalOrdinal getMinLocalIndex() const
The minimum local index.
KOKKOS_INLINE_FUNCTION GlobalOrdinal getMaxGlobalIndex() const
The maximum global index on the calling process.
device_type no_uvm_device_type
The hash will be CudaSpace, not CudaUVMSpace.
Forward declaration of Tpetra::Details::LocalMap.
KOKKOS_INLINE_FUNCTION LocalOrdinal getLocalNumElements() const
The number of indices that live on the calling process.
typename device_type::memory_space memory_space
The Kokkos memory space.
KOKKOS_INLINE_FUNCTION GlobalOrdinal getIndexBase() const
The (global) index base.
KOKKOS_INLINE_FUNCTION LocalOrdinal getMaxLocalIndex() const
The maximum local index.
LO local_ordinal_type
The type of local indices.