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 
22  AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
23 
24  AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
25 
26  AFAPI af_err afcl_get_device_id(cl_device_id *id);
27 
28 #ifdef __cplusplus
29 }
30 #endif
31 
32 #ifdef __cplusplus
33 
34 #include <af/array.h>
35 #include <af/dim4.hpp>
36 #include <af/exception.h>
37 #include <af/device.h>
38 #include <stdio.h>
39 
40 namespace afcl
41 {
57  static inline cl_context getContext(bool retain = false)
58  {
59  cl_context ctx;
60  af_err err = afcl_get_context(&ctx, retain);
61  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
62  return ctx;
63  }
64 
73  static inline cl_command_queue getQueue(bool retain = false)
74  {
75  cl_command_queue queue;
76  af_err err = afcl_get_queue(&queue, retain);
77  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
78  return queue;
79  }
80 
85  static inline cl_device_id getDeviceId()
86  {
87  cl_device_id id;
88  af_err err = afcl_get_device_id(&id);
89  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
90 
91  return id;
92  }
93 
105  static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
106  {
107  const unsigned ndims = (unsigned)idims.ndims();
108  const dim_t *dims = idims.get();
109 
110  cl_context context;
111  cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
112  if (clerr != CL_SUCCESS) {
113  throw af::exception("Failed to get context from cl_mem object \"buf\" ");
114  }
115 
116  if (context != getContext()) {
117  throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
118  }
119 
120 
121  if (retain) clerr = clRetainMemObject(buf);
122 
123  af_array out;
124  af_err err = af_device_array(&out, buf, ndims, dims, type);
125 
126  if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
127  if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
128  throw af::exception("Failed to create device array");
129  }
130 
131  return af::array(out);
132  }
133 
145  static inline af::array array(dim_t dim0,
146  cl_mem buf, af::dtype type, bool retain=false)
147  {
148  return afcl::array(af::dim4(dim0), buf, type, retain);
149  }
150 
163  static inline af::array array(dim_t dim0, dim_t dim1,
164  cl_mem buf, af::dtype type, bool retain=false)
165  {
166  return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
167  }
168 
182  static inline af::array array(dim_t dim0, dim_t dim1,
183  dim_t dim2,
184  cl_mem buf, af::dtype type, bool retain=false)
185  {
186  return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
187  }
188 
203  static inline af::array array(dim_t dim0, dim_t dim1,
204  dim_t dim2, dim_t dim3,
205  cl_mem buf, af::dtype type, bool retain=false)
206  {
207  return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
208  }
209 
213 }
214 
215 namespace af {
216  template<> AFAPI cl_mem *array::device() const
217  {
218  cl_mem *mem = new cl_mem;
219  af_err err = af_get_device_ptr((void **)mem, get());
220  if (err != AF_SUCCESS) throw af::exception("Failed to get cl_mem from array object");
221  return mem;
222  }
223 }
224 
225 #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:203
Definition: algorithm.h:14
AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain)
The function returned successfully.
Definition: defines.h:62
static cl_device_id getDeviceId()
Get the device ID for ArrayFire's current active device.
Definition: opencl.h:85
A multi dimensional data container.
Definition: array.h:27
af_err
Definition: defines.h:58
AFAPI af_err afcl_get_device_id(cl_device_id *id)
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:57
#define AFAPI
Definition: defines.h:31
dim_t ndims()
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:105
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:40
void * af_array
Definition: defines.h:172
AFAPI af_err af_get_device_ptr(void **ptr, const af_array arr)
Definition: dim4.hpp:26
AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain)
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire's OpenCL command queue.
Definition: opencl.h:73
af_dtype
Definition: defines.h:151
T * device() const