use arrayfire::{handle_error_general, AfError};
use cl_sys::{
cl_command_queue, cl_context, cl_device_id, CL_DEVICE_TYPE_ACCELERATOR, CL_DEVICE_TYPE_ALL,
CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT, CL_DEVICE_TYPE_GPU,
};
use libc::c_int;
use std::mem;
#[repr(i32)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum VendorPlatform {
AMD = 0,
APPLE = 1,
INTEL = 2,
NVIDIA = 3,
BEIGNET = 4,
POCL = 5,
UNKNOWN = -1,
}
#[repr(u64)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DeviceType {
DEFAULT = CL_DEVICE_TYPE_DEFAULT,
CPU = CL_DEVICE_TYPE_CPU,
GPU = CL_DEVICE_TYPE_GPU,
ACCEL = CL_DEVICE_TYPE_ACCELERATOR,
ALL = CL_DEVICE_TYPE_ALL,
}
extern "C" {
fn afcl_get_context(ctx: *mut cl_context, retain: bool) -> c_int;
fn afcl_get_queue(queue: *mut cl_command_queue, retain: bool) -> c_int;
fn afcl_get_device_id(out: *mut cl_device_id) -> c_int;
fn afcl_set_device_id(id: cl_device_id) -> c_int;
fn afcl_add_device_context(
dev_id: cl_device_id,
ctx: cl_context,
queue: cl_command_queue,
) -> c_int;
fn afcl_set_device_context(dev_id: cl_device_id, ctx: cl_context) -> c_int;
fn afcl_delete_device_context(dev_id: cl_device_id, ctx: cl_context) -> c_int;
fn afcl_get_device_type(dtype: *mut c_int) -> c_int;
fn afcl_get_platform(ptype: *mut c_int) -> c_int;
}
pub fn get_context(retain: bool) -> cl_context {
unsafe {
let mut out: cl_context = std::ptr::null_mut();
let err_val = afcl_get_context(&mut out as *mut cl_context, retain);
handle_error_general(AfError::from(err_val));
out
}
}
pub fn get_queue(retain: bool) -> cl_command_queue {
unsafe {
let mut out: cl_command_queue = std::ptr::null_mut();
let err_val = afcl_get_queue(&mut out as *mut cl_command_queue, retain);
handle_error_general(AfError::from(err_val));
out
}
}
pub fn get_device_id() -> cl_device_id {
unsafe {
let mut out: cl_device_id = std::ptr::null_mut();
let err_val = afcl_get_device_id(&mut out as *mut cl_device_id);
handle_error_general(AfError::from(err_val));
out
}
}
pub fn set_device_id(dev_id: cl_device_id) {
unsafe {
let err_val = afcl_set_device_id(dev_id);
handle_error_general(AfError::from(err_val));
}
}
pub fn add_device_context(dev_id: cl_device_id, ctx: cl_context, queue: cl_command_queue) {
unsafe {
let err_val = afcl_add_device_context(dev_id, ctx, queue);
handle_error_general(AfError::from(err_val));
}
}
pub fn set_device_context(dev_id: cl_device_id, ctx: cl_context) {
unsafe {
let err_val = afcl_set_device_context(dev_id, ctx);
handle_error_general(AfError::from(err_val));
}
}
pub fn delete_device_context(dev_id: cl_device_id, ctx: cl_context) {
unsafe {
let err_val = afcl_delete_device_context(dev_id, ctx);
handle_error_general(AfError::from(err_val));
}
}
pub fn get_device_type() -> DeviceType {
unsafe {
let mut out: i32 = 0;
let err_val = afcl_get_device_type(&mut out as *mut c_int);
handle_error_general(AfError::from(err_val));
match out {
-1 => mem::transmute(out as u64),
_ => DeviceType::ALL,
}
}
}
pub fn get_platform() -> VendorPlatform {
unsafe {
let mut out: i32 = 0;
let err_val = afcl_get_platform(&mut out as *mut c_int);
handle_error_general(AfError::from(err_val));
mem::transmute(out)
}
}