// 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: // // 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/. /***************************************************************** * TensorSyclExprConstructor.h * * \brief: * This file re-create an expression on the SYCL device in order * to use the original tensor evaluator. * *****************************************************************/ #ifndef UNSUPPORTED_EIGEN_CXX11_SRC_TENSORYSYCL_EXPR_CONSTRUCTOR_HPP #define UNSUPPORTED_EIGEN_CXX11_SRC_TENSORYSYCL_EXPR_CONSTRUCTOR_HPP namespace Eigen { namespace TensorSycl { namespace internal { /// this class is used by EvalToOp in order to create an lhs expression which is /// a pointer from an accessor on device-only buffer template struct EvalToLHSConstructor { PtrType expr; EvalToLHSConstructor(const utility::tuple::Tuple &t) : expr((&(*(utility::tuple::get(t).get_pointer())))) {} }; /// \struct ExprConstructor is used to reconstruct the expression on the device /// and /// recreate the expression with MakeGlobalPointer containing the device address /// space for the TensorMap pointers used in eval function. /// It receives the original expression type, the functor of the node, the tuple /// of accessors, and the device expression type to re-instantiate the /// expression tree for the device template struct ExprConstructor; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorMap template class MakePointer_, size_t N, typename... Params> struct ExprConstructor< const TensorMap, Options2_, MakeGlobalPointer>, const Eigen::internal::PlaceHolder< const TensorMap, Options3_, MakePointer_>, N>, Params...> { using Type = const TensorMap, Options2_, MakeGlobalPointer>; Type expr; template ExprConstructor(FuncDetector &fd, const utility::tuple::Tuple &t) : expr(Type((&(*(utility::tuple::get(t).get_pointer()))), fd.dimensions())) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorMap template class MakePointer_, size_t N, typename... Params> struct ExprConstructor< TensorMap, Options2_, MakeGlobalPointer>, Eigen::internal::PlaceHolder< TensorMap, Options3_, MakePointer_>, N>, Params...> { using Type = TensorMap, Options2_, MakeGlobalPointer>; Type expr; template ExprConstructor(FuncDetector &fd, const utility::tuple::Tuple &t) : expr(Type((&(*(utility::tuple::get(t).get_pointer()))), fd.dimensions())) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorCwiseNullaryOp template struct ExprConstructor, TensorCwiseNullaryOp, Params...> { using my_type = ExprConstructor; my_type rhsExpr; using Type = TensorCwiseNullaryOp; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : rhsExpr(funcD.rhsExpr, t), expr(rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorCwiseNullaryOp template struct ExprConstructor, const TensorCwiseNullaryOp, Params...> { using my_type = const ExprConstructor; my_type rhsExpr; using Type = const TensorCwiseNullaryOp; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : rhsExpr(funcD.rhsExpr, t), expr(rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorBroadcastingOp template struct ExprConstructor, TensorBroadcastingOp, Params...> { using my_type = ExprConstructor; my_type rhsExpr; using Type = TensorBroadcastingOp; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : rhsExpr(funcD.rhsExpr, t), expr(rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorBroadcastingOp template struct ExprConstructor, const TensorBroadcastingOp, Params...> { using my_type = const ExprConstructor; my_type rhsExpr; using Type = const TensorBroadcastingOp; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : rhsExpr(funcD.rhsExpr, t), expr(rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorCwiseUnaryOp template struct ExprConstructor, TensorCwiseUnaryOp, Params...> { using my_type = ExprConstructor; using Type = TensorCwiseUnaryOp; my_type rhsExpr; Type expr; template ExprConstructor(FuncDetector &funcD, utility::tuple::Tuple &t) : rhsExpr(funcD.rhsExpr, t), expr(rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorCwiseUnaryOp template struct ExprConstructor, const TensorCwiseUnaryOp, Params...> { using my_type = ExprConstructor; using Type = const TensorCwiseUnaryOp; my_type rhsExpr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : rhsExpr(funcD.rhsExpr, t), expr(rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorCwiseBinaryOp template struct ExprConstructor, TensorCwiseBinaryOp, Params...> { using my_left_type = ExprConstructor; using my_right_type = ExprConstructor; using Type = TensorCwiseBinaryOp; my_left_type lhsExpr; my_right_type rhsExpr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : lhsExpr(funcD.lhsExpr, t), rhsExpr(funcD.rhsExpr, t), expr(lhsExpr.expr, rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorCwiseBinaryOp template struct ExprConstructor, const TensorCwiseBinaryOp, Params...> { using my_left_type = ExprConstructor; using my_right_type = ExprConstructor; using Type = const TensorCwiseBinaryOp; my_left_type lhsExpr; my_right_type rhsExpr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : lhsExpr(funcD.lhsExpr, t), rhsExpr(funcD.rhsExpr, t), expr(lhsExpr.expr, rhsExpr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorCwiseTernaryOp template struct ExprConstructor< const TensorCwiseTernaryOp, const TensorCwiseTernaryOp, Params...> { using my_arg1_type = ExprConstructor; using my_arg2_type = ExprConstructor; using my_arg3_type = ExprConstructor; using Type = const TensorCwiseTernaryOp; my_arg1_type arg1Expr; my_arg2_type arg2Expr; my_arg3_type arg3Expr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : arg1Expr(funcD.arg1Expr, t), arg2Expr(funcD.arg2Expr, t), arg3Expr(funcD.arg3Expr, t), expr(arg1Expr.expr, arg2Expr.expr, arg3Expr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorCwiseTernaryOp template struct ExprConstructor< TensorCwiseTernaryOp, TensorCwiseTernaryOp, Params...> { using my_arg1_type = ExprConstructor; using my_arg2_type = ExprConstructor; using my_arg3_type = ExprConstructor; using Type = TensorCwiseTernaryOp; my_arg1_type arg1Expr; my_arg2_type arg2Expr; my_arg3_type arg3Expr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : arg1Expr(funcD.arg1Expr, t), arg2Expr(funcD.arg2Expr, t), arg3Expr(funcD.arg3Expr, t), expr(arg1Expr.expr, arg2Expr.expr, arg3Expr.expr, funcD.func) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorCwiseSelectOp template struct ExprConstructor< const TensorSelectOp, const TensorSelectOp, Params...> { using my_if_type = ExprConstructor; using my_then_type = ExprConstructor; using my_else_type = ExprConstructor; using Type = const TensorSelectOp; my_if_type ifExpr; my_then_type thenExpr; my_else_type elseExpr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : ifExpr(funcD.ifExpr, t), thenExpr(funcD.thenExpr, t), elseExpr(funcD.elseExpr, t), expr(ifExpr.expr, thenExpr.expr, elseExpr.expr) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorCwiseSelectOp template struct ExprConstructor, TensorSelectOp, Params...> { using my_if_type = ExprConstructor; using my_then_type = ExprConstructor; using my_else_type = ExprConstructor; using Type = TensorSelectOp; my_if_type ifExpr; my_then_type thenExpr; my_else_type elseExpr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : ifExpr(funcD.ifExpr, t), thenExpr(funcD.thenExpr, t), elseExpr(funcD.elseExpr, t), expr(ifExpr.expr, thenExpr.expr, elseExpr.expr) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorAssignOp template struct ExprConstructor, TensorAssignOp, Params...> { using my_left_type = ExprConstructor; using my_right_type = ExprConstructor; using Type = TensorAssignOp; my_left_type lhsExpr; my_right_type rhsExpr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : lhsExpr(funcD.lhsExpr, t), rhsExpr(funcD.rhsExpr, t), expr(lhsExpr.expr, rhsExpr.expr) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorAssignOp template struct ExprConstructor, const TensorAssignOp, Params...> { using my_left_type = ExprConstructor; using my_right_type = ExprConstructor; using Type = const TensorAssignOp; my_left_type lhsExpr; my_right_type rhsExpr; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : lhsExpr(funcD.lhsExpr, t), rhsExpr(funcD.rhsExpr, t), expr(lhsExpr.expr, rhsExpr.expr) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorEvalToOp template struct ExprConstructor, const TensorEvalToOp, Params...> { using my_expr_type = ExprConstructor; using my_buffer_type = typename TensorEvalToOp::PointerType; using Type = const TensorEvalToOp; my_expr_type nestedExpression; EvalToLHSConstructor buffer; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : nestedExpression(funcD.rhsExpr, t), buffer(t), expr(buffer.expr, nestedExpression.expr) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorEvalToOp template struct ExprConstructor, TensorEvalToOp, Params...> { using my_expr_type = ExprConstructor; using my_buffer_type = typename TensorEvalToOp::PointerType; using Type = TensorEvalToOp; my_expr_type nestedExpression; EvalToLHSConstructor buffer; Type expr; template ExprConstructor(FuncDetector &funcD, const utility::tuple::Tuple &t) : nestedExpression(funcD.rhsExpr, t), buffer(t), expr(buffer.expr, nestedExpression.expr) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// const TensorForcedEvalOp template struct ExprConstructor< const TensorForcedEvalOp, const Eigen::internal::PlaceHolder, N>, Params...> { using Type = const TensorMap< Tensor::Scalar, TensorForcedEvalOp::NumDimensions, 0, typename TensorForcedEvalOp::Index>, 0, MakeGlobalPointer>; Type expr; template ExprConstructor(FuncDetector &fd, const utility::tuple::Tuple &t) : expr(Type((&(*(utility::tuple::get(t).get_pointer()))), fd.dimensions())) {} }; /// specialisation of the \ref ExprConstructor struct when the node type is /// TensorForcedEvalOp template struct ExprConstructor< const TensorForcedEvalOp, const Eigen::internal::PlaceHolder, N>, Params...> { using Type = TensorMap< Tensor::Scalar, 1, 0, typename TensorForcedEvalOp::Index>, 0, MakeGlobalPointer>; Type expr; template ExprConstructor(FuncDetector &fd, const utility::tuple::Tuple &t) : expr(Type((&(*(utility::tuple::get(t).get_pointer()))), fd.dimensions())) {} }; /// template deduction for \ref ExprConstructor struct template auto createDeviceExpression(FuncD &funcD, const utility::tuple::Tuple &t) -> decltype(ExprConstructor(funcD, t)) { return ExprConstructor(funcD, t); } } } } // namespace Eigen #endif // UNSUPPORTED_EIGEN_CXX11_SRC_TENSORYSYCL_EXPR_CONSTRUCTOR_HPP