aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/test/cxx11_tensor_chipping.cpp
diff options
context:
space:
mode:
authorGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2015-01-14 15:46:04 -0800
committerGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2015-01-14 15:46:04 -0800
commitb5124e7cfda27ed99dcfcec8cb1b674efa1ef4a3 (patch)
tree7f8378843a756af14785e563689b4765e062a953 /unsupported/test/cxx11_tensor_chipping.cpp
parent54e3633b437e44ed4d370c9f8868535192308ca3 (diff)
Created many additional tests
Diffstat (limited to 'unsupported/test/cxx11_tensor_chipping.cpp')
-rw-r--r--unsupported/test/cxx11_tensor_chipping.cpp183
1 files changed, 139 insertions, 44 deletions
diff --git a/unsupported/test/cxx11_tensor_chipping.cpp b/unsupported/test/cxx11_tensor_chipping.cpp
index 0027b2888..0de7bbac6 100644
--- a/unsupported/test/cxx11_tensor_chipping.cpp
+++ b/unsupported/test/cxx11_tensor_chipping.cpp
@@ -13,18 +13,20 @@
using Eigen::Tensor;
-
+template<int DataLayout>
static void test_simple_chip()
{
- Tensor<float, 5> tensor(2,3,5,7,11);
+ Tensor<float, 5, DataLayout> tensor(2,3,5,7,11);
tensor.setRandom();
- Tensor<float, 4> chip1;
- chip1 = tensor.chip<0>(1);
+ Tensor<float, 4, DataLayout> chip1;
+ chip1 = tensor.template chip<0>(1);
+
VERIFY_IS_EQUAL(chip1.dimension(0), 3);
VERIFY_IS_EQUAL(chip1.dimension(1), 5);
VERIFY_IS_EQUAL(chip1.dimension(2), 7);
VERIFY_IS_EQUAL(chip1.dimension(3), 11);
+
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 5; ++j) {
for (int k = 0; k < 7; ++k) {
@@ -35,7 +37,7 @@ static void test_simple_chip()
}
}
- Tensor<float, 4> chip2 = tensor.chip<1>(1);
+ Tensor<float, 4, DataLayout> chip2 = tensor.template chip<1>(1);
VERIFY_IS_EQUAL(chip2.dimension(0), 2);
VERIFY_IS_EQUAL(chip2.dimension(1), 5);
VERIFY_IS_EQUAL(chip2.dimension(2), 7);
@@ -50,7 +52,7 @@ static void test_simple_chip()
}
}
- Tensor<float, 4> chip3 = tensor.chip<2>(2);
+ Tensor<float, 4, DataLayout> chip3 = tensor.template chip<2>(2);
VERIFY_IS_EQUAL(chip3.dimension(0), 2);
VERIFY_IS_EQUAL(chip3.dimension(1), 3);
VERIFY_IS_EQUAL(chip3.dimension(2), 7);
@@ -65,7 +67,7 @@ static void test_simple_chip()
}
}
- Tensor<float, 4> chip4(tensor.chip<3>(5));
+ Tensor<float, 4, DataLayout> chip4(tensor.template chip<3>(5));
VERIFY_IS_EQUAL(chip4.dimension(0), 2);
VERIFY_IS_EQUAL(chip4.dimension(1), 3);
VERIFY_IS_EQUAL(chip4.dimension(2), 5);
@@ -80,7 +82,7 @@ static void test_simple_chip()
}
}
- Tensor<float, 4> chip5(tensor.chip<4>(7));
+ Tensor<float, 4, DataLayout> chip5(tensor.template chip<4>(7));
VERIFY_IS_EQUAL(chip5.dimension(0), 2);
VERIFY_IS_EQUAL(chip5.dimension(1), 3);
VERIFY_IS_EQUAL(chip5.dimension(2), 5);
@@ -96,14 +98,97 @@ static void test_simple_chip()
}
}
+template<int DataLayout>
+static void test_dynamic_chip()
+{
+ Tensor<float, 5, DataLayout> tensor(2,3,5,7,11);
+ tensor.setRandom();
+
+ Tensor<float, 4, DataLayout> chip1;
+ chip1 = tensor.chip(1, 0);
+ VERIFY_IS_EQUAL(chip1.dimension(0), 3);
+ VERIFY_IS_EQUAL(chip1.dimension(1), 5);
+ VERIFY_IS_EQUAL(chip1.dimension(2), 7);
+ VERIFY_IS_EQUAL(chip1.dimension(3), 11);
+ for (int i = 0; i < 3; ++i) {
+ for (int j = 0; j < 5; ++j) {
+ for (int k = 0; k < 7; ++k) {
+ for (int l = 0; l < 11; ++l) {
+ VERIFY_IS_EQUAL(chip1(i,j,k,l), tensor(1,i,j,k,l));
+ }
+ }
+ }
+ }
+
+ Tensor<float, 4, DataLayout> chip2 = tensor.chip(1, 1);
+ VERIFY_IS_EQUAL(chip2.dimension(0), 2);
+ VERIFY_IS_EQUAL(chip2.dimension(1), 5);
+ VERIFY_IS_EQUAL(chip2.dimension(2), 7);
+ VERIFY_IS_EQUAL(chip2.dimension(3), 11);
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 7; ++k) {
+ for (int l = 0; l < 11; ++l) {
+ VERIFY_IS_EQUAL(chip2(i,j,k,l), tensor(i,1,j,k,l));
+ }
+ }
+ }
+ }
+
+ Tensor<float, 4, DataLayout> chip3 = tensor.chip(2, 2);
+ VERIFY_IS_EQUAL(chip3.dimension(0), 2);
+ VERIFY_IS_EQUAL(chip3.dimension(1), 3);
+ VERIFY_IS_EQUAL(chip3.dimension(2), 7);
+ VERIFY_IS_EQUAL(chip3.dimension(3), 11);
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 7; ++k) {
+ for (int l = 0; l < 11; ++l) {
+ VERIFY_IS_EQUAL(chip3(i,j,k,l), tensor(i,j,2,k,l));
+ }
+ }
+ }
+ }
+
+ Tensor<float, 4, DataLayout> chip4(tensor.chip(5, 3));
+ VERIFY_IS_EQUAL(chip4.dimension(0), 2);
+ VERIFY_IS_EQUAL(chip4.dimension(1), 3);
+ VERIFY_IS_EQUAL(chip4.dimension(2), 5);
+ VERIFY_IS_EQUAL(chip4.dimension(3), 11);
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 5; ++k) {
+ for (int l = 0; l < 7; ++l) {
+ VERIFY_IS_EQUAL(chip4(i,j,k,l), tensor(i,j,k,5,l));
+ }
+ }
+ }
+ }
+
+ Tensor<float, 4, DataLayout> chip5(tensor.chip(7, 4));
+ VERIFY_IS_EQUAL(chip5.dimension(0), 2);
+ VERIFY_IS_EQUAL(chip5.dimension(1), 3);
+ VERIFY_IS_EQUAL(chip5.dimension(2), 5);
+ VERIFY_IS_EQUAL(chip5.dimension(3), 7);
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 5; ++k) {
+ for (int l = 0; l < 7; ++l) {
+ VERIFY_IS_EQUAL(chip5(i,j,k,l), tensor(i,j,k,l,7));
+ }
+ }
+ }
+ }
+}
+template<int DataLayout>
static void test_chip_in_expr() {
- Tensor<float, 5> input1(2,3,5,7,11);
+ Tensor<float, 5, DataLayout> input1(2,3,5,7,11);
input1.setRandom();
- Tensor<float, 4> input2(3,5,7,11);
+ Tensor<float, 4, DataLayout> input2(3,5,7,11);
input2.setRandom();
- Tensor<float, 4> result = input1.chip<0>(0) + input2;
+ Tensor<float, 4, DataLayout> result = input1.template chip<0>(0) + input2;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 5; ++j) {
for (int k = 0; k < 7; ++k) {
@@ -115,9 +200,9 @@ static void test_chip_in_expr() {
}
}
- Tensor<float, 3> input3(3,7,11);
+ Tensor<float, 3, DataLayout> input3(3,7,11);
input3.setRandom();
- Tensor<float, 3> result2 = input1.chip<0>(0).chip<1>(2) + input3;
+ Tensor<float, 3, DataLayout> result2 = input1.template chip<0>(0).template chip<1>(2) + input3;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 7; ++j) {
for (int k = 0; k < 11; ++k) {
@@ -128,16 +213,16 @@ static void test_chip_in_expr() {
}
}
-
+template<int DataLayout>
static void test_chip_as_lvalue()
{
- Tensor<float, 5> input1(2,3,5,7,11);
+ Tensor<float, 5, DataLayout> input1(2,3,5,7,11);
input1.setRandom();
- Tensor<float, 4> input2(3,5,7,11);
+ Tensor<float, 4, DataLayout> input2(3,5,7,11);
input2.setRandom();
- Tensor<float, 5> tensor = input1;
- tensor.chip<0>(1) = input2;
+ Tensor<float, 5, DataLayout> tensor = input1;
+ tensor.template chip<0>(1) = input2;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 5; ++k) {
@@ -154,10 +239,10 @@ static void test_chip_as_lvalue()
}
}
- Tensor<float, 4> input3(2,5,7,11);
+ Tensor<float, 4, DataLayout> input3(2,5,7,11);
input3.setRandom();
tensor = input1;
- tensor.chip<1>(1) = input3;
+ tensor.template chip<1>(1) = input3;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 5; ++k) {
@@ -174,10 +259,10 @@ static void test_chip_as_lvalue()
}
}
- Tensor<float, 4> input4(2,3,7,11);
+ Tensor<float, 4, DataLayout> input4(2,3,7,11);
input4.setRandom();
tensor = input1;
- tensor.chip<2>(3) = input4;
+ tensor.template chip<2>(3) = input4;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 5; ++k) {
@@ -194,10 +279,10 @@ static void test_chip_as_lvalue()
}
}
- Tensor<float, 4> input5(2,3,5,11);
+ Tensor<float, 4, DataLayout> input5(2,3,5,11);
input5.setRandom();
tensor = input1;
- tensor.chip<3>(4) = input5;
+ tensor.template chip<3>(4) = input5;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 5; ++k) {
@@ -214,10 +299,10 @@ static void test_chip_as_lvalue()
}
}
- Tensor<float, 4> input6(2,3,5,7);
+ Tensor<float, 4, DataLayout> input6(2,3,5,7);
input6.setRandom();
tensor = input1;
- tensor.chip<4>(5) = input6;
+ tensor.template chip<4>(5) = input6;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 5; ++k) {
@@ -235,47 +320,57 @@ static void test_chip_as_lvalue()
}
}
-
+template<int DataLayout>
static void test_chip_raw_data()
{
- Tensor<float, 5> tensor(2,3,5,7,11);
+ Tensor<float, 5, DataLayout> tensor(2,3,5,7,11);
tensor.setRandom();
- typedef TensorEvaluator<decltype(tensor.chip<4>(3)), DefaultDevice> Evaluator4;
- auto chip = Evaluator4(tensor.chip<4>(3), DefaultDevice());
+ typedef TensorEvaluator<decltype(tensor.template chip<4>(3)), DefaultDevice> Evaluator4;
+ auto chip = Evaluator4(tensor.template chip<4>(3), DefaultDevice());
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 5; ++k) {
for (int l = 0; l < 7; ++l) {
- int chip_index = i + 2 * (j + 3 * (k + 5 * l));
+ int chip_index;
+ if (DataLayout == ColMajor) {
+ chip_index = i + 2 * (j + 3 * (k + 5 * l));
+ } else {
+ chip_index = 11 * (l + 7 * (k + 5 * (j + 3 * i)));
+ }
VERIFY_IS_EQUAL(chip.data()[chip_index], tensor(i,j,k,l,3));
}
}
}
}
- typedef TensorEvaluator<decltype(tensor.chip<0>(0)), DefaultDevice> Evaluator0;
- auto chip0 = Evaluator0(tensor.chip<0>(0), DefaultDevice());
+ typedef TensorEvaluator<decltype(tensor.template chip<0>(0)), DefaultDevice> Evaluator0;
+ auto chip0 = Evaluator0(tensor.template chip<0>(0), DefaultDevice());
VERIFY_IS_EQUAL(chip0.data(), static_cast<float*>(0));
- typedef TensorEvaluator<decltype(tensor.chip<1>(0)), DefaultDevice> Evaluator1;
- auto chip1 = Evaluator1(tensor.chip<1>(0), DefaultDevice());
+ typedef TensorEvaluator<decltype(tensor.template chip<1>(0)), DefaultDevice> Evaluator1;
+ auto chip1 = Evaluator1(tensor.template chip<1>(0), DefaultDevice());
VERIFY_IS_EQUAL(chip1.data(), static_cast<float*>(0));
- typedef TensorEvaluator<decltype(tensor.chip<2>(0)), DefaultDevice> Evaluator2;
- auto chip2 = Evaluator2(tensor.chip<2>(0), DefaultDevice());
+ typedef TensorEvaluator<decltype(tensor.template chip<2>(0)), DefaultDevice> Evaluator2;
+ auto chip2 = Evaluator2(tensor.template chip<2>(0), DefaultDevice());
VERIFY_IS_EQUAL(chip2.data(), static_cast<float*>(0));
- typedef TensorEvaluator<decltype(tensor.chip<3>(0)), DefaultDevice> Evaluator3;
- auto chip3 = Evaluator3(tensor.chip<3>(0), DefaultDevice());
+ typedef TensorEvaluator<decltype(tensor.template chip<3>(0)), DefaultDevice> Evaluator3;
+ auto chip3 = Evaluator3(tensor.template chip<3>(0), DefaultDevice());
VERIFY_IS_EQUAL(chip3.data(), static_cast<float*>(0));
}
-
void test_cxx11_tensor_chipping()
{
- CALL_SUBTEST(test_simple_chip());
- CALL_SUBTEST(test_chip_in_expr());
- CALL_SUBTEST(test_chip_as_lvalue());
- CALL_SUBTEST(test_chip_raw_data());
+ CALL_SUBTEST(test_simple_chip<ColMajor>());
+ CALL_SUBTEST(test_simple_chip<RowMajor>());
+ CALL_SUBTEST(test_dynamic_chip<ColMajor>());
+ CALL_SUBTEST(test_dynamic_chip<RowMajor>());
+ CALL_SUBTEST(test_chip_in_expr<ColMajor>());
+ CALL_SUBTEST(test_chip_in_expr<RowMajor>());
+ CALL_SUBTEST(test_chip_as_lvalue<ColMajor>());
+ CALL_SUBTEST(test_chip_as_lvalue<RowMajor>());
+ CALL_SUBTEST(test_chip_raw_data<ColMajor>());
+ CALL_SUBTEST(test_chip_raw_data<RowMajor>());
}