Skip to content

Commit

Permalink
Enable OpenCL core functions with dynamic feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenba committed Dec 3, 2024
1 parent 4a04e4a commit 06c7e5e
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 64 deletions.
32 changes: 16 additions & 16 deletions src/command_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl CommandQueue {
/// # Safety
///
/// This is unsafe when a device is not a member of context.
#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
#[cfg_attr(
any(
feature = "CL_VERSION_2_0",
Expand Down Expand Up @@ -136,7 +136,7 @@ impl CommandQueue {
///
/// returns a Result containing the new CommandQueue
/// or the error code from the OpenCL C API function.
#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
#[cfg_attr(
any(
feature = "CL_VERSION_2_0",
Expand Down Expand Up @@ -171,7 +171,7 @@ impl CommandQueue {
/// # Safety
///
/// This is unsafe when a device is not a member of context.
#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn create_with_properties(
context: &Context,
device_id: cl_device_id,
Expand Down Expand Up @@ -212,7 +212,7 @@ impl CommandQueue {
///
/// returns a Result containing the new CommandQueue
/// or the error code from the OpenCL C API function.
#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub fn create_default_with_properties(
context: &Context,
properties: cl_command_queue_properties,
Expand Down Expand Up @@ -376,7 +376,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
#[allow(clippy::as_ptr_cast_mut)]
pub unsafe fn enqueue_fill_buffer<T>(
&self,
Expand Down Expand Up @@ -523,7 +523,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub unsafe fn enqueue_fill_image(
&self,
image: &mut Image,
Expand Down Expand Up @@ -706,7 +706,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub unsafe fn enqueue_migrate_mem_object(
&self,
num_mem_objects: cl_uint,
Expand Down Expand Up @@ -778,7 +778,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
#[cfg_attr(
any(
feature = "CL_VERSION_2_0",
Expand Down Expand Up @@ -840,7 +840,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub unsafe fn enqueue_marker_with_wait_list(
&self,
event_wait_list: &[cl_event],
Expand All @@ -857,7 +857,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub unsafe fn enqueue_barrier_with_wait_list(
&self,
event_wait_list: &[cl_event],
Expand All @@ -874,7 +874,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn enqueue_svm_free(
&self,
svm_pointers: &[*const c_void],
Expand Down Expand Up @@ -905,7 +905,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn enqueue_svm_mem_cpy(
&self,
blocking_copy: cl_bool,
Expand All @@ -930,7 +930,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn enqueue_svm_mem_fill<T>(
&self,
svm_ptr: *mut c_void,
Expand All @@ -954,7 +954,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn enqueue_svm_map<T>(
&self,
blocking_map: cl_bool,
Expand All @@ -978,7 +978,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
#[allow(clippy::as_ptr_cast_mut)]
pub unsafe fn enqueue_svm_unmap<T>(
&self,
Expand All @@ -998,7 +998,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[cfg(feature = "CL_VERSION_2_1")]
#[cfg(any(feature = "CL_VERSION_2_1", feature = "dynamic"))]
pub unsafe fn enqueue_svm_migrate_mem(
&self,
svm_pointers: &[*const c_void],
Expand Down
6 changes: 3 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
pub use cl3::context;

use super::device::Device;
#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
use super::device::SubDevice;
use super::Result;

Expand Down Expand Up @@ -169,7 +169,7 @@ impl Context {
///
/// returns a Result containing the new OpenCL context
/// or the error code from the OpenCL C API function.
#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn from_sub_devices(
sub_devices: &[SubDevice],
properties: &[cl_context_properties],
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Context {
self.devices.len() as cl_uint
}

#[cfg(feature = "CL_VERSION_3_0")]
#[cfg(any(feature = "CL_VERSION_3_0", feature = "dynamic"))]
#[inline]
pub fn set_destructor_callback(
&self,
Expand Down
24 changes: 12 additions & 12 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,40 @@ pub fn get_all_devices(device_type: cl_device_type) -> Result<Vec<cl_device_id>>
Ok(device_ids)
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
#[derive(Debug)]
pub struct SubDevice {
id: cl_device_id,
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
impl From<cl_device_id> for SubDevice {
fn from(id: cl_device_id) -> Self {
Self { id }
}
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
impl From<SubDevice> for cl_device_id {
fn from(value: SubDevice) -> Self {
value.id
}
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
impl Drop for SubDevice {
fn drop(&mut self) {
unsafe { release_device(self.id()).expect("Error: clReleaseDevice") };
}
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
unsafe impl Send for SubDevice {}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
unsafe impl Sync for SubDevice {}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
impl SubDevice {
pub const fn new(id: cl_device_id) -> Self {
Self { id }
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Device {
///
/// returns a Result containing a vector of available SubDevices
/// or the error code from the OpenCL C API function.
#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn create_sub_devices(
&self,
properties: &[cl_device_partition_property],
Expand All @@ -135,13 +135,13 @@ impl Device {
.collect::<Vec<SubDevice>>())
}

#[cfg(feature = "CL_VERSION_2_1")]
#[cfg(any(feature = "CL_VERSION_2_1", feature = "dynamic"))]
#[inline]
pub fn get_device_and_host_timer(&self) -> Result<[cl_ulong; 2]> {
Ok(get_device_and_host_timer(self.id())?)
}

#[cfg(feature = "CL_VERSION_2_1")]
#[cfg(any(feature = "CL_VERSION_2_1", feature = "dynamic"))]
#[inline]
pub fn get_host_timer(&self) -> Result<cl_ulong> {
Ok(get_host_timer(self.id())?)
Expand Down Expand Up @@ -956,7 +956,7 @@ mod tests {
use super::*;
use crate::platform::get_platforms;
use cl3::info_type::InfoType;
#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
use std::ptr;

#[test]
Expand Down Expand Up @@ -986,7 +986,7 @@ mod tests {
}
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
#[test]
fn test_get_sub_devices() {
let platforms = get_platforms().unwrap();
Expand Down
24 changes: 12 additions & 12 deletions src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl From<Kernel> for cl_kernel {
}
}

#[cfg(feature = "CL_VERSION_2_1")]
#[cfg(any(feature = "CL_VERSION_2_1", feature = "dynamic"))]
impl Clone for Kernel {
/// Clone an OpenCL kernel object.
/// CL_VERSION_2_1 see: [Copying Kernel Objects](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#_copying_kernel_objects)
Expand Down Expand Up @@ -136,7 +136,7 @@ impl Kernel {
/// # Safety
///
/// This function is unsafe because the index and ptr must be valid.
#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn set_arg_svm_pointer(
&self,
arg_index: cl_uint,
Expand All @@ -156,7 +156,7 @@ impl Kernel {
/// # Safety
///
/// This function is unsafe because the name and ptr must be valid.
#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn set_exec_info<T>(
&self,
param_name: cl_kernel_exec_info,
Expand Down Expand Up @@ -200,34 +200,34 @@ impl Kernel {
Ok(get_kernel_data(self.kernel, param_name)?)
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn get_arg_address_qualifier(&self, arg_indx: cl_uint) -> Result<cl_uint> {
Ok(get_kernel_arg_info(self.kernel, arg_indx, CL_KERNEL_ARG_ADDRESS_QUALIFIER)?.into())
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn get_arg_access_qualifier(&self, arg_indx: cl_uint) -> Result<cl_uint> {
Ok(get_kernel_arg_info(self.kernel, arg_indx, CL_KERNEL_ARG_ACCESS_QUALIFIER)?.into())
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn get_arg_type_qualifier(&self, arg_indx: cl_uint) -> Result<cl_ulong> {
Ok(get_kernel_arg_info(self.kernel, arg_indx, CL_KERNEL_ARG_TYPE_QUALIFIER)?.into())
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn get_arg_type_name(&self, arg_indx: cl_uint) -> Result<String> {
Ok(get_kernel_arg_info(self.kernel, arg_indx, CL_KERNEL_ARG_TYPE_NAME)?.into())
}

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn get_arg_name(&self, arg_indx: cl_uint) -> Result<String> {
Ok(get_kernel_arg_info(self.kernel, arg_indx, CL_KERNEL_ARG_NAME)?.into())
}

/// Get data about arguments of an OpenCL kernel.
/// Calls clGetKernelArgInfo to get the desired data about arguments of the kernel.
#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
pub fn get_arg_data(
&self,
arg_indx: cl_uint,
Expand Down Expand Up @@ -431,7 +431,7 @@ impl<'a> ExecuteKernel<'a> {
/// # Safety
///
/// This function is unsafe because ptr must be valid.
#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
#[track_caller]
pub unsafe fn set_arg_svm<T>(&mut self, arg_ptr: *const T) -> &mut Self {
assert!(
Expand Down Expand Up @@ -463,7 +463,7 @@ impl<'a> ExecuteKernel<'a> {
/// # Safety
///
/// This function is unsafe because name and ptr must be valid.
#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub unsafe fn set_exec_info<T>(
&mut self,
param_name: cl_kernel_exec_info,
Expand Down Expand Up @@ -752,7 +752,7 @@ mod tests {
println!("kernel.attributes(): {}", value);
// assert!(value.is_empty());

#[cfg(feature = "CL_VERSION_1_2")]
#[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))]
{
let arg0_address = kernels[0]
.get_arg_address_qualifier(0)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub mod kernel;
pub mod memory;
pub mod platform;
pub mod program;
#[cfg(feature = "CL_VERSION_2_0")]
#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))]
pub mod svm;

pub mod error_codes {
Expand Down
Loading

0 comments on commit 06c7e5e

Please sign in to comment.