Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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:
73  typedef LocalOrdinal local_ordinal_type;
74  typedef GlobalOrdinal global_ordinal_type;
75  typedef DeviceType device_type;
76 
77  LocalMap () :
78  indexBase_ (0),
79  myMinGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
80  myMaxGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
81  firstContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
82  lastContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
83  numLocalElements_ (0),
84  contiguous_ (false)
85  {}
86  LocalMap (const ::Tpetra::Details::FixedHashTable<GlobalOrdinal, LocalOrdinal, DeviceType>& glMap,
87  const ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, DeviceType>& lgMap,
88  const GlobalOrdinal indexBase,
89  const GlobalOrdinal myMinGid,
90  const GlobalOrdinal myMaxGid,
91  const GlobalOrdinal firstContiguousGid,
92  const GlobalOrdinal lastContiguousGid,
93  const LocalOrdinal numLocalElements,
94  const bool contiguous) :
95  glMap_ (glMap),
96  lgMap_ (lgMap),
97  indexBase_ (indexBase),
98  myMinGid_ (myMinGid),
99  myMaxGid_ (myMaxGid),
100  firstContiguousGid_ (firstContiguousGid),
101  lastContiguousGid_ (lastContiguousGid),
102  numLocalElements_ (numLocalElements),
103  contiguous_ (contiguous)
104  {}
105 
107  KOKKOS_INLINE_FUNCTION LocalOrdinal getNodeNumElements () const {
108  return numLocalElements_;
109  }
110 
112  KOKKOS_INLINE_FUNCTION GlobalOrdinal getIndexBase () const {
113  return indexBase_;
114  }
115 
120  KOKKOS_INLINE_FUNCTION bool isContiguous () const {
121  return contiguous_;
122  }
123 
125  KOKKOS_INLINE_FUNCTION LocalOrdinal getMinLocalIndex () const {
126  return 0;
127  }
128 
130  KOKKOS_INLINE_FUNCTION LocalOrdinal
132  {
133  if (numLocalElements_ == 0) {
134  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
135  } else { // Local indices are always zero-based.
136  return static_cast<LocalOrdinal> (numLocalElements_ - 1);
137  }
138  }
139 
141  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex () const {
142  return myMinGid_;
143  }
144 
146  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMaxGlobalIndex () const {
147  return myMaxGid_;
148  }
149 
151  KOKKOS_INLINE_FUNCTION LocalOrdinal
152  getLocalElement (const GlobalOrdinal globalIndex) const
153  {
154  if (contiguous_) {
155  if (globalIndex < myMinGid_ || globalIndex > myMaxGid_) {
156  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
157  }
158  return static_cast<LocalOrdinal> (globalIndex - myMinGid_);
159  }
160  else if (globalIndex >= firstContiguousGid_ &&
161  globalIndex <= lastContiguousGid_) {
162  return static_cast<LocalOrdinal> (globalIndex - firstContiguousGid_);
163  }
164  else {
165  // If the given global index is not in the table, this returns
166  // the same value as OrdinalTraits<LocalOrdinal>::invalid().
167  return glMap_.get (globalIndex);
168  }
169  }
170 
172  KOKKOS_INLINE_FUNCTION GlobalOrdinal
173  getGlobalElement (const LocalOrdinal localIndex) const
174  {
175  if (localIndex < getMinLocalIndex () || localIndex > getMaxLocalIndex ()) {
176  return ::Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ();
177  }
178  if (isContiguous ()) {
179  return getMinGlobalIndex () + localIndex;
180  }
181  else {
182  return lgMap_(localIndex);
183  }
184  }
185 
186 private:
203  ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, DeviceType> lgMap_;
204  GlobalOrdinal indexBase_;
205  GlobalOrdinal myMinGid_;
206  GlobalOrdinal myMaxGid_;
207  GlobalOrdinal firstContiguousGid_;
208  GlobalOrdinal lastContiguousGid_;
209  LocalOrdinal numLocalElements_;
210  bool contiguous_;
211 };
212 
213 } // namespace Details
214 } // namespace Tpetra
215 
216 #endif // TPETRA_DETAILS_LOCALMAP_HPP
217 
KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex() const
The minimum global index on the calling process.
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.
&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.
KOKKOS_INLINE_FUNCTION bool isContiguous() const
Whether the Map is (locally) contiguous.
KOKKOS_INLINE_FUNCTION LocalOrdinal getNodeNumElements() const
The number of indices that live on the calling process.
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 GlobalOrdinal getIndexBase() const
The (global) index base.
KOKKOS_INLINE_FUNCTION LocalOrdinal getMaxLocalIndex() const
The maximum local index.