TensorDevice.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_DEVICE_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_DEVICE_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
30 template <typename ExpressionType, typename DeviceType>
31 class TensorDevice {
32  public:
33  TensorDevice(const DeviceType& device, ExpressionType& expression) : m_device(device), m_expression(expression) {}
34 
36 
37  template <typename OtherDerived>
38  EIGEN_STRONG_INLINE TensorDevice& operator=(const OtherDerived& other) {
40  Assign assign(m_expression, other);
42  return *this;
43  }
44 
45  template <typename OtherDerived>
46  EIGEN_STRONG_INLINE TensorDevice& operator+=(const OtherDerived& other) {
47  typedef typename OtherDerived::Scalar Scalar;
48  typedef TensorCwiseBinaryOp<internal::scalar_sum_op<Scalar>, const ExpressionType, const OtherDerived> Sum;
49  Sum sum(m_expression, other);
51  Assign assign(m_expression, sum);
53  return *this;
54  }
55 
56  template <typename OtherDerived>
57  EIGEN_STRONG_INLINE TensorDevice& operator-=(const OtherDerived& other) {
58  typedef typename OtherDerived::Scalar Scalar;
59  typedef TensorCwiseBinaryOp<internal::scalar_difference_op<Scalar>, const ExpressionType, const OtherDerived>
60  Difference;
61  Difference difference(m_expression, other);
63  Assign assign(m_expression, difference);
65  return *this;
66  }
67 
68  protected:
69  const DeviceType& m_device;
70  ExpressionType& m_expression;
71 };
72 
87 template <typename ExpressionType, typename DeviceType, typename DoneCallback>
89  public:
90  TensorAsyncDevice(const DeviceType& device, ExpressionType& expression, DoneCallback done)
91  : m_device(device), m_expression(expression), m_done(std::move(done)) {}
92 
93  template <typename OtherDerived>
94  EIGEN_STRONG_INLINE TensorAsyncDevice& operator=(const OtherDerived& other) {
97 
98  Assign assign(m_expression, other);
99  Executor::run(assign, m_device);
100  m_done();
101 
102  return *this;
103  }
104 
105  protected:
106  const DeviceType& m_device;
107  ExpressionType& m_expression;
108  DoneCallback m_done;
109 };
110 
111 #ifdef EIGEN_USE_THREADS
112 template <typename ExpressionType, typename DoneCallback>
113 class TensorAsyncDevice<ExpressionType, ThreadPoolDevice, DoneCallback> {
114  public:
115  TensorAsyncDevice(const ThreadPoolDevice& device, ExpressionType& expression, DoneCallback done)
116  : m_device(device), m_expression(expression), m_done(std::move(done)) {}
117 
118  template <typename OtherDerived>
119  EIGEN_STRONG_INLINE TensorAsyncDevice& operator=(const OtherDerived& other) {
120  typedef TensorAssignOp<ExpressionType, const OtherDerived> Assign;
121  typedef internal::TensorAsyncExecutor<const Assign, ThreadPoolDevice, DoneCallback> Executor;
122 
123  // WARNING: After assignment 'm_done' callback will be in undefined state.
124  Assign assign(m_expression, other);
125  Executor::runAsync(assign, m_device, std::move(m_done));
126 
127  return *this;
128  }
129 
130  protected:
131  const ThreadPoolDevice& m_device;
132  ExpressionType& m_expression;
133  DoneCallback m_done;
134 };
135 #endif
136 
137 } // end namespace Eigen
138 
139 #endif // EIGEN_CXX11_TENSOR_TENSOR_DEVICE_H
#define EIGEN_DEFAULT_COPY_CONSTRUCTOR(CLASS)
Macro to explicitly define the default copy constructor. This is necessary, because the implicit defi...
Definition: Macros.h:1119
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
SCALAR Scalar
Definition: bench_gemm.cpp:45
Definition: TensorAssign.h:57
Pseudo expression providing an operator = that will evaluate its argument asynchronously on the speci...
Definition: TensorDevice.h:88
TensorAsyncDevice(const DeviceType &device, ExpressionType &expression, DoneCallback done)
Definition: TensorDevice.h:90
const DeviceType & m_device
Definition: TensorDevice.h:106
EIGEN_STRONG_INLINE TensorAsyncDevice & operator=(const OtherDerived &other)
Definition: TensorDevice.h:94
ExpressionType & m_expression
Definition: TensorDevice.h:107
DoneCallback m_done
Definition: TensorDevice.h:108
Definition: TensorExpr.h:162
Pseudo expression providing an operator = that will evaluate its argument on the specified computing ...
Definition: TensorDevice.h:31
TensorDevice(const DeviceType &device, ExpressionType &expression)
Definition: TensorDevice.h:33
EIGEN_STRONG_INLINE TensorDevice & operator+=(const OtherDerived &other)
Definition: TensorDevice.h:46
EIGEN_STRONG_INLINE TensorDevice & operator-=(const OtherDerived &other)
Definition: TensorDevice.h:57
const DeviceType & m_device
Definition: TensorDevice.h:69
ExpressionType & m_expression
Definition: TensorDevice.h:70
EIGEN_STRONG_INLINE TensorDevice & operator=(const OtherDerived &other)
Definition: TensorDevice.h:38
Definition: TensorExecutor.h:78
static EIGEN_STRONG_INLINE void run(const Expression &expr, const Device &device=DefaultDevice())
Definition: TensorExecutor.h:92
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317