$darkmode
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
15 extern "C" {
16 #endif
17 
18 #if AF_API_VERSION >= 39
19 typedef enum
20 {
21  //TODO: update? are these relevant in sycl
30 #endif
31 
32 #if 0
33 
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
68 
74 AFAPI af_err afcl_set_device_id(cl_device_id id);
75 #endif
76 
77 #if AF_API_VERSION >= 39
78 
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
96 
102 AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
103 #endif
104 
105 #if AF_API_VERSION >= 39
106 
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 */
125 #endif
126 
127 #if AF_API_VERSION >= 39
128 
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
155 
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
206 
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
219 
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
241 
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
255 
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
276  typedef afcl_platform platform;
277 #endif
278 
279 #if AF_API_VERSION >= 39
280 
283 static 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
293 
296 static 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
An ArrayFire exception class.
Definition: exception.h:21
AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx)
Set active device using cl_context and cl_device_id.
The function returned successfully.
Definition: defines.h:75
AFAPI af_err afcl_get_queue(cl_command_queue *queue, const bool retain)
Get a handle to ArrayFire&#39;s OpenCL command queue.
AFAPI af_err afcl_get_device_id(cl_device_id *id)
Get the device ID for ArrayFire&#39;s current active device.
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.
static platform getPlatform()
Get a vendor enumeration for the current platform.
Definition: opencl.h:310
afcl_device_type deviceType
Definition: opencl.h:289
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
A multi dimensional data container.
Definition: array.h:37
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
Definition: opencl.h:37
AFAPI af_err afcl_get_platform(afcl_platform *res)
Get the platform of the current device.
AFAPI af_err afcl_get_context(cl_context *ctx, const bool retain)
Get a handle to ArrayFire&#39;s OpenCL context
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
af_err
Definition: defines.h:71
static deviceType getDeviceType()
Get the type of the current device.
Definition: opencl.h:297
AFAPI int getDeviceId(const array &in)
af_oneapi_platform
Definition: oneapi.h:19
afcl_platform platform
Definition: opencl.h:290
dim_t * get()
Returns the underlying pointer to the dim4 object.
Definition: dim4.hpp:103
long long dim_t
Definition: defines.h:56
static cl_context getContext(bool retain=false)
Get a handle to ArrayFire&#39;s OpenCL context
Definition: opencl.h:182
#define AFAPI
Definition: defines.h:38
dim_t ndims()
Returns the number of axis whose values are greater than one.
afcl_device_type
Definition: opencl.h:27
static void setDeviceId(cl_device_id id)
Set ArrayFire&#39;s active device based on id of type cl_device_id.
Definition: opencl.h:225
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_device_type(afcl_device_type *res)
Get the type of the current device.
void * af_array
Definition: defines.h:240
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.
Generic object that represents size and shape.
Definition: dim4.hpp:25
static cl_command_queue getQueue(bool retain=false)
Get a handle to ArrayFire&#39;s OpenCL command queue
Definition: opencl.h:198
af_dtype
Definition: defines.h:210
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 void setDevice(const int device)
Sets the current device.