cxx11_tensor_striding.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 #include <Eigen/CXX11/Tensor>
13 
14 using Eigen::Tensor;
15 
16 template<int DataLayout>
17 static void test_simple_striding()
18 {
19  Tensor<float, 4, DataLayout> tensor(2,3,5,7);
20  tensor.setRandom();
22  strides[0] = 1;
23  strides[1] = 1;
24  strides[2] = 1;
25  strides[3] = 1;
26 
28  no_stride = tensor.stride(strides);
29 
30  VERIFY_IS_EQUAL(no_stride.dimension(0), 2);
31  VERIFY_IS_EQUAL(no_stride.dimension(1), 3);
32  VERIFY_IS_EQUAL(no_stride.dimension(2), 5);
33  VERIFY_IS_EQUAL(no_stride.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_stride(i,j,k,l));
40  }
41  }
42  }
43  }
44 
45  strides[0] = 2;
46  strides[1] = 4;
47  strides[2] = 2;
48  strides[3] = 3;
50  stride = tensor.stride(strides);
51 
52  VERIFY_IS_EQUAL(stride.dimension(0), 1);
53  VERIFY_IS_EQUAL(stride.dimension(1), 1);
54  VERIFY_IS_EQUAL(stride.dimension(2), 3);
55  VERIFY_IS_EQUAL(stride.dimension(3), 3);
56 
57  for (int i = 0; i < 1; ++i) {
58  for (int j = 0; j < 1; ++j) {
59  for (int k = 0; k < 3; ++k) {
60  for (int l = 0; l < 3; ++l) {
61  VERIFY_IS_EQUAL(tensor(2*i,4*j,2*k,3*l), stride(i,j,k,l));
62  }
63  }
64  }
65  }
66 }
67 
68 
69 template<int DataLayout>
71 {
72  Tensor<float, 4, DataLayout> tensor(2,3,5,7);
73  tensor.setRandom();
75  strides[0] = 2;
76  strides[1] = 4;
77  strides[2] = 2;
78  strides[3] = 3;
79 
80  Tensor<float, 4, DataLayout> result(3, 12, 10, 21);
81  result.stride(strides) = tensor;
82 
83  for (int i = 0; i < 2; ++i) {
84  for (int j = 0; j < 3; ++j) {
85  for (int k = 0; k < 5; ++k) {
86  for (int l = 0; l < 7; ++l) {
87  VERIFY_IS_EQUAL(tensor(i,j,k,l), result(2*i,4*j,2*k,3*l));
88  }
89  }
90  }
91  }
92 
93  array<ptrdiff_t, 4> no_strides;
94  no_strides[0] = 1;
95  no_strides[1] = 1;
96  no_strides[2] = 1;
97  no_strides[3] = 1;
98  Tensor<float, 4, DataLayout> result2(3, 12, 10, 21);
99  result2.stride(strides) = tensor.stride(no_strides);
100 
101  for (int i = 0; i < 2; ++i) {
102  for (int j = 0; j < 3; ++j) {
103  for (int k = 0; k < 5; ++k) {
104  for (int l = 0; l < 7; ++l) {
105  VERIFY_IS_EQUAL(tensor(i,j,k,l), result2(2*i,4*j,2*k,3*l));
106  }
107  }
108  }
109  }
110 }
111 
112 
113 EIGEN_DECLARE_TEST(cxx11_tensor_striding)
114 {
115  CALL_SUBTEST(test_simple_striding<ColMajor>());
116  CALL_SUBTEST(test_simple_striding<RowMajor>());
117  CALL_SUBTEST(test_striding_as_lvalue<ColMajor>());
118  CALL_SUBTEST(test_striding_as_lvalue<RowMajor>());
119 }
Eigen::Tensor::dimension
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:101
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
Eigen::internal::strides
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:26
Eigen::array
Definition: EmulateArray.h:21
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
test_simple_striding
static void test_simple_striding()
Definition: cxx11_tensor_striding.cpp:17
result
Values result
Definition: OdometryOptimize.cpp:8
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
l
static const Line3 l(Rot3(), 1, 1)
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_striding)
Definition: cxx11_tensor_striding.cpp:113
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::setRandom
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:996
main.h
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::stride
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE TensorStridingOp< const Strides, const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > > stride(const Strides &strides) const
Definition: TensorBase.h:1134
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
test_striding_as_lvalue
static void test_striding_as_lvalue()
Definition: cxx11_tensor_striding.cpp:70
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:09