aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/test/cxx11_tensor_executor.cpp
blob: 5ae45ac5b0d939e73eacd7a58bd742fd3dc7a79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2018 Eugene Zhulenev <ezhulenev@google.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/.

#define EIGEN_USE_THREADS

#include "main.h"

#include <Eigen/CXX11/Tensor>

using Eigen::Index;
using Eigen::Tensor;
using Eigen::RowMajor;
using Eigen::ColMajor;

// A set of tests to verify that different TensorExecutor strategies yields the
// same results for all the ops, supporting tiled execution.

template <typename Device, bool Vectorizable, bool Tileable, int Layout>
static void test_execute_binary_expr(Device d) {
  // Pick a large enough tensor size to bypass small tensor block evaluation
  // optimization.
  Tensor<float, 3> lhs(840, 390, 37);
  Tensor<float, 3> rhs(840, 390, 37);
  Tensor<float, 3> dst(840, 390, 37);

  lhs.setRandom();
  rhs.setRandom();

  const auto expr = lhs + rhs;

  using Assign = TensorAssignOp<decltype(dst), const decltype(expr)>;
  using Executor =
      internal::TensorExecutor<const Assign, Device, Vectorizable, Tileable>;

  Executor::run(Assign(dst, expr), d);

  for (int i = 0; i < 840; ++i) {
    for (int j = 0; j < 390; ++j) {
      for (int k = 0; k < 37; ++k) {
        float sum = lhs(i, j, k) + rhs(i, j, k);
        VERIFY_IS_EQUAL(sum, dst(i, j, k));
      }
    }
  }
}

#define CALL_SUBTEST_COMBINATIONS(NAME)                                        \
  CALL_SUBTEST((NAME<DefaultDevice, false, false, ColMajor>(default_device))); \
  CALL_SUBTEST((NAME<DefaultDevice, false, true, ColMajor>(default_device)));  \
  CALL_SUBTEST((NAME<DefaultDevice, true, false, ColMajor>(default_device)));  \
  CALL_SUBTEST((NAME<DefaultDevice, true, true, ColMajor>(default_device)));   \
  CALL_SUBTEST((NAME<DefaultDevice, false, false, RowMajor>(default_device))); \
  CALL_SUBTEST((NAME<DefaultDevice, false, true, RowMajor>(default_device)));  \
  CALL_SUBTEST((NAME<DefaultDevice, true, false, RowMajor>(default_device)));  \
  CALL_SUBTEST((NAME<DefaultDevice, true, true, RowMajor>(default_device)));   \
  CALL_SUBTEST((NAME<ThreadPoolDevice, false, false, ColMajor>(tp_device)));   \
  CALL_SUBTEST((NAME<ThreadPoolDevice, false, true, ColMajor>(tp_device)));    \
  CALL_SUBTEST((NAME<ThreadPoolDevice, true, false, ColMajor>(tp_device)));    \
  CALL_SUBTEST((NAME<ThreadPoolDevice, true, true, ColMajor>(tp_device)));     \
  CALL_SUBTEST((NAME<ThreadPoolDevice, false, false, RowMajor>(tp_device)));   \
  CALL_SUBTEST((NAME<ThreadPoolDevice, false, true, RowMajor>(tp_device)));    \
  CALL_SUBTEST((NAME<ThreadPoolDevice, true, false, RowMajor>(tp_device)));    \
  CALL_SUBTEST((NAME<ThreadPoolDevice, true, true, RowMajor>(tp_device)))

EIGEN_DECLARE_TEST(cxx11_tensor_executor) {
  Eigen::DefaultDevice default_device;

  const auto num_threads = internal::random<int>(1, 24);
  Eigen::ThreadPool tp(num_threads);
  Eigen::ThreadPoolDevice tp_device(&tp, num_threads);

  CALL_SUBTEST_COMBINATIONS(test_execute_binary_expr);
}

#undef CALL_SUBTEST_COMBINATIONS