VisionCpp  0.0.1
pyramid_with_auto_mem_gen.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 pyramid_with_auto_mem_gen.hpp
21 /// \brief This file contains the construction of the pyramid node where a user
22 /// can pass general filter2d functor and general downsampling functor to
23 /// execute. Here, the user does need to pass the output memory. The output
24 /// memory will be created based on the depth of pyramid. Also an output file
25 /// will be created which merge all the result of the pyramid image in one file.
26 
27 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_COMPLEX_OPS_PYRAMID_WITH_AUTO_MEM_GEN_HPP_
28 #define VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_COMPLEX_OPS_PYRAMID_WITH_AUTO_MEM_GEN_HPP_
29 
30 namespace visioncpp {
31 namespace internal {
32 /// \struct PyramidExecuteAutoMemGen
33 /// \brief here we execute the pyramid; automatically construct the output tuple
34 /// based on the depth of the pyramid; and construct the kernels.
35 /// template parameters:
36 /// \tparam SatisfyingConds: a boolean variable is used to determine the end of
37 /// the recursive creation of the pyramid kernel at compile time.
38 /// \tparam Fltr2DOP: general Filter2D functor
39 /// \tparam DownSmplOP: downsampling function for pyramid
40 /// \tparam Cols: determines the column size of the input pyramid
41 /// \tparam Rows: determines the row size of the input pyramid
42 /// \tparam LeafType: determines the type of the leafNode {Buffer2D, Buffer1D,
43 /// Host, Image}
44 /// \tparam LVL: the current level of the node based on the previous creation
45 /// of the kernel. This level expresses the a new level for RHS. This level is an
46 /// artificial level. In each step we consider an expression needed to be
47 /// executed by a kernel as a subexpression of a big expression tree. The LVL
48 /// here represent the level of the root of each subexpression inside that big
49 /// artificial expression.
50 /// \tparam LC: is the column size of local memory required by Filter2D and
51 /// DownSmplOP
52 /// \tparam LR: is the row size of local memory required by Filter2D and
53 /// DownSmplOP
54 /// \tparam LCT: is the column size of workgroup
55 /// \tparam LRT: is the row size of workgroup
56 /// \tparam Depth: represents the depth of down sampling
57 /// \tparam CurrentDepth: represents the number of kernel created so far by
58 /// recursive calling of PyramidExecuteGen struct.
59 /// \tparam LHS is the final output of the pyramid combining all the node
60 /// together
61 /// \tparam RHS is the intermediate input passed for the current kernel to be
62 /// executed as a leafNode
63 /// \tparam Fltr2D is the filter2d node
64 /// \tparam PyramidMem: is a tuple of pyramid output memory
65 template <bool SatisfyingConds, typename Fltr2DOP, typename DownSmplOP,
66  size_t Cols, size_t Rows, size_t LeafType, size_t OffsetCol,
67  size_t OffsetRow, size_t LVL, size_t LC, size_t LR, size_t LRT,
68  size_t LCT, size_t Depth, size_t CurrentDepth, typename LHS,
69  typename RHS, typename Fltr2D, typename PyramidMem, typename DeviceT>
71  /// function sub_execute
72  /// \brief is the function used to construct a subexpression tree
73  /// corresponding to the CurrentDepth of the pyramid and launch the kernel
74  /// \param rhs: is the intermediate input passed for the current kernel to
75  /// be executed as a leafNode
76  /// \param fltr2D is the filter2d node
77  /// \param mem: is the tuple of pyramid output memory
78  /// \param dev : the selected device for executing the expression
79  /// \return void
80  static void sub_execute(RHS &rhs, Fltr2D &fltr2D, PyramidMem &mem,
81  const DeviceT &dev) {
82  // apply reduction
83  auto a = StnFilt<LocalBinaryOp<Fltr2DOP, typename RHS::OutType,
84  typename Fltr2D::OutType>,
85  Fltr2D::Type::Rows / 2, Fltr2D::Type::Cols / 2,
86  Fltr2D::Type::Rows / 2, Fltr2D::Type::Cols / 2, RHS,
87  Fltr2D, Cols, Rows, LeafType,
88  1 + tools::StaticIf<(RHS::Level > Fltr2D::Level), RHS,
89  Fltr2D>::Type::Level>(rhs, fltr2D);
90  auto b =
91  RDCN<LocalUnaryOp<DownSmplOP, typename decltype(a)::OutType>,
92  decltype(a), Cols / 2, Rows / 2, LeafType, 1 + decltype(a)::Level>(
93  a);
94  using RHSType = typename tools::RemoveAll<decltype(
95  tools::tuple::get<CurrentDepth>(mem))>::Type;
96  auto c = Assign<RHSType, decltype(b), Cols / 2, Rows / 2, LeafType, LVL>(
97  tools::tuple::get<CurrentDepth>(mem), b);
98  fuse<LC, LR, LCT, LRT>(c, dev);
99 
101  (Depth == (CurrentDepth + 1)), Fltr2DOP, DownSmplOP, Cols / 2, Rows / 2,
102  LeafType, OffsetCol, OffsetRow + Rows / 2, RHSType::Level + 1, LC, LR,
103  LCT, LRT, Depth, CurrentDepth + 1, LHS, RHSType, Fltr2D,
104  PyramidMem, DeviceT>::sub_execute(tools::tuple::get<CurrentDepth>(mem), fltr2D,
105  mem, dev);
106  }
107 };
108 /// \brief specialisation of the PyramidExecuteAutoMemGen when the
109 /// SatisfyingConds is true. It does nothing but representing the end of
110 /// recursive constructing and launching of an expression tree
111 template <typename Fltr2DOP, typename DownSmplOP, size_t Cols, size_t Rows,
112  size_t LeafType, size_t OffsetCol, size_t OffsetRow, size_t LVL,
113  size_t LC, size_t LR, size_t LCT, size_t LRT, size_t Depth,
114  size_t CurrentDepth, typename LHS, typename RHS, typename Fltr2D,
115  typename PyramidMem, typename DeviceT>
117  true, Fltr2DOP, DownSmplOP, Cols, Rows, LeafType, OffsetCol, OffsetRow, LVL,
118  LC, LR, LCT, LRT, Depth, CurrentDepth, LHS, RHS, Fltr2D, PyramidMem, DeviceT> {
119  static void sub_execute(RHS &rhs, Fltr2D &fltr2D, PyramidMem &mem,
120  const DeviceT &dev) {}
121 };
122 
123 /// \struct PyramidAutomemGen
124 /// \brief PyramidDownGen is used to construct a pyramid node in the expression
125 /// tree with general Filter2D and general DownSmplOP functors. It automatically
126 /// generates the tuple of output based on the depth.
127 /// template parameters:
128 /// \tparam Fltr2DOP: general Filter2D function
129 /// \tparam DownSmplOP: downsampling function for pyramid
130 /// \tparam Cols: determines the column size of the input pyramid
131 /// \tparam RHS is the input passed for pyramid
132 /// \tparam Fltr2D is the filter2d node
133 /// \tparam Rows: determines the row size of the input pyramid
134 /// \tparam LfType: determines the type of the leafNode {Buffer2D, Buffer1D,
135 /// Host, Image}
136 /// \tparam LVL: the level of the node in the expression tree
137 /// \tparam Dp: represents the depth of down sampling
138 template <typename Fltr2DOP, typename DownSmplOP, typename RHS, typename Fltr2D,
139  size_t Cols, size_t Rows, size_t LfType, size_t LVL, size_t Dp>
141  static constexpr bool has_out = false;
142  using OutType = typename DownSmplOP::OutType;
143  using Type =
144  typename OutputMemory<OutType, LfType, Cols + Cols / 2, Rows, LVL>::Type;
146  static constexpr size_t Level = LVL;
147  static constexpr size_t LeafType = Type::LeafType;
148  static constexpr bool SubExpressionEvaluationNeeded = true;
150  static constexpr size_t RThread = Rows;
151  static constexpr size_t CThread = Cols;
152  static constexpr size_t ND_Category = expr_category::Binary;
153  static constexpr size_t Depth = Dp;
154  using PyramidMem =
155  typename CreatePyramidTupleType<false, Cols / 2, Rows / 2, LeafType,
156  Depth, 0, LHSExpr>::Type;
157 
158  RHS rhs;
159  Fltr2D fltr2D;
162  size_t node_reseter;
164 
165  PyramidAutomemGen(RHS rhsArg, Fltr2D fltr2DArg)
166  : rhs(rhsArg),
167  fltr2D(fltr2DArg),
169  first_time(true),
170  node_reseter(0),
171  mem(create_pyramid_memory<Cols / 2, Rows / 2, Depth, 0, LHSExpr>()) {}
172 
173  void reset(bool reset) {
174  rhs.reset(reset);
176  }
177 
178  /// pyramid type
179  using PyramidType = PyramidAutomemGen<Fltr2DOP, DownSmplOP, RHS, Fltr2D, Cols,
180  Rows, LfType, LVL, Dp>;
181  /// method get
182  template <size_t N>
184  return PyramidLeafNode<PyramidType, N>(*this);
185  }
186 
187  /// sub_expression_evaluation
188  /// \brief This function is used to break the expression tree whenever
189  /// necessary. The decision for breaking the tree will be determined based on
190  /// the static parameter called SubExpressionEvaluationNeeded. When this is
191  /// set to true, the sub_expression_evaluation is called recursively from the
192  /// root of the tree. Each node based on their parent decision will decide to
193  /// launch a kernel for itself. Also, they decide for each of their children
194  /// whether or not to launch a kernel separately.
195  /// template parameters:
196  ///\tparam ForcedToExec : a boolean value representing the decision made by
197  /// the parent of this node for launching a kernel.
198  /// \tparam LC: is the column size of local memory required by Filter2D and
199  /// DownSmplOP
200  /// \tparam LR: is the row size of local memory required by Filter2D and
201  /// DownSmplOP
202  /// \tparam LCT: is the column size of workgroup
203  /// \tparam LRT: is the row size of workgroup
204  /// \tparam DeviceT: type representing the device
205  /// function parameters:
206  /// \param dev : the selected device for executing the expression
207  /// \return LeafNode
208  template <bool ForcedToExec, size_t LC, size_t LR, size_t LCT, size_t LRT,
209  typename DeviceT>
211  const DeviceT &dev) {
212  // clearing the board
213  auto eval_sub =
214  rhs.template sub_expression_evaluation<false, LC, LR, LCT, LRT>(dev);
216  Depth == 0, typename Fltr2DOP::OP, typename DownSmplOP::OP, Cols, Rows,
217  LeafType, Cols, 0, 1 + LVL, LC, LR, LCT, LRT, Depth, 0, LHSExpr,
218  decltype(eval_sub), Fltr2D, PyramidMem, DeviceT>::sub_execute(eval_sub, fltr2D,
219  mem, dev);
220  return get<0>();
221  }
222 };
223 } // internal
224 /// pyramid_auto_generate
225 /// \brief template deduction for PyramidAutomemGen
226 template <typename Fltr2DOP, typename DownSmplOP, size_t Depth, typename RHS,
227  typename Fltr2D>
228 auto pyramid_down(RHS rhs, Fltr2D fltr2D) -> internal::PyramidAutomemGen<
229  internal::LocalBinaryOp<Fltr2DOP, typename RHS::OutType,
230  typename Fltr2D::OutType>,
232  RHS::Type::Cols, RHS::Type::Rows, RHS::Type::LeafType,
233  1 + internal::tools::StaticIf<(RHS::Level > Fltr2D::Level), RHS,
234  Fltr2D>::Type::Level,
235  Depth> {
237  internal::LocalBinaryOp<Fltr2DOP, typename RHS::OutType,
238  typename Fltr2D::OutType>,
240  RHS::Type::Cols, RHS::Type::Rows, RHS::Type::LeafType,
241  1 + internal::tools::StaticIf<(RHS::Level > Fltr2D::Level), RHS,
242  Fltr2D>::Type::Level,
243  Depth>(rhs, fltr2D);
244 }
245 } // visioncpp
246 #endif // VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_COMPLEX_OPS_PYRAMID_WITH_AUTO_MEM_GEN_HPP_
CreatePyramidTupleType< false, Cols, Rows, LHS::Type::LeafType, Depth, 0, LHS >::Type create_pyramid_memory()
create_pyramid_memory template deduction function for CreatePyramidTuple struct.
VisionCpp namespace.
Definition: sycl/device.hpp:24
auto pyramid_down(RHS rhs, Fltr2D fltr2D) -> internal::PyramidAutomemGen< internal::LocalBinaryOp< Fltr2DOP, typename RHS::OutType, typename Fltr2D::OutType >, internal::LocalUnaryOp< DownSmplOP, typename RHS::OutType >, RHS, Fltr2D, RHS::Type::Cols, RHS::Type::Rows, RHS::Type::LeafType, 1+internal::tools::StaticIf<(RHS::Level > Fltr2D::Level), RHS, Fltr2D >::Type::Level, Depth >
pyramid_auto_generate
The definition is in Assign file.
Definition: assign.hpp:44
CreatePyramidTupleType: This file is used to create each output element type for each downsampling ou...
Definition: pyramid_mem.hpp:51
the definition is in LeafNode.
Definition: leaf_node.hpp:38
This class is used to encapsulate the local binary functor and the types of each operand in this func...
Definition: expr_tree.hpp:69
This class is used to encapsulate the local unary functor and the types of each operand in this funct...
Definition: expr_tree.hpp:53
OutputMemory is used to deduce the output type of each node in the expression tree by using certain p...
Definition: memory.hpp:53
PyramidDownGen is used to construct a pyramid node in the expression tree with general Filter2D and g...
typename CreatePyramidTupleType< false, Cols/2, Rows/2, LeafType, Depth, 0, LHSExpr >::Type PyramidMem
typename OutputMemory< OutType, LfType, Cols+Cols/2, Rows, LVL >::Type Type
PyramidLeafNode< PyramidType, 0 > sub_expression_evaluation(const DeviceT &dev)
sub_expression_evaluation
PyramidLeafNode< PyramidType, N > get()
method get
here we execute the pyramid; automatically construct the output tuple based on the depth of the pyram...
static void sub_execute(RHS &rhs, Fltr2D &fltr2D, PyramidMem &mem, const DeviceT &dev)
function sub_execute
The definition is in RDCN file.
Definition: reduction.hpp:42
The definition is in StnFilt file.
These methods are used to remove all the & const and * from a type.
It is used to select either of the input type based the Conds template parameters.
Definition: static_if.hpp:52