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 (const ::Tpetra::Details::FixedHashTable<GlobalOrdinal, LocalOrdinal, DeviceType>& glMap,
77  const ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, DeviceType>& lgMap,
78  const GlobalOrdinal indexBase,
79  const GlobalOrdinal myMinGid,
80  const GlobalOrdinal myMaxGid,
81  const GlobalOrdinal firstContiguousGid,
82  const GlobalOrdinal lastContiguousGid,
83  const LocalOrdinal numLocalElements,
84  const bool contiguous) :
85  glMap_ (glMap),
86  lgMap_ (lgMap),
87  indexBase_ (indexBase),
88  myMinGid_ (myMinGid),
89  myMaxGid_ (myMaxGid),
90  firstContiguousGid_ (firstContiguousGid),
91  lastContiguousGid_ (lastContiguousGid),
92  numLocalElements_ (numLocalElements),
93  contiguous_ (contiguous)
94  {}
95 
97  KOKKOS_INLINE_FUNCTION LocalOrdinal getNodeNumElements () const {
98  return numLocalElements_;
99  }
100 
102  KOKKOS_INLINE_FUNCTION GlobalOrdinal getIndexBase () const {
103  return indexBase_;
104  }
105 
110  KOKKOS_INLINE_FUNCTION bool isContiguous () const {
111  return contiguous_;
112  }
113 
115  KOKKOS_INLINE_FUNCTION LocalOrdinal getMinLocalIndex () const {
116  return 0;
117  }
118 
120  KOKKOS_INLINE_FUNCTION LocalOrdinal
122  {
123  if (numLocalElements_ == 0) {
124  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
125  } else { // Local indices are always zero-based.
126  return static_cast<LocalOrdinal> (numLocalElements_ - 1);
127  }
128  }
129 
131  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMinGlobalIndex () const {
132  return myMinGid_;
133  }
134 
136  KOKKOS_INLINE_FUNCTION GlobalOrdinal getMaxGlobalIndex () const {
137  return myMaxGid_;
138  }
139 
141  KOKKOS_INLINE_FUNCTION LocalOrdinal
142  getLocalElement (const GlobalOrdinal globalIndex) const
143  {
144  if (contiguous_) {
145  if (globalIndex < myMinGid_ || globalIndex > myMaxGid_) {
146  return ::Tpetra::Details::OrdinalTraits<LocalOrdinal>::invalid ();
147  }
148  return static_cast<LocalOrdinal> (globalIndex - myMinGid_);
149  }
150  else if (globalIndex >= firstContiguousGid_ &&
151  globalIndex <= lastContiguousGid_) {
152  return static_cast<LocalOrdinal> (globalIndex - firstContiguousGid_);
153  }
154  else {
155  // If the given global index is not in the table, this returns
156  // the same value as OrdinalTraits<LocalOrdinal>::invalid().
157  return glMap_.get (globalIndex);
158  }
159  }
160 
162  KOKKOS_INLINE_FUNCTION GlobalOrdinal
163  getGlobalElement (const LocalOrdinal localIndex) const
164  {
165  if (localIndex < getMinLocalIndex () || localIndex > getMaxLocalIndex ()) {
166  return ::Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ();
167  }
168  if (isContiguous ()) {
169  return getMinGlobalIndex () + localIndex;
170  }
171  else {
172  return lgMap_(localIndex);
173  }
174  }
175 
176 private:
193  ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, DeviceType> lgMap_;
194  GlobalOrdinal indexBase_;
195  GlobalOrdinal myMinGid_;
196  GlobalOrdinal myMaxGid_;
197  GlobalOrdinal firstContiguousGid_;
198  GlobalOrdinal lastContiguousGid_;
199  LocalOrdinal numLocalElements_;
200  bool contiguous_;
201 };
202 
203 } // namespace Details
204 } // namespace Tpetra
205 
206 #endif // TPETRA_DETAILS_LOCALMAP_HPP
207 
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.