VisionCpp  0.0.1
pyramid_with_auto_mem_sep.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_sep.hpp
21 /// \brief This file contains the construction of the pyramid node where a
22 /// user can pass separable filter2d functor and general downsampling functor
23 /// to 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_SEP_HPP_
28 #define VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_COMPLEX_OPS_PYRAMID_WITH_AUTO_MEM_SEP_HPP_
29 
30 namespace visioncpp {
31 namespace internal {
32 
33 /// \struct PyramidExecuteAutoMemSep
34 /// \brief here we execute the pyramid; automatically construct the output tuple
35 /// based on the depth of the pyramid; and construct the kernels.
36 /// template parameters:
37 /// \tparam SatisfyingConds: a boolean variable is used to determine the end of
38 /// the recursive creation of the pyramid kernel at compile time.
39 /// \tparam Fltr2DOP: general Filter2D functor
40 /// \tparam DownSmplOP: downsampling function for pyramid
41 /// \tparam Cols: determines the column size of the input pyramid
42 /// \tparam Rows: determines the row size of the input pyramid
43 /// \tparam LeafType: determines the type of the leafNode {Buffer2D, Buffer1D,
44 /// Host, Image}
45 /// \tparam LVL: the current level of the node based on the previous creation
46 /// of the kernel. This level express the a new level for RHS. This level is an
47 /// artificial level. In each step we consider an expression needed to be
48 /// executed by a kernel as a subexpression of a big expression tree. The LVL
49 /// here represent the level of the root of each subexpression inside that big
50 /// artificial expression.
51 /// \tparam LC: is the column size of local memory required by Filter2D and
52 /// DownSmplOP
53 /// \tparam LR: is the row size of local memory required by Filter2D and
54 /// DownSmplOP
55 /// \tparam LCT: is the column size of workgroup
56 /// \tparam LRT: is the row size of workgroup
57 /// \tparam Depth: represents the depth of down sampling
58 /// \tparam CurrentDepth: represents the number of kernel created so far by
59 /// recursively calling of PyramidExecuteGen struct.
60 /// \tparam LHS is the final output of the pyramid combining all the node
61 /// together
62 /// \tparam RHS is the intermediate input passed for the current kernel to be
63 /// \tparam SepFilterRow is the separable filter for row
64 /// \tparam SepFilterCol is the separable filter for col
65 /// \tparam PyramidMem: is a tuple of pyramid output memory
66 template <bool SatisfyingConds, typename SepFltrColOP, typename SepFltrRowOP,
67  typename DownSmplOP, size_t Cols, size_t Rows, size_t LeafType,
68  size_t OffsetCol, size_t OffsetRow, size_t LVL, size_t LC, size_t LR,
69  size_t LCT, size_t LRT, size_t Depth, size_t CurrentDepth,
70  typename LHS, typename RHS, typename SepFilterCol,
71  typename SepFilterRow, typename PyramidMem, typename DeviceT>
73  /// function sub_execute
74  /// \brief is the function used to construct a subexpression tree
75  /// corresponding to the CurrentDepth of the pyramid and launch the kernel
76  /// \param rhs: is the intermediate input passed for the current kernel to
77  /// be executed as a leafNode
78  /// \param spFltrRow is the separable filter node for Row
79  /// \param spFltrCol is the separable filter node for Col
80  /// \param mem: is the tuple of pyramid output memory
81  /// \param dev : the selected device for executing the expression
82  /// \return void
83  static void sub_execute(RHS &rhs, SepFilterCol &spFltrCol,
84  SepFilterRow &spFltrRow, PyramidMem &mem,
85  const DeviceT &dev) {
86  auto a = StnFilt<LocalBinaryOp<SepFltrColOP, typename RHS::OutType,
87  typename SepFilterCol::OutType>,
88  SepFilterCol::Type::Rows / 2, SepFilterCol::Type::Cols / 2,
89  SepFilterCol::Type::Rows / 2, SepFilterCol::Type::Cols / 2,
90  RHS, SepFilterCol, Cols, Rows, LeafType, 1 + RHS::Level>(
91  rhs, spFltrCol);
92 
93  auto b = StnFilt<LocalBinaryOp<SepFltrRowOP, typename decltype(a)::OutType,
94  typename SepFilterRow::OutType>,
95  SepFilterRow::Type::Rows / 2, SepFilterRow::Type::Cols / 2,
96  SepFilterRow::Type::Rows / 2, SepFilterRow::Type::Cols / 2,
97  decltype(a), SepFilterRow, Cols, Rows, LeafType,
98  1 + decltype(a)::Level>(a, spFltrRow);
99  auto c =
100  RDCN<LocalUnaryOp<DownSmplOP, typename decltype(b)::OutType>,
101  decltype(b), Cols / 2, Rows / 2, LeafType, 1 + decltype(b)::Level>(
102  b);
103  using RHSType = typename tools::RemoveAll<decltype(
104  tools::tuple::get<CurrentDepth>(mem))>::Type;
105  auto d = Assign<RHSType, decltype(c), Cols / 2, Rows / 2, LeafType, LVL>(
106  tools::tuple::get<CurrentDepth>(mem), c);
107 
108  fuse<LC, LR, LCT, LRT>(d, dev);
109 
111  (Depth == (CurrentDepth + 1)), SepFltrColOP, SepFltrRowOP, DownSmplOP,
112  Cols / 2, Rows / 2, LeafType, OffsetCol, OffsetRow + Rows / 2,
113  RHSType::Level + 1, LC, LR, LCT, LRT, Depth, CurrentDepth + 1, LHS,
114  RHSType, SepFilterCol, SepFilterRow,
115  PyramidMem, DeviceT>::sub_execute(tools::tuple::get<CurrentDepth>(mem),
116  spFltrCol, spFltrRow, mem, dev);
117  }
118 };
119 
120 /// \brief specialisation of the PyramidExecuteAutoMemSep when the
121 /// SatisfyingConds is true. It does nothing but representing the end of
122 /// recursive constructing and launching of an expression tree
123 template <typename SepFltrColOP, typename SepFltrRowOP, typename DownSmplOP,
124  size_t Cols, size_t Rows, size_t LeafType, size_t OffsetCol,
125  size_t OffsetRow, size_t LVL, size_t LC, size_t LR, size_t LRT,
126  size_t LCT, size_t Depth, size_t CurrentDepth, typename LHS,
127  typename RHS, typename SepFilterCol, typename SepFilterRow,
128  typename PyramidMem, typename DeviceT>
129 struct PyramidExecuteAutoMemSep<true, SepFltrColOP, SepFltrRowOP, DownSmplOP,
130  Cols, Rows, LeafType, OffsetCol, OffsetRow, LVL,
131  LC, LR, LCT, LRT, Depth, CurrentDepth, LHS, RHS,
132  SepFilterCol, SepFilterRow, PyramidMem, DeviceT> {
133  static void sub_execute(RHS &, SepFilterCol &, SepFilterRow &, PyramidMem &,
134  const DeviceT &) {}
135 };
136 
137 /// \struct PyramidAutomemSep
138 /// \brief PyramidAutomemSep is used to construct a pyramid node in the
139 /// expression tree with two separable Filter for row and column and general
140 /// DownSmplOP functors. It automatically generates the tuple of output based on
141 /// the depth.
142 /// template parameters:
143 /// \tparam SepFltrColOP: separable Filter functor for col
144 /// \tparam SepFltrRowOP: separable Filter functor for row
145 /// \tparam DownSmplOP: downsampling function for pyramid
146 /// \tparam LHS is the input passed for pyramid
147 /// \tparam SepFilterCol: is the separable filter node for column
148 /// \tparam SepFilterRow: is the separable filter node for row
149 /// \tparam Cols: determines the column size of the input pyramid
150 /// \tparam Rows: determines the row size of the input pyramid
151 /// \tparam LfType: determines the type of the leafNode {Buffer2D, Buffer1D,
152 /// Host, Image}
153 /// \tparam LVL: the level of the node in the expression tree
154 /// \tparam Dp: represents the depth of downsampling
155 template <typename SepFltrColOP, typename SepFltrRowOP, typename DownSmplOP,
156  typename RHS, typename SepFilterCol, typename SepFilterRow,
157  size_t Cols, size_t Rows, size_t LfType, size_t LVL, size_t Dp>
159  public:
160  static constexpr bool has_out = false;
161  using OutType = typename DownSmplOP::OutType;
162  using Type =
163  typename OutputMemory<OutType, LfType, Cols + Cols / 2, Rows, LVL>::Type;
165  static constexpr size_t Level = LVL;
166  static constexpr size_t LeafType = Type::LeafType;
167  static constexpr bool SubExpressionEvaluationNeeded = true;
169  static constexpr size_t RThread = Rows;
170  static constexpr size_t CThread = Cols;
171  static constexpr size_t ND_Category = expr_category::Unary;
172  static constexpr size_t Depth = Dp;
173  using PyramidMem =
174  typename CreatePyramidTupleType<false, Cols / 2, Rows / 2, LeafType,
175  Depth, 0, LHSExpr>::Type;
176  RHS rhs;
177  SepFilterCol spFltrCol;
178  SepFilterRow spFltrRow;
179 
182  size_t node_reseter;
184  PyramidAutomemSep(RHS rhsArg, SepFilterCol fltrCol, SepFilterRow fltrRow)
185  : rhs(rhsArg),
186  spFltrCol(fltrCol),
187  spFltrRow(fltrRow),
189  first_time(true),
190  node_reseter(0),
191  mem(create_pyramid_memory<Cols / 2, Rows / 2, Depth, 0, LHSExpr>()) {}
192 
193  void reset(bool reset) {
194  rhs.reset(reset);
196  }
197 
198  /// pyramid type
199  using PyramidType = PyramidAutomemSep<SepFltrColOP, SepFltrRowOP, DownSmplOP,
200  RHS, SepFilterCol, SepFilterRow, Cols,
201  Rows, LfType, LVL, Dp>;
202  /// method get
203  template <size_t N>
205  return PyramidLeafNode<PyramidType, N>(*this);
206  }
207 
208  /// sub_expression_evaluation
209  /// \brief This function is used to break the expression tree whenever
210  /// necessary. The decision for breaking the tree will be determined based on
211  /// the static parameter called SubExpressionEvaluationNeeded. When this is
212  /// set to true, the sub_expression_evaluation is called recursively from the
213  /// root of the tree. Each node based on their parent decision will decide to
214  /// launch a kernel for itself. Also, they decide for each of their children
215  /// whether or not to launch a kernel separately.
216  /// template parameters:
217  ///\tparam ForcedToExec : a boolean value representing the decision made by
218  /// the parent of this node for launching a kernel.
219  /// \tparam LC: is the column size of local memory required by Filter2D and
220  /// DownSmplOP
221  /// \tparam LR: is the row size of local memory required by Filter2D and
222  /// DownSmplOP
223  /// \tparam LCT: is the column size of workgroup
224  /// \tparam LRT: is the row size of workgroup
225  /// \tparam DeviceT: type representing the device
226  /// function parameters:
227  /// \param dev : the selected device for executing the expression
228  /// \return LeafNode
229  template <bool ForcedToExec, size_t LC, size_t LR, size_t LCT, size_t LRT,
230  typename DeviceT>
232  const DeviceT &dev) {
233  // clearing the board
234  auto eval_sub =
235  rhs.template sub_expression_evaluation<false, LC, LR, LCT, LRT>(dev);
236 
238  Depth == 0, typename SepFltrColOP::OP, typename SepFltrRowOP::OP,
239  typename DownSmplOP::OP, Cols, Rows, LeafType, Cols, 0, 1 + LVL, LC, LR,
240  LCT, LRT, Depth, 0, LHSExpr, decltype(eval_sub), SepFilterCol,
241  SepFilterRow, PyramidMem, DeviceT>::sub_execute(eval_sub, spFltrCol, spFltrRow,
242  mem, dev);
243  return get<0>();
244  }
245 };
246 } // internal
247 
248 /// pyramid_auto_generate
249 /// \brief template deduction for PyramidAutomemGenSep
250 template <typename COP_C, typename COP_R, typename ROP, size_t Depth,
251  typename RHS, typename SepFilterCol, typename SepFilterRow,
252  typename... Params>
253 auto pyramid_down(RHS rhs, SepFilterCol spFltrCol, SepFilterRow spFltrRow)
255  internal::LocalBinaryOp<COP_C, typename RHS::OutType,
256  typename SepFilterCol::OutType>,
257  internal::LocalBinaryOp<COP_R, typename RHS::OutType,
258  typename SepFilterRow::OutType>,
260  SepFilterRow, RHS::Type::Cols, RHS::Type::Rows, RHS::Type::LeafType,
261  1 + RHS::Level, Depth> {
263  internal::LocalBinaryOp<COP_C, typename RHS::OutType,
264  typename SepFilterCol::OutType>,
265  internal::LocalBinaryOp<COP_R, typename RHS::OutType,
266  typename SepFilterRow::OutType>,
268  SepFilterRow, RHS::Type::Cols, RHS::Type::Rows, RHS::Type::LeafType,
269  1 + RHS::Level, Depth>(rhs, spFltrCol, spFltrRow);
270 }
271 } // visioncpp
272 #endif // VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_COMPLEX_OPS_PYRAMID_WITH_AUTO_MEM_SEP_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
PyramidAutomemSep is used to construct a pyramid node in the expression tree with two separable Filte...
PyramidLeafNode< PyramidType, N > get()
method get
typename OutputMemory< OutType, LfType, Cols+Cols/2, Rows, LVL >::Type Type
PyramidAutomemSep(RHS rhsArg, SepFilterCol fltrCol, SepFilterRow fltrRow)
PyramidLeafNode< PyramidType, 0 > sub_expression_evaluation(const DeviceT &dev)
sub_expression_evaluation
typename CreatePyramidTupleType< false, Cols/2, Rows/2, LeafType, Depth, 0, LHSExpr >::Type PyramidMem
here we execute the pyramid; automatically construct the output tuple based on the depth of the pyram...
static void sub_execute(RHS &rhs, SepFilterCol &spFltrCol, SepFilterRow &spFltrRow, 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.