VisionCpp  0.0.1
executor.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 executor.hpp
21 /// \brief This files contains a series of forward declaration and include files
22 /// required for executing an expression tree.
23 
24 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_EXECUTOR_EXECUTOR_HPP_
25 #define VISIONCPP_INCLUDE_FRAMEWORK_EXECUTOR_EXECUTOR_HPP_
26 
27 namespace visioncpp {
28 namespace internal {
29 
30 /// \struct FuseExpr
31 /// \brief The FuseExpr struct is used to generate one device kernel for the
32 /// expression which is not a terminal node (leafNode).
33 /// It is used to specialise the fuse function for terminal and
34 /// non-terminal nodes
35 /// template parameters:
36 /// \tparam LC: is the column size of local memory
37 /// \tparam LR: is the row size of local memory
38 /// \tparam LCT: is the column size of workgroup
39 /// \tparam LRT: is the row size of workgroup
40 /// \tparam Expr : the expression tree needed to be executed
41 template <size_t LC, size_t LR, size_t LCT, size_t LRT, typename Expr, typename DeviceT>
42 struct FuseExpr;
43 
44 // \struct NoFuseExpr
45 /// \brief The NoFuseExpr struct is used to generate one device kernel per each
46 /// non-terminal node in the expression. It is used to specialise the no_fuse
47 /// function for different non-terminal nodes
48 /// template parameters:
49 /// \tparam LC: is the column size of local memory
50 /// \tparam LR: is the row size of local memory
51 /// \tparam LCT: is the column size of workgroup
52 /// \tparam LRT: is the row size of workgroup
53 /// \tparam Expr : the node needed to be executed
54 ///\tparam Category: the Category type of the node (Binary, Unary, Nullary)
55 template <size_t LC, size_t LR, size_t LCT, size_t LRT, size_t Category,
56  typename Expr, typename DeviceT>
57 struct NoFuseExpr;
58 
59 /// \brief IfExprExecNeeded is used to decide:
60 /// 1) the expression should force its children to launch a separate kernel and
61 /// execute the code
62 /// 2) the expression should create a new subexpression tree with the result of
63 /// its children execution; execute the new sub expression and return the
64 /// LeafNode as a result.
65 /// template parameters
66 /// \tparam Conds: the decision made by Expr for its children stating whether or
67 /// not the child of Expr to be executed.
68 /// \tparam ParentConds: the decision mad by Expr parent stating that whether or
69 /// not the expr itself needed to be executed.
70 /// \tparam Category: representing to what expression Category the Expr
71 /// besize_ts
72 /// \tparam Expr: the type of the expression
73 template <bool Conds, bool ParentConds, size_t Category, typename Expr, typename DeviceT>
75 
76 /// \struct Executor
77 /// \brief The Executor struct is used to specialise the execute function for
78 /// different avaiable policies at compile time.
79 template <bool ExecPolicy, size_t LC, size_t LR, size_t LCT, size_t LRT,
80  typename Expr, typename DeviceT>
81 struct Executor;
82 
83 /// \brief specialisaton of Execute function when the policy is fuse.
84 template <size_t LC, size_t LR, size_t LCT, size_t LRT, typename Expr, typename DeviceT>
85 struct Executor<policy::Fuse, LC, LR, LCT, LRT, Expr, DeviceT> {
86  /// \brief executing the expression
87  /// parameters:
88  /// \param expr : the expression needed to be executed
89  /// \param dev : the selected device for executing the expression
90  /// return void
91  static inline void execute(Expr expr, const DeviceT &dev) {
92  fuse<LC, LR, LCT, LRT>(expr, dev);
93  }
94 };
95 
96 template <size_t LC, size_t LR, size_t LCT, size_t LRT, typename Expr,typename DeviceT>
97 struct Executor<policy::NoFuse, LC, LR, LCT, LRT, Expr, DeviceT> {
98  /// \brief executing the expression
99  /// parameters:
100  /// \param expr : the expression needed to be executed
101  /// \param dev : the selected device for executing the expression
102  /// return void
103  static inline void execute(Expr expr, const DeviceT &dev) {
104  no_fuse<LC, LR, LCT, LRT>(expr, dev);
105  }
106 };
107 
108 /// \struct SubExprExecute
109 /// \brief it is used to statically determine whether or not a subexpression
110 /// execution is needed. It increases the execution time by avoiding executing
111 /// the subexpression when it is not needed. Using this
112 /// struct with sub_expression_evaluation parameter in every non-terminal node,
113 /// it is possible to determine such condition at compile time.
114 template <bool Val, bool ExecPolicy, size_t LC, size_t LR, size_t LCT,
115  size_t LRT, typename Expr, typename DeviceT>
117  static void inline execute(Expr &expr, const DeviceT &dev) {
118  expr.reset(true);
119  Executor<ExecPolicy, LC, LR, LCT, LRT,
120  decltype(expr.template sub_expression_evaluation<false, LC, LR,
121  LCT, LRT>(dev)), DeviceT>::
122  execute(
123  expr.template sub_expression_evaluation<false, LC, LR, LCT, LRT>(
124  dev),
125  dev);
126  }
127 };
128 /// \brief specialisation of the status of when there is no need for
129 /// subexpression execution
130 template <bool ExecPolicy, size_t LC, size_t LR, size_t LCT, size_t LRT,
131  typename Expr, typename DeviceT>
132 struct SubExprExecute<false, ExecPolicy, LC, LR, LCT, LRT, Expr, DeviceT> {
133  static void inline execute(Expr &expr, const DeviceT &dev) {
135  }
136 };
137 
138 } // internal
139 
140 /// \brief execute function is called by user in order to execute an expression
141 /// template parameters:
142 /// \tparam ExecPolicy: determining which policy to be used for executing an
143 /// expression. this can be Fuse or NoFuse
144 /// \tparam LC the column size for local memory when needed
145 /// \tparam LR the row size for column memory when needed
146 /// \tparam LCT the size of the workgroup column.
147 /// \tparam LRT the size of the workgroup row.
148 /// \tparam Expr the expression type to be executed.
149 /// function parameters:
150 /// \param expr the expression to be executed
151 /// \param dev the selected device for executing the expression
152 /// \return void
153 template <bool ExecPolicy, size_t LC, size_t LR, size_t LCT, size_t LRT,
154  typename Expr, typename DeviceT>
155 void inline execute(Expr &expr, const DeviceT &dev) {
156  internal::SubExprExecute<Expr::SubExpressionEvaluationNeeded, ExecPolicy, LC,
157  LR, LCT, LRT, Expr, DeviceT>::execute(expr, dev);
158 }
159 
160 /// \brief special case of the execute function with default value for local
161 /// memory and workgroup size
162 /// template parameters:
163 /// \tparam ExecPolicy: determining which policy to be used for executing an
164 /// expression. this can be Fuse or NoFuse
165 /// \tparam Expr: the expression type to be executed.
166 /// function parameters:
167 /// \param expr: the expression to be executed
168 /// \param dev : the selected device for executing the expression
169 /// \return void
170 template <bool ExecPolicy, typename Expr, typename DeviceT>
171 void inline execute(Expr &expr, const DeviceT &dev) {
172  internal::SubExprExecute<Expr::SubExpressionEvaluationNeeded, ExecPolicy, 8,
173  8, 8, 8, Expr, DeviceT>::execute(expr, dev);
174 }
175 } // visioncpp
177 #include "policy/fuse.hpp"
178 #include "policy/nofuse.hpp"
179 #endif // VISIONCPP_INCLUDE_FRAMEWORK_EXECUTOR_EXECUTOR_HPP_
This file contains required classes for break an expression tree into subexpression trees for particu...
This file contains the specialisation of the FuseExpr for terminal and non-terminal nodes.
constexpr static bool NoFuse
constexpr static bool Fuse
VisionCpp namespace.
Definition: sycl/device.hpp:24
void execute(Expr &expr, const DeviceT &dev)
execute function is called by user in order to execute an expression template parameters:
Definition: executor.hpp:155
void execute(Expr &expr, const DeviceT &dev)
special case of the execute function with default value for local memory and workgroup size template ...
Definition: executor.hpp:171
This file contains the specialisation of the NoFuseExpr for different nodes.
static void execute(Expr expr, const DeviceT &dev)
executing the expression parameters:
Definition: executor.hpp:91
static void execute(Expr expr, const DeviceT &dev)
executing the expression parameters:
Definition: executor.hpp:103
The Executor struct is used to specialise the execute function for different avaiable policies at com...
Definition: executor.hpp:74
IfExprExecNeeded is used to decide: 1) the expression should force its children to launch a separate ...
Definition: executor.hpp:74
The NoFuseExpr struct is used to generate one device kernel per each non-terminal node in the express...
Definition: executor.hpp:57
it is used to statically determine whether or not a subexpression execution is needed.
Definition: executor.hpp:116
static void execute(Expr &expr, const DeviceT &dev)
Definition: executor.hpp:117