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 A shared remote blob instance
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  * @param core A reference to Inference Engine Core object
206  * @param deviceName A name of device to create a remote context for
207  * @param ctx A OpenCL context to be used to create shared remote context
208  * @return A shared remote context instance
209  */
210 static inline RemoteContext::Ptr make_shared_context(Core& core, std::string deviceName, cl_context ctx) {
211  ParamMap contextParams = {
212  { GPU_PARAM_KEY(CONTEXT_TYPE), GPU_PARAM_VALUE(OCL) },
213  { GPU_PARAM_KEY(OCL_CONTEXT), static_cast<gpu_handle_param>(ctx) }
214  };
215  return core.CreateContext(deviceName, contextParams);
216 }
217 
218 /**
219  * @brief This function is used to create remote blob object within default GPU plugin OpenCL context
220  * @param desc A tensor descriptor object representing remote blob configuration
221  * @param ctx A remote context used to create remote blob
222  * @return A remote blob instance
223  */
225  return std::dynamic_pointer_cast<Blob>(ctx->CreateBlob(desc));
226 }
227 
228 /**
229  * @brief This function is used to obtain remote blob object from user-supplied cl::Buffer wrapper object
230  * @param desc A tensor descriptor object representing remote blob configuration
231  * @param ctx A remote context used to create remote blob
232  * @param buffer A cl::Buffer object wrapped by a remote blob
233  * @return A remote blob instance
234  */
235 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, cl::Buffer& buffer) {
236  auto casted = std::dynamic_pointer_cast<ClContext>(ctx);
237  if (nullptr == casted) {
238  THROW_IE_EXCEPTION << "Invalid remote context passed";
239  }
240 
241  ParamMap params = {
242  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_BUFFER) },
243  { GPU_PARAM_KEY(MEM_HANDLE), static_cast<gpu_handle_param>(buffer.get()) }
244  };
245  return std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, params));
246 }
247 
248 /**
249  * @brief This function is used to obtain remote blob object from user-supplied OpenCL buffer handle
250  * @param desc A tensor descriptor object representing remote blob configuration
251  * @param ctx A remote context used to create remote blob
252  * @param buffer A cl_mem object wrapped by a remote blob
253  * @return A remote blob instance
254  */
255 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, cl_mem buffer) {
256  auto casted = std::dynamic_pointer_cast<ClContext>(ctx);
257  if (nullptr == casted) {
258  THROW_IE_EXCEPTION << "Invalid remote context passed";
259  }
260 
261  ParamMap params = {
262  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_BUFFER) },
263  { GPU_PARAM_KEY(MEM_HANDLE), static_cast<gpu_handle_param>(buffer) }
264  };
265  return std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, params));
266 }
267 
268 /**
269  * @brief This function is used to obtain remote blob object from user-supplied cl::Image2D wrapper object
270  * @param desc A tensor descriptor object representing remote blob configuration
271  * @param ctx A remote context used to create remote blob
272  * @param buffer A cl::Image2D object wrapped by a remote blob
273  * @return A remote blob instance
274  */
275 static inline Blob::Ptr make_shared_blob(const TensorDesc& desc, RemoteContext::Ptr ctx, cl::Image2D& image) {
276  auto casted = std::dynamic_pointer_cast<ClContext>(ctx);
277  if (nullptr == casted) {
278  THROW_IE_EXCEPTION << "Invalid remote context passed";
279  }
280 
281  ParamMap params = {
282  { GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_IMAGE2D) },
283  { GPU_PARAM_KEY(MEM_HANDLE), static_cast<gpu_handle_param>(image.get()) }
284  };
285  return std::dynamic_pointer_cast<Blob>(casted->CreateBlob(desc, params));
286 }
287 
288 } // namespace gpu
289 
290 } // namespace InferenceEngine
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:227
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
This class represents Inference Engine Core entity.
Definition: ie_core.hpp:29
@ U8
Definition: ie_precision.hpp:34
This class represents an Inference Engine abstraction to the memory allocated on the remote (non-CPU)...
Definition: ie_remote_context.hpp:31
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
This class represents an Inference Engine abstraction for remote (non-CPU) accelerator device-specifi...
Definition: ie_remote_context.hpp:94
virtual ParamMap getParams() const =0
Returns a map of device-specific parameters required for low-level operations with underlying object....
std::shared_ptr< RemoteContext > Ptr
A smart pointer to the RemoteContext object.
Definition: ie_remote_context.hpp:99
This class defines Tensor description.
Definition: ie_layouts.h:158
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
std::shared_ptr< ClBlob > Ptr
A smart pointer to the ClBlob object.
Definition: gpu_context_api_ocl.hpp:74
ClBlob(const TensorDesc &tensorDesc)
Creates a ClBlob object with the specified dimensions and layout.
Definition: gpu_context_api_ocl.hpp:80
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:89
ClBufferBlob(const TensorDesc &tensorDesc)
Creates a ClBufferBlob object with the specified dimensions and layout.
Definition: gpu_context_api_ocl.hpp:100
cl_mem get()
Returns the underlying OpenCL memory object handle.
Definition: gpu_context_api_ocl.hpp:105
This class represents an abstraction for GPU plugin remote context which is shared with OpenCL contex...
Definition: gpu_context_api_ocl.hpp:33
std::shared_ptr< ClContext > Ptr
A smart pointer to the ClContext object.
Definition: gpu_context_api_ocl.hpp:38
cl_context get()
Returns the underlying OpenCL context handle.
Definition: gpu_context_api_ocl.hpp:43
This class represents an abstraction for GPU plugin remote blob which can be shared with user-supplie...
Definition: gpu_context_api_ocl.hpp:132
ClImage2DBlob(const TensorDesc &tensorDesc)
Creates a ClImage2DBlob object with the specified dimensions and layout.
Definition: gpu_context_api_ocl.hpp:143
cl_mem get()
Returns the underlying OpenCL memory object handle.
Definition: gpu_context_api_ocl.hpp:148
This wrapper class is used to obtain low-level handles from remote blob or context object parameters.
Definition: gpu_context_helpers.hpp:23
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:210
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:224
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
a header that defines helpers for GPU plugin-specific wrappers
a header for properties of shared device contexts and shared device memory blobs for clDNN plugin
void * gpu_handle_param
Shortcut for defining a handle parameter.
Definition: gpu_params.hpp:20
#define GPU_PARAM_KEY(name)
Shortcut for defining configuration keys.
Definition: gpu_params.hpp:27
#define GPU_PARAM_VALUE(name)
Shortcut for defining configuration values.
Definition: gpu_params.hpp:32
@ NHWC
"NHWC" layout
Definition: ie_c_api.h:144
A header file for CompoundBlob.
This is a header file for the Inference Engine Core class C++ API.
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
This is a header file for the IE RemoteContext and RemoteBlob classes.
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
Represents detailed information for an error.
Definition: ie_c_api.h:122