Skip to content

The scope of the unsafe block can be appropriately reduced #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 135 additions & 137 deletions src/algorithm/mod.rs

Large diffs are not rendered by default.

58 changes: 27 additions & 31 deletions src/blas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,20 @@ pub fn gemm<T>(
) where
T: HasAfEnum + FloatingPoint,
{
unsafe {
let mut out = output.get();
let err_val = af_gemm(
let mut out = unsafe { output.get() };
let err_val = unsafe {
af_gemm(
&mut out as *mut af_array,
optlhs as c_uint,
optrhs as c_uint,
alpha.as_ptr() as *const c_void,
lhs.get(),
rhs.get(),
beta.as_ptr() as *const c_void,
);
HANDLE_ERROR(AfError::from(err_val));
output.set(out);
}
)
};
HANDLE_ERROR(AfError::from(err_val));
output.set(out);
}

/// Matrix multiple of two Arrays
Expand All @@ -162,18 +162,18 @@ pub fn matmul<T>(lhs: &Array<T>, rhs: &Array<T>, optlhs: MatProp, optrhs: MatPro
where
T: HasAfEnum + FloatingPoint,
{
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = af_matmul(
let mut temp: af_array = std::ptr::null_mut();
let err_val = unsafe {
af_matmul(
&mut temp as *mut af_array,
lhs.get(),
rhs.get(),
optlhs as c_uint,
optrhs as c_uint,
);
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
)
};
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

/// Calculate the dot product of vectors.
Expand All @@ -194,18 +194,18 @@ pub fn dot<T>(lhs: &Array<T>, rhs: &Array<T>, optlhs: MatProp, optrhs: MatProp)
where
T: HasAfEnum + FloatingPoint,
{
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = af_dot(
let mut temp: af_array = std::ptr::null_mut();
let err_val = unsafe {
af_dot(
&mut temp as *mut af_array,
lhs.get(),
rhs.get(),
optlhs as c_uint,
optrhs as c_uint,
);
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
)
};
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

/// Transpose of a matrix.
Expand All @@ -220,12 +220,10 @@ where
///
/// Transposed Array.
pub fn transpose<T: HasAfEnum>(arr: &Array<T>, conjugate: bool) -> Array<T> {
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = af_transpose(&mut temp as *mut af_array, arr.get(), conjugate);
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
let mut temp: af_array = std::ptr::null_mut();
let err_val = unsafe { af_transpose(&mut temp as *mut af_array, arr.get(), conjugate) };
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

/// Inplace transpose of a matrix.
Expand All @@ -236,10 +234,8 @@ pub fn transpose<T: HasAfEnum>(arr: &Array<T>, conjugate: bool) -> Array<T> {
/// - `conjugate` is a boolean that indicates if the transpose operation needs to be a conjugate
/// transpose
pub fn transpose_inplace<T: HasAfEnum>(arr: &mut Array<T>, conjugate: bool) {
unsafe {
let err_val = af_transpose_inplace(arr.get(), conjugate);
HANDLE_ERROR(AfError::from(err_val));
}
let err_val = unsafe { af_transpose_inplace(arr.get(), conjugate) };
HANDLE_ERROR(AfError::from(err_val));
}

/// Sets the cuBLAS math mode for the internal handle.
Expand Down
79 changes: 36 additions & 43 deletions src/core/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,10 @@ where
type Output = Array<T>;

fn not(self) -> Self::Output {
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = af_not(&mut temp as *mut af_array, self.get());
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
let mut temp: af_array = std::ptr::null_mut();
let err_val = unsafe { af_not(&mut temp as *mut af_array, self.get()) };
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
}

Expand All @@ -124,12 +122,12 @@ macro_rules! unary_func {
/// This is an element wise unary operation.
pub fn $fn_name<T: HasAfEnum>(input: &Array<T>) -> Array< T::$out_type >
where T::$out_type: HasAfEnum {
unsafe {

let mut temp: af_array = std::ptr::null_mut();
let err_val = $ffi_fn(&mut temp as *mut af_array, input.get());
let err_val = unsafe { $ffi_fn(&mut temp as *mut af_array, input.get()) };
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

}
)
}
Expand Down Expand Up @@ -256,12 +254,12 @@ macro_rules! unary_boolean_func {
///
/// This is an element wise unary operation.
pub fn $fn_name<T: HasAfEnum>(input: &Array<T>) -> Array<bool> {
unsafe {

let mut temp: af_array = std::ptr::null_mut();
let err_val = $ffi_fn(&mut temp as *mut af_array, input.get());
let err_val = unsafe { $ffi_fn(&mut temp as *mut af_array, input.get()) };
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

}
)
}
Expand Down Expand Up @@ -291,12 +289,11 @@ macro_rules! binary_func {
A: ImplicitPromote<B>,
B: ImplicitPromote<A>,
{
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = $ffi_fn(&mut temp as *mut af_array, lhs.get(), rhs.get(), batch);
HANDLE_ERROR(AfError::from(err_val));
Into::<Array<A::Output>>::into(temp)
}
let mut temp: af_array = std::ptr::null_mut();
let err_val =
unsafe { $ffi_fn(&mut temp as *mut af_array, lhs.get(), rhs.get(), batch) };
HANDLE_ERROR(AfError::from(err_val));
Into::<Array<A::Output>>::into(temp)
}
};
}
Expand Down Expand Up @@ -389,12 +386,11 @@ macro_rules! overloaded_binary_func {
A: ImplicitPromote<B>,
B: ImplicitPromote<A>,
{
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = $ffi_name(&mut temp as *mut af_array, lhs.get(), rhs.get(), batch);
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
let mut temp: af_array = std::ptr::null_mut();
let err_val =
unsafe { $ffi_name(&mut temp as *mut af_array, lhs.get(), rhs.get(), batch) };
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

#[doc=$doc_str]
Expand Down Expand Up @@ -491,12 +487,11 @@ macro_rules! overloaded_logic_func {
A: ImplicitPromote<B>,
B: ImplicitPromote<A>,
{
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = $ffi_name(&mut temp as *mut af_array, lhs.get(), rhs.get(), batch);
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
let mut temp: af_array = std::ptr::null_mut();
let err_val =
unsafe { $ffi_name(&mut temp as *mut af_array, lhs.get(), rhs.get(), batch) };
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

#[doc=$doc_str]
Expand Down Expand Up @@ -611,18 +606,18 @@ where
X: ImplicitPromote<Y>,
Y: ImplicitPromote<X>,
{
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = af_clamp(
let mut temp: af_array = std::ptr::null_mut();
let err_val = unsafe {
af_clamp(
&mut temp as *mut af_array,
inp.get(),
lo.get(),
hi.get(),
batch,
);
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
)
};
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}

/// Clamp the values of Array
Expand Down Expand Up @@ -979,10 +974,8 @@ pub fn bitnot<T: HasAfEnum>(input: &Array<T>) -> Array<T>
where
T: HasAfEnum + IntegralType,
{
unsafe {
let mut temp: af_array = std::ptr::null_mut();
let err_val = af_bitnot(&mut temp as *mut af_array, input.get());
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
let mut temp: af_array = std::ptr::null_mut();
let err_val = unsafe { af_bitnot(&mut temp as *mut af_array, input.get()) };
HANDLE_ERROR(AfError::from(err_val));
temp.into()
}
Loading