Intrepid2
Intrepid2_ViewIterator.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid2 Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 Kyungjoo Kim (kyukim@sandia.gov),
38 // Mauro Perego (mperego@sandia.gov), or
39 // Nate Roberts (nvrober@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
49 #ifndef Intrepid2_ViewIterator_h
50 #define Intrepid2_ViewIterator_h
51 
52 #include "Intrepid2_Utils.hpp"
53 
54 namespace Intrepid2
55 {
75  template<class ViewType,typename ScalarType>
77  {
78  ViewType view_;
79  Kokkos::Array<int,7> dims_; // 7 is the maximum rank of a Kokkos view
80  Kokkos::Array<int,7> index_;
81  public:
84  KOKKOS_INLINE_FUNCTION
85  ViewIterator(ViewType view)
86  :
87  view_(view)
88  {
89  for (unsigned d=0; d<getFunctorRank(view); d++)
90  {
91  dims_[d] = view.extent_int(d);
92  index_[d] = 0;
93  }
94  for (unsigned d=getFunctorRank(view); d<7; d++)
95  {
96  dims_[d] = 1;
97  index_[d] = 0;
98  }
99  }
100 
103  KOKKOS_INLINE_FUNCTION
104  ScalarType get()
105  {
106  return view_.access(index_[0],index_[1],index_[2],index_[3],index_[4],index_[5],index_[6]);
107  }
108 
111  KOKKOS_INLINE_FUNCTION
112  void set(const ScalarType &value)
113  {
114  view_.access(index_[0],index_[1],index_[2],index_[3],index_[4],index_[5],index_[6]) = value;
115  }
116 
119  KOKKOS_INLINE_FUNCTION
121  {
122  const int rank = getFunctorRank(view_);
123  for (int r=rank-1; r>=0; r--)
124  {
125  if (index_[r]+1 < dims_[r]) // can increment without going out of bounds in this dimension
126  {
127  return r;
128  }
129  }
130  // next increment will take us out of bounds
131  return -1;
132  }
133 
136  KOKKOS_INLINE_FUNCTION
137  int increment()
138  {
139  const int rank = getFunctorRank(view_);
140  for (int r=rank-1; r>=0; r--)
141  {
142  if (index_[r]+1 < dims_[r]) // can increment without going out of bounds in this dimension
143  {
144  index_[r]++;
145  // we've completed the increment
146  return r;
147  }
148  else
149  {
150  // next rank should be incremented -- this one should reset to 0
151  index_[r] = 0;
152  }
153  }
154  // if we get here, we have run through all ranks, setting them to 0 -- we've cycled around
155  // and in that sense have not completed the increment
156  return -1;
157  }
158 
161  KOKKOS_INLINE_FUNCTION
162  bool decrement()
163  {
164  const auto rank = view_.rank();
165  for (int r=rank-1; r>=0; r--)
166  {
167  if (index_[r]-1 >= 0) // can decrement without going out of bounds in this dimension
168  {
169  index_[r]--;
170  return true; // we've completed the decrement
171  }
172  else
173  {
174  // next rank should be decremented -- this one should cycle round to dim_[r]-1
175  index_[r] = dims_[r]-1;
176  }
177  }
178  // if we get here, we've gone past 0 in every dimension, so we should return false
179  // -- we have not completed the decrement in an in-bounds fashion, but have cycled round to the last value
180  // to maintain a clean state, let's reset
181  reset();
182  return false;
183  }
184 
187  KOKKOS_INLINE_FUNCTION
189  {
190  int index_1D = 0;
191  for (int d=0; d<7; d++)
192  {
193  if (d>0) index_1D *= dims_[d-1];
194  index_1D += index_[d];
195  }
196 
197  return index_1D;
198  }
199 
202  KOKKOS_INLINE_FUNCTION
203  void setEnumerationIndex(const int &enumerationIndex)
204  {
205  Kokkos::Array<int,7> location;
206  int remainder = enumerationIndex;
207  for (int d=6; d>=0; d--)
208  {
209  location[d] = remainder % dims_[d];
210  remainder /= dims_[d];
211  }
212 
213  setLocation(location);
214  }
215 
219  KOKKOS_INLINE_FUNCTION
220  int getIndex(int dimension)
221  {
222  return index_[dimension];
223  }
224 
228  KOKKOS_INLINE_FUNCTION
229  int getExtent(int dimension)
230  {
231  return dims_[dimension];
232  }
233 
236  KOKKOS_INLINE_FUNCTION
237  void reset(unsigned from_rank_number=0)
238  {
239  for (unsigned d=from_rank_number; d<view_.rank(); d++)
240  {
241  index_[d] = 0;
242  }
243  }
244 
247  KOKKOS_INLINE_FUNCTION
248  void setLocation(const Kokkos::Array<int,7> &location)
249  {
250  index_ = location;
251  }
252 
255  KOKKOS_INLINE_FUNCTION
256  Kokkos::Array<int,7> & getLocation()
257  {
258  return index_;
259  }
260  };
261 } // namespace Intrepid2
262 
263 #endif /* Intrepid2_ViewIterator_h */
KOKKOS_INLINE_FUNCTION void reset(unsigned from_rank_number=0)
KOKKOS_INLINE_FUNCTION int nextIncrementRank()
KOKKOS_INLINE_FUNCTION int getExtent(int dimension)
KOKKOS_INLINE_FUNCTION int getEnumerationIndex()
Header function for Intrepid2::Util class and other utility functions.
KOKKOS_INLINE_FUNCTION void set(const ScalarType &value)
KOKKOS_INLINE_FUNCTION bool decrement()
KOKKOS_INLINE_FUNCTION void setEnumerationIndex(const int &enumerationIndex)
KOKKOS_INLINE_FUNCTION int increment()
KOKKOS_INLINE_FUNCTION void setLocation(const Kokkos::Array< int, 7 > &location)
A helper class that allows iteration over some part of a Kokkos View, while allowing the calling code...
KOKKOS_INLINE_FUNCTION Kokkos::Array< int, 7 > & getLocation()
KOKKOS_INLINE_FUNCTION int getIndex(int dimension)
KOKKOS_INLINE_FUNCTION ViewIterator(ViewType view)