Intrepid2
Intrepid2_ViewIterator.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Intrepid2 Package
4//
5// Copyright 2007 NTESS and the Intrepid2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
15#ifndef Intrepid2_ViewIterator_h
16#define Intrepid2_ViewIterator_h
17
18#include "Intrepid2_Utils.hpp"
19
20namespace Intrepid2
21{
41 template<class ViewType,typename ScalarType>
43 {
44 ViewType view_;
45 Kokkos::Array<int,7> dims_; // 7 is the maximum rank of a Kokkos view
46 Kokkos::Array<int,7> index_;
47 public:
50 KOKKOS_INLINE_FUNCTION
51 ViewIterator(ViewType view)
52 :
53 view_(view)
54 {
55 for (unsigned d=0; d<getFunctorRank(view); d++)
56 {
57 dims_[d] = view.extent_int(d);
58 index_[d] = 0;
59 }
60 for (unsigned d=getFunctorRank(view); d<7; d++)
61 {
62 dims_[d] = 1;
63 index_[d] = 0;
64 }
65 }
66
69 KOKKOS_INLINE_FUNCTION
70 ScalarType get()
71 {
72 return view_.access(index_[0],index_[1],index_[2],index_[3],index_[4],index_[5],index_[6]);
73 }
74
77 KOKKOS_INLINE_FUNCTION
78 void set(const ScalarType &value)
79 {
80 view_.access(index_[0],index_[1],index_[2],index_[3],index_[4],index_[5],index_[6]) = value;
81 }
82
85 KOKKOS_INLINE_FUNCTION
87 {
88 const int rank = getFunctorRank(view_);
89 for (int r=rank-1; r>=0; r--)
90 {
91 if (index_[r]+1 < dims_[r]) // can increment without going out of bounds in this dimension
92 {
93 return r;
94 }
95 }
96 // next increment will take us out of bounds
97 return -1;
98 }
99
102 KOKKOS_INLINE_FUNCTION
104 {
105 const int rank = getFunctorRank(view_);
106 for (int r=rank-1; r>=0; r--)
107 {
108 if (index_[r]+1 < dims_[r]) // can increment without going out of bounds in this dimension
109 {
110 index_[r]++;
111 // we've completed the increment
112 return r;
113 }
114 else
115 {
116 // next rank should be incremented -- this one should reset to 0
117 index_[r] = 0;
118 }
119 }
120 // if we get here, we have run through all ranks, setting them to 0 -- we've cycled around
121 // and in that sense have not completed the increment
122 return -1;
123 }
124
127 KOKKOS_INLINE_FUNCTION
129 {
130 const auto rank = view_.rank();
131 for (int r=rank-1; r>=0; r--)
132 {
133 if (index_[r]-1 >= 0) // can decrement without going out of bounds in this dimension
134 {
135 index_[r]--;
136 return true; // we've completed the decrement
137 }
138 else
139 {
140 // next rank should be decremented -- this one should cycle round to dim_[r]-1
141 index_[r] = dims_[r]-1;
142 }
143 }
144 // if we get here, we've gone past 0 in every dimension, so we should return false
145 // -- we have not completed the decrement in an in-bounds fashion, but have cycled round to the last value
146 // to maintain a clean state, let's reset
147 reset();
148 return false;
149 }
150
153 KOKKOS_INLINE_FUNCTION
155 {
156 int index_1D = 0;
157 for (int d=0; d<7; d++)
158 {
159 if (d>0) index_1D *= dims_[d-1];
160 index_1D += index_[d];
161 }
162
163 return index_1D;
164 }
165
168 KOKKOS_INLINE_FUNCTION
169 void setEnumerationIndex(const int &enumerationIndex)
170 {
171 Kokkos::Array<int,7> location;
172 int remainder = enumerationIndex;
173 for (int d=6; d>=0; d--)
174 {
175 location[d] = remainder % dims_[d];
176 remainder /= dims_[d];
177 }
178
179 setLocation(location);
180 }
181
185 KOKKOS_INLINE_FUNCTION
186 int getIndex(int dimension)
187 {
188 return index_[dimension];
189 }
190
194 KOKKOS_INLINE_FUNCTION
195 int getExtent(int dimension)
196 {
197 return dims_[dimension];
198 }
199
202 KOKKOS_INLINE_FUNCTION
203 void reset(unsigned from_rank_number=0)
204 {
205 for (unsigned d=from_rank_number; d<view_.rank(); d++)
206 {
207 index_[d] = 0;
208 }
209 }
210
213 KOKKOS_INLINE_FUNCTION
214 void setLocation(const Kokkos::Array<int,7> &location)
215 {
216 index_ = location;
217 }
218
221 KOKKOS_INLINE_FUNCTION
222 Kokkos::Array<int,7> & getLocation()
223 {
224 return index_;
225 }
226 };
227} // namespace Intrepid2
228
229#endif /* Intrepid2_ViewIterator_h */
Header function for Intrepid2::Util class and other utility functions.
enable_if_t< has_rank_method< Functor >::value, unsigned > KOKKOS_INLINE_FUNCTION getFunctorRank(const Functor &functor)
A helper class that allows iteration over some part of a Kokkos View, while allowing the calling code...
KOKKOS_INLINE_FUNCTION int nextIncrementRank()
KOKKOS_INLINE_FUNCTION int getExtent(int dimension)
KOKKOS_INLINE_FUNCTION Kokkos::Array< int, 7 > & getLocation()
KOKKOS_INLINE_FUNCTION void set(const ScalarType &value)
KOKKOS_INLINE_FUNCTION int increment()
KOKKOS_INLINE_FUNCTION ScalarType get()
KOKKOS_INLINE_FUNCTION void setLocation(const Kokkos::Array< int, 7 > &location)
KOKKOS_INLINE_FUNCTION bool decrement()
KOKKOS_INLINE_FUNCTION ViewIterator(ViewType view)
KOKKOS_INLINE_FUNCTION void reset(unsigned from_rank_number=0)
KOKKOS_INLINE_FUNCTION void setEnumerationIndex(const int &enumerationIndex)
KOKKOS_INLINE_FUNCTION int getIndex(int dimension)
KOKKOS_INLINE_FUNCTION int getEnumerationIndex()