cxx11_tensor_morphing.cpp File Reference
#include "main.h"
#include <Eigen/CXX11/Tensor>

Macros

#define CALL_SUBTEST_PART(PART)   CALL_SUBTEST_##PART
 
#define CALL_SUBTESTS_TYPES_LAYOUTS(PART, NAME)
 

Functions

template<typename >
static void test_simple_reshape ()
 
template<typename >
static void test_static_reshape ()
 
template<typename >
static void test_reshape_in_expr ()
 
template<typename >
static void test_reshape_as_lvalue ()
 
template<typename T , int DataLayout>
static void test_simple_slice ()
 
template<typename T >
static void test_const_slice ()
 
template<typename T , int DataLayout>
static void test_slice_in_expr ()
 
template<typename T , int DataLayout>
static void test_slice_as_lvalue ()
 
template<typename T , int DataLayout>
static void test_slice_raw_data ()
 
template<typename T , int DataLayout>
static void test_strided_slice ()
 
template<typename T , int DataLayout>
static void test_strided_slice_write ()
 
template<typename T , int DataLayout>
static void test_composition ()
 
template<typename T , int DataLayout>
static void test_empty_slice ()
 
 EIGEN_DECLARE_TEST (cxx11_tensor_morphing)
 

Macro Definition Documentation

◆ CALL_SUBTEST_PART

#define CALL_SUBTEST_PART (   PART)    CALL_SUBTEST_##PART

◆ CALL_SUBTESTS_TYPES_LAYOUTS

#define CALL_SUBTESTS_TYPES_LAYOUTS (   PART,
  NAME 
)
Value:
CALL_SUBTEST_PART(PART)((NAME<float, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<float, RowMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, ColMajor>())); \
CALL_SUBTEST_PART(PART)((NAME<bool, RowMajor>()))
#define CALL_SUBTEST_PART(PART)
Definition: cxx11_tensor_morphing.cpp:530

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_tensor_morphing  )
538  {
539  CALL_SUBTEST_1(test_simple_reshape<void>());
540  CALL_SUBTEST_1(test_static_reshape<void>());
541  CALL_SUBTEST_1(test_reshape_as_lvalue<void>());
542  CALL_SUBTEST_1(test_reshape_in_expr<void>());
543  CALL_SUBTEST_1(test_const_slice<float>());
544 
551 }
static void test_composition()
Definition: cxx11_tensor_morphing.cpp:454
static void test_slice_raw_data()
Definition: cxx11_tensor_morphing.cpp:243
static void test_simple_slice()
Definition: cxx11_tensor_morphing.cpp:112
static void test_strided_slice()
Definition: cxx11_tensor_morphing.cpp:335
#define CALL_SUBTESTS_TYPES_LAYOUTS(PART, NAME)
Definition: cxx11_tensor_morphing.cpp:532
static void test_slice_as_lvalue()
Definition: cxx11_tensor_morphing.cpp:184
static void test_strided_slice_write()
Definition: cxx11_tensor_morphing.cpp:428
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4

References CALL_SUBTEST_1, CALL_SUBTESTS_TYPES_LAYOUTS, test_composition(), test_simple_slice(), test_slice_as_lvalue(), test_slice_raw_data(), test_strided_slice(), and test_strided_slice_write().

◆ test_composition()

template<typename T , int DataLayout>
static void test_composition ( )
static
454  {
456  matrix.setRandom();
457 
458  const DSizes<ptrdiff_t, 3> newDims(1, 1, 11);
460  matrix.slice(DSizes<ptrdiff_t, 2>(2, 0), DSizes<ptrdiff_t, 2>(1, 11)).reshape(newDims);
461 
462  VERIFY_IS_EQUAL(tensor.dimensions().TotalSize(), 11);
463  VERIFY_IS_EQUAL(tensor.dimension(0), 1);
464  VERIFY_IS_EQUAL(tensor.dimension(1), 1);
465  VERIFY_IS_EQUAL(tensor.dimension(2), 11);
466  for (int i = 0; i < 11; ++i) {
467  VERIFY_IS_EQUAL(tensor(0, 0, i), matrix(2, i));
468  }
469 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
The tensor class.
Definition: Tensor.h:68
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: Tensor.h:100
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:99
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
Definition: TensorDimensions.h:161
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex TotalSize() const
Definition: TensorDimensions.h:167

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimension(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimensions(), i, matrix(), Eigen::DSizes< DenseIndex, NumDims >::TotalSize(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_const_slice()

template<typename T >
static void test_const_slice ( )
static
136  {
137  const T b[1] = {42};
139  DSizes<DenseIndex, 1> offsets;
140  offsets[0] = 0;
141  TensorRef<Tensor<const T, 1>> slice_ref(m.slice(offsets, m.dimensions()));
142  VERIFY_IS_EQUAL(slice_ref(0), 42);
143 }
Scalar * b
Definition: benchVecAdd.cpp:17
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:33
A reference to a tensor expression The expression will be evaluated lazily (as much as possible).
Definition: TensorRef.h:114
int * m
Definition: level2_cplx_impl.h:294

References b, m, and VERIFY_IS_EQUAL.

◆ test_empty_slice()

template<typename T , int DataLayout>
static void test_empty_slice ( )
static
472  {
473  Tensor<T, 3, DataLayout> tensor(2, 3, 5);
474  tensor.setRandom();
476 
477  // empty size in first dimension
478  Eigen::DSizes<ptrdiff_t, 3> indices1(1, 2, 3);
479  Eigen::DSizes<ptrdiff_t, 3> sizes1(0, 1, 2);
480  Tensor<T, 3, DataLayout> slice1(0, 1, 2);
481  slice1.setRandom();
482  tensor.slice(indices1, sizes1) = slice1;
483 
484  // empty size in second dimension
485  Eigen::DSizes<ptrdiff_t, 3> indices2(1, 2, 3);
486  Eigen::DSizes<ptrdiff_t, 3> sizes2(1, 0, 2);
487  Tensor<T, 3, DataLayout> slice2(1, 0, 2);
488  slice2.setRandom();
489  tensor.slice(indices2, sizes2) = slice2;
490 
491  // empty size in third dimension
492  Eigen::DSizes<ptrdiff_t, 3> indices3(1, 2, 3);
493  Eigen::DSizes<ptrdiff_t, 3> sizes3(1, 1, 0);
494  Tensor<T, 3, DataLayout> slice3(1, 1, 0);
495  slice3.setRandom();
496  tensor.slice(indices3, sizes3) = slice3;
497 
498  // empty size in first and second dimension
499  Eigen::DSizes<ptrdiff_t, 3> indices4(1, 2, 3);
500  Eigen::DSizes<ptrdiff_t, 3> sizes4(0, 0, 2);
501  Tensor<T, 3, DataLayout> slice4(0, 0, 2);
502  slice4.setRandom();
503  tensor.slice(indices4, sizes4) = slice4;
504 
505  // empty size in second and third dimension
506  Eigen::DSizes<ptrdiff_t, 3> indices5(1, 2, 3);
507  Eigen::DSizes<ptrdiff_t, 3> sizes5(1, 0, 0);
508  Tensor<T, 3, DataLayout> slice5(1, 0, 0);
509  slice5.setRandom();
510  tensor.slice(indices5, sizes5) = slice5;
511 
512  // empty size in all dimensions
513  Eigen::DSizes<ptrdiff_t, 3> indices6(1, 2, 3);
514  Eigen::DSizes<ptrdiff_t, 3> sizes6(0, 0, 0);
515  Tensor<T, 3, DataLayout> slice6(0, 0, 0);
516  slice6.setRandom();
517  tensor.slice(indices6, sizes6) = slice6;
518 
519  // none of these operations should change the tensor's components
520  // because all of the rvalue slices have at least one zero dimension
521  for (int i = 0; i < 2; ++i) {
522  for (int j = 0; j < 3; ++j) {
523  for (int k = 0; k < 5; ++k) {
524  VERIFY_IS_EQUAL(tensor(i, j, k), copy(i, j, k));
525  }
526  }
527  }
528 }
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
char char char int int * k
Definition: level2_impl.h:374
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References copy(), i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::TensorBase< Derived, AccessLevel >::slice(), and VERIFY_IS_EQUAL.

◆ test_reshape_as_lvalue()

template<typename >
static void test_reshape_as_lvalue ( )
static
89  {
90  Tensor<float, 3> tensor(2, 3, 7);
91  tensor.setRandom();
92 
93  Tensor<float, 2> tensor2d(6, 7);
94  Tensor<float, 3>::Dimensions dim(2, 3, 7);
95  tensor2d.reshape(dim) = tensor;
96 
97  float scratch[2 * 3 * 1 * 7 * 1];
98  TensorMap<Tensor<float, 5>> tensor5d(scratch, 2, 3, 1, 7, 1);
99  tensor5d.reshape(dim).device(Eigen::DefaultDevice()) = tensor;
100 
101  for (int i = 0; i < 2; ++i) {
102  for (int j = 0; j < 3; ++j) {
103  for (int k = 0; k < 7; ++k) {
104  VERIFY_IS_EQUAL(tensor2d(i + 2 * j, k), tensor(i, j, k));
105  VERIFY_IS_EQUAL(tensor5d(i, j, 0, k, 0), tensor(i, j, k));
106  }
107  }
108  }
109 }
Definition: TensorDeviceDefault.h:19

References i, j, k, Eigen::TensorBase< Derived, AccessLevel >::reshape(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

◆ test_reshape_in_expr()

template<typename >
static void test_reshape_in_expr ( )
static
64  {
65  MatrixXf m1(2, 3 * 5 * 7 * 11);
66  MatrixXf m2(3 * 5 * 7 * 11, 13);
67  m1.setRandom();
68  m2.setRandom();
69  MatrixXf m3 = m1 * m2;
70 
71  TensorMap<Tensor<float, 5>> tensor1(m1.data(), 2, 3, 5, 7, 11);
72  TensorMap<Tensor<float, 5>> tensor2(m2.data(), 3, 5, 7, 11, 13);
73  Tensor<float, 2>::Dimensions newDims1(2, 3 * 5 * 7 * 11);
74  Tensor<float, 2>::Dimensions newDims2(3 * 5 * 7 * 11, 13);
76  array<DimPair, 1> contract_along{{DimPair(1, 0)}};
77  Tensor<float, 2> tensor3(2, 13);
78  tensor3 = tensor1.reshape(newDims1).contract(tensor2.reshape(newDims2), contract_along);
79 
80  Map<MatrixXf> res(tensor3.data(), 2, 13);
81  for (int i = 0; i < 2; ++i) {
82  for (int j = 0; j < 13; ++j) {
83  VERIFY_IS_APPROX(res(i, j), m3(i, j));
84  }
85  }
86 }
Matrix3d m1
Definition: IOFormat.cpp:2
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
MatrixType m2(n_dims)
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
Tensor< float, 1 >::DimensionPair DimPair
Definition: cxx11_tensor_contraction.cpp:17
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
std::array< T, N > array
Definition: EmulateArray.h:231

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), i, j, m1, m2(), res, Eigen::TensorBase< Derived, AccessLevel >::reshape(), and VERIFY_IS_APPROX.

◆ test_simple_reshape()

template<typename >
static void test_simple_reshape ( )
static
17  {
18  Tensor<float, 5> tensor1(2, 3, 1, 7, 1);
19  tensor1.setRandom();
20 
21  Tensor<float, 3> tensor2(2, 3, 7);
22  Tensor<float, 2> tensor3(6, 7);
23  Tensor<float, 2> tensor4(2, 21);
24 
25  Tensor<float, 3>::Dimensions dim1(2, 3, 7);
26  tensor2 = tensor1.reshape(dim1);
28  tensor3 = tensor1.reshape(dim2);
29  Tensor<float, 2>::Dimensions dim3(2, 21);
30  tensor4 = tensor1.reshape(dim1).reshape(dim3);
31 
32  for (int i = 0; i < 2; ++i) {
33  for (int j = 0; j < 3; ++j) {
34  for (int k = 0; k < 7; ++k) {
35  VERIFY_IS_EQUAL(tensor1(i, j, 0, k, 0), tensor2(i, j, k));
36  VERIFY_IS_EQUAL(tensor1(i, j, 0, k, 0), tensor3(i + 2 * j, k));
37  VERIFY_IS_EQUAL(tensor1(i, j, 0, k, 0), tensor4(i, j + 3 * k));
38  }
39  }
40  }
41 }

References i, j, k, Eigen::TensorBase< Derived, AccessLevel >::reshape(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

◆ test_simple_slice()

template<typename T , int DataLayout>
static void test_simple_slice ( )
static
112  {
113  Tensor<T, 5, DataLayout> tensor(2, 3, 5, 7, 11);
114  tensor.setRandom();
115 
116  Tensor<T, 5, DataLayout> slice1(1, 1, 1, 1, 1);
117  Eigen::DSizes<ptrdiff_t, 5> indices(1, 2, 3, 4, 5);
118  Eigen::DSizes<ptrdiff_t, 5> sizes(1, 1, 1, 1, 1);
119  slice1 = tensor.slice(indices, sizes);
120  VERIFY_IS_EQUAL(slice1(0, 0, 0, 0, 0), tensor(1, 2, 3, 4, 5));
121 
122  Tensor<T, 5, DataLayout> slice2(1, 1, 2, 2, 3);
123  Eigen::DSizes<ptrdiff_t, 5> indices2(1, 1, 3, 4, 5);
124  Eigen::DSizes<ptrdiff_t, 5> sizes2(1, 1, 2, 2, 3);
125  slice2 = tensor.slice(indices2, sizes2);
126  for (int i = 0; i < 2; ++i) {
127  for (int j = 0; j < 2; ++j) {
128  for (int k = 0; k < 3; ++k) {
129  VERIFY_IS_EQUAL(slice2(0, 0, i, j, k), tensor(1, 1, 3 + i, 4 + j, 5 + k));
130  }
131  }
132  }
133 }
std::vector< Array2i > sizes
Definition: dense_solvers.cpp:12

References i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), sizes, Eigen::TensorBase< Derived, AccessLevel >::slice(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_slice_as_lvalue()

template<typename T , int DataLayout>
static void test_slice_as_lvalue ( )
static
184  {
185  Tensor<T, 3, DataLayout> tensor1(2, 2, 7);
186  tensor1.setRandom();
187  Tensor<T, 3, DataLayout> tensor2(2, 2, 7);
188  tensor2.setRandom();
189  Tensor<T, 3, DataLayout> tensor3(4, 3, 5);
190  tensor3.setRandom();
191  Tensor<T, 3, DataLayout> tensor4(4, 3, 2);
192  tensor4.setRandom();
193  Tensor<T, 3, DataLayout> tensor5(10, 13, 12);
194  tensor5.setRandom();
195 
196  Tensor<T, 3, DataLayout> result(4, 5, 7);
197  Eigen::DSizes<ptrdiff_t, 3> sizes12(2, 2, 7);
198  Eigen::DSizes<ptrdiff_t, 3> first_slice(0, 0, 0);
199  result.slice(first_slice, sizes12) = tensor1;
200  Eigen::DSizes<ptrdiff_t, 3> second_slice(2, 0, 0);
201  result.slice(second_slice, sizes12).device(Eigen::DefaultDevice()) = tensor2;
202 
203  Eigen::DSizes<ptrdiff_t, 3> sizes3(4, 3, 5);
204  Eigen::DSizes<ptrdiff_t, 3> third_slice(0, 2, 0);
205  result.slice(third_slice, sizes3) = tensor3;
206 
207  Eigen::DSizes<ptrdiff_t, 3> sizes4(4, 3, 2);
208  Eigen::DSizes<ptrdiff_t, 3> fourth_slice(0, 2, 5);
209  result.slice(fourth_slice, sizes4) = tensor4;
210 
211  for (int j = 0; j < 2; ++j) {
212  for (int k = 0; k < 7; ++k) {
213  for (int i = 0; i < 2; ++i) {
214  VERIFY_IS_EQUAL(result(i, j, k), tensor1(i, j, k));
215  VERIFY_IS_EQUAL(result(i + 2, j, k), tensor2(i, j, k));
216  }
217  }
218  }
219  for (int i = 0; i < 4; ++i) {
220  for (int j = 2; j < 5; ++j) {
221  for (int k = 0; k < 5; ++k) {
222  VERIFY_IS_EQUAL(result(i, j, k), tensor3(i, j - 2, k));
223  }
224  for (int k = 5; k < 7; ++k) {
225  VERIFY_IS_EQUAL(result(i, j, k), tensor4(i, j - 2, k - 5));
226  }
227  }
228  }
229 
230  Eigen::DSizes<ptrdiff_t, 3> sizes5(4, 5, 7);
231  Eigen::DSizes<ptrdiff_t, 3> fifth_slice(0, 0, 0);
232  result.slice(fifth_slice, sizes5) = tensor5.slice(fifth_slice, sizes5);
233  for (int i = 0; i < 4; ++i) {
234  for (int j = 2; j < 5; ++j) {
235  for (int k = 0; k < 7; ++k) {
236  VERIFY_IS_EQUAL(result(i, j, k), tensor5(i, j, k));
237  }
238  }
239  }
240 }

References i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::TensorBase< Derived, AccessLevel >::slice(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_slice_in_expr()

template<typename T , int DataLayout>
static void test_slice_in_expr ( )
static
146  {
148  Mtx m1(7, 7);
149  Mtx m2(3, 3);
150  m1.setRandom();
151  m2.setRandom();
152 
153  Mtx m3 = m1.block(1, 2, 3, 3) * m2.block(0, 2, 3, 1);
154 
155  TensorMap<Tensor<T, 2, DataLayout>> tensor1(m1.data(), 7, 7);
156  TensorMap<Tensor<T, 2, DataLayout>> tensor2(m2.data(), 3, 3);
157  Tensor<T, 2, DataLayout> tensor3(3, 1);
158  typedef typename Tensor<T, 1>::DimensionPair DimPair;
159  array<DimPair, 1> contract_along{{DimPair(1, 0)}};
160 
161  Eigen::DSizes<ptrdiff_t, 2> indices1(1, 2);
162  Eigen::DSizes<ptrdiff_t, 2> sizes1(3, 3);
163  Eigen::DSizes<ptrdiff_t, 2> indices2(0, 2);
164  Eigen::DSizes<ptrdiff_t, 2> sizes2(3, 1);
165  tensor3 = tensor1.slice(indices1, sizes1).contract(tensor2.slice(indices2, sizes2), contract_along);
166 
167  Map<Mtx> res(tensor3.data(), 3, 1);
168  for (int i = 0; i < 3; ++i) {
169  for (int j = 0; j < 1; ++j) {
170  VERIFY_IS_APPROX(res(i, j), m3(i, j));
171  }
172  }
173 
174  // Take an arbitrary slice of an arbitrarily sized tensor.
175  TensorMap<Tensor<const T, 2, DataLayout>> tensor4(m1.data(), 7, 7);
176  Tensor<T, 1, DataLayout> tensor6 =
177  tensor4.reshape(DSizes<ptrdiff_t, 1>(7 * 7)).exp().slice(DSizes<ptrdiff_t, 1>(0), DSizes<ptrdiff_t, 1>(35));
178  for (int i = 0; i < 35; ++i) {
179  VERIFY_IS_APPROX(tensor6(i), expf(tensor4.data()[i]));
180  }
181 }
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorReshapingOp< const NewDimensions, const Derived > reshape(const NewDimensions &newDimensions) const
Definition: TensorBase.h:1106

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorMap< PlainObjectType, Options_, MakePointer_ >::data(), i, j, m1, m2(), res, Eigen::TensorBase< Derived, AccessLevel >::reshape(), Eigen::TensorBase< Derived, AccessLevel >::slice(), and VERIFY_IS_APPROX.

◆ test_slice_raw_data()

template<typename T , int DataLayout>
static void test_slice_raw_data ( )
static
243  {
244  Tensor<T, 4, DataLayout> tensor(3, 5, 7, 11);
245  tensor.setRandom();
246 
247  Eigen::DSizes<ptrdiff_t, 4> offsets(1, 2, 3, 4);
248  Eigen::DSizes<ptrdiff_t, 4> extents(1, 1, 1, 1);
249  typedef TensorEvaluator<decltype(tensor.slice(offsets, extents)), DefaultDevice> SliceEvaluator;
250  auto slice1 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
251  VERIFY_IS_EQUAL(slice1.dimensions().TotalSize(), 1);
252  VERIFY_IS_EQUAL(slice1.data()[0], tensor(1, 2, 3, 4));
253 
254  if (DataLayout == ColMajor) {
255  extents = Eigen::DSizes<ptrdiff_t, 4>(2, 1, 1, 1);
256  auto slice2 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
257  VERIFY_IS_EQUAL(slice2.dimensions().TotalSize(), 2);
258  VERIFY_IS_EQUAL(slice2.data()[0], tensor(1, 2, 3, 4));
259  VERIFY_IS_EQUAL(slice2.data()[1], tensor(2, 2, 3, 4));
260  } else {
261  extents = Eigen::DSizes<ptrdiff_t, 4>(1, 1, 1, 2);
262  auto slice2 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
263  VERIFY_IS_EQUAL(slice2.dimensions().TotalSize(), 2);
264  VERIFY_IS_EQUAL(slice2.data()[0], tensor(1, 2, 3, 4));
265  VERIFY_IS_EQUAL(slice2.data()[1], tensor(1, 2, 3, 5));
266  }
267 
268  extents = Eigen::DSizes<ptrdiff_t, 4>(1, 2, 1, 1);
269  auto slice3 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
270  VERIFY_IS_EQUAL(slice3.dimensions().TotalSize(), 2);
271  VERIFY_IS_EQUAL(slice3.data(), static_cast<T*>(0));
272 
273  if (DataLayout == ColMajor) {
274  offsets = Eigen::DSizes<ptrdiff_t, 4>(0, 2, 3, 4);
275  extents = Eigen::DSizes<ptrdiff_t, 4>(3, 2, 1, 1);
276  auto slice4 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
277  VERIFY_IS_EQUAL(slice4.dimensions().TotalSize(), 6);
278  for (int i = 0; i < 3; ++i) {
279  for (int j = 0; j < 2; ++j) {
280  VERIFY_IS_EQUAL(slice4.data()[i + 3 * j], tensor(i, 2 + j, 3, 4));
281  }
282  }
283  } else {
284  offsets = Eigen::DSizes<ptrdiff_t, 4>(1, 2, 3, 0);
285  extents = Eigen::DSizes<ptrdiff_t, 4>(1, 1, 2, 11);
286  auto slice4 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
287  VERIFY_IS_EQUAL(slice4.dimensions().TotalSize(), 22);
288  for (int l = 0; l < 11; ++l) {
289  for (int k = 0; k < 2; ++k) {
290  VERIFY_IS_EQUAL(slice4.data()[l + 11 * k], tensor(1, 2, 3 + k, l));
291  }
292  }
293  }
294 
295  if (DataLayout == ColMajor) {
296  offsets = Eigen::DSizes<ptrdiff_t, 4>(0, 0, 0, 4);
297  extents = Eigen::DSizes<ptrdiff_t, 4>(3, 5, 7, 2);
298  auto slice5 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
299  VERIFY_IS_EQUAL(slice5.dimensions().TotalSize(), 210);
300  for (int i = 0; i < 3; ++i) {
301  for (int j = 0; j < 5; ++j) {
302  for (int k = 0; k < 7; ++k) {
303  for (int l = 0; l < 2; ++l) {
304  int slice_index = i + 3 * (j + 5 * (k + 7 * l));
305  VERIFY_IS_EQUAL(slice5.data()[slice_index], tensor(i, j, k, l + 4));
306  }
307  }
308  }
309  }
310  } else {
311  offsets = Eigen::DSizes<ptrdiff_t, 4>(1, 0, 0, 0);
312  extents = Eigen::DSizes<ptrdiff_t, 4>(2, 5, 7, 11);
313  auto slice5 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
314  VERIFY_IS_EQUAL(slice5.dimensions().TotalSize(), 770);
315  for (int l = 0; l < 11; ++l) {
316  for (int k = 0; k < 7; ++k) {
317  for (int j = 0; j < 5; ++j) {
318  for (int i = 0; i < 2; ++i) {
319  int slice_index = l + 11 * (k + 7 * (j + 5 * i));
320  VERIFY_IS_EQUAL(slice5.data()[slice_index], tensor(i + 1, j, k, l));
321  }
322  }
323  }
324  }
325  }
326 
327  offsets = Eigen::DSizes<ptrdiff_t, 4>(0, 0, 0, 0);
328  extents = Eigen::DSizes<ptrdiff_t, 4>(3, 5, 7, 11);
329  auto slice6 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice());
330  VERIFY_IS_EQUAL(slice6.dimensions().TotalSize(), 3 * 5 * 7 * 11);
331  VERIFY_IS_EQUAL(slice6.data(), tensor.data());
332 }
static const int DataLayout
Definition: cxx11_tensor_image_patch_sycl.cpp:24
@ ColMajor
Definition: Constants.h:318
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31

References Eigen::ColMajor, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), DataLayout, i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::TensorBase< Derived, AccessLevel >::slice(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_static_reshape()

template<typename >
static void test_static_reshape ( )
static
44  {
45  using Eigen::type2index;
46 
47  Tensor<float, 5> tensor(2, 3, 1, 7, 1);
48  tensor.setRandom();
49 
50  // New dimensions: [2, 3, 7]
52  Tensor<float, 3> reshaped = tensor.reshape(static_cast<Eigen::DSizes<ptrdiff_t, 3>>(dim));
53 
54  for (int i = 0; i < 2; ++i) {
55  for (int j = 0; j < 3; ++j) {
56  for (int k = 0; k < 7; ++k) {
57  VERIFY_IS_EQUAL(tensor(i, j, 0, k, 0), reshaped(i, j, k));
58  }
59  }
60  }
61 }
A reshaped()
Definition: TensorIndexList.h:271
Definition: TensorIndexList.h:39

References i, j, k, Eigen::TensorBase< Derived, AccessLevel >::reshape(), reshaped(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

◆ test_strided_slice()

template<typename T , int DataLayout>
static void test_strided_slice ( )
static
335  {
336  typedef Tensor<T, 5, DataLayout> Tensor5f;
338  typedef Tensor<T, 2, DataLayout> Tensor2f;
340  Tensor<T, 5, DataLayout> tensor(2, 3, 5, 7, 11);
341  Tensor<T, 2, DataLayout> tensor2(7, 11);
342  tensor.setRandom();
343  tensor2.setRandom();
344 
345  if (true) {
346  Tensor2f slice(2, 3);
347  Index2 strides(-2, -1);
348  Index2 indicesStart(5, 7);
349  Index2 indicesStop(0, 4);
350  slice = tensor2.stridedSlice(indicesStart, indicesStop, strides);
351  for (int j = 0; j < 2; ++j) {
352  for (int k = 0; k < 3; ++k) {
353  VERIFY_IS_EQUAL(slice(j, k), tensor2(5 - 2 * j, 7 - k));
354  }
355  }
356  }
357 
358  if (true) {
359  Tensor2f slice(0, 1);
360  Index2 strides(1, 1);
361  Index2 indicesStart(5, 4);
362  Index2 indicesStop(5, 5);
363  slice = tensor2.stridedSlice(indicesStart, indicesStop, strides);
364  }
365 
366  if (true) { // test clamped degenerate interavls
367  Tensor2f slice(7, 11);
368  Index2 strides(1, -1);
369  Index2 indicesStart(-3, 20); // should become 0,10
370  Index2 indicesStop(20, -11); // should become 11, -1
371  slice = tensor2.stridedSlice(indicesStart, indicesStop, strides);
372  for (int j = 0; j < 7; ++j) {
373  for (int k = 0; k < 11; ++k) {
374  VERIFY_IS_EQUAL(slice(j, k), tensor2(j, 10 - k));
375  }
376  }
377  }
378 
379  if (true) {
380  Tensor5f slice1(1, 1, 1, 1, 1);
381  Eigen::DSizes<Eigen::DenseIndex, 5> indicesStart(1, 2, 3, 4, 5);
382  Eigen::DSizes<Eigen::DenseIndex, 5> indicesStop(2, 3, 4, 5, 6);
384  slice1 = tensor.stridedSlice(indicesStart, indicesStop, strides);
385  VERIFY_IS_EQUAL(slice1(0, 0, 0, 0, 0), tensor(1, 2, 3, 4, 5));
386  }
387 
388  if (true) {
389  Tensor5f slice(1, 1, 2, 2, 3);
390  Index5 start(1, 1, 3, 4, 5);
391  Index5 stop(2, 2, 5, 6, 8);
392  Index5 strides(1, 1, 1, 1, 1);
393  slice = tensor.stridedSlice(start, stop, strides);
394  for (int i = 0; i < 2; ++i) {
395  for (int j = 0; j < 2; ++j) {
396  for (int k = 0; k < 3; ++k) {
397  VERIFY_IS_EQUAL(slice(0, 0, i, j, k), tensor(1, 1, 3 + i, 4 + j, 5 + k));
398  }
399  }
400  }
401  }
402 
403  if (true) {
404  Tensor5f slice(1, 1, 2, 2, 3);
405  Index5 strides3(1, 1, -2, 1, -1);
406  Index5 indices3Start(1, 1, 4, 4, 7);
407  Index5 indices3Stop(2, 2, 0, 6, 4);
408  slice = tensor.stridedSlice(indices3Start, indices3Stop, strides3);
409  for (int i = 0; i < 2; ++i) {
410  for (int j = 0; j < 2; ++j) {
411  for (int k = 0; k < 3; ++k) {
412  VERIFY_IS_EQUAL(slice(0, 0, i, j, k), tensor(1, 1, 4 - 2 * i, 4 + j, 7 - k));
413  }
414  }
415  }
416  }
417 
418  if (false) { // tests degenerate interval
419  Tensor5f slice(1, 1, 2, 2, 3);
420  Index5 strides3(1, 1, 2, 1, 1);
421  Index5 indices3Start(1, 1, 4, 4, 7);
422  Index5 indices3Stop(2, 2, 0, 6, 4);
423  slice = tensor.stridedSlice(indices3Start, indices3Stop, strides3);
424  }
425 }
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:29
void start(const unsigned &i)
(Re-)start i-th timer
Definition: oomph_utilities.cc:243

References i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), oomph::CumulativeTimings::start(), Eigen::TensorBase< Derived, AccessLevel >::stridedSlice(), Eigen::internal::strides(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_strided_slice_write()

template<typename T , int DataLayout>
static void test_strided_slice_write ( )
static
428  {
429  typedef Tensor<T, 2, DataLayout> Tensor2f;
431 
432  Tensor<T, 2, DataLayout> tensor(7, 11), tensor2(7, 11);
433  tensor.setRandom();
434  tensor2 = tensor;
435  Tensor2f slice(2, 3);
436 
437  slice.setRandom();
438 
439  Index2 strides(1, 1);
440  Index2 indicesStart(3, 4);
441  Index2 indicesStop(5, 7);
442  Index2 lengths(2, 3);
443 
444  tensor.slice(indicesStart, lengths) = slice;
445  tensor2.stridedSlice(indicesStart, indicesStop, strides) = slice;
446 
447  for (int i = 0; i < 7; i++)
448  for (int j = 0; j < 11; j++) {
449  VERIFY_IS_EQUAL(tensor(i, j), tensor2(i, j));
450  }
451 }

References i, j, Eigen::TensorBase< Derived, AccessLevel >::stridedSlice(), Eigen::internal::strides(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().