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  KOKKOS_DEFAULTED_FUNCTION LocalMap() = default;
90 
91  LocalMap (const ::Tpetra::Details::FixedHashTable<GlobalOrdinal, LocalOrdinal, device_type>& glMap,
92  const ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, device_type>& lgMap,
93  const GlobalOrdinal indexBase,
94  const GlobalOrdinal myMinGid,
95  const GlobalOrdinal myMaxGid,
96  const GlobalOrdinal firstContiguousGid,
97  const GlobalOrdinal lastContiguousGid,
98  const LocalOrdinal numLocalElements,
99  const bool contiguous) :
100  glMap_ (glMap),
101  lgMap_ (lgMap),
102  indexBase_ (indexBase),
103  myMinGid_ (myMinGid),
104  myMaxGid_ (myMaxGid),
105  firstContiguousGid_ (firstContiguousGid),
106  lastContiguousGid_ (lastContiguousGid),
107  numLocalElements_ (numLocalElements),
108  contiguous_ (contiguous)
109  {}
110 
112  KOKKOS_INLINE_FUNCTION LocalOrdinal getLocalNumElements () const {
113  return numLocalElements_;
114  }
115 
117  KOKKOS_INLINE_FUNCTION GlobalOrdinal getIndexBase () const {
118  return indexBase_;
119  }
120 
125  KOKKOS_INLINE_FUNCTION bool isContiguous () const {
126  return contiguous_;
127  }
128 
130  KOKKOS_INLINE_FUNCTION LocalOrdinal getMinLocalIndex () const {
131  return 0;
132  }
133 
135  KOKKOS_INLINE_FUNCTION LocalOrdinal
137  {
138  if (numLocalElements_ == 0) {
139  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
140  } else { // Local indices are always zero-based.
141  return static_cast<LocalOrdinal> (numLocalElements_ - 1);
142  }
143  }
144 
146  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex () const {
147  return myMinGid_;
148  }
149 
151  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMaxGlobalIndex () const {
152  return myMaxGid_;
153  }
154 
156  KOKKOS_INLINE_FUNCTION LocalOrdinal
157  getLocalElement (const GlobalOrdinal globalIndex) const
158  {
159  if (contiguous_) {
160  if (globalIndex < myMinGid_ || globalIndex > myMaxGid_) {
161  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
162  }
163  return static_cast<LocalOrdinal> (globalIndex - myMinGid_);
164  }
165  else if (globalIndex >= firstContiguousGid_ &&
166  globalIndex <= lastContiguousGid_) {
167  return static_cast<LocalOrdinal> (globalIndex - firstContiguousGid_);
168  }
169  else {
170  // If the given global index is not in the table, this returns
171  // the same value as OrdinalTraits<LocalOrdinal>::invalid().
172  return glMap_.get (globalIndex);
173  }
174  }
175 
177  KOKKOS_INLINE_FUNCTION GlobalOrdinal
178  getGlobalElement (const LocalOrdinal localIndex) const
179  {
180  if (localIndex < getMinLocalIndex () || localIndex > getMaxLocalIndex ()) {
181  return ::Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ();
182  }
183  if (isContiguous ()) {
184  return getMinGlobalIndex () + localIndex;
185  }
186  else {
187  return lgMap_(localIndex);
188  }
189  }
190 
191 private:
208  ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, device_type> lgMap_;
209 
210  GlobalOrdinal indexBase_ = 0;
211  GlobalOrdinal myMinGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
212  GlobalOrdinal myMaxGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
213  GlobalOrdinal firstContiguousGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
214  GlobalOrdinal lastContiguousGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
215  LocalOrdinal numLocalElements_ = 0;
216  bool contiguous_ = false;
217 };
218 
219 } // namespace Details
220 } // namespace Tpetra
221 
222 #endif // TPETRA_DETAILS_LOCALMAP_HPP
223 
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_DEFAULTED_FUNCTION LocalMap()=default
Default constructor.
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.
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.