Skip to content

Commit

Permalink
Merge pull request #35 from passware/dynamic-runtime
Browse files Browse the repository at this point in the history
OpenCL dynamic runtime support added
  • Loading branch information
kenba authored Nov 26, 2024
2 parents b3fc355 + 7f113bd commit 444af7a
Show file tree
Hide file tree
Showing 20 changed files with 766 additions and 639 deletions.
176 changes: 83 additions & 93 deletions src/command_queue.rs

Large diffs are not rendered by default.

49 changes: 26 additions & 23 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

//! `OpenCL` Context API.
#![allow(unused_unsafe)]
#![allow(non_camel_case_types)]
#![allow(clippy::not_unsafe_ptr_arg_deref)]

Expand All @@ -24,20 +25,13 @@ pub use opencl_sys::{
CL_SUCCESS,
};

use opencl_sys::{
clCreateContext, clCreateContextFromType, clGetContextInfo, clReleaseContext, clRetainContext,
};

#[cfg(feature = "CL_VERSION_3_0")]
use opencl_sys::clSetContextDestructorCallback;

use super::info_type::InfoType;
use super::{api_info_size, api_info_value, api_info_vector};
use libc::{c_char, c_void, intptr_t, size_t};
use std::mem;
use std::ptr;

/// Create an `OpenCL` context.
/// Create an `OpenCL` context.
/// Calls clCreateContext to create an `OpenCL` context.
///
/// * `devices` - a slice of unique devices for an `OpenCL` platform.
Expand All @@ -48,6 +42,7 @@ use std::ptr;
///
/// returns a Result containing the new `OpenCL` context
/// or the error code from the `OpenCL` C API function.
#[allow(unused_unsafe)]
#[allow(clippy::cast_possible_truncation)]
#[inline]
pub fn create_context(
Expand All @@ -58,14 +53,14 @@ pub fn create_context(
) -> Result<cl_context, cl_int> {
let mut status: cl_int = CL_INVALID_VALUE;
let context = unsafe {
clCreateContext(
cl_call!(clCreateContext(
properties,
devices.len() as cl_uint,
devices.as_ptr(),
pfn_notify,
user_data,
&mut status,
)
))
};
if CL_SUCCESS == status {
Ok(context)
Expand All @@ -74,7 +69,7 @@ pub fn create_context(
}
}

/// Create an `OpenCL` context from a specific device type.
/// Create an `OpenCL` context from a specific device type.
/// Calls `clCreateContextFromType` to create an `OpenCL` context.
///
/// * `device_type` - the type of `OpenCL` device, see:
Expand All @@ -95,7 +90,13 @@ pub fn create_context_from_type(
) -> Result<cl_context, cl_int> {
let mut status: cl_int = CL_INVALID_VALUE;
let context = unsafe {
clCreateContextFromType(properties, device_type, pfn_notify, user_data, &mut status)
cl_call!(clCreateContextFromType(
properties,
device_type,
pfn_notify,
user_data,
&mut status
))
};
if CL_SUCCESS == status {
Ok(context)
Expand All @@ -104,7 +105,7 @@ pub fn create_context_from_type(
}
}

/// Retain an `OpenCL` context.
/// Retain an `OpenCL` context.
/// Calls clRetainContext to increment the context reference count.
///
/// * `context` - the `cl_context` of the `OpenCL` context.
Expand All @@ -116,15 +117,15 @@ pub fn create_context_from_type(
/// This function is unsafe because it changes the `OpenCL` object reference count.
#[inline]
pub unsafe fn retain_context(context: cl_context) -> Result<(), cl_int> {
let status: cl_int = clRetainContext(context);
let status: cl_int = cl_call!(clRetainContext(context));
if CL_SUCCESS == status {
Ok(())
} else {
Err(status)
}
}

/// Release an `OpenCL` context.
/// Release an `OpenCL` context.
/// Calls clReleaseContext to decrement the context reference count.
///
/// * `context` - the `cl_context` of the `OpenCL` context.
Expand All @@ -136,7 +137,7 @@ pub unsafe fn retain_context(context: cl_context) -> Result<(), cl_int> {
/// This function is unsafe because it changes the `OpenCL` object reference count.
#[inline]
pub unsafe fn release_context(context: cl_context) -> Result<(), cl_int> {
let status: cl_int = clReleaseContext(context);
let status: cl_int = cl_call!(clReleaseContext(context));
if CL_SUCCESS == status {
Ok(())
} else {
Expand All @@ -156,7 +157,7 @@ pub fn get_context_data(
get_vector(context, param_name, size)
}

/// Get specific information about an `OpenCL` context.
/// Get specific information about an `OpenCL` context.
/// Calls `clGetContextInfo` to get the desired information about the context.
///
/// * `context` - the `cl_context` of the `OpenCL` context.
Expand Down Expand Up @@ -187,8 +188,8 @@ pub fn get_context_info(
}
}

/// Register a callback function with a context that is called when the `context` is destroyed.
/// Calls `clSetContextDestructorCallback`.
/// Register a callback function with a context that is called when the `context` is destroyed.
/// Calls `clSetContextDestructorCallback`.
/// `CL_VERSION_3_0`
///
/// * `context` - the `cl_context` of the `OpenCL` context.
Expand All @@ -203,7 +204,11 @@ pub fn set_context_destructor_callback(
pfn_notify: Option<unsafe extern "C" fn(cl_context, *mut c_void)>,
user_data: *mut c_void,
) -> Result<(), cl_int> {
let status: cl_int = unsafe { clSetContextDestructorCallback(context, pfn_notify, user_data) };
let status: cl_int = unsafe {
cl_call!(clSetContextDestructorCallback(
context, pfn_notify, user_data
))
};
if CL_SUCCESS == status {
Ok(())
} else {
Expand Down Expand Up @@ -253,8 +258,6 @@ mod tests {
println!("CL_CONTEXT_NUM_DEVICES: {}", value);
assert!(0 < value);

unsafe {
release_context(context).unwrap();
}
unsafe { release_context(context).unwrap() };
}
}
8 changes: 4 additions & 4 deletions src/d3d10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ pub unsafe fn get_supported_d3d10_texture_formats_intel(
image_type: cl_mem_object_type,
) -> Result<Vec<cl_uint>, cl_int> {
let mut count: cl_uint = 0;
let status: cl_int = clGetSupportedD3D10TextureFormatsINTEL(
let status: cl_int = cl_call!(cl_icd::clGetSupportedD3D10TextureFormatsINTEL(
context,
flags,
image_type,
0,
ptr::null_mut(),
&mut count,
);
));
if CL_SUCCESS != status {
Err(status)
} else if 0 < count {
// Get the d3d11_formats.
let len = count as usize;
let mut ids: Vec<cl_uint> = Vec::with_capacity(len);
let status: cl_int = clGetSupportedD3D10TextureFormatsINTEL(
let status: cl_int = cl_call!(cl_d3d10::clGetSupportedD3D10TextureFormatsINTEL(
context,
flags,
image_type,
count,
ids.as_mut_ptr(),
ptr::null_mut(),
);
));
if CL_SUCCESS == status {
Ok(ids)
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/d3d11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//! FFI bindings for `cl_d3d11.h`
//!
//! `cl_d3d11.h` contains `OpenCL` extensions that provide interoperability with `Direct3D` 11.
//! `cl_d3d11.h` contains `OpenCL` extensions that provide interoperability with `Direct3D` 11.
//! `OpenCL` extensions are documented in the [OpenCL-Registry](https://github.com/KhronosGroup/OpenCL-Registry)
#![allow(clippy::missing_safety_doc)]
Expand All @@ -37,30 +37,30 @@ pub unsafe fn get_supported_d3d11_texture_formats_intel(
plane: cl_uint,
) -> Result<Vec<cl_uint>, cl_int> {
let mut count: cl_uint = 0;
let status: cl_int = clGetSupportedD3D11TextureFormatsINTEL(
let status: cl_int = cl_call!(cl_d3d11::clGetSupportedD3D11TextureFormatsINTEL(
context,
flags,
image_type,
plane,
0,
ptr::null_mut(),
&mut count,
);
));
if CL_SUCCESS != status {
Err(status)
} else if 0 < count {
// Get the d3d11_formats.
let len = count as usize;
let mut ids: Vec<cl_uint> = Vec::with_capacity(len);
let status: cl_int = clGetSupportedD3D11TextureFormatsINTEL(
let status: cl_int = cl_call!(cl_d3d11::clGetSupportedD3D11TextureFormatsINTEL(
context,
flags,
image_type,
plane,
count,
ids.as_mut_ptr(),
ptr::null_mut(),
);
));
if CL_SUCCESS == status {
Ok(ids)
} else {
Expand Down
Loading

0 comments on commit 444af7a

Please sign in to comment.