From ecd09eb2cd82b5168dc2fd791d13f8249a878895 Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Thu, 8 Sep 2022 23:48:46 +0900 Subject: [PATCH] Split lax::alloc submodule --- lax/src/alloc.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ lax/src/lib.rs | 60 ++---------------------------------------------- 2 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 lax/src/alloc.rs diff --git a/lax/src/alloc.rs b/lax/src/alloc.rs new file mode 100644 index 00000000..62116f85 --- /dev/null +++ b/lax/src/alloc.rs @@ -0,0 +1,60 @@ +use cauchy::*; +use std::mem::MaybeUninit; + +/// Helper for getting pointer of slice +pub(crate) trait AsPtr: Sized { + type Elem; + fn as_ptr(vec: &[Self]) -> *const Self::Elem; + fn as_mut_ptr(vec: &mut [Self]) -> *mut Self::Elem; +} + +macro_rules! impl_as_ptr { + ($target:ty, $elem:ty) => { + impl AsPtr for $target { + type Elem = $elem; + fn as_ptr(vec: &[Self]) -> *const Self::Elem { + vec.as_ptr() as *const _ + } + fn as_mut_ptr(vec: &mut [Self]) -> *mut Self::Elem { + vec.as_mut_ptr() as *mut _ + } + } + }; +} +impl_as_ptr!(i32, i32); +impl_as_ptr!(f32, f32); +impl_as_ptr!(f64, f64); +impl_as_ptr!(c32, lapack_sys::__BindgenComplex); +impl_as_ptr!(c64, lapack_sys::__BindgenComplex); +impl_as_ptr!(MaybeUninit, i32); +impl_as_ptr!(MaybeUninit, f32); +impl_as_ptr!(MaybeUninit, f64); +impl_as_ptr!(MaybeUninit, lapack_sys::__BindgenComplex); +impl_as_ptr!(MaybeUninit, lapack_sys::__BindgenComplex); + +pub(crate) trait VecAssumeInit { + type Target; + unsafe fn assume_init(self) -> Self::Target; +} + +impl VecAssumeInit for Vec> { + type Target = Vec; + unsafe fn assume_init(self) -> Self::Target { + // FIXME use Vec::into_raw_parts instead after stablized + // https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_raw_parts + let mut me = std::mem::ManuallyDrop::new(self); + Vec::from_raw_parts(me.as_mut_ptr() as *mut T, me.len(), me.capacity()) + } +} + +/// Create a vector without initialization +/// +/// Safety +/// ------ +/// - Memory is not initialized. Do not read the memory before write. +/// +pub(crate) unsafe fn vec_uninit(n: usize) -> Vec> { + let mut v = Vec::with_capacity(n); + v.set_len(n); + v +} diff --git a/lax/src/lib.rs b/lax/src/lib.rs index 47f3eedc..8f697fbc 100644 --- a/lax/src/lib.rs +++ b/lax/src/lib.rs @@ -84,6 +84,7 @@ pub mod error; pub mod flags; pub mod layout; +mod alloc; mod cholesky; mod eig; mod eigh; @@ -113,6 +114,7 @@ pub use self::svddc::*; pub use self::triangular::*; pub use self::tridiagonal::*; +use self::alloc::*; use cauchy::*; use std::mem::MaybeUninit; @@ -140,61 +142,3 @@ impl Lapack for f32 {} impl Lapack for f64 {} impl Lapack for c32 {} impl Lapack for c64 {} - -/// Helper for getting pointer of slice -pub(crate) trait AsPtr: Sized { - type Elem; - fn as_ptr(vec: &[Self]) -> *const Self::Elem; - fn as_mut_ptr(vec: &mut [Self]) -> *mut Self::Elem; -} - -macro_rules! impl_as_ptr { - ($target:ty, $elem:ty) => { - impl AsPtr for $target { - type Elem = $elem; - fn as_ptr(vec: &[Self]) -> *const Self::Elem { - vec.as_ptr() as *const _ - } - fn as_mut_ptr(vec: &mut [Self]) -> *mut Self::Elem { - vec.as_mut_ptr() as *mut _ - } - } - }; -} -impl_as_ptr!(i32, i32); -impl_as_ptr!(f32, f32); -impl_as_ptr!(f64, f64); -impl_as_ptr!(c32, lapack_sys::__BindgenComplex); -impl_as_ptr!(c64, lapack_sys::__BindgenComplex); -impl_as_ptr!(MaybeUninit, i32); -impl_as_ptr!(MaybeUninit, f32); -impl_as_ptr!(MaybeUninit, f64); -impl_as_ptr!(MaybeUninit, lapack_sys::__BindgenComplex); -impl_as_ptr!(MaybeUninit, lapack_sys::__BindgenComplex); - -pub(crate) trait VecAssumeInit { - type Target; - unsafe fn assume_init(self) -> Self::Target; -} - -impl VecAssumeInit for Vec> { - type Target = Vec; - unsafe fn assume_init(self) -> Self::Target { - // FIXME use Vec::into_raw_parts instead after stablized - // https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_raw_parts - let mut me = std::mem::ManuallyDrop::new(self); - Vec::from_raw_parts(me.as_mut_ptr() as *mut T, me.len(), me.capacity()) - } -} - -/// Create a vector without initialization -/// -/// Safety -/// ------ -/// - Memory is not initialized. Do not read the memory before write. -/// -unsafe fn vec_uninit(n: usize) -> Vec> { - let mut v = Vec::with_capacity(n); - v.set_len(n); - v -}