Intrepid2
Intrepid2_TensorViewIterator.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_TensorViewIterator_h
50 #define Intrepid2_TensorViewIterator_h
51 
54 
55 #include "Kokkos_Vector.hpp"
56 #include <vector>
57 
58 namespace Intrepid2
59 {
72  template<class TensorViewType, class ViewType1, class ViewType2 ,typename ScalarType>
74  {
75  public:
76  enum RankCombinationType
77  {
78  DIMENSION_MATCH,
79  TENSOR_PRODUCT,
80  TENSOR_CONTRACTION
81  };
82  protected:
83 
84  ViewIterator<TensorViewType, ScalarType> tensor_view_iterator_;
87 
88  Kokkos::vector<RankCombinationType> rank_combination_types_;
89  public:
109  KOKKOS_INLINE_FUNCTION
110  TensorViewIterator(TensorViewType tensor_view, ViewType1 view1, ViewType2 view2,
111  Kokkos::vector<RankCombinationType> rank_combination_types)
112  :
113  tensor_view_iterator_(tensor_view),
114  view1_iterator_(view1),
115  view2_iterator_(view2),
116  rank_combination_types_(rank_combination_types)
117  {
118  // rank_combination_type should have length equal to the maximum rank of the views provided
119  /*
120  Examples:
121  1. vector dot product in third dimension: {DIMENSION_MATCH, DIMENSION_MATCH, TENSOR_CONTRACTION}
122  - view1 and view2 should both be rank 3, and should match in all dimensions
123  - tensor_view should be rank 2, and should match view1 and view2 in first two dimensions
124  2. vector outer product in third dimension: {DIMENSION_MATCH, DIMENSION_MATCH, TENSOR_PRODUCT}
125  - view1 and view2 should both be rank 3, and should match in first two dimensions
126  - tensor_view should be rank 3, and should match view1 and view2 in first two dimensions
127  - in third dimension, tensor_view should have dimension equal to the product of the third dimension of view1 and the third dimension of view2
128  3. rank-3 view1 treated as vector times scalar rank-2 view2: {DIMENSION_MATCH, DIMENSION_MATCH, TENSOR_PRODUCT}
129  - here, the rank-2 view2 is interpreted as having an extent 1 third dimension
130 
131  We only allow TENSOR_CONTRACTION in final dimension(s)
132  */
133  // check that the above rules are satisfied:
134  unsigned max_component_rank = (view1.rank() > view2.rank()) ? view1.rank() : view2.rank();
135  unsigned max_rank = (tensor_view.rank() > max_component_rank) ? tensor_view.rank() : max_component_rank;
136 
137  INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(rank_combination_types.extent(0) != max_rank, std::invalid_argument, "need to provide RankCombinationType for the largest-rank View");
138 
139  unsigned expected_rank = 0;
140  bool contracting = false;
141  for (unsigned d=0; d<rank_combination_types.extent(0); d++)
142  {
143  if (rank_combination_types[d] == TENSOR_CONTRACTION)
144  {
145  // check that view1 and view2 agree on the length of this dimension
146  INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(view1.extent_int(d) != view2.extent_int(d), std::invalid_argument, "Contractions can only occur along ranks of equal length");
147  contracting = true;
148  }
149  else
150  {
151  INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(contracting, std::invalid_argument, "encountered a non-contraction rank combination after a contraction; contractions can only go at the end");
152  expected_rank++;
153  if (rank_combination_types[d] == TENSOR_PRODUCT)
154  {
155  INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(tensor_view.extent_int(d) != view1.extent_int(d) * view2.extent_int(d), std::invalid_argument, "For TENSOR_PRODUCT rank combination, the tensor View must have length in that dimension equal to the product of the two component views in that dimension");
156  }
157  else // matching
158  {
159  INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(view1.extent_int(d) != view2.extent_int(d), std::invalid_argument, "For DIMENSION_MATCH rank combination, all three views must have length equal to each other in that rank");
160  INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(tensor_view.extent_int(d) != view1.extent_int(d), std::invalid_argument, "For DIMENSION_MATCH rank combination, all three views must have length equal to each other in that rank");
161  }
162  }
163  }
164  INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(expected_rank != tensor_view.rank(), std::invalid_argument, "Tensor view does not match expected rank");
165  }
166 
169  KOKKOS_INLINE_FUNCTION
171  {
172  int view2_next_increment_rank = view2_iterator_.nextIncrementRank();
173  int view1_next_increment_rank = view1_iterator_.nextIncrementRank();
174  if (view2_next_increment_rank > view1_next_increment_rank) return view2_next_increment_rank;
175  else return view1_next_increment_rank;
176  }
177 
180  KOKKOS_INLINE_FUNCTION
181  int increment()
182  {
183  // proceed to the next view1/view2 combination
184  // where we're doing a dimension match, then all three iterators should increment in tandem
185  // where we're doing a contraction, view1/view2 should increment in tandem, while tensor_view should be fixed
186  // where we're doing a tensor product, view1 and tensor_view increment in tandem, while view2 is fixed
187 
188  // note that regardless of the choice, view1 should be incremented, with one exception:
189  // If we are doing a tensor product, then view1 can be understood to be in an interior for loop, and it should loop around.
190  // We can detect this by checking which the least rank that would be updated -- if view2's least rank exceeds view1's, then:
191  // - view1 should be reset, AND
192  // - view2 should be incremented (as should the tensor view)
193  int view2_next_increment_rank = view2_iterator_.nextIncrementRank();
194  int view1_next_increment_rank = view1_iterator_.nextIncrementRank();
195  if (view2_next_increment_rank > view1_next_increment_rank)
196  {
197  // if we get here, we should be doing a tensor product in the view2 rank that will change
198  device_assert(rank_combination_types_[view2_next_increment_rank]==TENSOR_PRODUCT);
199  view1_iterator_.reset(view2_next_increment_rank); // set to 0 from the tensor product rank inward -- this is "looping around"
200  view2_iterator_.increment();
201  tensor_view_iterator_.increment();
202  return view2_next_increment_rank;
203  }
204  else
205  {
206  int view1_rank_change = view1_iterator_.increment();
207  if (view1_rank_change >= 0)
208  {
209  switch (rank_combination_types_[view1_rank_change])
210  {
211  case DIMENSION_MATCH:
212  view2_iterator_.increment();
213  tensor_view_iterator_.increment();
214  break;
215  case TENSOR_PRODUCT:
216  // view1 increments fastest; the only time we increment view2 is when view1 loops around; we handle that above
217  tensor_view_iterator_.increment();
218  break;
219  case TENSOR_CONTRACTION:
220  // view1 and view2 increment in tandem; we don't increment tensor_view while contraction is taking place
221  view2_iterator_.increment();
222  }
223  }
224  return view1_rank_change;
225  }
226  }
227 
230  KOKKOS_INLINE_FUNCTION
231  void setLocation(const Kokkos::Array<int,7> location)
232  {
233  view1_iterator_.setLocation(location);
234  view2_iterator_.setLocation(location);
235  tensor_view_iterator_.setLocation(location);
236  }
237 
241  KOKKOS_INLINE_FUNCTION
242  void setLocation(Kokkos::Array<int,7> location1, Kokkos::Array<int,7> location2)
243  {
244  view1_iterator_.setLocation(location1);
245  view2_iterator_.setLocation(location2);
246  Kokkos::Array<int,7> tensor_location = location1;
247  for (unsigned d=0; d<rank_combination_types_.extent(0); d++)
248  {
249  switch (rank_combination_types_[d])
250  {
251  case TENSOR_PRODUCT:
252  // view1 index is fastest-moving:
253  tensor_location[d] = location2[d] * view1_iterator_.getExtent(d) + location1[d];
254  break;
255  case DIMENSION_MATCH:
256  // we copied location1 into tensor_location to initialize -- that's correct in this dimension
257  break;
258  case TENSOR_CONTRACTION:
259  tensor_location[d] = 0;
260  break;
261  }
262  }
263 #ifdef HAVE_INTREPID2_DEBUG
264  // check that the location makes sense
265  for (unsigned d=0; d<rank_combination_types_.extent(0); d++)
266  {
267  switch (rank_combination_types_[d])
268  {
269  case TENSOR_PRODUCT:
270  // in this case, the two indices are independent
271  break;
272  case DIMENSION_MATCH:
273  case TENSOR_CONTRACTION:
274  device_assert(location1[d] == location2[d]);
275  break;
276  }
277  // let's check that the indices are in bounds:
278  device_assert(location1[d] < view1_iterator_.getExtent(d));
279  device_assert(location2[d] < view2_iterator_.getExtent(d));
280  device_assert(tensor_location[d] < tensor_view_iterator_.getExtent(d));
281  }
282 #endif
283  tensor_view_iterator_.setLocation(tensor_location);
284  }
285 
288  KOKKOS_INLINE_FUNCTION
289  ScalarType getView1Entry()
290  {
291  return view1_iterator_.get();
292  }
293 
296  KOKKOS_INLINE_FUNCTION
297  ScalarType getView2Entry()
298  {
299  return view2_iterator_.get();
300  }
301 
304  KOKKOS_INLINE_FUNCTION
305  void set(ScalarType value)
306  {
307  tensor_view_iterator_.set(value);
308  }
309  };
310 
311 } // namespace Intrepid2
312 
313 #endif /* Intrepid2_TensorViewIterator_h */
KOKKOS_INLINE_FUNCTION ScalarType getView1Entry()
KOKKOS_INLINE_FUNCTION int nextIncrementRank()
KOKKOS_INLINE_FUNCTION int getExtent(int dimension)
KOKKOS_INLINE_FUNCTION TensorViewIterator(TensorViewType tensor_view, ViewType1 view1, ViewType2 view2, Kokkos::vector< RankCombinationType > rank_combination_types)
Constructor.
KOKKOS_INLINE_FUNCTION ScalarType get()
KOKKOS_INLINE_FUNCTION void setLocation(Kokkos::Array< int, 7 > location1, Kokkos::Array< int, 7 > location2)
Implementation of an assert that can safely be called from device code.
KOKKOS_INLINE_FUNCTION void reset(int from_rank_number=0)
KOKKOS_INLINE_FUNCTION void set(ScalarType &value)
Iterator allows linear traversal of (part of) a Kokkos View in a manner that is agnostic to its rank...
KOKKOS_INLINE_FUNCTION void set(ScalarType value)
KOKKOS_INLINE_FUNCTION void setLocation(const Kokkos::Array< int, 7 > location)
KOKKOS_INLINE_FUNCTION ScalarType getView2Entry()
KOKKOS_INLINE_FUNCTION int increment()
KOKKOS_INLINE_FUNCTION void setLocation(const Kokkos::Array< int, 7 > &location)
KOKKOS_INLINE_FUNCTION int nextIncrementRank()
A helper class that allows iteration over three Kokkos Views simultaneously, according to tensor comb...