From f786897e4b96737767effc85bedb78f06dc46dc5 Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Fri, 17 Oct 2014 15:33:27 -0700 Subject: Added access to the unerlying raw data of a tnsor slice/chip whenever possible --- unsupported/test/cxx11_tensor_morphing.cpp | 64 ++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) (limited to 'unsupported/test/cxx11_tensor_morphing.cpp') diff --git a/unsupported/test/cxx11_tensor_morphing.cpp b/unsupported/test/cxx11_tensor_morphing.cpp index fd1b1fa32..78b0dade0 100644 --- a/unsupported/test/cxx11_tensor_morphing.cpp +++ b/unsupported/test/cxx11_tensor_morphing.cpp @@ -12,7 +12,6 @@ #include using Eigen::Tensor; -using Eigen::IndexPair; static void test_simple_reshape() { @@ -53,7 +52,8 @@ static void test_reshape_in_expr() { TensorMap> tensor2(m2.data(), 3,5,7,11,13); Tensor::Dimensions newDims1{{2,3*5*7*11}}; Tensor::Dimensions newDims2{{3*5*7*11,13}}; - Eigen::array, 1> contract_along{{IndexPair(1, 0)}}; + typedef Tensor::DimensionPair DimPair; + array contract_along{{DimPair(1, 0)}}; Tensor tensor3(2,13); tensor3 = tensor1.reshape(newDims1).contract(tensor2.reshape(newDims2), contract_along); @@ -126,7 +126,8 @@ static void test_slice_in_expr() { TensorMap> tensor1(m1.data(), 7, 7); TensorMap> tensor2(m2.data(), 3, 3); Tensor tensor3(3,1); - array, 1> contract_along{{IndexPair(1, 0)}}; + typedef Tensor::DimensionPair DimPair; + array contract_along{{DimPair(1, 0)}}; Eigen::DSizes indices1(1,2); Eigen::DSizes sizes1(3,3); @@ -190,6 +191,62 @@ static void test_slice_as_lvalue() } +static void test_slice_raw_data() +{ + Tensor tensor(3,5,7,11); + tensor.setRandom(); + + Eigen::DSizes offsets(1,2,3,4); + Eigen::DSizes extents(1,1,1,1); + typedef TensorEvaluator SliceEvaluator; + auto slice1 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); + VERIFY_IS_EQUAL(slice1.dimensions().TotalSize(), 1ul); + VERIFY_IS_EQUAL(slice1.data()[0], tensor(1,2,3,4)); + + extents = Eigen::DSizes(2,1,1,1); + auto slice2 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); + VERIFY_IS_EQUAL(slice2.dimensions().TotalSize(), 2ul); + VERIFY_IS_EQUAL(slice2.data()[0], tensor(1,2,3,4)); + VERIFY_IS_EQUAL(slice2.data()[1], tensor(2,2,3,4)); + + extents = Eigen::DSizes(1,2,1,1); + auto slice3 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); + VERIFY_IS_EQUAL(slice3.dimensions().TotalSize(), 2ul); + VERIFY_IS_EQUAL(slice3.data(), static_cast(0)); + + offsets = Eigen::DSizes(0,2,3,4); + extents = Eigen::DSizes(3,2,1,1); + auto slice4 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); + VERIFY_IS_EQUAL(slice4.dimensions().TotalSize(), 6ul); + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 2; ++j) { + VERIFY_IS_EQUAL(slice4.data()[i+3*j], tensor(i,2+j,3,4)); + } + } + + offsets = Eigen::DSizes(0,0,0,4); + extents = Eigen::DSizes(3,5,7,2); + auto slice5 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); + VERIFY_IS_EQUAL(slice5.dimensions().TotalSize(), 210ul); + 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 < 2; ++l) { + int slice_index = i + 3 * (j + 5 * (k + 7 * l)); + VERIFY_IS_EQUAL(slice5.data()[slice_index], tensor(i,j,k,l+4)); + } + } + } + } + + offsets = Eigen::DSizes(0,0,0,0); + extents = Eigen::DSizes(3,5,7,11); + auto slice6 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); + VERIFY_IS_EQUAL(slice6.dimensions().TotalSize(), 3ul*5*7*11); + VERIFY_IS_EQUAL(slice6.data(), tensor.data()); +} + + void test_cxx11_tensor_morphing() { CALL_SUBTEST(test_simple_reshape()); @@ -199,4 +256,5 @@ void test_cxx11_tensor_morphing() CALL_SUBTEST(test_simple_slice()); CALL_SUBTEST(test_slice_in_expr()); CALL_SUBTEST(test_slice_as_lvalue()); + CALL_SUBTEST(test_slice_raw_data()); } -- cgit v1.2.3