opencl.h Source File

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 #if defined(__APPLE__) || defined(__MACOSX)
12 #include <OpenCL/cl.h>
13 #else
14 #include <CL/cl.h>
15 #endif
16
17 #include <af/defines.h>
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 #if AF_API_VERSION >= 33
24 typedef enum
25 {
26 AFCL_DEVICE_TYPE_CPU = CL_DEVICE_TYPE_CPU,
27 AFCL_DEVICE_TYPE_GPU = CL_DEVICE_TYPE_GPU,
28 AFCL_DEVICE_TYPE_ACC = CL_DEVICE_TYPE_ACCELERATOR,
29 AFCL_DEVICE_TYPE_UNKNOWN = -1
30 } afcl_device_type;
31 #endif
32
33 #if AF_API_VERSION >= 33
34 typedef enum
35 {
36 AFCL_PLATFORM_AMD = 0,
37 AFCL_PLATFORM_APPLE = 1,
38 AFCL_PLATFORM_INTEL = 2,
39 AFCL_PLATFORM_NVIDIA = 3,
40 AFCL_PLATFORM_BEIGNET = 4,
41 AFCL_PLATFORM_POCL = 5,
42 AFCL_PLATFORM_UNKNOWN = -1
43 } afcl_platform;
44 #endif
45
59 AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
60
70 AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
71
78 AFAPI af_err afcl_get_device_id(cl_device_id *id);
79
80 #if AF_API_VERSION >= 32
81
87 AFAPI af_err afcl_set_device_id(cl_device_id id);
88 #endif
89
90 #if AF_API_VERSION >= 33
91
105 AFAPI af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que);
106 #endif
107
108 #if AF_API_VERSION >= 33
109
115 AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
116 #endif
117
118 #if AF_API_VERSION >= 33
119
130 AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
131 #endif
132
133 #if AF_API_VERSION >= 33
134
137 AFAPI af_err afcl_get_device_type(afcl_device_type *res);
138 #endif
139
140 #if AF_API_VERSION >= 33
141
144 AFAPI af_err afcl_get_platform(afcl_platform *res);
145 #endif
146
151 #ifdef __cplusplus
152 }
153 #endif
154
155 #ifdef __cplusplus
156
157 #include <af/array.h>
158 #include <af/dim4.hpp>
159 #include <af/exception.h>
160 #include <af/device.h>
161 #include <stdio.h>
162
163 namespace afcl
164 {
165
179 static inline cl_context getContext(bool retain = false)
180 {
181 cl_context ctx;
182 af_err err = afcl_get_context(&ctx, retain);
183 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
184 return ctx;
185 }
186
195 static inline cl_command_queue getQueue(bool retain = false)
196 {
197 cl_command_queue queue;
198 af_err err = afcl_get_queue(&queue, retain);
199 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
200 return queue;
201 }
202
207 static inline cl_device_id getDeviceId()
208 {
209 cl_device_id id;
210 af_err err = afcl_get_device_id(&id);
211 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
212
213 return id;
214 }
215
216 #if AF_API_VERSION >= 32
217
222 static inline void setDeviceId(cl_device_id id)
223 {
224 af_err err = afcl_set_device_id(id);
225 if (err != AF_SUCCESS) throw af::exception("Failed to set OpenCL device as active device");
226 }
227 #endif
228
229 #if AF_API_VERSION >= 33
230
244 static inline void addDevice(cl_device_id dev, cl_context ctx, cl_command_queue que)
245 {
246 af_err err = afcl_add_device_context(dev, ctx, que);
247 if (err!=AF_SUCCESS) throw af::exception("Failed to push user provided device/context to ArrayFire pool");
248 }
249 #endif
250
251 #if AF_API_VERSION >= 33
252
258 static inline void setDevice(cl_device_id dev, cl_context ctx)
259 {
260 af_err err = afcl_set_device_context(dev, ctx);
261 if (err!=AF_SUCCESS) throw af::exception("Failed to set device based on cl_device_id & cl_context");
262 }
263 #endif
264
265 #if AF_API_VERSION >= 33
266
277 static inline void deleteDevice(cl_device_id dev, cl_context ctx)
278 {
279 af_err err = afcl_delete_device_context(dev, ctx);
280 if (err!=AF_SUCCESS) throw af::exception("Failed to remove the requested device from ArrayFire device pool");
281 }
282 #endif
283
284
285 #if AF_API_VERSION >= 33
286 typedef afcl_device_type deviceType;
287 typedef afcl_platform platform;
288 #endif
289
290 #if AF_API_VERSION >= 33
291
294 static inline deviceType getDeviceType()
295 {
296 afcl_device_type res = AFCL_DEVICE_TYPE_UNKNOWN;
297 af_err err = afcl_get_device_type(&res);
298 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL device type");
299 return res;
300 }
301 #endif
302
303 #if AF_API_VERSION >= 33
304
307 static inline platform getPlatform()
308 {
309 afcl_platform res = AFCL_PLATFORM_UNKNOWN;
310 af_err err = afcl_get_platform(&res);
311 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL platform");
312 return res;
313 }
314 #endif
315
327 static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
328 {
329 const unsigned ndims = (unsigned)idims.ndims();
330 const dim_t *dims = idims.get();
331
332 cl_context context;
333 cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
334 if (clerr != CL_SUCCESS) {
335 throw af::exception("Failed to get context from cl_mem object \"buf\" ");
336 }
337
338 if (context != getContext()) {
339 throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
340 }
341
342
343 if (retain) clerr = clRetainMemObject(buf);
344
345 af_array out;
346 af_err err = af_device_array(&out, buf, ndims, dims, type);
347
348 if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
349 if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
350 throw af::exception("Failed to create device array");
351 }
352
353 return af::array(out);
354 }
355
367 static inline af::array array(dim_t dim0,
368 cl_mem buf, af::dtype type, bool retain=false)
369 {
370 return afcl::array(af::dim4(dim0), buf, type, retain);
371 }
372
385 static inline af::array array(dim_t dim0, dim_t dim1,
386 cl_mem buf, af::dtype type, bool retain=false)
387 {
388 return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
389 }
390
404 static inline af::array array(dim_t dim0, dim_t dim1,
405 dim_t dim2,
406 cl_mem buf, af::dtype type, bool retain=false)
407 {
408 return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
409 }
410
425 static inline af::array array(dim_t dim0, dim_t dim1,
426 dim_t dim2, dim_t dim3,
427 cl_mem buf, af::dtype type, bool retain=false)
428 {
429 return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
430 }
431
435 }
436
437
438 #endif
af::exception
Definition: exception.h:19
AFCL_PLATFORM_AMD
Definition: opencl.h:36
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(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:425
AF_SUCCESS
The function returned successfully.
Definition: defines.h:67
afcl_get_queue
AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain)
Get a handle to ArrayFire&#39;s OpenCL command queue.
array.h
afcl_get_device_id
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire&#39;s current active device.
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_DEVICE_TYPE_CPU
Definition: opencl.h:26
afcl::getDeviceId
static cl_device_id getDeviceId()
Get the device ID for ArrayFire&#39;s current active device.
Definition: opencl.h:207
afcl::getPlatform
static platform getPlatform()
Get a vendor enumeration for the current platform.
Definition: opencl.h:307
afcl::deviceType
afcl_device_type deviceType
Definition: opencl.h:286
AFCL_PLATFORM_INTEL
Definition: opencl.h:38
AFCL_PLATFORM_BEIGNET
Definition: opencl.h:40
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:277
AFCL_PLATFORM_POCL
Definition: opencl.h:41
device.h
AFCL_DEVICE_TYPE_UNKNOWN
Definition: opencl.h:29
exception.h
AFCL_PLATFORM_UNKNOWN
Definition: opencl.h:42
af::array
A multi dimensional data container.
Definition: array.h:27
afcl_platform
afcl_platform
Definition: opencl.h:34
dim4.hpp
afcl_get_platform
AFAPI af_err afcl_get_platform(afcl_platform *res)
Get the platform of the current device.
afcl_get_context
AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain)
Get a handle to ArrayFire&#39;s OpenCL context.
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:244
af_err
af_err
Definition: defines.h:63
afcl::getDeviceType
static deviceType getDeviceType()
Get the type of the current device.
Definition: opencl.h:294
defines.h
afcl::platform
afcl_platform platform
Definition: opencl.h:287
AFCL_PLATFORM_APPLE
Definition: opencl.h:37
af::dim4::get
dim_t * get()
Definition: dim4.hpp:52
dim_t
long long dim_t
Definition: defines.h:50
AFCL_PLATFORM_NVIDIA
Definition: opencl.h:39
afcl::getContext
static cl_context getContext(bool retain=false)
Get a handle to ArrayFire&#39;s OpenCL context.
Definition: opencl.h:179
AFAPI
#define AFAPI
Definition: defines.h:31
af::dim4::ndims
dim_t ndims()
afcl_device_type
afcl_device_type
Definition: opencl.h:24
afcl::setDeviceId
static void setDeviceId(cl_device_id id)
Set ArrayFire&#39;s active device based on id of type cl_device_id.
Definition: opencl.h:222
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:327
afcl_get_device_type
AFAPI af_err afcl_get_device_type(afcl_device_type *res)
Get the type of the current device.
af_device_array
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.
afcl
Definition: opencl.h:163
af_array
void * af_array
Definition: defines.h:222
afcl_set_device_id
AFAPI af_err afcl_set_device_id(cl_device_id id)
Set ArrayFire&#39;s active device based on id of type cl_device_id.
af::dim4
Definition: dim4.hpp:26
afcl::getQueue
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire&#39;s OpenCL command queue.
Definition: opencl.h:195
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:258
AFCL_DEVICE_TYPE_ACC
Definition: opencl.h:28
af_dtype
af_dtype
Definition: defines.h:195
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_DEVICE_TYPE_GPU
Definition: opencl.h:27