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 #if defined(__APPLE__) || defined(__MACOSX)
11 #include <OpenCL/cl.h>
12 #else
13 #include <CL/cl.h>
14 #endif
15 
16 #include <af/defines.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
35 AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
36 
46 AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
47 
54 AFAPI af_err afcl_get_device_id(cl_device_id *id);
55 
56 #if AF_API_VERSION >= 32
57 
63 AFAPI af_err afcl_set_device_id(cl_device_id id);
64 #endif
65 
70 #ifdef __cplusplus
71 }
72 #endif
73 
74 #ifdef __cplusplus
75 
76 #include <af/array.h>
77 #include <af/dim4.hpp>
78 #include <af/exception.h>
79 #include <af/device.h>
80 #include <stdio.h>
81 
82 namespace afcl
83 {
84 
100  static inline cl_context getContext(bool retain = false)
101  {
102  cl_context ctx;
103  af_err err = afcl_get_context(&ctx, retain);
104  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
105  return ctx;
106  }
107 
116  static inline cl_command_queue getQueue(bool retain = false)
117  {
118  cl_command_queue queue;
119  af_err err = afcl_get_queue(&queue, retain);
120  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
121  return queue;
122  }
123 
128  static inline cl_device_id getDeviceId()
129  {
130  cl_device_id id;
131  af_err err = afcl_get_device_id(&id);
132  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
133 
134  return id;
135  }
136 
137 #if AF_API_VERSION >= 32
138 
143  static inline void setDeviceId(cl_device_id id)
144  {
145  af_err err = afcl_set_device_id(id);
146  if (err != AF_SUCCESS) throw af::exception("Failed to set OpenCL device as active device");
147  }
148 #endif
149 
161  static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
162  {
163  const unsigned ndims = (unsigned)idims.ndims();
164  const dim_t *dims = idims.get();
165 
166  cl_context context;
167  cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
168  if (clerr != CL_SUCCESS) {
169  throw af::exception("Failed to get context from cl_mem object \"buf\" ");
170  }
171 
172  if (context != getContext()) {
173  throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
174  }
175 
176 
177  if (retain) clerr = clRetainMemObject(buf);
178 
179  af_array out;
180  af_err err = af_device_array(&out, buf, ndims, dims, type);
181 
182  if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
183  if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
184  throw af::exception("Failed to create device array");
185  }
186 
187  return af::array(out);
188  }
189 
201  static inline af::array array(dim_t dim0,
202  cl_mem buf, af::dtype type, bool retain=false)
203  {
204  return afcl::array(af::dim4(dim0), buf, type, retain);
205  }
206 
219  static inline af::array array(dim_t dim0, dim_t dim1,
220  cl_mem buf, af::dtype type, bool retain=false)
221  {
222  return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
223  }
224 
238  static inline af::array array(dim_t dim0, dim_t dim1,
239  dim_t dim2,
240  cl_mem buf, af::dtype type, bool retain=false)
241  {
242  return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
243  }
244 
259  static inline af::array array(dim_t dim0, dim_t dim1,
260  dim_t dim2, dim_t dim3,
261  cl_mem buf, af::dtype type, bool retain=false)
262  {
263  return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
264  }
265 
270 }
271 
272 namespace af
273 {
274 
275 template<> AFAPI cl_mem *array::device() const
276 {
277  cl_mem *mem = new cl_mem;
278  af_err err = af_get_device_ptr((void **)mem, get());
279  if (err != AF_SUCCESS) throw af::exception("Failed to get cl_mem from array object");
280  return mem;
281 }
282 
283 }
284 
285 #endif
Definition: exception.h:19
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:259
Definition: algorithm.h:14
AFAPI af_err af_get_device_ptr(void **ptr, const af_array arr)
Get the device pointer and lock the buffer in memory manager.
The function returned successfully.
Definition: defines.h:71
AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain)
Get a handle to ArrayFire's OpenCL command queue.
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire's current active device.
static cl_device_id getDeviceId()
Get the device ID for ArrayFire's current active device.
Definition: opencl.h:128
A multi dimensional data container.
Definition: array.h:27
AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain)
Get a handle to ArrayFire's OpenCL context.
af_err
Definition: defines.h:67
dim_t * get()
Definition: dim4.hpp:52
long long dim_t
Definition: defines.h:50
static cl_context getContext(bool retain=false)
Get a handle to ArrayFire's OpenCL context.
Definition: opencl.h:100
#define AFAPI
Definition: defines.h:31
dim_t ndims()
static void setDeviceId(cl_device_id id)
Set ArrayFire's active device based on id of type cl_device_id.
Definition: opencl.h:143
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:161
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:82
void * af_array
Definition: defines.h:219
AFAPI af_err afcl_set_device_id(cl_device_id id)
Set ArrayFire's active device based on id of type cl_device_id.
Definition: dim4.hpp:26
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire's OpenCL command queue.
Definition: opencl.h:116
af_dtype
Definition: defines.h:192
T * device() const