Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorDevice.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_DEVICE_H
12#define EIGEN_TENSOR_TENSOR_DEVICE_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
30template <typename ExpressionType, typename DeviceType>
31class TensorDevice {
32 public:
33 TensorDevice(const DeviceType& device, ExpressionType& expression) : m_device(device), m_expression(expression) {}
34
35 EIGEN_DEFAULT_COPY_CONSTRUCTOR(TensorDevice)
36
37 template <typename OtherDerived>
38 EIGEN_STRONG_INLINE TensorDevice& operator=(const OtherDerived& other) {
40 Assign assign(m_expression, other);
41 internal::TensorExecutor<const Assign, DeviceType>::run(assign, m_device);
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);
52 internal::TensorExecutor<const Assign, DeviceType>::run(assign, m_device);
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);
64 internal::TensorExecutor<const Assign, DeviceType>::run(assign, m_device);
65 return *this;
66 }
67
68 protected:
69 const DeviceType& m_device;
70 ExpressionType& m_expression;
71};
72
86
87template <typename ExpressionType, typename DeviceType, typename DoneCallback>
88class TensorAsyncDevice {
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
112template <typename ExpressionType, typename DoneCallback>
113class 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_TENSOR_TENSOR_DEVICE_H
Definition TensorAssign.h:47
Pseudo expression providing an operator = that will evaluate its argument asynchronously on the speci...
Definition TensorDevice.h:88
Tensor binary expression.
Definition TensorExpr.h:153
The tensor executor class.
Definition TensorExecutor.h:38
Namespace containing all symbols from the Eigen library.