A high-performance general-purpose compute library
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
15extern "C" {
16#endif
17
18#if AF_API_VERSION >= 39
30#endif
31
32#if 0
46AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain);
47
57AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain);
58
65AFAPI af_err afcl_get_device_id(cl_device_id *id);
66
67#if AF_API_VERSION >= 39
74AFAPI af_err afcl_set_device_id(cl_device_id id);
75#endif
76
77#if AF_API_VERSION >= 39
92AFAPI 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
102AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
103#endif
104
105#if AF_API_VERSION >= 39
117AFAPI 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*/
125#endif
126
127#if AF_API_VERSION >= 39
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
151namespace 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
233static 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
247static 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
266static 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
283static inline deviceType getDeviceType()
284{
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
296static inline platform getPlatform()
297{
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
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.
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.
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.
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.
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.
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire's current active device.
af_oneapi_platform
Definition oneapi.h:20
@ AF_ONEAPI_PLATFORM_APPLE
Definition oneapi.h:23
@ AF_ONEAPI_PLATFORM_AMD
Definition oneapi.h:22
@ AF_ONEAPI_PLATFORM_UNKNOWN
Definition oneapi.h:28
@ AF_ONEAPI_PLATFORM_INTEL
Definition oneapi.h:24
@ AF_ONEAPI_PLATFORM_BEIGNET
Definition oneapi.h:26
@ AF_ONEAPI_PLATFORM_POCL
Definition oneapi.h:27
@ AF_ONEAPI_PLATFORM_NVIDIA
Definition oneapi.h:25
afcl_device_type
Definition opencl.h:28
@ AFCL_DEVICE_TYPE_UNKNOWN
Definition opencl.h:32
afcl_platform
Definition opencl.h:38
@ AFCL_PLATFORM_UNKNOWN
Definition opencl.h:45