VisionCpp  0.0.1
stencil_with_filter.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 stencil_with_filter.hpp
21 /// \brief This file contains the StnNoFilt struct which is used to construct a
22 /// convolutional operation when the value of the filter is fixed and there is
23 /// no
24 /// need to pass it as a parameter.
25 
26 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_NEIGHBOUR_OPS_STENCIL_WITH_FILTER_HPP_
27 #define VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_NEIGHBOUR_OPS_STENCIL_WITH_FILTER_HPP_
28 
29 namespace visioncpp {
30 namespace internal {
31 /// \struct StnFilt
32 /// \brief stencil with filter is used to construct a general convolutional
33 /// operation for a custom filter. It can be used for NeighbourOP
34 /// \tparam FilterOP: convolution functor with no filter parameter
35 /// \tparam Halo_T: The top side size of Halo
36 /// \tparam Halo_L:. The left side size of Halo
37 /// \tparam Halo_B: The bottom side size of Halo
38 /// \tparam Halo_R: The right side size of Halo
39 /// \tparam LHS is the input node
40 /// \tparam RHS is the filter2d node
41 /// \tparam Cols: determines the column size of the output
42 /// \tparam Rows: determines the row size of the output
43 /// \tparam LfType: determines the type of the leafNode {Buffer2D, Buffer1D,
44 /// Host, Image}
45 /// \tparam LVL: the level of the node in the expression tree
46 template <typename Conv_OP, size_t Halo_T, size_t Halo_L, size_t Halo_B,
47  size_t Halo_R, typename LHS, typename RHS, size_t Cols, size_t Rows,
48  size_t LfType, size_t LVL>
49 struct StnFilt {
50  public:
51  static constexpr bool has_out = false;
52  using OutType = typename Conv_OP::OutType;
53  using OPType = Conv_OP;
54  using LHSExpr = LHS;
55  using RHSExpr = RHS;
56  using Type = typename visioncpp::internal::OutputMemory<OutType, LfType, Cols,
57  Rows, LVL>::Type;
58  static constexpr size_t Level = LVL;
59  static constexpr size_t LeafType = Type::LeafType;
60  static constexpr size_t Operation_type = Conv_OP::Operation_type;
61  static constexpr size_t Halo_Top = Halo_T;
62  static constexpr size_t Halo_Butt = Halo_B;
63  static constexpr size_t Halo_Left = Halo_L;
64  static constexpr size_t Halo_Right = Halo_R;
65  static constexpr size_t RThread = Rows;
66  static constexpr size_t CThread = Cols;
67  static constexpr size_t ND_Category = internal::expr_category::Binary;
68  static constexpr bool Stencil_Conds =
69  ((RThread != LHS::RThread) || (CThread != LHS::CThread));
70  static constexpr bool SubExpressionEvaluationNeeded =
71  Stencil_Conds || LHS::SubExpressionEvaluationNeeded ||
72  RHS::SubExpressionEvaluationNeeded;
73 
74  template <typename TmpLHS, typename TmpRHS>
75  using ExprExchange =
76  internal::StnFilt<Conv_OP, Halo_T, Halo_L, Halo_B, Halo_R, TmpLHS, TmpRHS,
77  Cols, Rows, LfType, LVL>;
78 
79  LHS lhs;
80  RHS rhs;
82  StnFilt(LHS lhsArg, RHS rhsArg)
83  : lhs(lhsArg), rhs(rhsArg), subexpr_execution_reseter(false) {}
84 
85  void reset(bool reset) {
86  lhs.reset(reset);
87  rhs.reset(reset);
89  }
90 
91  /// sub_expression_evaluation
92  /// \brief This function is used to break the expression tree whenever
93  /// necessary. The decision for breaking the tree will be determined based on
94  /// the static parameter called SubExpressionEvaluationNeeded. When this is
95  /// set to true, the sub_expression_evaluation is called recursively from the
96  /// root of the tree. Each node based on their parent decision will decide to
97  /// launch a kernel for itself. Also, they decide for each of their children
98  /// whether or not to launch a kernel separately.
99  /// template parameters:
100  ///\tparam ForcedToExec : a boolean value representing the decision made by
101  /// the parent of this node for launching a kernel.
102  /// \tparam LC: is the column size of local memory required by Filter2D and
103  /// DownSmplOP
104  /// \tparam LR: is the row size of local memory required by Filter2D and
105  /// DownSmplOP
106  /// \tparam LCT: is the column size of workgroup
107  /// \tparam LRT: is the row size of workgroup
108  /// \tparam DeviceT: type representing the device
109  /// function parameters:
110  /// \param dev : the selected device for executing the expression
111  /// \return LeafNode
112  template <bool ForcedToExec, size_t LC, size_t LR, size_t LCT, size_t LRT,
113  typename DeviceT>
114  auto inline sub_expression_evaluation(const DeviceT& dev)
115  -> decltype(execute_expr<Stencil_Conds, ForcedToExec,
116  ExprExchange<LHS, RHS>, LC, LR, LCT, LRT>(
117  lhs.template sub_expression_evaluation<Stencil_Conds, LC, LR, LRT,
118  LCT>(dev),
119  rhs.template sub_expression_evaluation<Stencil_Conds, LC, LR, LRT,
120  LCT>(dev),
121  dev)) {
122  return execute_expr<Stencil_Conds, ForcedToExec, ExprExchange<LHS, RHS>, LC,
123  LR, LCT, LRT>(
124  lhs.template sub_expression_evaluation<Stencil_Conds, LC, LR, LCT, LRT>(
125  dev),
126  rhs.template sub_expression_evaluation<Stencil_Conds, LC, LR, LCT, LRT>(
127  dev),
128  dev);
129  }
130 };
131 } // internal
132 
133 /// \brief template deduction for StnFilt class when the memory types of the
134 /// output and column and row are defined by a user.
135 template <typename OP, size_t Cols, size_t Rows, size_t LeafType, typename LHS,
136  typename RHS>
137 auto neighbour_operation(LHS lhs, RHS rhs) -> internal::StnFilt<
139  RHS::Type::Rows / 2, RHS::Type::Cols / 2, RHS::Type::Rows / 2,
140  RHS::Type::Cols / 2, LHS, RHS, Cols, Rows, LeafType,
141  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
142  RHS>::Type::Level> {
143  return internal::StnFilt<
145  RHS::Type::Rows / 2, RHS::Type::Cols / 2, RHS::Type::Rows / 2,
146  RHS::Type::Cols / 2, LHS, RHS, Cols, Rows, LeafType,
147  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
148  RHS>::Type::Level>(lhs, rhs);
149 }
150 
151 /// \brief template deduction for StnFilt class when the memory type of the
152 /// output and column and row are automatically deduced from the input.
153 template <typename OP, typename LHS, typename RHS>
154 auto neighbour_operation(LHS lhs, RHS rhs) -> internal::StnFilt<
156  RHS::Type::Rows / 2, RHS::Type::Cols / 2, RHS::Type::Rows / 2,
157  RHS::Type::Cols / 2, LHS, RHS, LHS::Type::Cols, LHS::Type::Rows,
158  LHS::Type::LeafType,
159  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
160  RHS>::Type::Level> {
161  return internal::StnFilt<
163  RHS::Type::Rows / 2, RHS::Type::Cols / 2, RHS::Type::Rows / 2,
164  RHS::Type::Cols / 2, LHS, RHS, LHS::Type::Cols, LHS::Type::Rows,
165  LHS::Type::LeafType,
166  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
167  RHS>::Type::Level>(lhs, rhs);
168 }
169 
170 /// \brief template deduction for StnFilt class when the memory type of the
171 /// output; halos; and column and row are defined by a user.
172 template <typename OP, size_t Halo_T, size_t Halo_L, size_t Halo_B,
173  size_t Halo_R, size_t Cols, size_t Rows, size_t LeafType,
174  typename LHS, typename RHS>
175 auto neighbour_operation(LHS lhs, RHS rhs) -> internal::StnFilt<
177  Halo_T, Halo_L, Halo_B, Halo_R, LHS, RHS, Cols, Rows, LeafType,
178  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
179  RHS>::Type::Level> {
180  return internal::StnFilt<
182  Halo_T, Halo_L, Halo_B, Halo_R, LHS, RHS, Cols, Rows, LeafType,
183  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
184  RHS>::Type::Level>(lhs, rhs);
185 }
186 
187 /// \brief template deduction for StnFilt class when the memory type of the
188 /// output and column and row are automatically deduced from the input. However,
189 /// the halos are defined by user.
190 template <typename OP, size_t Halo_T, size_t Halo_L, size_t Halo_B,
191  size_t Halo_R, typename LHS, typename RHS>
192 auto neighbour_operation(LHS lhs, RHS rhs) -> internal::StnFilt<
194  Halo_T, Halo_L, Halo_B, Halo_R, LHS, RHS, LHS::Type::Cols, LHS::Type::Rows,
195  LHS::Type::LeafType,
196  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
197  RHS>::Type::Level> {
198  return internal::StnFilt<
200  Halo_T, Halo_L, Halo_B, Halo_R, LHS, RHS, LHS::Type::Cols,
201  LHS::Type::Rows, LHS::Type::LeafType,
202  1 + internal::tools::StaticIf<(LHS::Level > RHS::Level), LHS,
203  RHS>::Type::Level>(lhs, rhs);
204 }
205 } // visioncpp
206 #endif // VISIONCPP_INCLUDE_FRAMEWORK_EXPR_TREE_NEIGHBOUR_OPS_STENCIL_WITH_FILTER_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
This class is used to encapsulate the local binary functor and the types of each operand in this func...
Definition: expr_tree.hpp:69
OutputMemory is used to deduce the output type of each node in the expression tree by using certain p...
Definition: memory.hpp:53
The definition is in StnFilt file.
static constexpr size_t ND_Category
static constexpr size_t Level
static constexpr size_t Operation_type
static constexpr size_t Halo_Left
static constexpr bool Stencil_Conds
typename visioncpp::internal::OutputMemory< OutType, LfType, Cols, Rows, LVL >::Type Type
auto sub_expression_evaluation(const DeviceT &dev) -> decltype(execute_expr< Stencil_Conds, ForcedToExec, ExprExchange< LHS, RHS >, LC, LR, LCT, LRT >(lhs.template sub_expression_evaluation< Stencil_Conds, LC, LR, LRT, LCT >(dev), rhs.template sub_expression_evaluation< Stencil_Conds, LC, LR, LRT, LCT >(dev), dev))
sub_expression_evaluation
static constexpr size_t Halo_Right
static constexpr size_t CThread
static constexpr size_t Halo_Top
static constexpr bool SubExpressionEvaluationNeeded
static constexpr size_t Halo_Butt
static constexpr size_t RThread
StnFilt(LHS lhsArg, RHS rhsArg)
typename Conv_OP::OutType OutType
static constexpr size_t LeafType
It is used to select either of the input type based the Conds template parameters.
Definition: static_if.hpp:52