A high-performance general-purpose compute library
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#ifndef CL_TARGET_OPENCL_VERSION
12#define CL_TARGET_OPENCL_VERSION 120
13#endif
14#if defined(__APPLE__) || defined(__MACOSX)
15#include <OpenCL/cl.h>
16#else
17#include <CL/cl.h>
18#endif
19
20#include <af/defines.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#if AF_API_VERSION >= 33
27typedef enum
28{
29 AFCL_DEVICE_TYPE_CPU = CL_DEVICE_TYPE_CPU,
30 AFCL_DEVICE_TYPE_GPU = CL_DEVICE_TYPE_GPU,
31 AFCL_DEVICE_TYPE_ACC = CL_DEVICE_TYPE_ACCELERATOR,
34#endif
35
36#if AF_API_VERSION >= 33
37typedef enum
38{
47#endif
48
62AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
63
73AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
74
82
83#if AF_API_VERSION >= 32
91#endif
92
93#if AF_API_VERSION >= 33
108AFAPI af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que);
109#endif
110
111#if AF_API_VERSION >= 33
118AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
119#endif
120
121#if AF_API_VERSION >= 33
133AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
134#endif
135
136#if AF_API_VERSION >= 33
141#endif
142
143#if AF_API_VERSION >= 33
148#endif
149
154#ifdef __cplusplus
155}
156#endif
157
158#ifdef __cplusplus
159
160#include <af/array.h>
161#include <af/dim4.hpp>
162#include <af/exception.h>
163#include <af/device.h>
164#include <stdio.h>
165
166namespace afcl
167{
168
182 static inline cl_context getContext(bool retain = false)
183 {
184 cl_context ctx;
185 af_err err = afcl_get_context(&ctx, retain);
186 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
187 return ctx;
188 }
189
198 static inline cl_command_queue getQueue(bool retain = false)
199 {
200 cl_command_queue queue;
201 af_err err = afcl_get_queue(&queue, retain);
202 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
203 return queue;
204 }
205
210 static inline cl_device_id getDeviceId()
211 {
212 cl_device_id id;
213 af_err err = afcl_get_device_id(&id);
214 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
215
216 return id;
217 }
218
219#if AF_API_VERSION >= 32
225 static inline void setDeviceId(cl_device_id id)
226 {
227 af_err err = afcl_set_device_id(id);
228 if (err != AF_SUCCESS) throw af::exception("Failed to set OpenCL device as active device");
229 }
230#endif
231
232#if AF_API_VERSION >= 33
247static inline void addDevice(cl_device_id dev, cl_context ctx, cl_command_queue que)
248{
249 af_err err = afcl_add_device_context(dev, ctx, que);
250 if (err!=AF_SUCCESS) throw af::exception("Failed to push user provided device/context to ArrayFire pool");
251}
252#endif
253
254#if AF_API_VERSION >= 33
261static inline void setDevice(cl_device_id dev, cl_context ctx)
262{
263 af_err err = afcl_set_device_context(dev, ctx);
264 if (err!=AF_SUCCESS) throw af::exception("Failed to set device based on cl_device_id & cl_context");
265}
266#endif
267
268#if AF_API_VERSION >= 33
280static inline void deleteDevice(cl_device_id dev, cl_context ctx)
281{
282 af_err err = afcl_delete_device_context(dev, ctx);
283 if (err!=AF_SUCCESS) throw af::exception("Failed to remove the requested device from ArrayFire device pool");
284}
285#endif
286
287
288#if AF_API_VERSION >= 33
291#endif
292
293#if AF_API_VERSION >= 33
298{
300 af_err err = afcl_get_device_type(&res);
301 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL device type");
302 return res;
303}
304#endif
305
306#if AF_API_VERSION >= 33
310static inline platform getPlatform()
311{
313 af_err err = afcl_get_platform(&res);
314 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL platform");
315 return res;
316}
317#endif
318
330 static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
331 {
332 const unsigned ndims = (unsigned)idims.ndims();
333 const dim_t *dims = idims.get();
334
335 cl_context context;
336 cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
337 if (clerr != CL_SUCCESS) {
338 throw af::exception("Failed to get context from cl_mem object \"buf\" ");
339 }
340
341 if (context != getContext()) {
342 throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
343 }
344
345
346 if (retain) clerr = clRetainMemObject(buf);
347
348 af_array out;
349 af_err err = af_device_array(&out, buf, ndims, dims, type);
350
351 if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
352 if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
353 throw af::exception("Failed to create device array");
354 }
355
356 return af::array(out);
357 }
358
370 static inline af::array array(dim_t dim0,
371 cl_mem buf, af::dtype type, bool retain=false)
372 {
373 return afcl::array(af::dim4(dim0), buf, type, retain);
374 }
375
388 static inline af::array array(dim_t dim0, dim_t dim1,
389 cl_mem buf, af::dtype type, bool retain=false)
390 {
391 return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
392 }
393
407 static inline af::array array(dim_t dim0, dim_t dim1,
408 dim_t dim2,
409 cl_mem buf, af::dtype type, bool retain=false)
410 {
411 return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
412 }
413
428 static inline af::array array(dim_t dim0, dim_t dim1,
429 dim_t dim2, dim_t dim3,
430 cl_mem buf, af::dtype type, bool retain=false)
431 {
432 return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
433 }
434
438}
439
440
441#endif
A multi dimensional data container.
Definition: array.h:37
Generic object that represents size and shape.
Definition: dim4.hpp:26
dim_t ndims()
Returns the number of axis whose values are greater than one.
dim_t * get()
Returns the underlying pointer to the dim4 object.
Definition: dim4.hpp:103
An ArrayFire exception class.
Definition: exception.h:22
af_dtype
Definition: defines.h:210
long long dim_t
Definition: defines.h:56
af_err
Definition: defines.h:71
@ AF_SUCCESS
The function returned successfully.
Definition: defines.h:75
void * af_array
Definition: defines.h:240
#define AFAPI
Definition: defines.h:38
AFAPI af_err af_device_array(af_array *arr, void *data, const unsigned ndims, const dim_t *const dims, const af_dtype type)
Create array from device memory.
afcl_platform platform
Definition: opencl.h:290
static cl_context getContext(bool retain=false)
Get a handle to ArrayFire's OpenCL context.
Definition: opencl.h:182
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.
static void setDeviceId(cl_device_id id)
Set ArrayFire's active device based on id of type cl_device_id.
Definition: opencl.h:225
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.
static void setDevice(cl_device_id dev, cl_context ctx)
Set active device using cl_context and cl_device_id.
Definition: opencl.h:261
AFAPI af_err afcl_get_platform(afcl_platform *res)
Get the platform of the current device.
AFAPI af_err afcl_get_device_type(afcl_device_type *res)
Get the type of the current device.
AFAPI af_err afcl_set_device_id(cl_device_id id)
Set ArrayFire's active device based on id of type cl_device_id.
static platform getPlatform()
Get a vendor enumeration for the current platform.
Definition: opencl.h:310
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:280
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire's OpenCL command queue.
Definition: opencl.h:198
static deviceType getDeviceType()
Get the type of the current device.
Definition: opencl.h:297
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 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:330
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_context(cl_context *ctx, const bool retain)
Get a handle to ArrayFire's OpenCL context.
static cl_device_id getDeviceId()
Get the device ID for ArrayFire's current active device.
Definition: opencl.h:210
afcl_device_type deviceType
Definition: opencl.h:289
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire's current active device.
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:247
Definition: opencl.h:167
afcl_device_type
Definition: opencl.h:28
@ AFCL_DEVICE_TYPE_CPU
Definition: opencl.h:29
@ AFCL_DEVICE_TYPE_ACC
Definition: opencl.h:31
@ AFCL_DEVICE_TYPE_UNKNOWN
Definition: opencl.h:32
@ AFCL_DEVICE_TYPE_GPU
Definition: opencl.h:30
afcl_platform
Definition: opencl.h:38
@ AFCL_PLATFORM_POCL
Definition: opencl.h:44
@ AFCL_PLATFORM_BEIGNET
Definition: opencl.h:43
@ AFCL_PLATFORM_INTEL
Definition: opencl.h:41
@ AFCL_PLATFORM_NVIDIA
Definition: opencl.h:42
@ AFCL_PLATFORM_APPLE
Definition: opencl.h:40
@ AFCL_PLATFORM_UNKNOWN
Definition: opencl.h:45
@ AFCL_PLATFORM_AMD
Definition: opencl.h:39