mgcpp
A C++ Math Library Based on CUDA
generic_expr.hpp
Go to the documentation of this file.
1 
2 // Copyright RedPortal, mujjingun 2017 - 2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef GENERIC_OP_HPP
8 #define GENERIC_OP_HPP
9 
10 #include <memory>
11 #include <tuple>
12 #include <utility>
13 
16 #include <mgcpp/global/shape.hpp>
19 
20 namespace mgcpp {
21 
22 template <typename TagType,
23  size_t Tag,
24  template <typename> class ResultExprType,
25  typename ResultType,
26  size_t NParameters,
27  typename... OperandTypes>
28 struct generic_expr : public ResultExprType<generic_expr<TagType,
29  Tag,
30  ResultExprType,
31  ResultType,
32  NParameters,
33  OperandTypes...>> {
34  // Type of self
35  using this_type = generic_expr<TagType,
36  Tag,
37  ResultExprType,
38  ResultType,
39  NParameters,
40  OperandTypes...>;
41 
42  // The resulting type from eval()-ing this node (i.e. device_matrix<float>)
43  using result_type = ResultType;
44 
45  template <typename T>
46  using result_expr_type = ResultExprType<T>;
47 
48  // Is this node a terminal node (i.e. with no child nodes)
49  static constexpr bool is_terminal = sizeof...(OperandTypes) == NParameters;
50  static constexpr size_t n_parameters = NParameters;
51  static constexpr size_t tag = Tag;
52 
53  // Operand expressions (first NParameter elements are non-expression
54  // parameters)
55  std::tuple<OperandTypes...> exprs;
56 
57  // Returns tuple containing operands
58  inline decltype(auto) operands() const noexcept;
59 
60  // Returns tuple containing parameters
61  inline decltype(auto) parameters() const noexcept;
62 
63  // Convenience getters for the operands
64  inline decltype(auto) first() const noexcept;
65  inline decltype(auto) second() const noexcept;
66 
67  // Constructor
68  inline generic_expr(OperandTypes... args) noexcept;
69 
70  // Build cache info by traversing the entire graph
71  inline void traverse() const;
72 
76  inline ResultType eval() const;
77 
82  inline ResultType eval(eval_context const& ctx) const;
83 
87  template <
88  typename T = ResultType,
89  typename = typename std::enable_if<std::is_same<T, ResultType>::value &&
90  mgcpp::has_shape<T>::value>::type>
91  inline typename T::shape_type shape(eval_context const& ctx) const;
92 };
93 
94 // A unary operator with 1 operand (i.e. map)
95 template <typename TagType,
96  template <typename> class ResultExprType,
97  typename ResultType,
98  typename Expr>
99 using unary_expr =
100  generic_expr<TagType, 0, ResultExprType, ResultType, 0, Expr>;
101 
102 // A binary operator with left and right operands (i.e. addition,
103 // multiplication)
104 template <typename TagType,
105  template <typename> class ResultExprType,
106  typename ResultType,
107  typename LhsExpr,
108  typename RhsExpr>
109 using binary_expr =
110  generic_expr<TagType, 0, ResultExprType, ResultType, 0, LhsExpr, RhsExpr>;
111 } // namespace mgcpp
112 
113 #include <mgcpp/expressions/generic_expr.tpp>
114 #endif // GENERIC_OP_HPP
decltype(auto) first() const noexcept
decltype(auto) operands() const noexcept
Definition: adapter_base.hpp:12
static constexpr size_t tag
Definition: generic_expr.hpp:51
Definition: eval_context.hpp:20
Definition: shape.hpp:33
ResultType eval() const
Definition: shape.hpp:10
void traverse() const
Definition: shape_type.hpp:11
Definition: forward.hpp:25
ResultType result_type
Definition: generic_expr.hpp:43
ResultExprType< T > result_expr_type
Definition: generic_expr.hpp:46
decltype(auto) second() const noexcept
generic_expr(OperandTypes... args) noexcept
static constexpr bool is_terminal
Definition: generic_expr.hpp:49
generic_expr< TagType, Tag, ResultExprType, ResultType, NParameters, OperandTypes... > this_type
Definition: generic_expr.hpp:40
std::tuple< OperandTypes... > exprs
Definition: generic_expr.hpp:55
decltype(auto) parameters() const noexcept
static constexpr size_t n_parameters
Definition: generic_expr.hpp:50