Intrepid2
Intrepid2_TensorArgumentIterator.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_TensorArgumentIterator_h
16#define Intrepid2_TensorArgumentIterator_h
17
19#include "Intrepid2_Types.hpp"
20
21namespace Intrepid2
22{
29 Kokkos::Array<ordinal_type,Parameters::MaxTensorComponents> arguments_;
30 Kokkos::Array<ordinal_type,Parameters::MaxTensorComponents> bounds_;
31 ordinal_type numTensorComponents_;
32 public:
33 template<class Scalar, typename ExecSpaceType>
34 KOKKOS_INLINE_FUNCTION
35 TensorArgumentIterator(const TensorData<Scalar,ExecSpaceType> &tensorData, const ordinal_type argumentOrdinal)
36 :
37 numTensorComponents_(tensorData.numTensorComponents())
38 {
39 for (ordinal_type r=0; r<numTensorComponents_; r++)
40 {
41 arguments_[r] = 0;
42 bounds_[r] = tensorData.getTensorComponent(r).extent_int(argumentOrdinal);
43 }
44 }
45
47 template<class Scalar, typename ExecSpaceType>
48 KOKKOS_INLINE_FUNCTION
49 TensorArgumentIterator(const TensorData<Scalar,ExecSpaceType> &tensorData, const ordinal_type argumentOrdinal, const ordinal_type numTensorComponents)
50 :
51 numTensorComponents_(numTensorComponents)
52 {
53 for (ordinal_type r=0; r<numTensorComponents_; r++)
54 {
55 arguments_[r] = 0;
56 bounds_[r] = tensorData.getTensorComponent(r).extent_int(argumentOrdinal);
57 }
58 }
59
61 TensorArgumentIterator(const std::vector<ordinal_type> tensorComponentBounds)
62 :
63 numTensorComponents_(tensorComponentBounds.size())
64 {
65 for (ordinal_type r=0; r<numTensorComponents_; r++)
66 {
67 arguments_[r] = 0;
68 bounds_[r] = tensorComponentBounds[r];
69 }
70 }
71
73 template<size_t rank>
74 KOKKOS_INLINE_FUNCTION
75 TensorArgumentIterator(const Kokkos::Array<ordinal_type,rank> &tensorComponentBounds)
76 :
77 numTensorComponents_(rank)
78 {
79 for (ordinal_type r=0; r<rank; r++)
80 {
81 arguments_[r] = 0;
82 bounds_[r] = tensorComponentBounds[r];
83 }
84 }
85
87 KOKKOS_INLINE_FUNCTION ordinal_type increment()
88 {
89 ordinal_type r = numTensorComponents_ - 1;
90 while (arguments_[r] + 1 >= bounds_[r])
91 {
92 arguments_[r] = 0; // reset
93 r--;
94 if (r < 0) break;
95 }
96 if (r >= 0) ++arguments_[r];
97 return r;
98 }
99
102 KOKKOS_INLINE_FUNCTION
103 ordinal_type nextIncrementResult() const
104 {
105 ordinal_type r = numTensorComponents_ - 1;
106 while (arguments_[r] + 1 >= bounds_[r])
107 {
108 r--;
109 if (r < 0) break;
110 }
111 return r;
112 }
113
116 KOKKOS_INLINE_FUNCTION const ordinal_type & argument(const ordinal_type &r) const
117 {
118 return arguments_[r];
119 }
120
122 KOKKOS_INLINE_FUNCTION ordinal_type enumerationIndex() const
123 {
124 // commented-out code belongs to implementation with rightmost argument as the fastest-moving. We may want to support this as an option.
125// ordinal_type i = 0;
126// for (ordinal_type r=0; r<numTensorComponents_-1; r++)
127// {
128// i += arguments_[r];
129// i *= bounds_[r+1];
130// }
131// i += arguments_[numTensorComponents_-1];
132// return i;
133
134 // TensorData's numbering has the leftmost argument as the fastest-moving
135 // We return that numbering here.
136 ordinal_type i = 0;
137 for (ordinal_type r=numTensorComponents_-1; r>0; r--)
138 {
139 i += arguments_[r];
140 i *= bounds_[r-1];
141 }
142 i += arguments_[0];
143 return i;
144 }
145
147 KOKKOS_INLINE_FUNCTION ordinal_type relativeEnumerationIndex(const ordinal_type &startingComponent) const
148 {
149 // commented-out code belongs to implementation with rightmost argument as the fastest-moving. We may want to support this as an option.
150// ordinal_type i = 0;
151// for (ordinal_type r=startingComponent; r<numTensorComponents_-1; r++)
152// {
153// i += arguments_[r];
154// i *= bounds_[r+1];
155// }
156// i += arguments_[numTensorComponents_-1];
157// return i;
158
159 // TensorData's numbering has the leftmost argument as the fastest-moving
160 // We return that numbering here.
161 ordinal_type i = 0;
162 for (ordinal_type r=numTensorComponents_-1; r>startingComponent; r--)
163 {
164 i += arguments_[r];
165 i *= bounds_[r-1];
166 }
167 i += arguments_[startingComponent];
168 return i;
169 }
170
172 KOKKOS_INLINE_FUNCTION ordinal_type relativeEnumerationSpan(const ordinal_type &startingComponent) const
173 {
174 ordinal_type i = 1;
175 for (ordinal_type r=startingComponent; r<numTensorComponents_; r++)
176 {
177 i *= bounds_[r];
178 }
179 return i;
180 }
181
184 KOKKOS_INLINE_FUNCTION
185 void reset(ordinal_type from_component_number=0)
186 {
187 for (ordinal_type r=from_component_number; r<numTensorComponents_; r++)
188 {
189 arguments_[r] = 0;
190 }
191 }
192
196 KOKKOS_INLINE_FUNCTION
197 void setArgumentForComponent(const ordinal_type &r, const ordinal_type &i)
198 {
199 arguments_[r] = i;
200 }
201
207 KOKKOS_INLINE_FUNCTION
208 void setEnumerationIndex(const ordinal_type &enumerationIndex)
209 {
210 ordinal_type remainder = enumerationIndex;
211 for (ordinal_type d=0; d<numTensorComponents_; d++)
212 {
213 arguments_[d] = remainder % bounds_[d];
214 remainder /= bounds_[d];
215 }
216 }
217
220 KOKKOS_INLINE_FUNCTION
221 void copyArguments(TensorArgumentIterator &otherArgumentIterator, const ordinal_type &r0_from, const ordinal_type &r0_to, const ordinal_type &numArguments)
222 {
223 for (ordinal_type i=0; i<numArguments; i++)
224 {
225 arguments_[r0_to + i] = otherArgumentIterator.argument(r0_from + i);
226 }
227 }
228
229 };
230} // namespace Intrepid2
231
232#endif /* Intrepid2_TensorArgumentIterator_h */
View-like interface to tensor data; tensor components are stored separately and multiplied together a...
Contains definitions of custom data types in Intrepid2.
KOKKOS_INLINE_FUNCTION int extent_int(const int &r) const
Returns the logical extent in the specified dimension.
Allows systematic enumeration of all entries in a TensorData object, tracking indices for each tensor...
KOKKOS_INLINE_FUNCTION ordinal_type relativeEnumerationSpan(const ordinal_type &startingComponent) const
total number of enumeration indices with arguments prior to the startingComponent fixed
KOKKOS_INLINE_FUNCTION ordinal_type relativeEnumerationIndex(const ordinal_type &startingComponent) const
Note: relativeEnumerationIndex() matches the ordering in TensorData. This is different from the order...
KOKKOS_INLINE_FUNCTION ordinal_type increment()
Proceed to next entry.
KOKKOS_INLINE_FUNCTION void setEnumerationIndex(const ordinal_type &enumerationIndex)
Sets the enumeration index; this refers to a 1D enumeration of the possible in-bound arguments.
KOKKOS_INLINE_FUNCTION void setArgumentForComponent(const ordinal_type &r, const ordinal_type &i)
TensorArgumentIterator(const std::vector< ordinal_type > tensorComponentBounds)
Basic constructor in which only the bounds of the tensor components are required.
KOKKOS_INLINE_FUNCTION void reset(ordinal_type from_component_number=0)
KOKKOS_INLINE_FUNCTION TensorArgumentIterator(const TensorData< Scalar, ExecSpaceType > &tensorData, const ordinal_type argumentOrdinal, const ordinal_type numTensorComponents)
Variant that allows truncation of the tensor components at the specified number of components.
KOKKOS_INLINE_FUNCTION void copyArguments(TensorArgumentIterator &otherArgumentIterator, const ordinal_type &r0_from, const ordinal_type &r0_to, const ordinal_type &numArguments)
Sets a subset of this iterator's component arguments to match the component arguments from otherArgum...
KOKKOS_INLINE_FUNCTION ordinal_type enumerationIndex() const
Note: enumerationIndex() matches the ordering in TensorData. This is different from the order in whic...
KOKKOS_INLINE_FUNCTION TensorArgumentIterator(const Kokkos::Array< ordinal_type, rank > &tensorComponentBounds)
Basic constructor in which only the bounds of the tensor components are required.
KOKKOS_INLINE_FUNCTION const ordinal_type & argument(const ordinal_type &r) const
KOKKOS_INLINE_FUNCTION ordinal_type nextIncrementResult() const
View-like interface to tensor data; tensor components are stored separately and multiplied together a...
KOKKOS_INLINE_FUNCTION const Data< Scalar, DeviceType > & getTensorComponent(const ordinal_type &r) const
Returns the requested tensor component.
KOKKOS_INLINE_FUNCTION ordinal_type numTensorComponents() const
Return the number of tensorial components.