aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/test
diff options
context:
space:
mode:
authorGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2015-08-31 08:18:53 -0700
committerGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2015-08-31 08:18:53 -0700
commitf41831e445f3fdd9dc324561135b2a19eafd9a56 (patch)
tree045cb917d62685b342ce129e384e03e63c916898 /unsupported/test
parent2ab603316af7c1bcf1d5e87d9ba50a2589b36e37 (diff)
Added support for argmax/argmin
Diffstat (limited to 'unsupported/test')
-rw-r--r--unsupported/test/CMakeLists.txt2
-rw-r--r--unsupported/test/cxx11_tensor_argmax.cpp294
-rw-r--r--unsupported/test/cxx11_tensor_argmax_cuda.cpp241
3 files changed, 537 insertions, 0 deletions
diff --git a/unsupported/test/CMakeLists.txt b/unsupported/test/CMakeLists.txt
index 7c8fb8dde..b161cb370 100644
--- a/unsupported/test/CMakeLists.txt
+++ b/unsupported/test/CMakeLists.txt
@@ -130,6 +130,7 @@ if(EIGEN_TEST_CXX11)
ei_add_test(cxx11_tensor_image_patch "-std=c++0x")
ei_add_test(cxx11_tensor_volume_patch "-std=c++0x")
ei_add_test(cxx11_tensor_reduction "-std=c++0x")
+ ei_add_test(cxx11_tensor_argmax "-std=c++0x")
ei_add_test(cxx11_tensor_shuffling "-std=c++0x")
ei_add_test(cxx11_tensor_striding "-std=c++0x")
ei_add_test(cxx11_tensor_thread_pool "-std=c++0x")
@@ -148,5 +149,6 @@ if(EIGEN_TEST_CXX11)
# ei_add_test(cxx11_tensor_contract_cuda "-std=c++0x")
# ei_add_test(cxx11_tensor_reduction_cuda "-std=c++0x")
# ei_add_test(cxx11_tensor_random_cuda "-std=c++0x")
+# ei_add_test(cxx11_tensor_argmax_cuda "-std=c++0x")
endif()
diff --git a/unsupported/test/cxx11_tensor_argmax.cpp b/unsupported/test/cxx11_tensor_argmax.cpp
new file mode 100644
index 000000000..4c532409e
--- /dev/null
+++ b/unsupported/test/cxx11_tensor_argmax.cpp
@@ -0,0 +1,294 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2015 Eugene Brevdo <ebrevdo@google.com>
+// 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::array;
+using Eigen::Tuple;
+
+template <int DataLayout>
+static void test_simple_index_tuples()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ Tensor<Tuple<DenseIndex, float>, 4, DataLayout> index_tuples(2,3,5,7);
+ index_tuples = tensor.index_tuples();
+
+ for (DenseIndex n = 0; n < 2*3*5*7; ++n) {
+ const Tuple<DenseIndex, float>& v = index_tuples.coeff(n);
+ VERIFY_IS_EQUAL(v.first, n);
+ VERIFY_IS_EQUAL(v.second, tensor.coeff(n));
+ }
+}
+
+template <int DataLayout>
+static void test_index_tuples_dim()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ Tensor<Tuple<DenseIndex, float>, 4, DataLayout> index_tuples(2,3,5,7);
+
+ index_tuples = tensor.index_tuples();
+
+ for (Eigen::DenseIndex n = 0; n < tensor.size(); ++n) {
+ const Tuple<DenseIndex, float>& v = index_tuples(n); //(i, j, k, l);
+ VERIFY_IS_EQUAL(v.first, n);
+ VERIFY_IS_EQUAL(v.second, tensor(n));
+ }
+}
+
+template <int DataLayout>
+static void test_argmax_tuple_reducer()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ Tensor<Tuple<DenseIndex, float>, 4, DataLayout> index_tuples(2,3,5,7);
+ index_tuples = tensor.index_tuples();
+
+ Tensor<Tuple<DenseIndex, float>, 1, DataLayout> reduced(1);
+ DimensionList<DenseIndex, 4> dims;
+ reduced = index_tuples.reduce(
+ dims, internal::ArgMaxTupleReducer<Tuple<DenseIndex, float>>());
+
+ Tensor<float, 1, DataLayout> maxi = tensor.maximum();
+
+ VERIFY_IS_EQUAL(maxi(0), reduced(0).second);
+
+ array<DenseIndex, 3> reduce_dims;
+ for (int d = 0; d < 3; ++d) reduce_dims[d] = d;
+ Tensor<Tuple<DenseIndex, float>, 1, DataLayout> reduced_by_dims(7);
+ reduced_by_dims = index_tuples.reduce(
+ reduce_dims, internal::ArgMaxTupleReducer<Tuple<DenseIndex, float>>());
+
+ Tensor<float, 1, DataLayout> max_by_dims = tensor.maximum(reduce_dims);
+
+ for (int l = 0; l < 7; ++l) {
+ VERIFY_IS_EQUAL(max_by_dims(l), reduced_by_dims(l).second);
+ }
+}
+
+template <int DataLayout>
+static void test_argmin_tuple_reducer()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ Tensor<Tuple<DenseIndex, float>, 4, DataLayout> index_tuples(2,3,5,7);
+ index_tuples = tensor.index_tuples();
+
+ Tensor<Tuple<DenseIndex, float>, 1, DataLayout> reduced(1);
+ DimensionList<DenseIndex, 4> dims;
+ reduced = index_tuples.reduce(
+ dims, internal::ArgMinTupleReducer<Tuple<DenseIndex, float>>());
+
+ Tensor<float, 1, DataLayout> mini = tensor.minimum();
+
+ VERIFY_IS_EQUAL(mini(0), reduced(0).second);
+
+ array<DenseIndex, 3> reduce_dims;
+ for (int d = 0; d < 3; ++d) reduce_dims[d] = d;
+ Tensor<Tuple<DenseIndex, float>, 1, DataLayout> reduced_by_dims(7);
+ reduced_by_dims = index_tuples.reduce(
+ reduce_dims, internal::ArgMinTupleReducer<Tuple<DenseIndex, float>>());
+
+ Tensor<float, 1, DataLayout> min_by_dims = tensor.minimum(reduce_dims);
+
+ for (int l = 0; l < 7; ++l) {
+ VERIFY_IS_EQUAL(min_by_dims(l), reduced_by_dims(l).second);
+ }
+}
+
+template <int DataLayout>
+static void test_simple_argmax()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+ tensor(0,0,0,0) = 10.0;
+
+ Tensor<DenseIndex, 1, DataLayout> tensor_argmax(1);
+
+ tensor_argmax = tensor.argmax();
+
+ VERIFY_IS_EQUAL(tensor_argmax(0), 0);
+
+ tensor(1,2,4,6) = 20.0;
+
+ tensor_argmax = tensor.argmax();
+
+ VERIFY_IS_EQUAL(tensor_argmax(0), 2*3*5*7 - 1);
+}
+
+template <int DataLayout>
+static void test_simple_argmin()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+ tensor(0,0,0,0) = -10.0;
+
+ Tensor<DenseIndex, 1, DataLayout> tensor_argmin(1);
+
+ tensor_argmin = tensor.argmin();
+
+ VERIFY_IS_EQUAL(tensor_argmin(0), 0);
+
+ tensor(1,2,4,6) = -20.0;
+
+ tensor_argmin = tensor.argmin();
+
+ VERIFY_IS_EQUAL(tensor_argmin(0), 2*3*5*7 - 1);
+}
+
+template <int DataLayout>
+static void test_argmax_dim()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ std::vector<int> dims {2, 3, 5, 7};
+
+ for (int dim = 0; dim < 4; ++dim) {
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ Tensor<DenseIndex, 3, DataLayout> tensor_argmax;
+ array<DenseIndex, 4> ix;
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != 0) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = 10.0
+ tensor(ix) = 10.0;
+ }
+ }
+ }
+ }
+
+ tensor_argmax = tensor.argmax(dim);
+
+ VERIFY_IS_EQUAL(tensor_argmax.dimensions().TotalSize(),
+ size_t(2*3*5*7 / tensor.dimension(dim)));
+ for (size_t n = 0; n < tensor_argmax.dimensions().TotalSize(); ++n) {
+ // Expect max to be in the first index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_argmax.data()[n], 0);
+ }
+
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != tensor.dimension(dim) - 1) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = 20.0
+ tensor(ix) = 20.0;
+ }
+ }
+ }
+ }
+
+ tensor_argmax = tensor.argmax(dim);
+
+ VERIFY_IS_EQUAL(tensor_argmax.dimensions().TotalSize(),
+ size_t(2*3*5*7 / tensor.dimension(dim)));
+ for (size_t n = 0; n < tensor_argmax.dimensions().TotalSize(); ++n) {
+ // Expect max to be in the last index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_argmax.data()[n], tensor.dimension(dim) - 1);
+ }
+ }
+}
+
+template <int DataLayout>
+static void test_argmin_dim()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ std::vector<int> dims {2, 3, 5, 7};
+
+ for (int dim = 0; dim < 4; ++dim) {
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ Tensor<DenseIndex, 3, DataLayout> tensor_argmin;
+ array<DenseIndex, 4> ix;
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != 0) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = -10.0
+ tensor(ix) = -10.0;
+ }
+ }
+ }
+ }
+
+ tensor_argmin = tensor.argmin(dim);
+
+ VERIFY_IS_EQUAL(tensor_argmin.dimensions().TotalSize(),
+ size_t(2*3*5*7 / tensor.dimension(dim)));
+ for (size_t n = 0; n < tensor_argmin.dimensions().TotalSize(); ++n) {
+ // Expect min to be in the first index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_argmin.data()[n], 0);
+ }
+
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != tensor.dimension(dim) - 1) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = -20.0
+ tensor(ix) = -20.0;
+ }
+ }
+ }
+ }
+
+ tensor_argmin = tensor.argmin(dim);
+
+ VERIFY_IS_EQUAL(tensor_argmin.dimensions().TotalSize(),
+ size_t(2*3*5*7 / tensor.dimension(dim)));
+ for (size_t n = 0; n < tensor_argmin.dimensions().TotalSize(); ++n) {
+ // Expect min to be in the last index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_argmin.data()[n], tensor.dimension(dim) - 1);
+ }
+ }
+}
+
+void test_cxx11_tensor_argmax()
+{
+ CALL_SUBTEST(test_simple_index_tuples<RowMajor>());
+ CALL_SUBTEST(test_simple_index_tuples<ColMajor>());
+ CALL_SUBTEST(test_index_tuples_dim<RowMajor>());
+ CALL_SUBTEST(test_index_tuples_dim<ColMajor>());
+ CALL_SUBTEST(test_argmax_tuple_reducer<RowMajor>());
+ CALL_SUBTEST(test_argmax_tuple_reducer<ColMajor>());
+ CALL_SUBTEST(test_argmin_tuple_reducer<RowMajor>());
+ CALL_SUBTEST(test_argmin_tuple_reducer<ColMajor>());
+ CALL_SUBTEST(test_simple_argmax<RowMajor>());
+ CALL_SUBTEST(test_simple_argmax<ColMajor>());
+ CALL_SUBTEST(test_simple_argmin<RowMajor>());
+ CALL_SUBTEST(test_simple_argmin<ColMajor>());
+ CALL_SUBTEST(test_argmax_dim<RowMajor>());
+ CALL_SUBTEST(test_argmax_dim<ColMajor>());
+ CALL_SUBTEST(test_argmin_dim<RowMajor>());
+ CALL_SUBTEST(test_argmin_dim<ColMajor>());
+}
diff --git a/unsupported/test/cxx11_tensor_argmax_cuda.cpp b/unsupported/test/cxx11_tensor_argmax_cuda.cpp
new file mode 100644
index 000000000..d37490d15
--- /dev/null
+++ b/unsupported/test/cxx11_tensor_argmax_cuda.cpp
@@ -0,0 +1,241 @@
+// 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/.
+
+// TODO(mdevin): Free the cuda memory.
+
+#define EIGEN_TEST_FUNC cxx11_tensor_cuda
+#define EIGEN_USE_GPU
+
+#include "main.h"
+#include <unsupported/Eigen/CXX11/Tensor>
+
+using Eigen::Tensor;
+
+template <int Layout>
+void test_cuda_simple_argmax()
+{
+ Tensor<double, 3, Layout> in(Eigen::array<DenseIndex, 3>(72,53,97));
+ Tensor<DenseIndex, 1, Layout> out_max(Eigen::array<DenseIndex, 1>(1));
+ Tensor<DenseIndex, 1, Layout> out_min(Eigen::array<DenseIndex, 1>(1));
+ in.setRandom();
+ in *= in.constant(100.0);
+ in(0, 0, 0) = -1000.0;
+ in(71, 52, 96) = 1000.0;
+
+ std::size_t in_bytes = in.size() * sizeof(double);
+ std::size_t out_bytes = out_max.size() * sizeof(DenseIndex);
+
+ double* d_in;
+ DenseIndex* d_out_max;
+ DenseIndex* d_out_min;
+ cudaMalloc((void**)(&d_in), in_bytes);
+ cudaMalloc((void**)(&d_out_max), out_bytes);
+ cudaMalloc((void**)(&d_out_min), out_bytes);
+
+ cudaMemcpy(d_in, in.data(), in_bytes, cudaMemcpyHostToDevice);
+
+ Eigen::CudaStreamDevice stream;
+ Eigen::GpuDevice gpu_device(&stream);
+
+ Eigen::TensorMap<Eigen::Tensor<double, 3, Layout>, Aligned > gpu_in(d_in, Eigen::array<DenseIndex, 3>(72,53,97));
+ Eigen::TensorMap<Eigen::Tensor<DenseIndex, 1, Layout>, Aligned > gpu_out_max(d_out_max, Eigen::array<DenseIndex, 1>(1));
+ Eigen::TensorMap<Eigen::Tensor<DenseIndex, 1, Layout>, Aligned > gpu_out_min(d_out_min, Eigen::array<DenseIndex, 1>(1));
+
+ gpu_out_max.device(gpu_device) = gpu_in.argmax();
+ gpu_out_min.device(gpu_device) = gpu_in.argmin();
+
+ assert(cudaMemcpyAsync(out_max.data(), d_out_max, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
+ assert(cudaMemcpyAsync(out_min.data(), d_out_min, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
+ assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
+
+ VERIFY_IS_EQUAL(out_max(Eigen::array<DenseIndex, 1>(0)), 72*53*97 - 1);
+ VERIFY_IS_EQUAL(out_min(Eigen::array<DenseIndex, 1>(0)), 0);
+}
+
+template <int DataLayout>
+void test_cuda_argmax_dim()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ std::vector<int> dims;
+ dims.push_back(2); dims.push_back(3); dims.push_back(5); dims.push_back(7);
+
+ for (int dim = 0; dim < 4; ++dim) {
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ array<DenseIndex, 3> out_shape;
+ for (int d = 0; d < 3; ++d) out_shape[d] = (d < dim) ? dims[d] : dims[d+1];
+
+ Tensor<DenseIndex, 3, DataLayout> tensor_arg(out_shape);
+
+ array<DenseIndex, 4> ix;
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != 0) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = 10.0
+ tensor(ix) = 10.0;
+ }
+ }
+ }
+ }
+
+ std::size_t in_bytes = tensor.size() * sizeof(float);
+ std::size_t out_bytes = tensor_arg.size() * sizeof(DenseIndex);
+
+ float* d_in;
+ DenseIndex* d_out;
+ cudaMalloc((void**)(&d_in), in_bytes);
+ cudaMalloc((void**)(&d_out), out_bytes);
+
+ cudaMemcpy(d_in, tensor.data(), in_bytes, cudaMemcpyHostToDevice);
+
+ Eigen::CudaStreamDevice stream;
+ Eigen::GpuDevice gpu_device(&stream);
+
+ Eigen::TensorMap<Eigen::Tensor<float, 4, DataLayout>, Aligned > gpu_in(d_in, Eigen::array<DenseIndex, 4>(2, 3, 5, 7));
+ Eigen::TensorMap<Eigen::Tensor<DenseIndex, 3, DataLayout>, Aligned > gpu_out(d_out, out_shape);
+
+ gpu_out.device(gpu_device) = gpu_in.argmax(dim);
+
+ assert(cudaMemcpyAsync(tensor_arg.data(), d_out, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
+ assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
+
+ VERIFY_IS_EQUAL(tensor_arg.dimensions().TotalSize(),
+ size_t(2*3*5*7 / tensor.dimension(dim)));
+
+ for (size_t n = 0; n < tensor_arg.dimensions().TotalSize(); ++n) {
+ // Expect max to be in the first index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_arg.data()[n], 0);
+ }
+
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != tensor.dimension(dim) - 1) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = 20.0
+ tensor(ix) = 20.0;
+ }
+ }
+ }
+ }
+
+ cudaMemcpy(d_in, tensor.data(), in_bytes, cudaMemcpyHostToDevice);
+
+ gpu_out.device(gpu_device) = gpu_in.argmax(dim);
+
+ assert(cudaMemcpyAsync(tensor_arg.data(), d_out, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
+ assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
+
+ for (size_t n = 0; n < tensor_arg.dimensions().TotalSize(); ++n) {
+ // Expect max to be in the last index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_arg.data()[n], tensor.dimension(dim) - 1);
+ }
+ }
+}
+
+template <int DataLayout>
+void test_cuda_argmin_dim()
+{
+ Tensor<float, 4, DataLayout> tensor(2,3,5,7);
+ std::vector<int> dims;
+ dims.push_back(2); dims.push_back(3); dims.push_back(5); dims.push_back(7);
+
+ for (int dim = 0; dim < 4; ++dim) {
+ tensor.setRandom();
+ tensor = (tensor + tensor.constant(0.5)).log();
+
+ array<DenseIndex, 3> out_shape;
+ for (int d = 0; d < 3; ++d) out_shape[d] = (d < dim) ? dims[d] : dims[d+1];
+
+ Tensor<DenseIndex, 3, DataLayout> tensor_arg(out_shape);
+
+ array<DenseIndex, 4> ix;
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != 0) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 0, k, l) = 10.0
+ tensor(ix) = -10.0;
+ }
+ }
+ }
+ }
+
+ std::size_t in_bytes = tensor.size() * sizeof(float);
+ std::size_t out_bytes = tensor_arg.size() * sizeof(DenseIndex);
+
+ float* d_in;
+ DenseIndex* d_out;
+ cudaMalloc((void**)(&d_in), in_bytes);
+ cudaMalloc((void**)(&d_out), out_bytes);
+
+ cudaMemcpy(d_in, tensor.data(), in_bytes, cudaMemcpyHostToDevice);
+
+ Eigen::CudaStreamDevice stream;
+ Eigen::GpuDevice gpu_device(&stream);
+
+ Eigen::TensorMap<Eigen::Tensor<float, 4, DataLayout>, Aligned > gpu_in(d_in, Eigen::array<DenseIndex, 4>(2, 3, 5, 7));
+ Eigen::TensorMap<Eigen::Tensor<DenseIndex, 3, DataLayout>, Aligned > gpu_out(d_out, out_shape);
+
+ gpu_out.device(gpu_device) = gpu_in.argmin(dim);
+
+ assert(cudaMemcpyAsync(tensor_arg.data(), d_out, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
+ assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
+
+ VERIFY_IS_EQUAL(tensor_arg.dimensions().TotalSize(),
+ size_t(2*3*5*7 / tensor.dimension(dim)));
+
+ for (size_t n = 0; n < tensor_arg.dimensions().TotalSize(); ++n) {
+ // Expect min to be in the first index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_arg.data()[n], 0);
+ }
+
+ 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) {
+ ix[0] = i; ix[1] = j; ix[2] = k; ix[3] = l;
+ if (ix[dim] != tensor.dimension(dim) - 1) continue;
+ // suppose dim == 1, then for all i, k, l, set tensor(i, 2, k, l) = 20.0
+ tensor(ix) = -20.0;
+ }
+ }
+ }
+ }
+
+ cudaMemcpy(d_in, tensor.data(), in_bytes, cudaMemcpyHostToDevice);
+
+ gpu_out.device(gpu_device) = gpu_in.argmin(dim);
+
+ assert(cudaMemcpyAsync(tensor_arg.data(), d_out, out_bytes, cudaMemcpyDeviceToHost, gpu_device.stream()) == cudaSuccess);
+ assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
+
+ for (size_t n = 0; n < tensor_arg.dimensions().TotalSize(); ++n) {
+ // Expect max to be in the last index of the reduced dimension
+ VERIFY_IS_EQUAL(tensor_arg.data()[n], tensor.dimension(dim) - 1);
+ }
+ }
+}
+
+void test_cxx11_tensor_cuda()
+{
+ CALL_SUBTEST(test_cuda_simple_argmax<RowMajor>());
+ CALL_SUBTEST(test_cuda_simple_argmax<ColMajor>());
+ CALL_SUBTEST(test_cuda_argmax_dim<RowMajor>());
+ CALL_SUBTEST(test_cuda_argmax_dim<ColMajor>());
+ CALL_SUBTEST(test_cuda_argmin_dim<RowMajor>());
+ CALL_SUBTEST(test_cuda_argmin_dim<ColMajor>());
+}