VisionCpp  0.0.1
mem_vision.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_vision.hpp
21 /// \brief this file contains the memory struct used to store {Sycl buffer ,
22 /// Host pointer or Sycl image}.
23 
24 #ifndef VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEM_VISION_HPP_
25 #define VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEM_VISION_HPP_
26 
27 namespace visioncpp {
28 namespace internal {
29 
30 /// \struct VisionMemory
31 /// \brief VisionMemory is the memory container of visioncpp. It has been
32 /// defined general enough that can be used for not only sycl buffer and image,
33 /// but also for host backend such as openmp.
34 /// template parameters:
35 /// \tparam MapAllocator: a boolean value representing whether or not the buffer
36 /// has map allocator
37 /// \tparam ScalarType: represent whether the element of the memory is Basic or
38 /// Struct.
39 /// \tparam MemoryType: represent the type of the memory.(e.g. Buffer2D,
40 /// Buffer1D, Image, Host)
41 /// \tparam Sclr: represent the type of each channel of the elements of the
42 /// memory
43 /// \tparam Col: represents the column size of the memory
44 /// \tparam Row: represents the Row size of the memory
45 /// \tparam ElementTp: represents the types of each element inside the memory
46 /// \tparam Elements: represents the number of channels for each element
47 /// \tparam Sc: represents the memory target on the device (global memory or
48 /// constant memory)- Default is global
49 // LVL : represent the level of the memory in the expression tree
50 template <bool MapAllocator, size_t ScalarType, size_t MemoryType,
51  typename Sclr, size_t Col, size_t Row, typename ElementTp,
52  size_t Elements, size_t Sc, size_t LVL>
53 struct VisionMemory {
54  public:
55  static constexpr size_t Rows = Row;
56  static constexpr size_t Cols = Col;
57  static constexpr size_t Channels = Elements;
58  static constexpr size_t LeafType = MemoryType;
59  static constexpr size_t Dim = MemDimension<MemoryType>::Dim;
60  static constexpr bool SubExpressionEvaluationNeeded = false;
61  static constexpr size_t MemoryCategory = ScalarType;
62  static constexpr bool HasMapAllocator = MapAllocator;
63  static constexpr size_t scope = Sc;
64  using ElementType = ElementTp;
65  using Scalar = Sclr;
66  using AccessType = ElementType; // it should be changed later on when it is
67  // going to be used for image. This is the reason it has been added now
68  template <cl::sycl::access::mode acMd>
69  using Accessor = typename SyclAccessor<LeafType, Dim, acMd, ElementType,
71  // FIXME:: we have to make it work for image as well , the cl::sycl must be
72  // removed as well
73  template <cl::sycl::access::mode acMd>
74  using HostAccessor =
75  typename SyclAccessor<LeafType, Dim, acMd, ElementType, Scalar,
78  Rows, ElementTp, Elements, scope, LVL>;
79  static constexpr size_t Level = LVL;
80  using syclBuffer =
82  std::shared_ptr<syclBuffer> syclData;
83  std::shared_ptr<HostAccessor<cl::sycl::access::mode::read>> hostAcc;
84 
85  static constexpr size_t used_memory() {
86  return (Rows * Cols * Channels * sizeof(Scalar));
87  }
88 
89  static constexpr size_t get_size() { return (Type::used_memory()); }
90 
92  create_sycl_buffer<LeafType, ElementType, Scalar>(
93  syclData, dt, get_range<Dim>(Rows, Cols));
94  }
95  /// buffer copy is lightweight no need to pass by ref
96  VisionMemory(syclBuffer dt) { syclData = std::make_shared<syclBuffer>(dt); }
97 
99  create_sycl_buffer<LeafType, ElementType, Scalar>(
100  syclData, get_range<Dim>(Rows, Cols));
101  }
102  /// sub_expression_evaluation
103  /// \brief This function is used to break the expression tree whenever
104  /// necessary. The decision for breaking the tree will be determined based on
105  /// the static parameter called SubExpressionEvaluationNeeded. When this is
106  /// set to true, the sub_expression_evaluation is called recursively from the
107  /// root of the tree. Each node based on their parent decision will decide to
108  /// launch a kernel for itself. Also, they decide for each of their children
109  /// whether or not to launch a kernel separately.
110  /// template parameters:
111  ///\tparam ForcedToExec : a boolean value representing the decision made by
112  /// the parent of this node for launching a kernel.
113  /// \tparam LC: is the column size of local memory required by Filter2D and
114  /// DownSmplOP
115  /// \tparam LR: is the row size of local memory required by Filter2D and
116  /// DownSmplOP
117  /// \tparam LCT: is the column size of workgroup
118  /// \tparam LRT: is the row size of workgroup
119  /// function parameters:
120  /// \return LeafNode
121  template <bool ForcedToExec, size_t LC, size_t LR, size_t LCT, size_t LRT,
122  typename DeviceT>
124  const DeviceT &) {
125  return internal::LeafNode<Type, Level>(*this);
126  }
127  /// \brief This function is used to get the device access on the memory.
128  /// The mode of the accessor represents the type of the access.
129  /// template parameters:
130  /// \tparam accMode : represents the sycl type of access
131  /// function parameters:
132  /// \param cgh: sycl command group handler
133  /// \return Accessor
134  template <cl::sycl::access::mode accMode>
135  Accessor<accMode> get_device_accessor(cl::sycl::handler &cgh) {
136  return Accessor<accMode>(*syclData, cgh);
137  }
138  /// \brief reset_input is used to manually reset the input value of an input
139  /// sycl buffer. This can be used when we are dealing with video streaming and
140  /// we want to pass different input stream to the expression.
141  /// \return void
142  void reset_input(Scalar *dt) {
143  buffer_update<LeafType, Rows, Cols, ElementType, Scalar>(syclData, dt);
144  }
145 
146  /// \brief set_output function is used to destroy the sycl buffer and manually
147  /// allocated the data to the provided pointer. This is used when we needed to
148  /// return the value of the device-only buffer.
149  /// \param ptr: the pointer for manually allocating the data
150  /// \return void
151  void set_output(std::shared_ptr<Scalar> &ptr) {
152  syclData.get()->set_final_data(ptr);
153  syclData.reset(); // that my needed to be added
154  }
155  /// \brief lock function is used to access the sycl buffer on the host using
156  /// a host pointer. Because the host accessor is blocking we are creating it
157  /// dynamically so by calling the lock function. It is the responsibility of
158  /// a user to call unlock function in order to destroy the accessor once his
159  /// work is finished. This is useful when we are dealing with video and we
160  /// want to display each frame of the video at the end of each iteration.
161  /// \return void
162  void lock() {
163  hostAcc = std::make_shared<HostAccessor<cl::sycl::access::mode::read>>(
165  }
166  /// \brief The unlock function for destroying the host accessor. It must be
167  /// called by user if the lock has been called in order to destroy the host
168  /// accessor and release the execution.
169  /// \return void
170  void unlock() { hostAcc.reset(); }
171 };
172 } // internal
173 } // visioncpp
174 #endif // VISIONCPP_INCLUDE_FRAMEWORK_MEMORY_MEM_VISION_HPP_
static constexpr ScopeType Host_Buffer
VisionCpp namespace.
Definition: sycl/device.hpp:24
the definition is in LeafNode.
Definition: leaf_node.hpp:38
this is used to determine the dimension of the memory based on the memory type template parameters:
Definition: memory.hpp:294
This struct is used to create a sycl accessor type based on access mode; dimension and memory type.
Definition: memory.hpp:216
SyclMem is used to create VisionMemory data storage.
Definition: memory.hpp:254
Definition of VisionMemory.
Definition: mem_vision.hpp:53
std::shared_ptr< HostAccessor< cl::sycl::access::mode::read > > hostAcc
Definition: mem_vision.hpp:83
static constexpr size_t Rows
Definition: mem_vision.hpp:55
static constexpr size_t LeafType
Definition: mem_vision.hpp:58
typename SyclAccessor< LeafType, Dim, acMd, ElementType, Scalar, scope >::Accessor Accessor
Definition: mem_vision.hpp:70
void unlock()
The unlock function for destroying the host accessor.
Definition: mem_vision.hpp:170
void lock()
lock function is used to access the sycl buffer on the host using a host pointer.
Definition: mem_vision.hpp:162
static constexpr size_t Dim
Definition: mem_vision.hpp:59
static constexpr size_t get_size()
Definition: mem_vision.hpp:89
static constexpr bool HasMapAllocator
Definition: mem_vision.hpp:62
void set_output(std::shared_ptr< Scalar > &ptr)
set_output function is used to destroy the sycl buffer and manually allocated the data to the provide...
Definition: mem_vision.hpp:151
static constexpr size_t Channels
Definition: mem_vision.hpp:57
static constexpr size_t Level
Definition: mem_vision.hpp:79
static constexpr bool SubExpressionEvaluationNeeded
Definition: mem_vision.hpp:60
Accessor< accMode > get_device_accessor(cl::sycl::handler &cgh)
This function is used to get the device access on the memory.
Definition: mem_vision.hpp:135
internal::LeafNode< Type, Level > sub_expression_evaluation(const DeviceT &)
sub_expression_evaluation
Definition: mem_vision.hpp:123
static constexpr size_t Cols
Definition: mem_vision.hpp:56
static constexpr size_t MemoryCategory
Definition: mem_vision.hpp:61
std::shared_ptr< syclBuffer > syclData
Definition: mem_vision.hpp:82
void reset_input(Scalar *dt)
reset_input is used to manually reset the input value of an input sycl buffer.
Definition: mem_vision.hpp:142
static constexpr size_t used_memory()
Definition: mem_vision.hpp:85
static constexpr size_t scope
Definition: mem_vision.hpp:63
typename SyclMem< HasMapAllocator, LeafType, Dim, ElementType >::Type syclBuffer
Definition: mem_vision.hpp:81
typename SyclAccessor< LeafType, Dim, acMd, ElementType, Scalar, scope::Host_Buffer >::Accessor HostAccessor
Definition: mem_vision.hpp:76
VisionMemory(syclBuffer dt)
buffer copy is lightweight no need to pass by ref
Definition: mem_vision.hpp:96