gpu_context_api_ocl.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief a header that defines wrappers for internal GPU plugin-specific
7  * OpenCL context and OpenCL shared memory blobs
8  *
9  * @file gpu_context_api_ocl.hpp
10  */
11 #pragma once
12 
13 #include <memory>
14 #include <string>
15 
16 #include "ie_compound_blob.h"
17 #include "ie_remote_context.hpp"
18 #include "ie_core.hpp"
19 
20 #include "gpu/gpu_params.hpp"
21 #include "gpu/gpu_ocl_wrapper.hpp"
23 
24 namespace InferenceEngine {
25 
26 namespace gpu {
27 /**
28 * @brief This class represents an abstraction for GPU plugin remote context
29 * which is shared with OpenCL context object.
30 * The plugin object derived from this class can be obtained either with
31 * GetContext() method of Executable network or using CreateContext() Core call.
32 */
34 public:
35  /**
36  * @brief A smart pointer to the ClContext object
37  */
38  using Ptr = std::shared_ptr<ClContext>;
39 
40  /**
41  * @brief Returns the underlying OpenCL context handle.
42  */
43  cl_context get() {
44  return _ObjFromParams<cl_context, gpu_handle_param>(getParams(), GPU_PARAM_KEY(OCL_CONTEXT),
45  GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(OCL), GPU_PARAM_VALUE(VA_SHARED));
46  }
47 
48  /**
49  * @brief OpenCL context handle conversion operator for the ClContext object.
50  * @return Underlying OpenCL context handle
51  */
52  operator cl_context() {
53  return get();
54  }
55 
56  /**
57  * @brief Standard Khronos cl::Context wrapper conversion operator for the ClContext object.
58  * @return cl::Context object
59  */
60  operator cl::Context() {
61  return cl::Context(get(), true);
62  }
63 };
64 
65 /**
66 * @brief The basic class for all GPU plugin remote blob objects.
67 * The OpenCL memory object handle (cl_mem) can be obtained from this class object.
68 */
69 class ClBlob : public RemoteBlob {
70 public:
71  /**
72  * @brief A smart pointer to the ClBlob object
73  */
74  using Ptr = std::shared_ptr<ClBlob>;
75 
76  /**
77  * @brief Creates a ClBlob object with the specified dimensions and layout.
78  * @param tensorDesc Tensor description
79  */
81 };
82 
83 /**
84 * @brief This class represents an abstraction for GPU plugin remote blob
85 * which can be shared with user-supplied OpenCL buffer.
86 * The plugin object derived from this class can be obtained with CreateBlob() call.
87 * @note User can obtain OpenCL buffer handle from this class.
88 */
90 public:
91  /**
92  * @brief A smart pointer to the ClBufferBlob object
93  */
94  using Ptr = std::shared_ptr<ClBufferBlob>;
95 
96  /**
97  * @brief Creates a ClBufferBlob object with the specified dimensions and layout.
98  * @param tensorDesc Tensor description
99  */
101 
102  /**
103  * @brief Returns the underlying OpenCL memory object handle.
104  */
105  cl_mem get() {
106  return _ObjFromParams<cl_mem, gpu_handle_param>(getParams(), GPU_PARAM_KEY(MEM_HANDLE),
107  GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_BUFFER), GPU_PARAM_VALUE(DX_BUFFER));
108  }
109 
110  /**
111  * @brief OpenCL memory handle conversion operator.
112  */
113  operator cl_mem() {
114  return get();
115  }
116 
117  /**
118  * @brief Standard Khronos cl::Buffer wrapper conversion operator.
119  * @return cl::Buffer object
120  */
121  operator cl::Buffer() {
122  return cl::Buffer(get(), true);
123  }
124 };
125 
126 /**
127 * @brief This class represents an abstraction for GPU plugin remote blob
128 * which can be shared with user-supplied OpenCL 2D Image.
129 * The plugin object derived from this class can be obtained with CreateBlob() call.
130 * @note User can obtain OpenCL image handle from this class.
131 */
133 public:
134  /**
135  * @brief A smart pointer to the ClImage2DBlob object
136  */
137  using Ptr = std::shared_ptr<ClImage2DBlob>;
138 
139  /**
140  * @brief Creates a ClImage2DBlob object with the specified dimensions and layout.
141  * @param tensorDesc Tensor description
142  */
144 
145  /**
146  * @brief Returns the underlying OpenCL memory object handle.
147  */
148  cl_mem get() {
149  return _ObjFromParams<cl_mem, gpu_handle_param>(getParams(), GPU_PARAM_KEY(MEM_HANDLE),
150  GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_IMAGE2D), GPU_PARAM_VALUE(VA_SURFACE));
151  }
152 
153  /**
154  * @brief OpenCL memory handle conversion operator.
155  */
156  operator cl_mem() {
157  return get();
158  }
159 
160  /**
161  * @brief Standard Khronos cl::Image2D wrapper conversion operator for the ClContext object.
162  * @return cl::Image2D object
163  */
164  operator cl::Image2D() {
165  return cl::Image2D(get(), true);
166  }
167 };
168 
169 /**
170 * @brief This function is used to construct a NV12 compound blob object from two cl::Image2D wrapper objects.
171 * The resulting compound contains two remote blobs for Y and UV planes of the surface.
172 * @param ctx RemoteContext plugin object derived from ClContext class.
173 * @param nv12_image_plane_y cl::Image2D object containing Y plane data.
174 * @param nv12_image_plane_uv cl::Image2D object containing UV plane data.
175 * @return Pointer to plugin-specific context class object, which is derived from RemoteContext.
176 */
177 static inline Blob::Ptr make_shared_blob_nv12(RemoteContext::Ptr ctx, cl::Image2D& nv12_image_plane_y, cl::Image2D& nv12_image_plane_uv) {
178  auto casted = std::dynamic_pointer_cast<ClContext>(ctx);
179  if (nullptr == casted) {
180  THROW_IE_EXCEPTION << "Invalid remote context passed";
181  }
182 
183  size_t width = nv12_image_plane_y.getImageInfo<CL_IMAGE_WIDTH>();
184  size_t height = nv12_image_plane_y.getImageInfo<CL_IMAGE_HEIGHT>();
185 
186  // despite of layout, blob dimensions always follow in N,C,H,W order
187  TensorDesc ydesc(Precision::U8, { 1, 1, height, width }, Layout::NHWC);
188 
189  ParamMap blobParams = {
190  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_IMAGE2D) },
191  { GPU_PARAM_KEY(MEM_HANDLE), static_cast<gpu_handle_param>(nv12_image_plane_y.get()) }
192  };
193  Blob::Ptr y_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(ydesc, blobParams));
194 
195  TensorDesc uvdesc(Precision::U8, { 1, 2, height / 2, width / 2 }, Layout::NHWC);
196  blobParams[GPU_PARAM_KEY(MEM_HANDLE)] = static_cast<gpu_handle_param>(nv12_image_plane_uv.get());
197  Blob::Ptr uv_blob = std::dynamic_pointer_cast<Blob>(casted->CreateBlob(uvdesc, blobParams));
198 
199  Blob::Ptr res = make_shared_blob<NV12Blob>(y_blob, uv_blob);
200  return res;
201 }
202 
203 /**
204 * @brief This function is used to obtain remote context object from user-supplied OpenCL context handle
205 */
206 static inline RemoteContext::Ptr make_shared_context(Core& core, std::string deviceName, cl_context ctx) {
207  ParamMap contextParams = {
208  { GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(OCL) },
209  { GPU_PARAM_KEY(OCL_CONTEXT), static_cast<gpu_handle_param>(ctx) }
210  };
211  return core.CreateContext(deviceName, contextParams);
212 }
213 
214 /**
215 * @brief This function is used to create remote blob object within default GPU plugin OpenCL context
216 */
218  return std::dynamic_pointer_cast<Blob>(ctx->CreateBlob(desc));
219 }
220 
221 /**
222 * @brief This function is used to obtain remote blob object from user-supplied cl::Buffer wrapper object
223 */
224 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, cl::Buffer& buffer) {
225  auto casted = std::dynamic_pointer_cast<ClContext>(ctx);
226  if (nullptr == casted) {
227  THROW_IE_EXCEPTION << "Invalid remote context passed";
228  }
229 
230  ParamMap params = {
231  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_BUFFER) },
232  { GPU_PARAM_KEY(MEM_HANDLE), static_cast<gpu_handle_param>(buffer.get()) }
233  };
234  return std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, params));
235 }
236 
237 /**
238 * @brief This function is used to obtain remote blob object from user-supplied OpenCL buffer handle
239 */
240 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, cl_mem buffer) {
241  auto casted = std::dynamic_pointer_cast<ClContext>(ctx);
242  if (nullptr == casted) {
243  THROW_IE_EXCEPTION << "Invalid remote context passed";
244  }
245 
246  ParamMap params = {
247  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_BUFFER) },
248  { GPU_PARAM_KEY(MEM_HANDLE), static_cast<gpu_handle_param>(buffer) }
249  };
250  return std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, params));
251 }
252 
253 /**
254 * @brief This function is used to obtain remote blob object from user-supplied cl::Image2D wrapper object
255 */
256 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, cl::Image2D& image) {
257  auto casted = std::dynamic_pointer_cast<ClContext>(ctx);
258  if (nullptr == casted) {
259  THROW_IE_EXCEPTION << "Invalid remote context passed";
260  }
261 
262  ParamMap params = {
263  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_IMAGE2D) },
264  { GPU_PARAM_KEY(MEM_HANDLE), static_cast<gpu_handle_param>(image.get()) }
265  };
266  return std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, params));
267 }
268 
269 } // namespace gpu
270 
271 } // namespace InferenceEngine
InferenceEngine::RemoteContext::getParams
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
InferenceEngine::gpu::details::param_map_obj_getter
This wrapper class is used to obtain low-level handles from remote blob or context object parameters.
Definition: gpu_context_helpers.hpp:23
InferenceEngine::RemoteContext
This class represents an Inference Engine abstraction for remote (non-CPU) accelerator device-specifi...
Definition: ie_remote_context.hpp:94
InferenceEngine::gpu::ClBlob::ClBlob
ClBlob(const TensorDesc &tensorDesc)
Creates a ClBlob object with the specified dimensions and layout.
Definition: gpu_context_api_ocl.hpp:80
InferenceEngine::Core
This class represents Inference Engine Core entity.
Definition: ie_core.hpp:29
InferenceEngine::gpu::make_shared_context
static RemoteContext::Ptr make_shared_context(Core &core, std::string deviceName, cl_context ctx)
This function is used to obtain remote context object from user-supplied OpenCL context handle.
Definition: gpu_context_api_ocl.hpp:206
InferenceEngine::Blob::tensorDesc
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:218
InferenceEngine::TensorDesc
This class defines Tensor description.
Definition: ie_layouts.h:153
InferenceEngine::gpu::ClImage2DBlob
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:132
InferenceEngine::RemoteBlob
This class represents an Inference Engine abstraction to the memory allocated on the remote (non-CPU)...
Definition: ie_remote_context.hpp:31
GPU_PARAM_VALUE
#define GPU_PARAM_VALUE(name)
Shortcut for defining configuration values.
Definition: gpu_params.hpp:32
InferenceEngine::gpu::ClBufferBlob
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:89
InferenceEngine::RemoteBlob::getParams
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
InferenceEngine::ParamMap
std::map< std::string, Parameter > ParamMap
An std::map object containing low-level object parameters of classes that are derived from RemoteBlob...
Definition: ie_remote_context.hpp:26
InferenceEngine::gpu::ClContext::Ptr
std::shared_ptr< ClContext > Ptr
A smart pointer to the ClContext object.
Definition: gpu_context_api_ocl.hpp:38
InferenceEngine::gpu::make_shared_blob
static Blob::Ptr make_shared_blob(const TensorDesc &desc, ClContext::Ptr ctx)
This function is used to create remote blob object within default GPU plugin OpenCL context.
Definition: gpu_context_api_ocl.hpp:217
InferenceEngine::RemoteContext::Ptr
std::shared_ptr< RemoteContext > Ptr
A smart pointer to the RemoteContext object.
Definition: ie_remote_context.hpp:99
InferenceEngine::gpu::ClBufferBlob::ClBufferBlob
ClBufferBlob(const TensorDesc &tensorDesc)
Creates a ClBufferBlob object with the specified dimensions and layout.
Definition: gpu_context_api_ocl.hpp:100
ie_compound_blob.h
A header file for CompoundBlob.
InferenceEngine::Blob::Ptr
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
GPU_PARAM_KEY
#define GPU_PARAM_KEY(name)
Shortcut for defining configuration keys.
Definition: gpu_params.hpp:27
InferenceEngine::gpu::ClBufferBlob::get
cl_mem get()
Returns the underlying OpenCL memory object handle.
Definition: gpu_context_api_ocl.hpp:105
THROW_IE_EXCEPTION
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
desc
Represents detailed information for an error.
Definition: ie_c_api.h:122
gpu_context_helpers.hpp
a header that defines helpers for GPU plugin-specific wrappers
InferenceEngine::gpu::ClImage2DBlob::ClImage2DBlob
ClImage2DBlob(const TensorDesc &tensorDesc)
Creates a ClImage2DBlob object with the specified dimensions and layout.
Definition: gpu_context_api_ocl.hpp:143
InferenceEngine::gpu::ClContext::get
cl_context get()
Returns the underlying OpenCL context handle.
Definition: gpu_context_api_ocl.hpp:43
InferenceEngine::gpu::make_shared_blob_nv12
static Blob::Ptr make_shared_blob_nv12(RemoteContext::Ptr ctx, cl::Image2D &nv12_image_plane_y, cl::Image2D &nv12_image_plane_uv)
This function is used to construct a NV12 compound blob object from two cl::Image2D wrapper objects....
Definition: gpu_context_api_ocl.hpp:177
ie_remote_context.hpp
This is a header file for the IE RemoteContext and RemoteBlob classes.
ie_core.hpp
This is a header file for the Inference Engine Core class C++ API.
InferenceEngine::Precision::U8
@ U8
Definition: ie_precision.hpp:34
InferenceEngine::gpu::ClBlob
The basic class for all GPU plugin remote blob objects. The OpenCL memory object handle (cl_mem) can ...
Definition: gpu_context_api_ocl.hpp:69
InferenceEngine::gpu::ClContext
This class represents an abstraction for GPU plugin remote context which is shared with OpenCL contex...
Definition: gpu_context_api_ocl.hpp:33
InferenceEngine::gpu_handle_param
void * gpu_handle_param
Shortcut for defining a handle parameter.
Definition: gpu_params.hpp:20
InferenceEngine::gpu::ClImage2DBlob::get
cl_mem get()
Returns the underlying OpenCL memory object handle.
Definition: gpu_context_api_ocl.hpp:148
InferenceEngine::gpu::ClBlob::Ptr
std::shared_ptr< ClBlob > Ptr
A smart pointer to the ClBlob object.
Definition: gpu_context_api_ocl.hpp:74
gpu_params.hpp
a header for properties of shared device contexts and shared device memory blobs for clDNN plugin