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 
59 #ifdef __cplusplus
60 }
61 #endif
62 
63 #ifdef __cplusplus
64 
65 #include <af/array.h>
66 #include <af/dim4.hpp>
67 #include <af/exception.h>
68 #include <af/device.h>
69 #include <stdio.h>
70 
71 namespace afcl
72 {
88  static inline cl_context getContext(bool retain = false)
89  {
90  cl_context ctx;
91  af_err err = afcl_get_context(&ctx, retain);
92  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
93  return ctx;
94  }
95 
104  static inline cl_command_queue getQueue(bool retain = false)
105  {
106  cl_command_queue queue;
107  af_err err = afcl_get_queue(&queue, retain);
108  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
109  return queue;
110  }
111 
116  static inline cl_device_id getDeviceId()
117  {
118  cl_device_id id;
119  af_err err = afcl_get_device_id(&id);
120  if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
121 
122  return id;
123  }
124 
136  static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
137  {
138  const unsigned ndims = (unsigned)idims.ndims();
139  const dim_t *dims = idims.get();
140 
141  cl_context context;
142  cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
143  if (clerr != CL_SUCCESS) {
144  throw af::exception("Failed to get context from cl_mem object \"buf\" ");
145  }
146 
147  if (context != getContext()) {
148  throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
149  }
150 
151 
152  if (retain) clerr = clRetainMemObject(buf);
153 
154  af_array out;
155  af_err err = af_device_array(&out, buf, ndims, dims, type);
156 
157  if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
158  if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
159  throw af::exception("Failed to create device array");
160  }
161 
162  return af::array(out);
163  }
164 
176  static inline af::array array(dim_t dim0,
177  cl_mem buf, af::dtype type, bool retain=false)
178  {
179  return afcl::array(af::dim4(dim0), buf, type, retain);
180  }
181 
194  static inline af::array array(dim_t dim0, dim_t dim1,
195  cl_mem buf, af::dtype type, bool retain=false)
196  {
197  return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
198  }
199 
213  static inline af::array array(dim_t dim0, dim_t dim1,
214  dim_t dim2,
215  cl_mem buf, af::dtype type, bool retain=false)
216  {
217  return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
218  }
219 
234  static inline af::array array(dim_t dim0, dim_t dim1,
235  dim_t dim2, dim_t dim3,
236  cl_mem buf, af::dtype type, bool retain=false)
237  {
238  return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
239  }
240 
244 }
245 
246 namespace af {
247  template<> AFAPI cl_mem *array::device() const
248  {
249  cl_mem *mem = new cl_mem;
250  af_err err = af_get_device_ptr((void **)mem, get());
251  if (err != AF_SUCCESS) throw af::exception("Failed to get cl_mem from array object");
252  return mem;
253  }
254 }
255 
256 #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:234
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:116
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:88
#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:136
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:71
void * af_array
Definition: defines.h:187
Definition: dim4.hpp:26
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire's OpenCL command queue.
Definition: opencl.h:104
af_dtype
Definition: defines.h:166
T * device() const