VisionCpp  0.0.1
mem_neighbour.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 mem_neighbour.hpp
21 /// this file contains different types of memory required for executing
22 /// neighbour operation to calculate each pixel output
23 
24 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEMORY_ACCESS_MEM_NEIGHBOUR_HPP_
25 #define VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEMORY_ACCESS_MEM_NEIGHBOUR_HPP_
26 
27 namespace visioncpp {
28 namespace internal {
29 /// \struct LocalNeighbour
30 /// \brief LocalNeighbour is used to provide local access for each
31 /// element of the local memory based on the Coordinate passed by eval
32 /// expression. It is used as an input type for user functor when the local
33 /// neighbour operation is required.
34 /// template parameters
35 /// \tparam T is the pixel type for the local memory
36 template <typename T> struct LocalNeighbour {
37 public:
38  using PixelType = T;
39  cl::sycl::local_ptr<T> &ptr;
40 
41  int I_c;
42  int I_r;
43  LocalNeighbour(cl::sycl::local_ptr<T> &ptr, size_t colsArg, size_t rowsArg)
44  : ptr(ptr), I_c(0), I_r(0), cols(colsArg), rows(rowsArg) {}
45  /// function set_offset:
46  /// \brief used to set the local memory offset for each local thread
47  /// function parameters:
48  /// \param c : column index
49  /// \param r: row index
50  /// \return void
51  inline void set_offset(int c, int r) {
52  I_c = c;
53  I_r = r;
54  }
55  /// function at provides access to a specific Coordinate for a 2d buffer
56  /// parameters:
57  /// \param c: column index
58  /// \param r: row index
59  /// \return PixelType
60  inline PixelType at(int c, int r) const {
61  c = (c >= 0 ? c : 0);
62  r = (r >= 0 ? r : 0);
63  return *(ptr + calculate_index(c, r, cols, rows));
64  }
65  /// function at provides access to a specific Coordinate for a 1d buffer
66  /// parameters:
67  /// \param c: index
68  /// \return PixelType
69  inline PixelType at(int c) const { return *(ptr + c); }
70 
71 private:
72  size_t cols;
73  size_t rows;
74 };
75 /// \struct GlobalNeighbour
76 /// \brief GlobalNeighbour is used to provide local access for each
77 /// element of the global memory based on the Coordinate passed by eval
78 /// expression. It is used as an input type for user functor when the global
79 /// neighbour operation is required.
80 /// template parameters
81 /// \tparam T is the pixel type for the global memory
82 template <typename T> struct GlobalNeighbour {
83  using PixelType = T;
84  size_t I_c;
85  size_t I_r;
86  size_t cols;
87  size_t rows;
88  cl::sycl::global_ptr<T> &ptr;
89  GlobalNeighbour(cl::sycl::global_ptr<T> &ptr, size_t colsArg, size_t rowsArg)
90  : I_c(0), I_r(0), cols(colsArg), rows(rowsArg), ptr(ptr) {}
91  /// function set_offset:
92  /// \brief used to set the global memory offset for each global thread
93  /// function parameters:
94  /// \param c : column index
95  /// \param r: row index
96  /// \return void
97  inline void set_offset(int c, int r) {
98  I_c = c;
99  I_r = r;
100  }
101  /// function at provides access to a specific Coordinate for a 2d buffer
102  /// parameters:
103  /// \param c: column index
104  /// \param r: row index
105  /// \return PixelType
106  inline PixelType at(int c, int r) const {
107  c = (c >= 0 ? c : 0);
108  r = (r >= 0 ? r : 0);
109  return *(ptr + calculate_index(c, r, cols, rows));
110  }
111  /// function at provides access to a specific coordinate for a 1d buffer
112  /// parameters:
113  /// \param c: index
114  /// \return PixelType
115  inline PixelType at(int c) const { return *(ptr + c); }
116 };
117 /// \struct ConstNeighbour
118 /// \brief ConstNeighbour is used to provide global access to the constant
119 /// memory. It is used as an input type for user functor when a constant pointer
120 /// needed to be passed on the device side. An example of such node can be a
121 /// filter node for convolution operation.
122 /// template parameters
123 /// \tparam T is the pixel type for the constant memory
124 template <typename T> struct ConstNeighbour {
125  using PixelType = T;
126  cl::sycl::constant_ptr<T> &ptr;
127  size_t cols;
128  size_t rows;
129  ConstNeighbour(cl::sycl::constant_ptr<T> &ptr, size_t colsArg, size_t rowsArg)
130  : ptr(ptr), cols(colsArg), rows(rowsArg) {}
131  /// function at provides access to a specific coordinate for a 2d buffer
132  /// parameters:
133  /// \param c: column index
134  /// \param r: row index
135  /// \return PixelType
136  inline PixelType at(int c, int r) const {
137  c = (c >= 0 ? c : 0);
138  r = (r >= 0 ? r : 0);
139  return *(ptr + calculate_index(c, r, cols, rows));
140  }
141  /// function at provides access to an specific Coordinate for a 1d buffer
142  /// parameters:
143  /// \param c: index
144  /// \return PixelType
145  inline PixelType at(int c) const { return *(ptr + c); }
146 };
147 } // namespace internal
148 } // namespace visioncpp
149 #endif // VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEMORY_ACCESS_MEM_NEIGHBOUR_HPP_
static size_t calculate_index(size_t c, size_t r, size_t cols, size_t rows)
function calculate_index
VisionCpp namespace.
Definition: sycl/device.hpp:24
ConstNeighbour is used to provide global access to the constant memory.
cl::sycl::constant_ptr< T > & ptr
PixelType at(int c, int r) const
function at provides access to a specific coordinate for a 2d buffer parameters:
ConstNeighbour(cl::sycl::constant_ptr< T > &ptr, size_t colsArg, size_t rowsArg)
PixelType at(int c) const
function at provides access to an specific Coordinate for a 1d buffer parameters:
GlobalNeighbour is used to provide local access for each element of the global memory based on the Co...
cl::sycl::global_ptr< T > & ptr
GlobalNeighbour(cl::sycl::global_ptr< T > &ptr, size_t colsArg, size_t rowsArg)
void set_offset(int c, int r)
function set_offset:
PixelType at(int c, int r) const
function at provides access to a specific Coordinate for a 2d buffer parameters:
PixelType at(int c) const
function at provides access to a specific coordinate for a 1d buffer parameters:
LocalNeighbour is used to provide local access for each element of the local memory based on the Coor...
LocalNeighbour(cl::sycl::local_ptr< T > &ptr, size_t colsArg, size_t rowsArg)
cl::sycl::local_ptr< T > & ptr
PixelType at(int c) const
function at provides access to a specific Coordinate for a 1d buffer parameters:
PixelType at(int c, int r) const
function at provides access to a specific Coordinate for a 2d buffer parameters:
void set_offset(int c, int r)
function set_offset: