opencl.h Source File

- Main Page
- Tutorials
- Functions
- 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 #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
107 AFAPI af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que);
108 #endif
109
110 #if AF_API_VERSION >= 33
111
117 AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
118 #endif
119
120 #if AF_API_VERSION >= 33
121
134 AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
135 #endif
136
137 #if AF_API_VERSION >= 33
138
141 AFAPI af_err afcl_get_device_type(afcl_device_type *res);
142 #endif
143
144 #if AF_API_VERSION >= 33
145
148 AFAPI af_err afcl_get_platform(afcl_platform *res);
149 #endif
150
155 #ifdef __cplusplus
156 }
157 #endif
158
159 #ifdef __cplusplus
160
161 #include <af/array.h>
162 #include <af/dim4.hpp>
163 #include <af/exception.h>
164 #include <af/device.h>
165 #include <stdio.h>
166
167 namespace afcl
168 {
169
185 static inline cl_context getContext(bool retain = false)
186 {
187 cl_context ctx;
188 af_err err = afcl_get_context(&ctx, retain);
189 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
190 return ctx;
191 }
192
201 static inline cl_command_queue getQueue(bool retain = false)
202 {
203 cl_command_queue queue;
204 af_err err = afcl_get_queue(&queue, retain);
205 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
206 return queue;
207 }
208
213 static inline cl_device_id getDeviceId()
214 {
215 cl_device_id id;
216 af_err err = afcl_get_device_id(&id);
217 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
218
219 return id;
220 }
221
222 #if AF_API_VERSION >= 32
223
228 static inline void setDeviceId(cl_device_id id)
229 {
230 af_err err = afcl_set_device_id(id);
231 if (err != AF_SUCCESS) throw af::exception("Failed to set OpenCL device as active device");
232 }
233 #endif
234
235 #if AF_API_VERSION >= 33
236
252 static inline void addDevice(cl_device_id dev, cl_context ctx, cl_command_queue que)
253 {
254 af_err err = afcl_add_device_context(dev, ctx, que);
255 if (err!=AF_SUCCESS) throw af::exception("Failed to push user provided device/context to ArrayFire pool");
256 }
257 #endif
258
259 #if AF_API_VERSION >= 33
260
266 static inline void setDevice(cl_device_id dev, cl_context ctx)
267 {
268 af_err err = afcl_set_device_context(dev, ctx);
269 if (err!=AF_SUCCESS) throw af::exception("Failed to set device based on cl_device_id & cl_context");
270 }
271 #endif
272
273 #if AF_API_VERSION >= 33
274
287 static inline void deleteDevice(cl_device_id dev, cl_context ctx)
288 {
289 af_err err = afcl_delete_device_context(dev, ctx);
290 if (err!=AF_SUCCESS) throw af::exception("Failed to remove the requested device from ArrayFire device pool");
291 }
292 #endif
293
294
295 #if AF_API_VERSION >= 33
296 typedef afcl_device_type deviceType;
297 typedef afcl_platform platform;
298 #endif
299
300 #if AF_API_VERSION >= 33
301
304 static inline deviceType getDeviceType()
305 {
306 afcl_device_type res = AFCL_DEVICE_TYPE_UNKNOWN;
307 af_err err = afcl_get_device_type(&res);
308 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL device type");
309 return res;
310 }
311 #endif
312
313 #if AF_API_VERSION >= 33
314
317 static inline platform getPlatform()
318 {
319 afcl_platform res = AFCL_PLATFORM_UNKNOWN;
320 af_err err = afcl_get_platform(&res);
321 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL platform");
322 return res;
323 }
324 #endif
325
337 static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
338 {
339 const unsigned ndims = (unsigned)idims.ndims();
340 const dim_t *dims = idims.get();
341
342 cl_context context;
343 cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
344 if (clerr != CL_SUCCESS) {
345 throw af::exception("Failed to get context from cl_mem object \"buf\" ");
346 }
347
348 if (context != getContext()) {
349 throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
350 }
351
352
353 if (retain) clerr = clRetainMemObject(buf);
354
355 af_array out;
356 af_err err = af_device_array(&out, buf, ndims, dims, type);
357
358 if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
359 if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
360 throw af::exception("Failed to create device array");
361 }
362
363 return af::array(out);
364 }
365
377 static inline af::array array(dim_t dim0,
378 cl_mem buf, af::dtype type, bool retain=false)
379 {
380 return afcl::array(af::dim4(dim0), buf, type, retain);
381 }
382
395 static inline af::array array(dim_t dim0, dim_t dim1,
396 cl_mem buf, af::dtype type, bool retain=false)
397 {
398 return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
399 }
400
414 static inline af::array array(dim_t dim0, dim_t dim1,
415 dim_t dim2,
416 cl_mem buf, af::dtype type, bool retain=false)
417 {
418 return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
419 }
420
435 static inline af::array array(dim_t dim0, dim_t dim1,
436 dim_t dim2, dim_t dim3,
437 cl_mem buf, af::dtype type, bool retain=false)
438 {
439 return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
440 }
441
445 }
446
447 namespace af
448 {
449
450 #if !defined(AF_OPENCL)
451 template<> AFAPI cl_mem *array::device() const
452 {
453 cl_mem *mem_ptr = new cl_mem;
454 af_err err = af_get_device_ptr((void **)mem_ptr, get());
455 if (err != AF_SUCCESS) throw af::exception("Failed to get cl_mem from array object");
456 return mem_ptr;
457 }
458 #endif
459
460 }
461
462 #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::setDevice
static void setDevice(cl_device_id dev, cl_context ctx)
Set active device using cl_context and cl_device_id.
Definition: opencl.h:266
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:252
af
Definition: algorithm.h:14
afcl::getDeviceId
static cl_device_id getDeviceId()
Get the device ID for ArrayFire&#39;s current active device.
Definition: opencl.h:213
af_get_device_ptr
AFAPI af_err af_get_device_ptr(void **ptr, const af_array arr)
Get the device pointer and lock the buffer in memory manager.
afcl::getPlatform
static platform getPlatform()
Get the type of the current device.
Definition: opencl.h:317
AF_SUCCESS
The function returned successfully.
Definition: defines.h:71
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_PLATFORM_INTEL
Definition: opencl.h:38
AFCL_PLATFORM_BEIGNET
Definition: opencl.h:40
afcl::deviceType
afcl_device_type deviceType
Definition: opencl.h:296
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
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:228
af::array
A multi dimensional data container.
Definition: array.h:27
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:337
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.
af_err
af_err
Definition: defines.h:67
defines.h
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:185
afcl::getDeviceType
static deviceType getDeviceType()
Get the type of the current device.
Definition: opencl.h:304
afcl::platform
afcl_platform platform
Definition: opencl.h:297
AFAPI
#define AFAPI
Definition: defines.h:31
af::dim4::ndims
dim_t ndims()
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:435
afcl_device_type
afcl_device_type
Definition: opencl.h:24
afcl::getQueue
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire&#39;s OpenCL command queue.
Definition: opencl.h:201
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:167
af_array
void * af_array
Definition: defines.h:226
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_DEVICE_TYPE_ACC
Definition: opencl.h:28
af_dtype
af_dtype
Definition: defines.h:199
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:287
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