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