aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2014-06-13 10:20:28 -0700
committerGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2014-06-13 10:20:28 -0700
commit774c3c1e0aca307e484b00997b735ee5964d96d4 (patch)
tree3a3dc13fca4e811f07abe6b44a3869270373a789
parentf80c8e17eb042fc95767417eeca26cd3fa0c6ad6 (diff)
Created additional unit tests for the tensor code and improved existing ones.
-rw-r--r--unsupported/test/CMakeLists.txt3
-rw-r--r--unsupported/test/cxx11_tensor_device.cpp28
-rw-r--r--unsupported/test/cxx11_tensor_lvalue.cpp42
-rw-r--r--unsupported/test/cxx11_tensor_morphing.cpp72
4 files changed, 143 insertions, 2 deletions
diff --git a/unsupported/test/CMakeLists.txt b/unsupported/test/CMakeLists.txt
index 34130a192..7458128fb 100644
--- a/unsupported/test/CMakeLists.txt
+++ b/unsupported/test/CMakeLists.txt
@@ -105,7 +105,10 @@ if(EIGEN_TEST_CXX11)
ei_add_test(cxx11_tensor_contraction "-std=c++0x")
ei_add_test(cxx11_tensor_convolution "-std=c++0x")
ei_add_test(cxx11_tensor_expr "-std=c++0x")
+# ei_add_test(cxx11_tensor_fixed_size "-std=c++0x")
+ ei_add_test(cxx11_tensor_lvalue "-std=c++0x")
ei_add_test(cxx11_tensor_map "-std=c++0x")
+ ei_add_test(cxx11_tensor_morphing "-std=c++0x")
# ei_add_test(cxx11_tensor_device "-std=c++0x")
# ei_add_test(cxx11_tensor_fixed_size "-std=c++0x")
# ei_add_test(cxx11_tensor_thread_pool "-std=c++0x")
diff --git a/unsupported/test/cxx11_tensor_device.cpp b/unsupported/test/cxx11_tensor_device.cpp
index 365b109c7..caf2e9735 100644
--- a/unsupported/test/cxx11_tensor_device.cpp
+++ b/unsupported/test/cxx11_tensor_device.cpp
@@ -65,6 +65,12 @@ static void test_contextual_eval(Context* context)
context->out() = context->in1() + context->in2() * 3.14f + context->in1().constant(2.718f);
}
+template <typename Context>
+static void test_forced_contextual_eval(Context* context)
+{
+ context->out() = (context->in1() + context->in2()).eval() * 3.14f + context->in1().constant(2.718f);
+}
+
static void test_cpu() {
Eigen::Tensor<float, 3> in1(Eigen::array<int, 3>(2,3,7));
Eigen::Tensor<float, 3> in2(Eigen::array<int, 3>(2,3,7));
@@ -72,9 +78,9 @@ static void test_cpu() {
in1.setRandom();
in2.setRandom();
+
CPUContext context(in1, in2, out);
test_contextual_eval(&context);
-
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 7; ++k) {
@@ -82,6 +88,15 @@ static void test_cpu() {
}
}
}
+
+ test_forced_contextual_eval(&context);
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 7; ++k) {
+ VERIFY_IS_APPROX(out(Eigen::array<int, 3>(i,j,k)), (in1(Eigen::array<int, 3>(i,j,k)) + in2(Eigen::array<int, 3>(i,j,k))) * 3.14f + 2.718f);
+ }
+ }
+ }
}
static void test_gpu() {
@@ -111,7 +126,6 @@ static void test_gpu() {
GPUContext context(gpu_in1, gpu_in2, gpu_out);
test_contextual_eval(&context);
-
cudaMemcpy(out.data(), d_out, out_bytes, cudaMemcpyDeviceToHost);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
@@ -120,6 +134,16 @@ static void test_gpu() {
}
}
}
+
+ test_forced_contextual_eval(&context);
+ cudaMemcpy(out.data(), d_out, out_bytes, cudaMemcpyDeviceToHost);
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 7; ++k) {
+ VERIFY_IS_APPROX(out(Eigen::array<int, 3>(i,j,k)), (in1(Eigen::array<int, 3>(i,j,k)) + in2(Eigen::array<int, 3>(i,j,k))) * 3.14f + 2.718f);
+ }
+ }
+ }
}
diff --git a/unsupported/test/cxx11_tensor_lvalue.cpp b/unsupported/test/cxx11_tensor_lvalue.cpp
new file mode 100644
index 000000000..071f5b406
--- /dev/null
+++ b/unsupported/test/cxx11_tensor_lvalue.cpp
@@ -0,0 +1,42 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include "main.h"
+
+#include <Eigen/CXX11/Tensor>
+
+using Eigen::Tensor;
+using Eigen::RowMajor;
+
+
+static void test_compound_assignment()
+{
+ Tensor<float, 3> mat1(2,3,7);
+ Tensor<float, 3> mat2(2,3,7);
+ Tensor<float, 3> mat3(2,3,7);
+
+ mat1.setRandom();
+ mat2.setRandom();
+ mat3 = mat1;
+ mat3 += mat2;
+
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 7; ++k) {
+ VERIFY_IS_APPROX(mat3(i,j,k), mat1(i,j,k) + mat2(i,j,k));
+ }
+ }
+ }
+}
+
+
+void test_cxx11_tensor_lvalue()
+{
+ CALL_SUBTEST(test_compound_assignment());
+}
diff --git a/unsupported/test/cxx11_tensor_morphing.cpp b/unsupported/test/cxx11_tensor_morphing.cpp
new file mode 100644
index 000000000..21af9e0b5
--- /dev/null
+++ b/unsupported/test/cxx11_tensor_morphing.cpp
@@ -0,0 +1,72 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include "main.h"
+
+#include <Eigen/CXX11/Tensor>
+
+using Eigen::Tensor;
+
+static void test_simple_reshape()
+{
+ Tensor<float, 5> tensor1(2,3,1,7,1);
+ tensor1.setRandom();
+
+ Tensor<float, 3> tensor2(2,3,7);
+ Tensor<float, 2> tensor3(6,7);
+ Tensor<float, 2> tensor4(2,21);
+
+ Tensor<float, 3>::Dimensions dim1{{2,3,7}};
+ tensor2 = tensor1.reshape(dim1);
+ Tensor<float, 2>::Dimensions dim2{{6,7}};
+ tensor3 = tensor1.reshape(dim2);
+ Tensor<float, 2>::Dimensions dim3{{2,21}};
+ tensor4 = tensor1.reshape(dim1).reshape(dim3);
+
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 3; ++j) {
+ for (int k = 0; k < 7; ++k) {
+ VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor2(i,j,k));
+ VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor3(i+2*j,k));
+ VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor4(i,j+3*k));
+ }
+ }
+ }
+}
+
+
+static void test_reshape_in_expr() {
+ MatrixXf m1(2,3*5*7*11);
+ MatrixXf m2(3*5*7*11,13);
+ m1.setRandom();
+ m2.setRandom();
+ MatrixXf m3 = m1 * m2;
+
+ TensorMap<Tensor<float, 5>> tensor1(m1.data(), 2,3,5,7,11);
+ TensorMap<Tensor<float, 5>> tensor2(m2.data(), 3,5,7,11,13);
+ Tensor<float, 2>::Dimensions newDims1{{2,3*5*7*11}};
+ Tensor<float, 2>::Dimensions newDims2{{3*5*7*11,13}};
+ typedef Tensor<float, 1>::DimensionPair DimPair;
+ array<DimPair, 1> contract_along{{DimPair(1, 0)}};
+ Tensor<float, 2> tensor3(2,13);
+ tensor3 = tensor1.reshape(newDims1).contract(tensor2.reshape(newDims2), contract_along);
+
+ Map<MatrixXf> res(tensor3.data(), 2, 13);
+ for (int i = 0; i < 2; ++i) {
+ for (int j = 0; j < 13; ++j) {
+ VERIFY_IS_APPROX(res(i,j), m3(i,j));
+ }
+ }
+}
+
+void test_cxx11_tensor_morphing()
+{
+ CALL_SUBTEST(test_simple_reshape());
+ CALL_SUBTEST(test_reshape_in_expr());
+}