VisionCpp  0.0.1
eval_expr_assign.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 eval_expr_assign.hpp
21 /// this file contains the specialisation of EvalExpr for Assign when it
22 /// is not a root
23 
24 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_EVALUATOR_EVAL_EXPRESSION_EVAL_EXPR_ASSIGN_HPP_
25 #define VISIONCPP_INCLUDE_FRAMEWORK_EVALUATOR_EVAL_EXPRESSION_EVAL_EXPR_ASSIGN_HPP_
26 
27 namespace visioncpp {
28 namespace internal {
29 /// \brief Partial specialisation of the EvalExpr when the expression is
30 /// an Assign expression. This node is not a root node.
31 template <typename LHS, typename RHS, size_t Cols, size_t Rows, size_t LfType,
32  size_t LVL, typename Loc, typename... Params>
33 struct EvalExpr<Assign<LHS, RHS, Cols, Rows, LfType, LVL>, Loc, Params...> {
34  using LHS_Eval_Expr = EvalExpr<LHS, Loc, Params...>;
35  using RHS_Eval_Expr = EvalExpr<RHS, Loc, Params...>;
37  -> decltype(LHS_Eval_Expr::get_accessor(t)) {
38  return LHS_Eval_Expr::get_accessor(t);
39  }
40  /// \brief evaluate function when the internal::ops_category is PointOP.
41  static auto eval_point(Loc &cOffset, const tools::tuple::Tuple<Params...> &t)
42  -> typename MemoryTrait<LfType,
43  decltype(LHS_Eval_Expr::get_accessor(t))>::Type {
44  auto x = RHS_Eval_Expr::eval_point(cOffset, t);
45  LHS_Eval_Expr::eval_point(cOffset, t) = x;
46  return x;
47  }
48  /// \brief evaluate function when the internal::ops_category is NeighbourOP.
49  template <bool IsRoot, size_t Halo_Top, size_t Halo_Left, size_t Halo_Butt,
50  size_t Halo_Right, size_t Offset, size_t Index, size_t LC,
51  size_t LR>
52  static auto eval_neighbour(Loc &cOffset,
54  -> decltype(
56  t)) {
57  constexpr bool isLocal =
58  Trait<typename tools::RemoveAll<decltype(
59  LHS_Eval_Expr::get_accessor(t))>::Type>::scope == scope::Local;
60  constexpr size_t LC_Ratio = RHS::CThread / Cols;
61  constexpr size_t LR_Ratio = RHS::RThread / Rows;
62  // lhs expression shared mem
63  auto nested_accessor =
64  RHS_Eval_Expr::template eval_neighbour<IsRoot, Halo_Top, Halo_Left,
65  Halo_Butt, Halo_Right, Offset,
66  Index, LC, LR>(cOffset, t);
67  constexpr bool isLocal_nested =
68  Trait<typename tools::RemoveAll<decltype(
69  nested_accessor)>::Type>::scope == scope::Local;
70  auto rhs_acc = nested_accessor.get_pointer();
71  auto lhs_acc = LHS_Eval_Expr::get_accessor(t).get_pointer();
72  if ((cOffset.l_c < (cOffset.cLRng / LC_Ratio)) &&
73  (cOffset.l_r < (cOffset.rLRng / LR_Ratio))) {
74  size_t g_c = ((cOffset.g_c - cOffset.l_c) / LC_Ratio) + cOffset.l_c;
75  size_t g_r = ((cOffset.g_r - cOffset.l_r) / LR_Ratio) + cOffset.l_r;
76  for (int i = 0; i < LC / LC_Ratio; i += (cOffset.cLRng / LC_Ratio)) {
77  if (get_compare<isLocal, LC / LC_Ratio, Cols>(cOffset.l_c, i, g_c)) {
78  for (size_t j = 0; j < LR / LR_Ratio;
79  j += (cOffset.rLRng / LR_Ratio)) {
80  if (get_compare<isLocal, LR / LR_Ratio, Rows>(cOffset.l_r, j,
81  g_r)) {
82  lhs_acc[calculate_index(id_val<isLocal>(cOffset.l_c, g_c) + i,
83  id_val<isLocal>(cOffset.l_r, g_r) + j,
84  id_val<isLocal>(LC / LC_Ratio, Cols),
85  id_val<isLocal>(LR / LR_Ratio, Rows))] =
86  tools::convert<typename MemoryTrait<
87  LfType, decltype(nested_accessor)>::Type>(
88  rhs_acc[calculate_index(
89  id_val<isLocal_nested>(cOffset.l_c, g_c) + i,
90  id_val<isLocal_nested>(cOffset.l_r, g_r) + j,
91  id_val<isLocal_nested>(LC / LC_Ratio, Cols),
92  id_val<isLocal_nested>(LR / LR_Ratio, Rows))]);
93  }
94  }
95  }
96  }
97  }
98  // here you need to put local barrier
99  cOffset.barrier();
100 
101  return tools::tuple::get<OutputLocation<IsRoot, Offset + Index - 1>::ID>(t);
102  }
103  /// \brief evaluate function when the internal::ops_category is
104  /// GlobalNeighbour.
105  template <bool IsRoot, size_t Offset, size_t Index, size_t LC, size_t LR>
106  static auto eval_global_neighbour(Loc &cOffset,
108  -> decltype(
110  t)) {
111  // lhs expression shared mem
112  auto rhs_acc = RHS_Eval_Expr::template eval_global_neighbour<IsRoot, Offset,
113  Index, LC, LR>(
114  cOffset, t).get_pointer();
115  auto lhs_acc = LHS_Eval_Expr::get_accessor(t).get_pointer();
116  // here the neighbour is the entire output
117  size_t index = 0;
118  for (int i = 0; i < LC; i += cOffset.cLRng) {
119  if (get_compare<false, LC, Cols>(cOffset.l_c, i, cOffset.g_c)) {
120  for (int j = 0; j < LR; j += cOffset.rLRng) {
121  if (get_compare<false, LR, Rows>(cOffset.l_r, j, cOffset.g_r)) {
122  index =
123  calculate_index(cOffset.g_c + i, cOffset.g_r + j, Cols, Rows);
124  lhs_acc[index] = rhs_acc[index];
125  }
126  }
127  }
128  }
129 
130  // here you need to put a local barrier
131  cOffset.barrier();
132 
133  return RHS_Eval_Expr::template eval_global_neighbour<IsRoot, Offset,
134  Index - 1>(cOffset, t);
135  }
136 };
137 } // internal
138 } // visioncpp
139 #endif // VISIONCPP_INCLUDE_FRAMEWORK_EVALUATOR_EVAL_EXPRESSION_EVAL_EXPR_ASSIGN_HPP_
EnableIf< k==0, typename ElemTypeHolder< 0, Tuple< Ts... > >::type & >::type get(Tuple< Ts... > &t)
get
Definition: tuple.hpp:99
T1 convert(T2 x)
function convert
Definition: convert.hpp:558
static size_t calculate_index(size_t c, size_t r, size_t cols, size_t rows)
function calculate_index
static constexpr ScopeType Local
VisionCpp namespace.
Definition: sycl/device.hpp:24
The definition is in Assign file.
Definition: assign.hpp:44
static auto eval_global_neighbour(Loc &cOffset, const tools::tuple::Tuple< Params... > &t) -> decltype(tools::tuple::get< OutputLocation< IsRoot, Offset+Index - 1 >::ID >(t))
evaluate function when the internal::ops_category is GlobalNeighbour.
static auto eval_neighbour(Loc &cOffset, const tools::tuple::Tuple< Params... > &t) -> decltype(tools::tuple::get< OutputLocation< IsRoot, Offset+Index - 1 >::ID >(t))
evaluate function when the internal::ops_category is NeighbourOP.
static auto eval_point(Loc &cOffset, const tools::tuple::Tuple< Params... > &t) -> typename MemoryTrait< LfType, decltype(LHS_Eval_Expr::get_accessor(t))>::Type
evaluate function when the internal::ops_category is PointOP.
static auto get_accessor(const tools::tuple::Tuple< Params... > &t) -> decltype(LHS_Eval_Expr::get_accessor(t))
eval_global_neighbour function:
Definition: evaluator.hpp:195
This class is used to determine the ElementType of accessor template parameters.
This is used to find whether a node should use a global memory output or a local memory output is cre...
Definition: evaluator.hpp:265
This struct is used to trait the value type inside the accessor.
Definition: evaluator.hpp:89
These methods are used to remove all the & const and * from a type.
The tuple is a fixed-size collection of heterogeneous values.
Definition: tuple.hpp:48