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 namespace Intrepid2
53 {
73  template<class ViewType,typename ScalarType>
75  {
76  ViewType view_;
77  Kokkos::Array<int,7> dims_; // 7 is the maximum rank of a Kokkos view
78  Kokkos::Array<int,7> index_;
79  public:
82  KOKKOS_INLINE_FUNCTION
83  ViewIterator(ViewType view)
84  :
85  view_(view)
86  {
87  for (unsigned d=0; d<view.rank(); d++)
88  {
89  dims_[d] = view.extent_int(d);
90  index_[d] = 0;
91  }
92  for (unsigned d=view.rank(); d<7; d++)
93  {
94  dims_[d] = 1;
95  index_[d] = 0;
96  }
97  }
98 
101  KOKKOS_INLINE_FUNCTION
102  ScalarType get()
103  {
104  return view_.access(index_[0],index_[1],index_[2],index_[3],index_[4],index_[5],index_[6]);
105  }
106 
109  KOKKOS_INLINE_FUNCTION
110  void set(ScalarType &value)
111  {
112  view_.access(index_[0],index_[1],index_[2],index_[3],index_[4],index_[5],index_[6]) = value;
113  }
114 
117  KOKKOS_INLINE_FUNCTION
119  {
120  const auto rank = view_.rank();
121  for (int r=rank-1; r>=0; r--)
122  {
123  if (index_[r]+1 < dims_[r]) // can increment without going out of bounds in this dimension
124  {
125  return r;
126  }
127  }
128  // next increment will take us out of bounds
129  return -1;
130  }
131 
134  KOKKOS_INLINE_FUNCTION
135  int increment()
136  {
137  const auto rank = view_.rank();
138  for (int r=rank-1; r>=0; r--)
139  {
140  if (index_[r]+1 < dims_[r]) // can increment without going out of bounds in this dimension
141  {
142  index_[r]++;
143  // we've completed the increment
144  return r;
145  }
146  else
147  {
148  // next rank should be incremented -- this one should reset to 0
149  index_[r] = 0;
150  }
151  }
152  // if we get here, we have run through all ranks, setting them to 0 -- we've cycled around
153  // and in that sense have not completed the increment
154  return -1;
155  }
156 
159  KOKKOS_INLINE_FUNCTION
160  bool decrement()
161  {
162  const auto rank = view_.rank();
163  for (int r=rank-1; r>=0; r--)
164  {
165  if (index_[r]-1 >= 0) // can decrement without going out of bounds in this dimension
166  {
167  index_[r]--;
168  return true; // we've completed the decrement
169  }
170  else
171  {
172  // next rank should be decremented -- this one should cycle round to dim_[r]-1
173  index_[r] = dims_[r]-1;
174  }
175  }
176  // if we get here, we've gone past 0 in every dimension, so we should return false
177  // -- we have not completed the decrement in an in-bounds fashion, but have cycled round to the last value
178  // to maintain a clean state, let's reset
179  reset();
180  return false;
181  }
182 
185  KOKKOS_INLINE_FUNCTION
187  {
188  int index_1D = 0;
189  for (int d=0; d<7; d++)
190  {
191  if (d>0) index_1D *= dims_[d-1];
192  index_1D += index_[d];
193  }
194 
195  return index_1D;
196  }
197 
201  KOKKOS_INLINE_FUNCTION
202  int getIndex(int dimension)
203  {
204  return index_[dimension];
205  }
206 
210  KOKKOS_INLINE_FUNCTION
211  int getExtent(int dimension)
212  {
213  return dims_[dimension];
214  }
215 
218  KOKKOS_INLINE_FUNCTION
219  void reset(int from_rank_number=0)
220  {
221  for (unsigned d=from_rank_number; d<view_.rank(); d++)
222  {
223  index_[d] = 0;
224  }
225  }
226 
229  KOKKOS_INLINE_FUNCTION
230  void setLocation(const Kokkos::Array<int,7> &location)
231  {
232  index_ = location;
233  }
234 
237  KOKKOS_INLINE_FUNCTION
238  Kokkos::Array<int,7> & getLocation()
239  {
240  return index_;
241  }
242  };
243 } // namespace Intrepid2
244 
245 #endif /* Intrepid2_ViewIterator_h */
KOKKOS_INLINE_FUNCTION int nextIncrementRank()
KOKKOS_INLINE_FUNCTION int getExtent(int dimension)
KOKKOS_INLINE_FUNCTION int getEnumerationIndex()
KOKKOS_INLINE_FUNCTION void reset(int from_rank_number=0)
KOKKOS_INLINE_FUNCTION void set(ScalarType &value)
KOKKOS_INLINE_FUNCTION bool decrement()
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)