aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/Eigen/CXX11/src/Tensor/TensorSyclExtractFunctors.h
blob: f69c5afcb16229c7059ac0b763bf7cb86cd804e3 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Mehdi Goli    Codeplay Software Ltd.
// Ralph Potter  Codeplay Software Ltd.
// Luke Iwanski  Codeplay Software Ltd.
// Contact: <eigen@codeplay.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/.

/*****************************************************************
 * TensorSyclextractFunctors.h
 *
 * \brief:
 *  Used to extract all the functors allocated to each node of the expression
*tree.
 *
*****************************************************************/

#ifndef UNSUPPORTED_EIGEN_CXX11_SRC_TENSORYSYCL_EXTRACT_FUNCTORS_HPP
#define UNSUPPORTED_EIGEN_CXX11_SRC_TENSORYSYCL_EXTRACT_FUNCTORS_HPP

namespace Eigen {
namespace TensorSycl {
namespace internal {
/// \struct FunctorExtractor:  This struct is used to extract the functors
/// constructed on
/// the host-side, to pack them and reuse them in reconstruction of the
/// expression on the device.
/// We have to do that as in Eigen the functors are not stateless so we cannot
/// re-instantiate them on the device.
/// We have to pass whatever instantiated to the device.
template <typename Evaluator>
struct FunctorExtractor;

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorMap:
template <typename PlainObjectType, int Options_, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<TensorMap<PlainObjectType, Options_>, Dev>> {
  using Dimensions = typename PlainObjectType::Dimensions;
  const Dimensions m_dimensions;
  const Dimensions& dimensions() const { return m_dimensions; }
  FunctorExtractor(
      const TensorEvaluator<TensorMap<PlainObjectType, Options_>, Dev>& expr)
      : m_dimensions(expr.dimensions()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorMap
template <typename PlainObjectType, int Options_, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<const TensorMap<PlainObjectType, Options_>, Dev>> {
  using Dimensions = typename PlainObjectType::Dimensions;
  const Dimensions m_dimensions;
  const Dimensions& dimensions() const { return m_dimensions; }
  FunctorExtractor(
      const TensorEvaluator<const TensorMap<PlainObjectType, Options_>, Dev>&
          expr)
      : m_dimensions(expr.dimensions()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorForcedEvalOp
template <typename Expr, typename Dev>
struct FunctorExtractor<TensorEvaluator<TensorForcedEvalOp<Expr>, Dev>> {
  using Dimensions = typename Expr::Dimensions;
  const Dimensions m_dimensions;
  const Dimensions& dimensions() const { return m_dimensions; }
  FunctorExtractor(const TensorEvaluator<TensorForcedEvalOp<Expr>, Dev>& expr)
      : m_dimensions(expr.dimensions()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorForcedEvalOp
template <typename Expr, typename Dev>
struct FunctorExtractor<TensorEvaluator<const TensorForcedEvalOp<Expr>, Dev>> {
  using Dimensions =
      typename TensorEvaluator<const TensorForcedEvalOp<Expr>, Dev>::Dimensions;
  const Dimensions m_dimensions;
  const Dimensions& dimensions() const { return m_dimensions; }
  FunctorExtractor(
      const TensorEvaluator<const TensorForcedEvalOp<Expr>, Dev>& expr)
      : m_dimensions(expr.dimensions()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorCwiseNullaryOp
template <typename OP, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<TensorCwiseNullaryOp<OP, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(
      TensorEvaluator<TensorCwiseNullaryOp<OP, RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()), func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorCwiseNullaryOp
template <typename OP, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<const TensorCwiseNullaryOp<OP, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(
      const TensorEvaluator<const TensorCwiseNullaryOp<OP, RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()), func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorBroadcastingOp
template <typename OP, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<TensorBroadcastingOp<OP, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(
      const TensorEvaluator<TensorBroadcastingOp<OP, RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()), func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorBroadcastingOp
template <typename OP, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<const TensorBroadcastingOp<OP, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(
      const TensorEvaluator<const TensorBroadcastingOp<OP, RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()), func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorCwiseUnaryOp
template <typename OP, typename RHSExpr, typename Dev>
struct FunctorExtractor<TensorEvaluator<TensorCwiseUnaryOp<OP, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(
      const TensorEvaluator<TensorCwiseUnaryOp<OP, RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()), func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorCwiseUnaryOp
template <typename OP, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<const TensorCwiseUnaryOp<OP, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(
      const TensorEvaluator<const TensorCwiseUnaryOp<OP, RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()), func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorCwiseBinaryOp
template <typename OP, typename LHSExpr, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<TensorCwiseBinaryOp<OP, LHSExpr, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<LHSExpr, Dev>> lhsExpr;
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(
      const TensorEvaluator<TensorCwiseBinaryOp<OP, LHSExpr, RHSExpr>, Dev>&
          expr)
      : lhsExpr(expr.left_impl()),
        rhsExpr(expr.right_impl()),
        func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorCwiseBinaryOp
template <typename OP, typename LHSExpr, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<const TensorCwiseBinaryOp<OP, LHSExpr, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<LHSExpr, Dev>> lhsExpr;
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  OP func;
  FunctorExtractor(const TensorEvaluator<
                   const TensorCwiseBinaryOp<OP, LHSExpr, RHSExpr>, Dev>& expr)
      : lhsExpr(expr.left_impl()),
        rhsExpr(expr.right_impl()),
        func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorCwiseTernaryOp
template <typename OP, typename Arg1Expr, typename Arg2Expr, typename Arg3Expr,
          typename Dev>
struct FunctorExtractor<TensorEvaluator<
    const TensorCwiseTernaryOp<OP, Arg1Expr, Arg2Expr, Arg3Expr>, Dev>> {
  FunctorExtractor<TensorEvaluator<Arg1Expr, Dev>> arg1Expr;
  FunctorExtractor<TensorEvaluator<Arg2Expr, Dev>> arg2Expr;
  FunctorExtractor<TensorEvaluator<Arg3Expr, Dev>> arg3Expr;
  OP func;
  FunctorExtractor(const TensorEvaluator<
                   const TensorCwiseTernaryOp<OP, Arg1Expr, Arg2Expr, Arg3Expr>,
                   Dev>& expr)
      : arg1Expr(expr.arg1Impl()),
        arg2Expr(expr.arg2Impl()),
        arg3Expr(expr.arg3Impl()),
        func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorCwiseTernaryOp
template <typename OP, typename Arg1Expr, typename Arg2Expr, typename Arg3Expr,
          typename Dev>
struct FunctorExtractor<TensorEvaluator<
    TensorCwiseTernaryOp<OP, Arg1Expr, Arg2Expr, Arg3Expr>, Dev>> {
  FunctorExtractor<TensorEvaluator<Arg1Expr, Dev>> arg1Expr;
  FunctorExtractor<TensorEvaluator<Arg2Expr, Dev>> arg2Expr;
  FunctorExtractor<TensorEvaluator<Arg3Expr, Dev>> arg3Expr;
  OP func;
  FunctorExtractor(
      const TensorEvaluator<
          TensorCwiseTernaryOp<OP, Arg1Expr, Arg2Expr, Arg3Expr>, Dev>& expr)
      : arg1Expr(expr.arg1Impl()),
        arg2Expr(expr.arg2Impl()),
        arg3Expr(expr.arg3Impl()),
        func(expr.functor()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorCwiseSelectOp
template <typename IfExpr, typename ThenExpr, typename ElseExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<const TensorSelectOp<IfExpr, ThenExpr, ElseExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<IfExpr, Dev>> ifExpr;
  FunctorExtractor<TensorEvaluator<ThenExpr, Dev>> thenExpr;
  FunctorExtractor<TensorEvaluator<ElseExpr, Dev>> elseExpr;
  FunctorExtractor(const TensorEvaluator<
                   const TensorSelectOp<IfExpr, ThenExpr, ElseExpr>, Dev>& expr)
      : ifExpr(expr.cond_impl()),
        thenExpr(expr.then_impl()),
        elseExpr(expr.else_impl()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorCwiseSelectOp
template <typename IfExpr, typename ThenExpr, typename ElseExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<TensorSelectOp<IfExpr, ThenExpr, ElseExpr>, Dev>> {
  FunctorExtractor<IfExpr> ifExpr;
  FunctorExtractor<ThenExpr> thenExpr;
  FunctorExtractor<ElseExpr> elseExpr;
  FunctorExtractor(
      const TensorEvaluator<TensorSelectOp<IfExpr, ThenExpr, ElseExpr>, Dev>&
          expr)
      : ifExpr(expr.cond_impl()),
        thenExpr(expr.then_impl()),
        elseExpr(expr.else_impl()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorAssignOp
template <typename LHSExpr, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<TensorAssignOp<LHSExpr, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<LHSExpr, Dev>> lhsExpr;
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  FunctorExtractor(
      const TensorEvaluator<TensorAssignOp<LHSExpr, RHSExpr>, Dev>& expr)
      : lhsExpr(expr.left_impl()), rhsExpr(expr.right_impl()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorAssignOp
template <typename LHSExpr, typename RHSExpr, typename Dev>
struct FunctorExtractor<
    TensorEvaluator<const TensorAssignOp<LHSExpr, RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<LHSExpr, Dev>> lhsExpr;
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  FunctorExtractor(
      const TensorEvaluator<const TensorAssignOp<LHSExpr, RHSExpr>, Dev>& expr)
      : lhsExpr(expr.left_impl()), rhsExpr(expr.right_impl()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// TensorEvalToOp
template <typename RHSExpr, typename Dev>
struct FunctorExtractor<TensorEvaluator<TensorEvalToOp<RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  FunctorExtractor(const TensorEvaluator<TensorEvalToOp<RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()) {}
};

/// specialisation of the \ref FunctorExtractor struct when the node type is
/// const TensorEvalToOp
template <typename RHSExpr, typename Dev>
struct FunctorExtractor<TensorEvaluator<const TensorEvalToOp<RHSExpr>, Dev>> {
  FunctorExtractor<TensorEvaluator<RHSExpr, Dev>> rhsExpr;
  FunctorExtractor(
      const TensorEvaluator<const TensorEvalToOp<RHSExpr>, Dev>& expr)
      : rhsExpr(expr.impl()) {}
};

/// template deduction function for FunctorExtractor
template <typename Evaluator>
auto extractFunctors(const Evaluator& evaluator)
    -> FunctorExtractor<Evaluator> {
  return FunctorExtractor<Evaluator>(evaluator);
}
}  // namespace internal
}  // namespace TensorSycl
}  // namespace Eigen

#endif  // UNSUPPORTED_EIGEN_CXX11_SRC_TENSORYSYCL_EXTRACT_FUNCTORS_HPP