A high-performance general-purpose compute library

- Main Page
- Tutorials
- Release Notes
- Examples

oneapi.h
Go to the documentation of this file.
1/*******************************************************
2 * Copyright (c) 2022, 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
12#include <af/defines.h>
13
14#ifdef __cplusplus
15 extern "C" {
16#endif
17
18#if AF_API_VERSION >= 39
19 typedef enum
20{
21 //TODO: update? are these relevant in sycl
22 AF_ONEAPI_PLATFORM_AMD = 0,
23 AF_ONEAPI_PLATFORM_APPLE = 1,
24 AF_ONEAPI_PLATFORM_INTEL = 2,
25 AF_ONEAPI_PLATFORM_NVIDIA = 3,
26 AF_ONEAPI_PLATFORM_BEIGNET = 4,
27 AF_ONEAPI_PLATFORM_POCL = 5,
28 AF_ONEAPI_PLATFORM_UNKNOWN = -1
29} af_oneapi_platform;
30#endif
31
32#if 0
46 AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
47
57 AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
58
65 AFAPI af_err afcl_get_device_id(cl_device_id *id);
66
67#if AF_API_VERSION >= 39
74 AFAPI af_err afcl_set_device_id(cl_device_id id);
75#endif
76
77#if AF_API_VERSION >= 39
92 AFAPI af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que);
93#endif
94
95#if AF_API_VERSION >= 39
102 AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
103#endif
104
105#if AF_API_VERSION >= 39
117 AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
118#endif
119
120#if AF_API_VERSION >= 39
121 Ge
122 t the type of the current device
123*/
124 AFAPI af_err afcl_get_device_type(afcl_device_type *res);
125#endif
126
127#if AF_API_VERSION >= 39
131 AFAPI af_err afcl_get_platform(afcl_platform *res);
132#endif
133
137#endif //if 0 comment
138
139#ifdef __cplusplus
140}
141#endif
142
143#ifdef __cplusplus
144
145#include <af/array.h>
146#include <af/dim4.hpp>
147#include <af/exception.h>
148#include <af/device.h>
149#include <stdio.h>
150
151 namespace afoneapi
152{
153
154#if 0
168 static inline cl_context getContext(bool retain = false)
169 {
170 cl_context ctx;
171 af_err err = afcl_get_context(&ctx, retain);
172 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL context from arrayfire");
173 return ctx;
174 }
175
184 static inline cl_command_queue getQueue(bool retain = false)
185 {
186 cl_command_queue queue;
187 af_err err = afcl_get_queue(&queue, retain);
188 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL command queue from arrayfire");
189 return queue;
190 }
191
196 static inline cl_device_id getDeviceId()
197 {
198 cl_device_id id;
199 af_err err = afcl_get_device_id(&id);
200 if (err != AF_SUCCESS) throw af::exception("Failed to get OpenCL device ID");
201
202 return id;
203 }
204
205#if AF_API_VERSION >= 39
211 static inline void setDeviceId(cl_device_id id)
212 {
213 af_err err = afcl_set_device_id(id);
214 if (err != AF_SUCCESS) throw af::exception("Failed to set OpenCL device as active device");
215 }
216#endif
217
218#if AF_API_VERSION >= 39
233 static inline void addDevice(cl_device_id dev, cl_context ctx, cl_command_queue que)
234{
235 af_err err = afcl_add_device_context(dev, ctx, que);
236 if (err!=AF_SUCCESS) throw af::exception("Failed to push user provided device/context to ArrayFire pool");
237}
238#endif
239
240#if AF_API_VERSION >= 39
247 static inline void setDevice(cl_device_id dev, cl_context ctx)
248{
249 af_err err = afcl_set_device_context(dev, ctx);
250 if (err!=AF_SUCCESS) throw af::exception("Failed to set device based on cl_device_id & cl_context");
251}
252#endif
253
254#if AF_API_VERSION >= 39
266 static inline void deleteDevice(cl_device_id dev, cl_context ctx)
267{
268 af_err err = afcl_delete_device_context(dev, ctx);
269 if (err!=AF_SUCCESS) throw af::exception("Failed to remove the requested device from ArrayFire device pool");
270}
271#endif
272
273
274#if AF_API_VERSION >= 39
275 typedef afcl_device_type deviceType;
276 typedef afcl_platform platform;
277#endif
278
279#if AF_API_VERSION >= 39
283 static inline deviceType getDeviceType()
284{
285 afcl_device_type res = AFCL_DEVICE_TYPE_UNKNOWN;
286 af_err err = afcl_get_device_type(&res);
287 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL device type");
288 return res;
289}
290#endif
291
292#if AF_API_VERSION >= 39
296 static inline platform getPlatform()
297{
298 afcl_platform res = AFCL_PLATFORM_UNKNOWN;
299 af_err err = afcl_get_platform(&res);
300 if (err!=AF_SUCCESS) throw af::exception("Failed to get OpenCL platform");
301 return res;
302}
303#endif
304
316 static inline af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
317 {
318 const unsigned ndims = (unsigned)idims.ndims();
319 const dim_t *dims = idims.get();
320
321 cl_context context;
322 cl_int clerr = clGetMemObjectInfo(buf, CL_MEM_CONTEXT, sizeof(cl_context), &context, NULL);
323 if (clerr != CL_SUCCESS) {
324 throw af::exception("Failed to get context from cl_mem object \"buf\" ");
325 }
326
327 if (context != getContext()) {
328 throw(af::exception("Context mismatch between input \"buf\" and arrayfire"));
329 }
330
331
332 if (retain) clerr = clRetainMemObject(buf);
333
334 af_array out;
335 af_err err = af_device_array(&out, buf, ndims, dims, type);
336
337 if (err != AF_SUCCESS || clerr != CL_SUCCESS) {
338 if (retain && clerr == CL_SUCCESS) clReleaseMemObject(buf);
339 throw af::exception("Failed to create device array");
340 }
341
342 return af::array(out);
343 }
344
356 static inline af::array array(dim_t dim0,
357 cl_mem buf, af::dtype type, bool retain=false)
358 {
359 return afcl::array(af::dim4(dim0), buf, type, retain);
360 }
361
374 static inline af::array array(dim_t dim0, dim_t dim1,
375 cl_mem buf, af::dtype type, bool retain=false)
376 {
377 return afcl::array(af::dim4(dim0, dim1), buf, type, retain);
378 }
379
393 static inline af::array array(dim_t dim0, dim_t dim1,
394 dim_t dim2,
395 cl_mem buf, af::dtype type, bool retain=false)
396 {
397 return afcl::array(af::dim4(dim0, dim1, dim2), buf, type, retain);
398 }
399
414 static inline af::array array(dim_t dim0, dim_t dim1,
415 dim_t dim2, dim_t dim3,
416 cl_mem buf, af::dtype type, bool retain=false)
417 {
418 return afcl::array(af::dim4(dim0, dim1, dim2, dim3), buf, type, retain);
419 }
420
424#endif //#IF 0 tmp comment
425
426}
427
428
429#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_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_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_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_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_get_device_id
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire's current active device.
afoneapi
Definition oneapi.h:152
af_oneapi_platform
af_oneapi_platform
Definition oneapi.h:20
AF_ONEAPI_PLATFORM_APPLE
@ AF_ONEAPI_PLATFORM_APPLE
Definition oneapi.h:23
AF_ONEAPI_PLATFORM_AMD
@ AF_ONEAPI_PLATFORM_AMD
Definition oneapi.h:22
AF_ONEAPI_PLATFORM_UNKNOWN
@ AF_ONEAPI_PLATFORM_UNKNOWN
Definition oneapi.h:28
AF_ONEAPI_PLATFORM_INTEL
@ AF_ONEAPI_PLATFORM_INTEL
Definition oneapi.h:24
AF_ONEAPI_PLATFORM_BEIGNET
@ AF_ONEAPI_PLATFORM_BEIGNET
Definition oneapi.h:26
AF_ONEAPI_PLATFORM_POCL
@ AF_ONEAPI_PLATFORM_POCL
Definition oneapi.h:27
AF_ONEAPI_PLATFORM_NVIDIA
@ AF_ONEAPI_PLATFORM_NVIDIA
Definition oneapi.h:25
afcl_device_type
afcl_device_type
Definition opencl.h:28
AFCL_DEVICE_TYPE_UNKNOWN
@ AFCL_DEVICE_TYPE_UNKNOWN
Definition opencl.h:32
afcl_platform
afcl_platform
Definition opencl.h:38
AFCL_PLATFORM_UNKNOWN
@ AFCL_PLATFORM_UNKNOWN
Definition opencl.h:45