opencl.h
Go to the documentation of this file.
1 /*******************************************************
2  * Copyright (c) 2014, ArrayFire
3  * All rights reserved.
4  *
5  * This file is distributed under 3-clause BSD license.
6  * The complete license agreement can be obtained at:
7  * http://arrayfire.com/licenses/BSD-3-Clause
8  ********************************************************/
9 
10 #pragma once
11 #if defined(__APPLE__) || defined(__MACOSX)
12 #include <OpenCL/cl.h>
13 #else
14 #include <CL/cl.h>
15 #endif
16 
17 #include <af/defines.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #if AF_API_VERSION >= 33
24 typedef enum
25 {
26  AFCL_DEVICE_TYPE_CPU = CL_DEVICE_TYPE_CPU,
27  AFCL_DEVICE_TYPE_GPU = CL_DEVICE_TYPE_GPU,
28  AFCL_DEVICE_TYPE_ACC = CL_DEVICE_TYPE_ACCELERATOR,
31 #endif
32 
33 #if AF_API_VERSION >= 33
34 typedef enum
35 {
44 #endif
45 
59 AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
60 
70 AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
71 
78 AFAPI af_err afcl_get_device_id(cl_device_id *id);
79 
80 #if AF_API_VERSION >= 32
81 
87 AFAPI af_err afcl_set_device_id(cl_device_id id);
88 #endif
89 
90 #if AF_API_VERSION >= 33
91 
105 AFAPI af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que);
106 #endif
107 
108 #if AF_API_VERSION >= 33
109 
115 AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
116 #endif
117 
118 #if AF_API_VERSION >= 33
119 
130 AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
131 #endif
132 
133 #if AF_API_VERSION >= 33
134 
138 #endif
139 
140 #if AF_API_VERSION >= 33
141 
145 #endif
146 
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #ifdef __cplusplus
156 
157 #include <af/array.h>
158 #include <af/dim4.hpp>
159 #include <af/exception.h>
160 #include <af/device.h>
161 #include <stdio.h>
162 
163 namespace afcl
164 {
165 
181  static inline cl_context getContext(bool retain = false)
182  {
183  cl_context ctx;
184  af_err err = afcl_get_context(&ctx, retain);
185  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
186  return ctx;
187  }
188 
197  static inline cl_command_queue getQueue(bool retain = false)
198  {
199  cl_command_queue queue;
200  af_err err = afcl_get_queue(&queue, retain);
201  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
202  return queue;
203  }
204 
209  static inline cl_device_id getDeviceId()
210  {
211  cl_device_id id;
212  af_err err = afcl_get_device_id(&id);
213  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
214 
215  return id;
216  }
217 
218 #if AF_API_VERSION >= 32
219 
224  static inline void setDeviceId(cl_device_id id)
225  {
226  af_err err = afcl_set_device_id(id);
227  if (err != AF_SUCCESS) throw af::exception("Failed to set OpenCL device as active device");
228  }
229 #endif
230 
231 #if AF_API_VERSION >= 33
232 
246 static inline void addDevice(cl_device_id dev, cl_context ctx, cl_command_queue que)
247 {
248  af_err err = afcl_add_device_context(dev, ctx, que);
249  if (err!=AF_SUCCESS) throw af::exception("Failed to push user provided device/context to ArrayFire pool");
250 }
251 #endif
252 
253 #if AF_API_VERSION >= 33
254 
260 static inline void setDevice(cl_device_id dev, cl_context ctx)
261 {
262  af_err err = afcl_set_device_context(dev, ctx);
263  if (err!=AF_SUCCESS) throw af::exception("Failed to set device based on cl_device_id & cl_context");
264 }
265 #endif
266 
267 #if AF_API_VERSION >= 33
268 
279 static inline void deleteDevice(cl_device_id dev, cl_context ctx)
280 {
281  af_err err = afcl_delete_device_context(dev, ctx);
282  if (err!=AF_SUCCESS) throw af::exception("Failed to remove the requested device from ArrayFire device pool");
283 }
284 #endif
285 
286 
287 #if AF_API_VERSION >= 33
290 #endif
291 
292 #if AF_API_VERSION >= 33
293 
296 static inline deviceType getDeviceType()
297 {
299  af_err err = afcl_get_device_type(&res);
300  if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL device type");
301  return res;
302 }
303 #endif
304 
305 #if AF_API_VERSION >= 33
306 
309 static inline platform getPlatform()
310 {
312  af_err err = afcl_get_platform(&res);
313  if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL platform");
314  return res;
315 }
316 #endif
317 
329  static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
330  {
331  const unsigned ndims = (unsigned)idims.ndims();
332  const dim_t *dims = idims.get();
333 
334  cl_context context;
335  cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
336  if (clerr != CL_SUCCESS) {
337  throw af::exception("Failed to get context from cl_mem object \"buf\" ");
338  }
339 
340  if (context != getContext()) {
341  throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
342  }
343 
344 
345  if (retain) clerr = clRetainMemObject(buf);
346 
347  af_array out;
348  af_err err = af_device_array(&out, buf, ndims, dims, type);
349 
350  if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
351  if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
352  throw af::exception("Failed to create device array");
353  }
354 
355  return af::array(out);
356  }
357 
369  static inline af::array array(dim_t dim0,
370  cl_mem buf, af::dtype type, bool retain=false)
371  {
372  return afcl::array(af::dim4(dim0), buf, type, retain);
373  }
374 
387  static inline af::array array(dim_t dim0, dim_t dim1,
388  cl_mem buf, af::dtype type, bool retain=false)
389  {
390  return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
391  }
392 
406  static inline af::array array(dim_t dim0, dim_t dim1,
407  dim_t dim2,
408  cl_mem buf, af::dtype type, bool retain=false)
409  {
410  return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
411  }
412 
427  static inline af::array array(dim_t dim0, dim_t dim1,
428  dim_t dim2, dim_t dim3,
429  cl_mem buf, af::dtype type, bool retain=false)
430  {
431  return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
432  }
433 
437 }
438 
439 namespace af
440 {
441 
442 #if !defined(AF_OPENCL)
443 template<> AFAPI cl_mem *array::device() const
444 {
445  cl_mem *mem_ptr = new cl_mem;
446  af_err err = af_get_device_ptr((void **)mem_ptr, get());
447  if (err != AF_SUCCESS) throw af::exception("Failed to get cl_mem from array object");
448  return mem_ptr;
449 }
450 #endif
451 
452 }
453 
454 #endif
Definition: exception.h:19
Definition: opencl.h:36
AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx)
Set active device using cl_context and cl_device_id.
static void setDevice(cl_device_id dev, cl_context ctx)
Set active device using cl_context and cl_device_id.
Definition: opencl.h:260
static void addDevice(cl_device_id dev, cl_context ctx, cl_command_queue que)
Push user provided device control constructs into the ArrayFire device manager pool.
Definition: opencl.h:246
Definition: algorithm.h:14
static cl_device_id getDeviceId()
Get the device ID for ArrayFire&#39;s current active device.
Definition: opencl.h:209
AFAPI af_err af_get_device_ptr(void **ptr, const af_array arr)
Get the device pointer and lock the buffer in memory manager.
static platform getPlatform()
Get the type of the current device.
Definition: opencl.h:309
The function returned successfully.
Definition: defines.h:67
AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain)
Get a handle to ArrayFire&#39;s OpenCL command queue.
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire&#39;s current active device.
AFAPI af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que)
Push user provided device control constructs into the ArrayFire device manager pool.
Definition: opencl.h:26
Definition: opencl.h:38
Definition: opencl.h:40
afcl_device_type deviceType
Definition: opencl.h:288
Definition: opencl.h:41
Definition: opencl.h:29
Definition: opencl.h:42
static void setDeviceId(cl_device_id id)
Set ArrayFire&#39;s active device based on id of type cl_device_id.
Definition: opencl.h:224
A multi dimensional data container.
Definition: array.h:27
static af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
Create an af::array object from an OpenCL cl_mem buffer.
Definition: opencl.h:329
afcl_platform
Definition: opencl.h:34
AFAPI af_err afcl_get_platform(afcl_platform *res)
Get the platform of the current device.
AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain)
Get a handle to ArrayFire&#39;s OpenCL context.
af_err
Definition: defines.h:63
Definition: opencl.h:37
dim_t * get()
Definition: dim4.hpp:52
long long dim_t
Definition: defines.h:50
Definition: opencl.h:39
static cl_context getContext(bool retain=false)
Get a handle to ArrayFire&#39;s OpenCL context.
Definition: opencl.h:181
static deviceType getDeviceType()
Get the type of the current device.
Definition: opencl.h:296
afcl_platform platform
Definition: opencl.h:289
#define AFAPI
Definition: defines.h:31
dim_t ndims()
static af::array array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, cl_mem buf, af::dtype type, bool retain=false)
Create an af::array object from an OpenCL cl_mem buffer.
Definition: opencl.h:427
afcl_device_type
Definition: opencl.h:24
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire&#39;s OpenCL command queue.
Definition: opencl.h:197
AFAPI af_err afcl_get_device_type(afcl_device_type *res)
Get the type of the current device.
AFAPI af_err af_device_array(af_array *arr, const void *data, const unsigned ndims, const dim_t *const dims, const af_dtype type)
Create array from device memory.
Definition: opencl.h:163
void * af_array
Definition: defines.h:222
AFAPI af_err afcl_set_device_id(cl_device_id id)
Set ArrayFire&#39;s active device based on id of type cl_device_id.
Definition: dim4.hpp:26
Definition: opencl.h:28
af_dtype
Definition: defines.h:195
static void deleteDevice(cl_device_id dev, cl_context ctx)
Remove the user provided device control constructs from the ArrayFire device manager pool...
Definition: opencl.h:279
AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx)
Remove the user provided device control constructs from the ArrayFire device manager pool...
Definition: opencl.h:27