VisionCpp  0.0.1
memory_access.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 memory_access.hpp
21 /// this file contains a set of forward declarations and include headers
22 /// required for constructing and accessing memory on both host and device.
23 
24 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEMORY_ACCESS_MEMORY_ACCESS_HPP_
25 #define VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEMORY_ACCESS_MEMORY_ACCESS_HPP_
26 
27 namespace visioncpp {
28 namespace internal {
29 /// \struct CompareIdBasedScope
30 /// \brief this is used for range check to make sure the
31 /// index is within the range. It uses the local value when the Conds
32 /// are true.
33 /// template parameters:
34 /// \tparam Conds: determines whether or not the local variable should be used
35 /// \tparam LDSize : determines the local dimension size
36 /// \tparam GDSize: determines the global dimension size
37 /// \tparam T determines the type of the dimension index
38 template <bool Conds, size_t LDSize, size_t GDSize, typename T>
40  /// function get
41  /// \brief returns the local range check:
42  /// \param l is the local dimension size
43  /// \param g is the global dimension size
44  /// \param i is the offset needed to be added to the local dimension
45  /// before comparison
46  /// \return bool
47  static inline bool get(T &l, int &i, T &g) { return (l + i < LDSize); }
48 };
49 
50 /// \brief specialisation of the CompareIdBasedScope when the Conds is false in
51 /// this case the range check is with the global size
52 /// template parameters:
53 /// \tparam LDSize : determines the local dimension size
54 /// \tparam GDSize: determines the global dimension size
55 /// \tparam T: determines the type of the dimension index
56 template <size_t LDSize, size_t GDSize, typename T>
57 struct CompareIdBasedScope<false, LDSize, GDSize, T> {
58  /// function get
59  /// \brief returns the global range check:
60  /// \param l is the local dimension size
61  /// \param g is the global dimension size
62  /// \param i is the offset needed to be added to the global dimension
63  /// before comparison
64  /// return bool
65  static inline bool get(T &l, int &i, T &g) {
66  return ((l + i < LDSize) && (g + i < GDSize));
67  }
68 };
69 /// function get_compare
70 /// template deduction for CompareIdBasedScope
71 /// template parameters:
72 /// \tparam Conds: determines whether or not the local variable should be used
73 /// \tparam LDSize : determines the local dimension size
74 /// \tparam GDSize: determines the global dimension size
75 /// \tparam T: determines the type of the dimension index
76 /// function parameters:
77 /// \param l is the local dimension size
78 /// \param g is the global dimension size
79 /// \param i is the offset needed to be added to the correct dimension
80 /// before comparison
81 /// return bool
82 template <bool Conds, size_t LDSize, size_t GDSize, typename T>
83 static inline bool get_compare(T l, int i, T g) {
85 }
86 
87 /// \struct GetIdBasedScope
88 /// brief This is used to get the correct Id based on the condition. True means
89 /// local
90 /// template parameters
91 /// \tparam Conds: determines whether or not the local variable should be used
92 /// \tparam T: determines the type of the dimension index
93 template <bool Conds, typename T>
95  /// function get
96  /// \brief returns the local range check:
97  /// \param l is the local dimension size
98  /// \param g is the global dimension size
99  /// \return T
100  static inline T get(T l, T g) { return l; }
101 };
102 /// \brief specialisation of the GetIdBasedScope when the condition is false. In
103 /// this case the get function return global index.
104 template <typename T>
105 struct GetIdBasedScope<false, T> {
106  /// function get
107  /// \brief returns the local range check:
108  /// \param l is the local dimension size
109  /// \param g is the global dimension size
110  /// \return T
111  static inline T get(T l, T g) { return g; }
112 };
113 
114 /// function id_val
115 /// \brief template deduction for GetIdBasedScope.
116 /// template parameters
117 /// \tparam Conds: determines whether or not the local variable should be used
118 /// \tparam T: determines the type of the dimension index
119 /// function parameters:
120 /// \param l is the local dimension size
121 /// \param g is the global dimension size
122 /// \return T
123 template <bool Conds, typename T>
124 static inline T id_val(T l, T g) {
125  return GetIdBasedScope<Conds, T>::get(l, g);
126 }
127 
128 /// \struct MemoryTrait
129 /// \brief This class is used to determine the ElementType of accessor
130 /// template parameters
131 /// \tparam LeafType: the type of the memory
132 /// \tparam T: the element type
133 template <size_t LeafType, typename T>
134 struct MemoryTrait {
136 };
137 /// \brief specialisation of MemoryTrait when the LeafType is Host
138 template <typename T>
139 struct MemoryTrait<memory_type::Host, T> {
140  using Type = typename tools::RemoveAll<T>::Type;
141 };
142 
143 /// function calculate_index
144 /// \brief this function is used to calculate the index access of the memory
145 /// pointer on the device.
146 /// parameters:
147 /// \param c : column index
148 /// \param r : row index
149 /// \param cols : column dimension
150 /// \param rows : row dimension
151 /// \return size_t
152 static inline size_t calculate_index(size_t c, size_t r, size_t cols,
153  size_t rows) {
154  return (((r * cols) + c) < cols * rows) ? ((r * cols) + c)
155  : (cols * rows) - 1;
156 }
157 } // internal
158 } // visioncpp
159 #include "mem_coordinate.hpp"
160 #include "mem_neighbour.hpp"
161 #endif // VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEMORY_ACCESS_MEMORY_ACCESS_HPP_
This files contains the Coordinate struct which is used to specify local/global offset for local/glob...
this file contains different types of memory required for executing neighbour operation to calculate ...
static size_t calculate_index(size_t c, size_t r, size_t cols, size_t rows)
function calculate_index
static T id_val(T l, T g)
function id_val
static bool get_compare(T l, int i, T g)
function get_compare template deduction for CompareIdBasedScope template parameters:
static constexpr size_t Host
VisionCpp namespace.
Definition: sycl/device.hpp:24
this is used for range check to make sure the index is within the range.
static bool get(T &l, int &i, T &g)
function get
brief This is used to get the correct Id based on the condition.
static T get(T l, T g)
function get
This class is used to determine the ElementType of accessor template parameters.
typename tools::RemoveAll< T >::Type::value_type Type
These methods are used to remove all the & const and * from a type.