A high-performance general-purpose compute library

- Main Page
- Tutorials
- Release Notes
- Examples

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
23 extern "C" {
24#endif
25
26#if AF_API_VERSION >= 33
27 typedef 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,
32 AFCL_DEVICE_TYPE_UNKNOWN = -1
33} afcl_device_type;
34#endif
35
36#if AF_API_VERSION >= 33
37 typedef enum
38{
39 AFCL_PLATFORM_AMD = 0,
40 AFCL_PLATFORM_APPLE = 1,
41 AFCL_PLATFORM_INTEL = 2,
42 AFCL_PLATFORM_NVIDIA = 3,
43 AFCL_PLATFORM_BEIGNET = 4,
44 AFCL_PLATFORM_POCL = 5,
45 AFCL_PLATFORM_UNKNOWN = -1
46} afcl_platform;
47#endif
48
62 AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
63
73 AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
74
81 AFAPI af_err afcl_get_device_id(cl_device_id *id);
82
83#if AF_API_VERSION >= 32
90 AFAPI af_err afcl_set_device_id(cl_device_id id);
91#endif
92
93#if AF_API_VERSION >= 33
108 AFAPI 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
118 AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
119#endif
120
121#if AF_API_VERSION >= 33
133 AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
134#endif
135
136#if AF_API_VERSION >= 33
140 AFAPI af_err afcl_get_device_type(afcl_device_type *res);
141#endif
142
143#if AF_API_VERSION >= 33
147 AFAPI af_err afcl_get_platform(afcl_platform *res);
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
166 namespace 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
247 static 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
261 static 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
280 static 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
289 typedef afcl_device_type deviceType;
290 typedef afcl_platform platform;
291#endif
292
293#if AF_API_VERSION >= 33
297 static inline deviceType getDeviceType()
298{
299 afcl_device_type res = AFCL_DEVICE_TYPE_UNKNOWN;
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
310 static inline platform getPlatform()
311{
312 afcl_platform res = AFCL_PLATFORM_UNKNOWN;
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
array.h
af::array
A multi dimensional data container.
Definition array.h:37
af::dim4
Generic object that represents size and shape.
Definition dim4.hpp:26
af::dim4::ndims
dim_t ndims()
Returns the number of axis whose values are greater than one.
af::dim4::get
dim_t * get()
Returns the underlying pointer to the dim4 object.
Definition dim4.hpp:103
af::exception
An ArrayFire exception class.
Definition exception.h:22
defines.h
af_dtype
af_dtype
Definition defines.h:210
dim_t
long long dim_t
Definition defines.h:56
af_err
af_err
Definition defines.h:71
AF_SUCCESS
@ AF_SUCCESS
The function returned successfully.
Definition defines.h:75
af_array
void * af_array
Definition defines.h:240
AFAPI
#define AFAPI
Definition defines.h:38
device.h
dim4.hpp
exception.h
af_device_array
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
afcl_platform platform
Definition opencl.h:290
afcl::getContext
static cl_context getContext(bool retain=false)
Get a handle to ArrayFire's OpenCL context.
Definition opencl.h:182
afcl_delete_device_context
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.
afcl::setDeviceId
static void setDeviceId(cl_device_id id)
Set ArrayFire's active device based on id of type cl_device_id.
Definition opencl.h:225
afcl_add_device_context
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.
afcl::setDevice
static void setDevice(cl_device_id dev, cl_context ctx)
Set active device using cl_context and cl_device_id.
Definition opencl.h:261
afcl_get_platform
AFAPI af_err afcl_get_platform(afcl_platform *res)
Get the platform of the current device.
afcl_get_device_type
AFAPI af_err afcl_get_device_type(afcl_device_type *res)
Get the type of the current device.
afcl_set_device_id
AFAPI af_err afcl_set_device_id(cl_device_id id)
Set ArrayFire's active device based on id of type cl_device_id.
afcl::getPlatform
static platform getPlatform()
Get a vendor enumeration for the current platform.
Definition opencl.h:310
afcl::deleteDevice
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
afcl::getQueue
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire's OpenCL command queue.
Definition opencl.h:198
afcl::getDeviceType
static deviceType getDeviceType()
Get the type of the current device.
Definition opencl.h:297
afcl_set_device_context
AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx)
Set active device using cl_context and cl_device_id.
afcl::array
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
afcl_get_queue
AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain)
Get a handle to ArrayFire's OpenCL command queue.
afcl_get_context
AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain)
Get a handle to ArrayFire's OpenCL context.
afcl::getDeviceId
static cl_device_id getDeviceId()
Get the device ID for ArrayFire's current active device.
Definition opencl.h:210
afcl::deviceType
afcl_device_type deviceType
Definition opencl.h:289
afcl_get_device_id
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire's current active device.
afcl::addDevice
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
afcl
Definition opencl.h:167
afcl_device_type
afcl_device_type
Definition opencl.h:28
AFCL_DEVICE_TYPE_CPU
@ AFCL_DEVICE_TYPE_CPU
Definition opencl.h:29
AFCL_DEVICE_TYPE_ACC
@ AFCL_DEVICE_TYPE_ACC
Definition opencl.h:31
AFCL_DEVICE_TYPE_UNKNOWN
@ AFCL_DEVICE_TYPE_UNKNOWN
Definition opencl.h:32
AFCL_DEVICE_TYPE_GPU
@ AFCL_DEVICE_TYPE_GPU
Definition opencl.h:30
afcl_platform
afcl_platform
Definition opencl.h:38
AFCL_PLATFORM_POCL
@ AFCL_PLATFORM_POCL
Definition opencl.h:44
AFCL_PLATFORM_BEIGNET
@ AFCL_PLATFORM_BEIGNET
Definition opencl.h:43
AFCL_PLATFORM_INTEL
@ AFCL_PLATFORM_INTEL
Definition opencl.h:41
AFCL_PLATFORM_NVIDIA
@ AFCL_PLATFORM_NVIDIA
Definition opencl.h:42
AFCL_PLATFORM_APPLE
@ AFCL_PLATFORM_APPLE
Definition opencl.h:40
AFCL_PLATFORM_UNKNOWN
@ AFCL_PLATFORM_UNKNOWN
Definition opencl.h:45
AFCL_PLATFORM_AMD
@ AFCL_PLATFORM_AMD
Definition opencl.h:39