VisionCpp  0.0.1
reduction.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 reduction.hpp
21 /// \brief This file contains RDCN struct which is used to construct a
22 /// node to shrink an image.
23 
24 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_NEIGHBOUR_OPS_REDUCTION_HPP_
25 #define VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_NEIGHBOUR_OPS_REDUCTION_HPP_
26 
27 namespace visioncpp {
28 namespace internal {
29 /// \struct RDCN
30 /// \brief RDCN is used to shrink the size of an input. It can be used both for
31 /// NeighbourOP and GlobalNeighbourOP.
32 /// \tparam DownSmplOP: downsampling function to shrink an input
33 /// \tparam LHS is the input
34 /// \tparam RHS is the filter2d node
35 /// \tparam Cols: determines the column size of the output
36 /// \tparam Rows: determines the row size of the output
37 /// \tparam LfType: determines the type of the leafNode {Buffer2D, Buffer1D,
38 /// Host, Image}
39 /// \tparam LVL: the level of the node in the expression tree
40 template <typename DownSmplOP, typename RHS, size_t Cols, size_t Rows,
41  size_t LfType, size_t LVL>
42 struct RDCN {
43  public:
44  static constexpr size_t LC_Ratio =
45  tools::IfConst<DownSmplOP::Operation_type ==
47  1, RHS::CThread / Cols>::Value;
48  static constexpr size_t LR_Ratio =
49  tools::IfConst<DownSmplOP::Operation_type ==
51  1, RHS::RThread / Rows>::Value;
52  using RHSExpr = RHS;
53  static constexpr bool has_out = false;
54  using OutType = typename DownSmplOP::OutType;
55  using OPType = DownSmplOP;
57  static constexpr size_t Level = LVL;
58  static constexpr size_t RThread = Rows * LR_Ratio;
59  static constexpr size_t CThread = Cols * LC_Ratio;
60  static constexpr size_t ND_Category = expr_category::Unary;
61  static constexpr size_t LeafType = Type::LeafType;
62  static constexpr bool SubExpressionEvaluationNeeded =
63  DownSmplOP::Operation_type == ops_category::GlobalNeighbourOP ||
64  RHS::SubExpressionEvaluationNeeded;
65  // if the coordinates are not the same
66  // with your child you have to break for others except reduction
67  static constexpr size_t Operation_type = DownSmplOP::Operation_type;
68  template <typename TmpRHS>
70 
71  RHS rhs;
73  RDCN(RHS rhsArg) : rhs(rhsArg), subexpr_execution_reseter(false) {}
74 
75  void reset(bool reset) {
76  rhs.reset(reset);
78  }
79  /// sub_expression_evaluation
80  /// \brief This function is used to break the expression tree whenever
81  /// necessary. The decision for breaking the tree will be determined based on
82  /// the static parameter called SubExpressionEvaluationNeeded. When this is
83  /// set to true, the sub_expression_evaluation is called recursively from the
84  /// root of the tree. Each node based on their parent decision will decide to
85  /// launch a kernel for itself. Also, they decide for each of their children
86  /// whether or not to launch a kernel separately.
87  /// template parameters:
88  ///\tparam ForcedToExec : a boolean value representing the decision made by
89  /// the parent of this node for launching a kernel.
90  /// \tparam LC: is the column size of local memory required by Filter2D and
91  /// DownSmplOP
92  /// \tparam LR: is the row size of local memory required by Filter2D and
93  /// DownSmplOP
94  /// \tparam LCT: is the column size of workgroup
95  /// \tparam LRT: is the row size of workgroup
96  /// \tparam DeviceT: type representing the device
97  /// function parameters:
98  /// \param dev : the selected device for executing the expression
99  /// \return LeafNode
100  template <bool ForcedToExec, size_t LC, size_t LR, size_t LCT, size_t LRT,
101  typename DeviceT>
102  auto inline sub_expression_evaluation(const DeviceT &dev)
103  -> decltype(execute_expr<
104  DownSmplOP::Operation_type == ops_category::GlobalNeighbourOP,
105  ForcedToExec, ExprExchange<RHS>, LC, LR, LCT, LRT>(
106  rhs.template sub_expression_evaluation< // decide for your child
107  DownSmplOP::Operation_type == ops_category::GlobalNeighbourOP, LC,
108  LR, LCT, LRT>(dev),
109  dev)) {
110  return execute_expr<DownSmplOP::Operation_type ==
112  ForcedToExec, ExprExchange<RHS>, LC, LR, LCT, LRT>(
113  rhs.template sub_expression_evaluation< // decide for your child
114  DownSmplOP::Operation_type == ops_category::GlobalNeighbourOP, LC,
115  LR, LCT, LRT>(dev),
116  dev);
117  }
118 };
119 } // internal
120 
121 /// \brief template deduction function for RDCN when it is used for
122 /// GlobalNeighbourOP.
123 template <typename OP, size_t Cols, size_t Rows, size_t LeafType, typename RHS>
124 auto global_operation(RHS rhs)
126  Cols, Rows, LeafType, 1 + RHS::Level> {
128  Cols, Rows, LeafType, 1 + RHS::Level>(rhs);
129 }
130 /// \brief template deduction function for RDCN when it is used for
131 /// NeighbourOP.
132 template <typename OP, size_t Cols, size_t Rows, size_t LeafType, typename RHS>
133 auto neighbour_operation(RHS rhs)
135  Cols, Rows, LeafType, 1 + RHS::Level> {
137  Cols, Rows, LeafType, 1 + RHS::Level>(rhs);
138 }
139 } // visioncpp
140 #endif // VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_NEIGHBOUR_OPS_REDUCTION_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
VisionCpp namespace.
Definition: sycl/device.hpp:24
auto neighbour_operation(RHS rhs) -> internal::RDCN< internal::LocalUnaryOp< OP, typename RHS::OutType >, RHS, Cols, Rows, LeafType, 1+RHS::Level >
template deduction function for RDCN when it is used for NeighbourOP.
Definition: reduction.hpp:133
auto global_operation(RHS rhs) -> internal::RDCN< internal::GlobalUnaryOp< OP, typename RHS::OutType >, RHS, Cols, Rows, LeafType, 1+RHS::Level >
template deduction function for RDCN when it is used for GlobalNeighbourOP.
Definition: reduction.hpp:124
The definition is in RDCN file.
Definition: reduction.hpp:42
auto sub_expression_evaluation(const DeviceT &dev) -> decltype(execute_expr< DownSmplOP::Operation_type==ops_category::GlobalNeighbourOP, ForcedToExec, ExprExchange< RHS >, LC, LR, LCT, LRT >(rhs.template sub_expression_evaluation< DownSmplOP::Operation_type==ops_category::GlobalNeighbourOP, LC, LR, LCT, LRT >(dev), dev))
sub_expression_evaluation
Definition: reduction.hpp:102
static constexpr size_t CThread
Definition: reduction.hpp:59
static constexpr bool SubExpressionEvaluationNeeded
Definition: reduction.hpp:62
static constexpr size_t Operation_type
Definition: reduction.hpp:67
static constexpr size_t RThread
Definition: reduction.hpp:58
typename OutputMemory< OutType, LfType, Cols, Rows, LVL >::Type Type
Definition: reduction.hpp:56
static constexpr bool has_out
Definition: reduction.hpp:53
static constexpr size_t Level
Definition: reduction.hpp:57
static constexpr size_t ND_Category
Definition: reduction.hpp:60
static constexpr size_t LeafType
Definition: reduction.hpp:61
typename DownSmplOP::OutType OutType
Definition: reduction.hpp:54
static constexpr size_t LR_Ratio
Definition: reduction.hpp:48
void reset(bool reset)
Definition: reduction.hpp:75
static constexpr size_t LC_Ratio
Definition: reduction.hpp:44
Definition of VisionMemory.
Definition: mem_vision.hpp:53