VisionCpp  0.0.1
resizable_binary.hpp
Go to the documentation of this file.
1 // This file is part of VisionCpp, a lightweight C++ template library
2 // for computer vision and image processing.
3 //
4 // Copyright (C) 2016 Codeplay Software Limited. All Rights Reserved.
5 //
6 // Contact: visioncpp@codeplay.com
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 
20 /// \file resizable_binary.hpp
21 /// \brief This file contains RBiOP (Binary Operation) struct which is used to
22 /// apply Binary Operation on left hand side(LHS) and right hand side(RHS)
23 /// operands.
24 
25 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_POINT_OPS_RESIZABLE_BINARY_HPP_
26 #define VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_POINT_OPS_RESIZABLE_BINARY_HPP_
27 
28 namespace visioncpp {
29 namespace internal {
30 /// \struct InheritTypeBinary
31 /// \brief This struct is used to extract the output type of the binary
32 /// operation from both input. This is useful when one of the operands passed is
33 /// a constant variable and the other one is a buffer. This inheritance allows to
34 /// swap the place of the constant variable in the node construction
35 /// template parameters
36 /// \tparam LHSExpr left hand side expression
37 /// \tparam RHSExpr right hand side expression
38 template <typename LHSExpr, typename RHSExpr>
40  using LHSTypeDetector = typename LHSExpr::Type;
41  using RHSTypeDetector = typename RHSExpr::Type;
42  using Type =
43  typename tools::StaticIf<LHSTypeDetector::LeafType != memory_type::Const,
45 };
46 
47 /// \struct OpTP
48 /// \brief This struct is used to determine the operation type for binary
49 /// operation based on the operation type of its children.
50 /// always pointop << neighbourop
51 /// template parameters
52 /// \tparam Conds : boolean value determines whether or not both types are equal
53 /// \tparam LhsOp : operation type for left-hand size expression
54 /// \tparam RhsOp : operation type for right-hand size expression
55 template <bool Conds, size_t LhsOP, size_t RhsOP>
56 struct OpTP;
57 /// \brief specialisation of the PointOP category where the two operation types
58 /// are not equal and the lhs operation type is PointOp
59 template <size_t RhsOP>
60 struct OpTP<false, internal::ops_category::PointOP, RhsOP> {
61  static constexpr size_t Operation_type = RhsOP;
62 };
63 /// \brief specialisation of the PointOP category where the two operation types
64 /// are not equal and the rhs operation type is PointOp
65 template <size_t LhsOP>
66 struct OpTP<false, LhsOP, internal::ops_category::PointOP> {
67  static constexpr size_t Operation_type = LhsOP;
68 };
69 /// \brief specialisation of the PointOP category where the two operation types
70 /// are equal
71 template <size_t OPType>
72 struct OpTP<true, OPType, OPType> {
73  static constexpr size_t Operation_type = OPType;
74 };
75 
76 /// \struct RBiOP
77 /// \brief RBiOP is used to
78 /// apply Binary Operation on left-hand side(LHS) and right-hand side(RHS)
79 /// operands.
80 /// template parameters:
81 /// \tparam LHS is the left-hand- side expression
82 /// \tparam RHS is the right-hand side expression
83 /// \tparam Cols: determines the column size of the output
84 /// \tparam Rows: determines the row size of the output
85 /// \tparam LfType: determines the type of the leafNode {Buffer2D, Buffer1D,
86 /// Host, Image}
87 /// \tparam LVL: the level of the node in the expression tree
88 template <typename BI_OP, typename LHS, typename RHS, size_t Cols, size_t Rows,
89  size_t LfType, size_t LVL>
90 struct RBiOP {
91  public:
92  static constexpr bool has_out = false;
93  using OutType = typename BI_OP::OutType;
94  using OPType = BI_OP;
95  using RHSExpr = RHS;
96  using LHSExpr = LHS;
97  using Type = typename visioncpp::internal::OutputMemory<OutType, LfType, Cols,
98  Rows, LVL>::Type;
99  static constexpr size_t Level = LVL;
100  static constexpr size_t RThread = Rows;
101  static constexpr size_t CThread = Cols;
102  static constexpr size_t ND_Category = internal::expr_category::Binary;
103  static constexpr size_t LeafType = Type::LeafType;
104  static constexpr bool Binary_Conds =
105  (LHS::LeafType != memory_type::Const &&
106  ((RThread != LHS::RThread) || (CThread != LHS::CThread))) ||
107  (RHS::LeafType != memory_type::Const &&
108  ((RThread != RHS::RThread) || (CThread != RHS::CThread)));
109  static constexpr bool SubExpressionEvaluationNeeded =
110  Binary_Conds || LHS::SubExpressionEvaluationNeeded ||
111  RHS::SubExpressionEvaluationNeeded;
112  static constexpr size_t Operation_type =
113  internal::OpTP<(LHS::Operation_type == RHS::Operation_type),
114  LHS::Operation_type, RHS::Operation_type>::Operation_type;
115 
116  template <typename TmpLHS, typename TmpRHS>
118 
119  LHS lhs;
120  RHS rhs;
122  RBiOP(LHS lhsArg, RHS rhsArg)
123  : lhs(lhsArg), rhs(rhsArg), subexpr_execution_reseter(false) {}
124 
125  void reset(bool reset) {
126  lhs.reset(reset);
127  rhs.reset(reset);
129  }
130  /// sub_expression_evaluation
131  /// \brief This function is used to break the expression tree whenever
132  /// necessary. The decision for breaking the tree will be determined based on
133  /// the static parameter called SubExpressionEvaluationNeeded. When this is
134  /// set to true, the sub_expression_evaluation is called recursively from the
135  /// root of the tree. Each node based on their parent decision will decide to
136  /// launch a kernel for itself. Also, they decide for each of their children
137  /// whether or not to launch a kernel separately.
138  /// template parameters:
139  ///\tparam ForcedToExec : a boolean value representing the decision made by
140  /// the parent of this node for launching a kernel.
141  /// \tparam LC: is the column size of local memory required by Filter2D and
142  /// DownSmplOP
143  /// \tparam LR: is the row size of local memory required by Filter2D and
144  /// DownSmplOP
145  /// \tparam LCT: is the column size of workgroup
146  /// \tparam LRT: is the row size of workgroup
147  /// function parameters:
148  /// \param dev : the selected device for executing the expression
149  /// \return LeafNode
150  template <bool ForcedToExec, size_t LC, size_t LR, size_t LCT, size_t LRT,
151  typename DeviceT>
152  auto inline sub_expression_evaluation(const DeviceT& dev)
153  -> decltype(execute_expr<Binary_Conds, ForcedToExec,
154  ExprExchange<LHS, RHS>, LC, LR, LCT, LRT>(
155  lhs.template sub_expression_evaluation<Binary_Conds, LC, LR, LRT,
156  LCT>(dev),
157  rhs.template sub_expression_evaluation<Binary_Conds, LC, LR, LRT,
158  LCT>(dev),
159  dev)) {
160  return execute_expr<Binary_Conds, ForcedToExec, ExprExchange<LHS, RHS>, LC,
161  LR, LCT, LRT>(
162  lhs.template sub_expression_evaluation<Binary_Conds, LC, LR, LCT, LRT>(
163  dev),
164  rhs.template sub_expression_evaluation<Binary_Conds, LC, LR, LCT, LRT>(
165  dev),
166  dev);
167  }
168 };
169 
170 /// \brief template deduction for the RBiOP struct where user determines the
171 /// Column, Row, and memory_type of the output.
172 template <typename OP, size_t Cols, size_t Rows, size_t LeafType, typename LHS,
173  typename RHS>
174 auto point_operation(LHS lhs, RHS rhs) -> RBiOP<
176  LHS, RHS, Cols, Rows, LeafType,
177  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
178  RHS>::Type::Level> {
179  return RBiOP<
181  LHS, RHS, Cols, Rows, LeafType,
182  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
183  RHS>::Type::Level>(lhs, rhs);
184 }
185 } // internal
186 
187 /// \brief template deduction for RBiOP struct where the Column, Row,
188 /// and memory_type of the output has been automatically deduced from LHS and
189 /// RHS operands
190 template <typename OP, typename LHS, typename RHS>
191 auto point_operation(LHS lhs, RHS rhs) -> internal::RBiOP<
196  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
197  RHS>::Type::Level> {
198  return internal::RBiOP<
203  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
204  RHS>::Type::Level>(lhs, rhs);
205 }
206 } // visioncpp
207 #endif // VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_POINT_OPS_RESIZABLE_BINARY_HPP_
auto execute_expr(NestedExpr nestedExpr, const DeviceT &dev) -> decltype(internal::IfExprExecNeeded< Conds, ParentConds, internal::expr_category::Unary, Expr, DeviceT >::template execute_expr< LC, LR, LCT, LRT >(nestedExpr, dev))
template deduction for IfExprExecNeeded when the expression category is unary
auto point_operation(LHS lhs, RHS rhs) -> RBiOP< internal::PixelBinaryOp< OP, typename LHS::OutType, typename RHS::OutType >, LHS, RHS, Cols, Rows, LeafType, 1+internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS, RHS >::Type::Level >
template deduction for the RBiOP struct where user determines the Column, Row, and memory_type of the...
static constexpr size_t Const
VisionCpp namespace.
Definition: sycl/device.hpp:24
auto point_operation(LHS lhs, RHS rhs) -> internal::RBiOP< internal::PixelBinaryOp< OP, typename LHS::OutType, typename RHS::OutType >, LHS, RHS, internal::InheritTypeBinary< LHS, RHS >::Type::Cols, internal::InheritTypeBinary< LHS, RHS >::Type::Rows, internal::InheritTypeBinary< LHS, RHS >::Type::LeafType, 1+internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS, RHS >::Type::Level >
template deduction for RBiOP struct where the Column, Row, and memory_type of the output has been aut...
This struct is used to extract the output type of the binary operation from both input.
typename tools::StaticIf< LHSTypeDetector::LeafType !=memory_type::Const, LHSTypeDetector, RHSTypeDetector >::Type Type
This struct is used to determine the operation type for binary operation based on the operation type ...
OutputMemory is used to deduce the output type of each node in the expression tree by using certain p...
Definition: memory.hpp:53
This class is used to encapsulate the binary point operation functor and the types of each operand in...
Definition: expr_tree.hpp:101
The definition is in RBiOP file.
static constexpr size_t CThread
static constexpr bool SubExpressionEvaluationNeeded
static constexpr size_t Level
static constexpr size_t RThread
static constexpr size_t ND_Category
typename BI_OP::OutType OutType
static constexpr size_t Operation_type
auto sub_expression_evaluation(const DeviceT &dev) -> decltype(execute_expr< Binary_Conds, ForcedToExec, ExprExchange< LHS, RHS >, LC, LR, LCT, LRT >(lhs.template sub_expression_evaluation< Binary_Conds, LC, LR, LRT, LCT >(dev), rhs.template sub_expression_evaluation< Binary_Conds, LC, LR, LRT, LCT >(dev), dev))
sub_expression_evaluation
static constexpr bool has_out
RBiOP(LHS lhsArg, RHS rhsArg)
static constexpr size_t LeafType
static constexpr bool Binary_Conds
typename visioncpp::internal::OutputMemory< OutType, LfType, Cols, Rows, LVL >::Type Type
It is used to select either of the input type based the Conds template parameters.
Definition: static_if.hpp:52