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

Functions

template<int DataLayout>
static void test_simple_shuffling ()
 
template<int DataLayout>
static void test_expr_shuffling ()
 
template<int DataLayout>
static void test_shuffling_as_value ()
 
template<int DataLayout>
static void test_shuffle_unshuffle ()
 
template<int DataLayout>
static void test_empty_shuffling ()
 
 EIGEN_DECLARE_TEST (cxx11_tensor_shuffling)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_tensor_shuffling  )
259  {
260  CALL_SUBTEST(test_simple_shuffling<ColMajor>());
261  CALL_SUBTEST(test_simple_shuffling<RowMajor>());
262  CALL_SUBTEST(test_expr_shuffling<ColMajor>());
263  CALL_SUBTEST(test_expr_shuffling<RowMajor>());
264  CALL_SUBTEST(test_shuffling_as_value<ColMajor>());
265  CALL_SUBTEST(test_shuffling_as_value<RowMajor>());
266  CALL_SUBTEST(test_shuffle_unshuffle<ColMajor>());
267  CALL_SUBTEST(test_shuffle_unshuffle<RowMajor>());
268  CALL_SUBTEST(test_empty_shuffling<ColMajor>());
269  CALL_SUBTEST(test_empty_shuffling<RowMajor>());
270 }
#define CALL_SUBTEST(FUNC)
Definition: main.h:382

References CALL_SUBTEST.

◆ test_empty_shuffling()

template<int DataLayout>
static void test_empty_shuffling ( )
static
209  {
210  Tensor<float, 4, DataLayout> tensor(2, 3, 0, 7);
211  tensor.setRandom();
212  array<ptrdiff_t, 4> shuffles;
213  shuffles[0] = 0;
214  shuffles[1] = 1;
215  shuffles[2] = 2;
216  shuffles[3] = 3;
217 
218  Tensor<float, 4, DataLayout> no_shuffle;
219  no_shuffle = tensor.shuffle(shuffles);
220 
221  VERIFY_IS_EQUAL(no_shuffle.dimension(0), 2);
222  VERIFY_IS_EQUAL(no_shuffle.dimension(1), 3);
223  VERIFY_IS_EQUAL(no_shuffle.dimension(2), 0);
224  VERIFY_IS_EQUAL(no_shuffle.dimension(3), 7);
225 
226  for (int i = 0; i < 2; ++i) {
227  for (int j = 0; j < 3; ++j) {
228  for (int k = 0; k < 0; ++k) {
229  for (int l = 0; l < 7; ++l) {
230  VERIFY_IS_EQUAL(tensor(i, j, k, l), no_shuffle(i, j, k, l));
231  }
232  }
233  }
234  }
235 
236  shuffles[0] = 2;
237  shuffles[1] = 3;
238  shuffles[2] = 1;
239  shuffles[3] = 0;
241  shuffle = tensor.shuffle(shuffles);
242 
243  VERIFY_IS_EQUAL(shuffle.dimension(0), 0);
244  VERIFY_IS_EQUAL(shuffle.dimension(1), 7);
245  VERIFY_IS_EQUAL(shuffle.dimension(2), 3);
246  VERIFY_IS_EQUAL(shuffle.dimension(3), 2);
247 
248  for (int i = 0; i < 2; ++i) {
249  for (int j = 0; j < 3; ++j) {
250  for (int k = 0; k < 0; ++k) {
251  for (int l = 0; l < 7; ++l) {
252  VERIFY_IS_EQUAL(tensor(i, j, k, l), shuffle(k, l, j, i));
253  }
254  }
255  }
256  }
257 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorShufflingOp< const Shuffle, const Derived > shuffle(const Shuffle &shfl) const
Definition: TensorBase.h:1187
The tensor class.
Definition: Tensor.h:68
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:99
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
EIGEN_STRONG_INLINE Packet2d shuffle(const Packet2d &m, const Packet2d &n, int mask)
Definition: LSX/PacketMath.h:150
std::array< T, N > array
Definition: EmulateArray.h:231
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimension(), i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::internal::shuffle(), Eigen::TensorBase< Derived, AccessLevel >::shuffle(), and VERIFY_IS_EQUAL.

◆ test_expr_shuffling()

template<int DataLayout>
static void test_expr_shuffling ( )
static
69  {
70  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
71  tensor.setRandom();
72 
73  array<ptrdiff_t, 4> shuffles;
74  shuffles[0] = 2;
75  shuffles[1] = 3;
76  shuffles[2] = 1;
77  shuffles[3] = 0;
79  expected = tensor.shuffle(shuffles);
80 
81  Tensor<float, 4, DataLayout> result(5, 7, 3, 2);
82 
83  array<ptrdiff_t, 4> src_slice_dim{{2, 3, 1, 7}};
84  array<ptrdiff_t, 4> src_slice_start{{0, 0, 0, 0}};
85  array<ptrdiff_t, 4> dst_slice_dim{{1, 7, 3, 2}};
86  array<ptrdiff_t, 4> dst_slice_start{{0, 0, 0, 0}};
87 
88  for (int i = 0; i < 5; ++i) {
89  result.slice(dst_slice_start, dst_slice_dim) = tensor.slice(src_slice_start, src_slice_dim).shuffle(shuffles);
90  src_slice_start[2] += 1;
91  dst_slice_start[0] += 1;
92  }
93 
94  VERIFY_IS_EQUAL(result.dimension(0), 5);
95  VERIFY_IS_EQUAL(result.dimension(1), 7);
96  VERIFY_IS_EQUAL(result.dimension(2), 3);
97  VERIFY_IS_EQUAL(result.dimension(3), 2);
98 
99  for (int i = 0; i < expected.dimension(0); ++i) {
100  for (int j = 0; j < expected.dimension(1); ++j) {
101  for (int k = 0; k < expected.dimension(2); ++k) {
102  for (int l = 0; l < expected.dimension(3); ++l) {
103  VERIFY_IS_EQUAL(result(i, j, k, l), expected(i, j, k, l));
104  }
105  }
106  }
107  }
108 
109  dst_slice_start[0] = 0;
110  result.setRandom();
111  for (int i = 0; i < 5; ++i) {
112  result.slice(dst_slice_start, dst_slice_dim) = tensor.shuffle(shuffles).slice(dst_slice_start, dst_slice_dim);
113  dst_slice_start[0] += 1;
114  }
115 
116  for (int i = 0; i < expected.dimension(0); ++i) {
117  for (int j = 0; j < expected.dimension(1); ++j) {
118  for (int k = 0; k < expected.dimension(2); ++k) {
119  for (int l = 0; l < expected.dimension(3); ++l) {
120  VERIFY_IS_EQUAL(result(i, j, k, l), expected(i, j, k, l));
121  }
122  }
123  }
124  }
125 }

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

◆ test_shuffle_unshuffle()

template<int DataLayout>
static void test_shuffle_unshuffle ( )
static
173  {
174  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
175  tensor.setRandom();
176 
177  // Choose a random permutation.
178  array<ptrdiff_t, 4> shuffles;
179  for (int i = 0; i < 4; ++i) {
180  shuffles[i] = i;
181  }
182  array<ptrdiff_t, 4> shuffles_inverse;
183  for (int i = 0; i < 4; ++i) {
184  const ptrdiff_t index = internal::random<ptrdiff_t>(i, 3);
185  shuffles_inverse[shuffles[index]] = i;
186  std::swap(shuffles[i], shuffles[index]);
187  }
188 
190  shuffle = tensor.shuffle(shuffles).shuffle(shuffles_inverse);
191 
192  VERIFY_IS_EQUAL(shuffle.dimension(0), 2);
193  VERIFY_IS_EQUAL(shuffle.dimension(1), 3);
194  VERIFY_IS_EQUAL(shuffle.dimension(2), 5);
195  VERIFY_IS_EQUAL(shuffle.dimension(3), 7);
196 
197  for (int i = 0; i < 2; ++i) {
198  for (int j = 0; j < 3; ++j) {
199  for (int k = 0; k < 5; ++k) {
200  for (int l = 0; l < 7; ++l) {
201  VERIFY_IS_EQUAL(tensor(i, j, k, l), shuffle(i, j, k, l));
202  }
203  }
204  }
205  }
206 }
EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:117

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

◆ test_shuffling_as_value()

template<int DataLayout>
static void test_shuffling_as_value ( )
static
128  {
129  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
130  tensor.setRandom();
131  array<ptrdiff_t, 4> shuffles;
132  shuffles[2] = 0;
133  shuffles[3] = 1;
134  shuffles[1] = 2;
135  shuffles[0] = 3;
137  shuffle.shuffle(shuffles) = tensor;
138 
139  VERIFY_IS_EQUAL(shuffle.dimension(0), 5);
140  VERIFY_IS_EQUAL(shuffle.dimension(1), 7);
141  VERIFY_IS_EQUAL(shuffle.dimension(2), 3);
142  VERIFY_IS_EQUAL(shuffle.dimension(3), 2);
143 
144  for (int i = 0; i < 2; ++i) {
145  for (int j = 0; j < 3; ++j) {
146  for (int k = 0; k < 5; ++k) {
147  for (int l = 0; l < 7; ++l) {
148  VERIFY_IS_EQUAL(tensor(i, j, k, l), shuffle(k, l, j, i));
149  }
150  }
151  }
152  }
153 
154  array<ptrdiff_t, 4> no_shuffle;
155  no_shuffle[0] = 0;
156  no_shuffle[1] = 1;
157  no_shuffle[2] = 2;
158  no_shuffle[3] = 3;
160  shuffle2.shuffle(shuffles) = tensor.shuffle(no_shuffle);
161  for (int i = 0; i < 5; ++i) {
162  for (int j = 0; j < 7; ++j) {
163  for (int k = 0; k < 3; ++k) {
164  for (int l = 0; l < 2; ++l) {
165  VERIFY_IS_EQUAL(shuffle2(i, j, k, l), shuffle(i, j, k, l));
166  }
167  }
168  }
169  }
170 }
EIGEN_STRONG_INLINE Packet4f shuffle2(const Packet4f &m, const Packet4f &n, int mask)
Definition: LSX/PacketMath.h:105

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

◆ test_simple_shuffling()

template<int DataLayout>
static void test_simple_shuffling ( )
static
18  {
19  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
20  tensor.setRandom();
21  array<ptrdiff_t, 4> shuffles;
22  shuffles[0] = 0;
23  shuffles[1] = 1;
24  shuffles[2] = 2;
25  shuffles[3] = 3;
26 
28  no_shuffle = tensor.shuffle(shuffles);
29 
30  VERIFY_IS_EQUAL(no_shuffle.dimension(0), 2);
31  VERIFY_IS_EQUAL(no_shuffle.dimension(1), 3);
32  VERIFY_IS_EQUAL(no_shuffle.dimension(2), 5);
33  VERIFY_IS_EQUAL(no_shuffle.dimension(3), 7);
34 
35  for (int i = 0; i < 2; ++i) {
36  for (int j = 0; j < 3; ++j) {
37  for (int k = 0; k < 5; ++k) {
38  for (int l = 0; l < 7; ++l) {
39  VERIFY_IS_EQUAL(tensor(i, j, k, l), no_shuffle(i, j, k, l));
40  }
41  }
42  }
43  }
44 
45  shuffles[0] = 2;
46  shuffles[1] = 3;
47  shuffles[2] = 1;
48  shuffles[3] = 0;
50  shuffle = tensor.shuffle(shuffles);
51 
52  VERIFY_IS_EQUAL(shuffle.dimension(0), 5);
53  VERIFY_IS_EQUAL(shuffle.dimension(1), 7);
54  VERIFY_IS_EQUAL(shuffle.dimension(2), 3);
55  VERIFY_IS_EQUAL(shuffle.dimension(3), 2);
56 
57  for (int i = 0; i < 2; ++i) {
58  for (int j = 0; j < 3; ++j) {
59  for (int k = 0; k < 5; ++k) {
60  for (int l = 0; l < 7; ++l) {
61  VERIFY_IS_EQUAL(tensor(i, j, k, l), shuffle(k, l, j, i));
62  }
63  }
64  }
65  }
66 }

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimension(), i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), Eigen::internal::shuffle(), Eigen::TensorBase< Derived, AccessLevel >::shuffle(), and VERIFY_IS_EQUAL.