From 06c7e5e41844056c41395ec89274b33d84f10f83 Mon Sep 17 00:00:00 2001 From: Ken Barker Date: Tue, 3 Dec 2024 06:58:06 +0000 Subject: [PATCH] Enable OpenCL core functions with dynamic feature. --- src/command_queue.rs | 32 ++++++++++++++++---------------- src/context.rs | 6 +++--- src/device.rs | 24 ++++++++++++------------ src/kernel.rs | 24 ++++++++++++------------ src/lib.rs | 2 +- src/memory.rs | 20 ++++++++++---------- src/platform.rs | 2 +- src/program.rs | 12 ++++++------ tests/integration_test.rs | 2 +- tests/opencl2_kernel_test.rs | 3 +-- 10 files changed, 63 insertions(+), 64 deletions(-) diff --git a/src/command_queue.rs b/src/command_queue.rs index 977059d..4a8bb61 100644 --- a/src/command_queue.rs +++ b/src/command_queue.rs @@ -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", @@ -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", @@ -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, @@ -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, @@ -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( &self, @@ -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, @@ -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, @@ -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", @@ -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], @@ -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], @@ -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], @@ -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, @@ -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( &self, svm_ptr: *mut c_void, @@ -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( &self, blocking_map: cl_bool, @@ -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( &self, @@ -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], diff --git a/src/context.rs b/src/context.rs index ed50155..18b1198 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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; @@ -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], @@ -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, diff --git a/src/device.rs b/src/device.rs index 692e31d..c5d99e0 100644 --- a/src/device.rs +++ b/src/device.rs @@ -35,40 +35,40 @@ pub fn get_all_devices(device_type: cl_device_type) -> Result> 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 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 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 } @@ -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], @@ -135,13 +135,13 @@ impl Device { .collect::>()) } - #[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 { Ok(get_host_timer(self.id())?) @@ -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] @@ -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(); diff --git a/src/kernel.rs b/src/kernel.rs index 425f5dd..0b34d61 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -41,7 +41,7 @@ impl From 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) @@ -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, @@ -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( &self, param_name: cl_kernel_exec_info, @@ -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 { 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 { 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 { 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 { 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 { 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, @@ -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(&mut self, arg_ptr: *const T) -> &mut Self { assert!( @@ -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( &mut self, param_name: cl_kernel_exec_info, @@ -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) diff --git a/src/lib.rs b/src/lib.rs index 079fc93..2f5499d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { diff --git a/src/memory.rs b/src/memory.rs index 40f1500..07ffbf6 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -189,7 +189,7 @@ impl Buffer { /// /// returns a Result containing the new OpenCL buffer object /// or the error code from the OpenCL C API function. - #[cfg(feature = "CL_VERSION_3_0")] + #[cfg(any(feature = "CL_VERSION_3_0", feature = "dynamic"))] pub unsafe fn create_with_properties( context: &Context, properties: *const cl_mem_properties, @@ -330,7 +330,7 @@ impl Image { /// /// returns a Result containing the new OpenCL image object /// 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 unsafe fn create( context: &Context, flags: cl_mem_flags, @@ -360,7 +360,7 @@ impl Image { /// /// returns a Result containing the new OpenCL image object /// or the error code from the OpenCL C API function. - #[cfg(feature = "CL_VERSION_3_0")] + #[cfg(any(feature = "CL_VERSION_3_0", feature = "dynamic"))] pub unsafe fn create_with_properties( context: &Context, properties: *const cl_mem_properties, @@ -597,7 +597,7 @@ impl Sampler { Ok(Self::new(sampler)) } - #[cfg(feature = "CL_VERSION_2_0")] + #[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] pub fn create_with_properties( context: &Context, properties: *const cl_sampler_properties, @@ -651,27 +651,27 @@ impl Sampler { /// Has methods to return information from calls to clGetPipeInfo with the /// appropriate parameters. /// Implements the Drop trait to call release_mem_object when the object is dropped. -#[cfg(feature = "CL_VERSION_2_0")] +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] #[derive(Debug)] pub struct Pipe { pipe: cl_mem, } -#[cfg(feature = "CL_VERSION_2_0")] +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] impl From for Pipe { fn from(pipe: cl_mem) -> Self { Self { pipe } } } -#[cfg(feature = "CL_VERSION_2_0")] +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] impl From for cl_mem { fn from(value: Pipe) -> Self { value.pipe as Self } } -#[cfg(feature = "CL_VERSION_2_0")] +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] impl ClMem for Pipe { fn get(&self) -> cl_mem { self.pipe @@ -682,14 +682,14 @@ impl ClMem for Pipe { } } -#[cfg(feature = "CL_VERSION_2_0")] +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] impl Drop for Pipe { fn drop(&mut self) { unsafe { memory::release_mem_object(self.get()).expect("Error: clReleaseMemObject") }; } } -#[cfg(feature = "CL_VERSION_2_0")] +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] impl Pipe { pub const fn new(pipe: cl_mem) -> Self { Self { pipe } diff --git a/src/platform.rs b/src/platform.rs index 3ca9543..6ca593a 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -199,7 +199,7 @@ impl Platform { /// # Safety /// /// Compiling is unsafe after the compiler has been unloaded. - #[cfg(feature = "CL_VERSION_1_2")] + #[cfg(any(feature = "CL_VERSION_1_2", feature = "dynamic"))] pub unsafe fn unload_compiler(&self) -> Result<()> { Ok(program::unload_platform_compiler(self.id())?) } diff --git a/src/program.rs b/src/program.rs index f051bea..33d99b1 100644 --- a/src/program.rs +++ b/src/program.rs @@ -186,7 +186,7 @@ impl Program { /// # 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"))] pub unsafe fn create_from_builtin_kernels( context: &Context, devices: &[cl_device_id], @@ -210,7 +210,7 @@ impl Program { /// /// returns a Result containing the new Program /// or the error code from the OpenCL C API function. - #[cfg(feature = "CL_VERSION_2_1")] + #[cfg(any(feature = "CL_VERSION_2_1", feature = "dynamic"))] pub fn create_from_il(context: &Context, il: &[u8]) -> Result { Ok(Self::new(create_program_with_il(context.get(), il)?, "")) } @@ -316,7 +316,7 @@ impl Program { /// /// returns a Result containing the new `Program` /// or the error code from the OpenCL C API function. - #[cfg(feature = "CL_VERSION_2_1")] + #[cfg(any(feature = "CL_VERSION_2_1", feature = "dynamic"))] pub fn create_and_build_from_il(context: &Context, il: &[u8], options: &str) -> Result { let mut program = Self::create_from_il(context, il)?; program.build(context.devices(), options)?; @@ -334,7 +334,7 @@ impl Program { /// /// returns a null Result /// 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 compile( &mut self, devices: &[cl_device_id], @@ -368,7 +368,7 @@ impl Program { /// # 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"))] pub unsafe fn link( &mut self, devices: &[cl_device_id], @@ -398,7 +398,7 @@ impl Program { /// of the specialization constant. /// /// returns an empty Result or the error code from the OpenCL C API function. - #[cfg(feature = "CL_VERSION_2_2")] + #[cfg(any(feature = "CL_VERSION_2_2", feature = "dynamic"))] pub unsafe fn set_specialization_constant( &self, spec_id: cl_uint, diff --git a/tests/integration_test.rs b/tests/integration_test.rs index de36faf..1559057 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -150,7 +150,7 @@ fn test_opencl_1_2_example() -> Result<()> { Ok(()) } -#[cfg(feature = "CL_VERSION_2_0")] +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] #[test] #[ignore] fn test_opencl_svm_example() -> Result<()> { diff --git a/tests/opencl2_kernel_test.rs b/tests/opencl2_kernel_test.rs index 5251df6..b360e71 100644 --- a/tests/opencl2_kernel_test.rs +++ b/tests/opencl2_kernel_test.rs @@ -12,8 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![cfg(feature = "CL_VERSION_2_0")] - +#[cfg(any(feature = "CL_VERSION_2_0", feature = "dynamic"))] extern crate opencl3; use cl3::device::{