15 #ifndef Intrepid2_TensorViewIterator_h
16 #define Intrepid2_TensorViewIterator_h
21 #include <Kokkos_Core.hpp>
39 template<
class TensorViewType,
class ViewType1,
class ViewType2 ,
typename ScalarType>
43 enum RankCombinationType :
int
49 using RankCombinationViewType = Kokkos::View<RankCombinationType*, typename TensorViewType::device_type>;
56 RankCombinationViewType rank_combination_types_;
77 KOKKOS_INLINE_FUNCTION
79 RankCombinationViewType rank_combination_types)
81 tensor_view_iterator_(tensor_view),
82 view1_iterator_(view1),
83 view2_iterator_(view2),
84 rank_combination_types_(rank_combination_types)
102 unsigned max_component_rank = (view1.rank() > view2.rank()) ? view1.rank() : view2.rank();
103 unsigned max_rank = (tensor_view.rank() > max_component_rank) ? tensor_view.rank() : max_component_rank;
107 unsigned expected_rank = 0;
108 bool contracting =
false;
109 for (
unsigned d=0; d<rank_combination_types.extent(0); d++)
111 if (rank_combination_types[d] == TENSOR_CONTRACTION)
122 if (rank_combination_types[d] == TENSOR_PRODUCT)
124 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");
128 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");
129 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");
143 KOKKOS_INLINE_FUNCTION
148 if (view2_next_increment_rank > view1_next_increment_rank)
return view2_next_increment_rank;
149 else return view1_next_increment_rank;
154 KOKKOS_INLINE_FUNCTION
169 if (view2_next_increment_rank > view1_next_increment_rank)
172 device_assert(rank_combination_types_[view2_next_increment_rank]==TENSOR_PRODUCT);
173 view1_iterator_.
reset(view2_next_increment_rank);
176 return view2_next_increment_rank;
180 int view1_rank_change = view1_iterator_.
increment();
181 if (view1_rank_change >= 0)
183 switch (rank_combination_types_[view1_rank_change])
185 case DIMENSION_MATCH:
193 case TENSOR_CONTRACTION:
198 return view1_rank_change;
204 KOKKOS_INLINE_FUNCTION
215 KOKKOS_INLINE_FUNCTION
216 void setLocation(Kokkos::Array<int,7> location1, Kokkos::Array<int,7> location2)
220 Kokkos::Array<int,7> tensor_location = location1;
221 for (
unsigned d=0; d<rank_combination_types_.extent(0); d++)
223 switch (rank_combination_types_[d])
227 tensor_location[d] = location2[d] * view1_iterator_.
getExtent(d) + location1[d];
229 case DIMENSION_MATCH:
232 case TENSOR_CONTRACTION:
233 tensor_location[d] = 0;
237 #ifdef HAVE_INTREPID2_DEBUG
239 for (
unsigned d=0; d<rank_combination_types_.extent(0); d++)
241 switch (rank_combination_types_[d])
246 case DIMENSION_MATCH:
247 case TENSOR_CONTRACTION:
248 device_assert(location1[d] == location2[d]);
252 device_assert(location1[d] < view1_iterator_.
getExtent(d));
253 device_assert(location2[d] < view2_iterator_.
getExtent(d));
254 device_assert(tensor_location[d] < tensor_view_iterator_.
getExtent(d));
257 tensor_view_iterator_.
setLocation(tensor_location);
262 KOKKOS_INLINE_FUNCTION
265 return view1_iterator_.
get();
270 KOKKOS_INLINE_FUNCTION
273 return view2_iterator_.
get();
278 KOKKOS_INLINE_FUNCTION
279 void set(ScalarType value)
281 tensor_view_iterator_.
set(value);
KOKKOS_INLINE_FUNCTION ScalarType getView1Entry()
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 TensorViewIterator(TensorViewType tensor_view, ViewType1 view1, ViewType2 view2, RankCombinationViewType rank_combination_types)
Constructor.
#define INTREPID2_TEST_FOR_EXCEPTION_DEVICE_SAFE(test, x, msg)
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.
Iterator allows linear traversal of (part of) a Kokkos View in a manner that is agnostic to its rank...
KOKKOS_INLINE_FUNCTION void set(const ScalarType &value)
KOKKOS_INLINE_FUNCTION void set(ScalarType value)
KOKKOS_INLINE_FUNCTION int increment()
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...