Skip to content

Commit

Permalink
Fix some unused warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
elftausend committed Sep 8, 2024
1 parent bec7d3d commit d97589e
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 74 deletions.
16 changes: 7 additions & 9 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::{
ffi::c_void,
mem::ManuallyDrop,
ops::{Deref, DerefMut},
};
Expand All @@ -11,9 +10,9 @@ use crate::cpu::{CPUPtr, CPU};
use crate::CPU;

use crate::{
flag::AllocFlag, shape::Shape, Alloc, Base, ClearBuf, CloneBuf, Device,
DevicelessAble, HasId, IsShapeIndep, OnDropBuffer, OnNewBuffer, PtrType, Read, ReplaceBuf,
ShallowCopy, Unit, WrappedData, WriteBuf, ZeroGrad,
flag::AllocFlag, shape::Shape, Alloc, Base, ClearBuf, CloneBuf, Device, DevicelessAble, HasId,
IsShapeIndep, OnDropBuffer, OnNewBuffer, PtrType, Read, ReplaceBuf, ShallowCopy, Unit,
WrappedData, WriteBuf, ZeroGrad,
};

pub use self::num::Num;
Expand Down Expand Up @@ -583,7 +582,7 @@ impl<'a, Mods: OnDropBuffer, T: Unit, S: Shape> Buffer<'a, T, CPU<Mods>, S> {
impl<'a, Mods: OnDropBuffer, T: Unit, S: Shape> Buffer<'a, T, crate::OpenCL<Mods>, S> {
/// Returns the OpenCL pointer of the `Buffer`.
#[inline]
pub fn cl_ptr(&self) -> *mut c_void {
pub fn cl_ptr(&self) -> *mut core::ffi::c_void {
assert!(
!self.base().ptr.is_null(),
"called cl_ptr() on an invalid OpenCL buffer"
Expand Down Expand Up @@ -687,15 +686,14 @@ where
T: Unit + Debug + Default + Clone + 'a,
D: Read<T, S> + Device + 'a,
for<'b> <D as Read<T, S>>::Read<'b>: Debug,
D::Data<T, S>: Debug,
D::Data<T, S>: Debug,
S: Shape,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Buffer")
.field("ptr", self.data());
f.debug_struct("Buffer").field("ptr", self.data());
writeln!(f, ",")?;

let data = self.read();
let data = self.read();
writeln!(f, "data: {data:?}")?;
writeln!(f, "len: {:?}", self.len())?;
write!(
Expand Down
6 changes: 2 additions & 4 deletions src/buffer/num.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use core::{
convert::Infallible,
ffi::c_void,
ops::{Deref, DerefMut},
ptr::null_mut,
};

use crate::{
flag::AllocFlag, Alloc, Buffer, CloneBuf, Device, HasId, OnDropBuffer, PtrType,
ShallowCopy, Unit, WrappedData,
flag::AllocFlag, Alloc, Buffer, CloneBuf, Device, HasId, OnDropBuffer, PtrType, ShallowCopy,
Unit, WrappedData,
};

#[derive(Debug, Default)]
Expand Down
24 changes: 11 additions & 13 deletions src/devices/cpu/cpu_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ impl<T> CPUPtr<T> {
/// ```
#[inline]
pub unsafe fn from_ptr(ptr: *mut T, len: usize, flag: AllocFlag) -> CPUPtr<T> {
CPUPtr {
ptr,
len,
flag,
}
CPUPtr { ptr, len, flag }
}
pub fn from_vec(mut vec: Vec<T>) -> CPUPtr<T> {
// CPUPtr only knows about the length, not the capacity -> deallocation happens with length, which may be less than the capacity
Expand Down Expand Up @@ -246,7 +242,7 @@ impl<T> ShallowCopy for CPUPtr<T> {

pub struct DeallocWithLayout {
ptr: core::mem::ManuallyDrop<CPUPtr<u8>>,
layout: Layout,
layout: Layout,
}

impl DeallocWithLayout {
Expand All @@ -255,14 +251,18 @@ impl DeallocWithLayout {
let (_, layout) = ptr.current_memory()?;
let ptr = core::mem::ManuallyDrop::new(ptr);
Some(Self {
ptr: core::mem::ManuallyDrop::new(CPUPtr { ptr: ptr.ptr as *mut u8, len: ptr.len, flag: ptr.flag }),
layout
ptr: core::mem::ManuallyDrop::new(CPUPtr {
ptr: ptr.ptr as *mut u8,
len: ptr.len,
flag: ptr.flag,
}),
layout,
})
}

#[inline]
#[inline]
pub fn layout(&self) -> &Layout {
&self.layout
&self.layout
}
}

Expand Down Expand Up @@ -452,9 +452,7 @@ mod tests {
#[test]
fn test_dealloc_with_layout() {
let data = CPUPtr::<f32>::new_initialized(10, crate::flag::AllocFlag::None);
let dealloc = unsafe {
DeallocWithLayout::new(data).unwrap()
};
let dealloc = unsafe { DeallocWithLayout::new(data).unwrap() };
assert_eq!(dealloc.layout().size(), 40)
}
}
6 changes: 2 additions & 4 deletions src/devices/cuda/cuda_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use core::{marker::PhantomData, ptr::null_mut};

use crate::{flag::AllocFlag, HasId, Id, PtrType, ShallowCopy};

use super::api::{cu_read, cufree, cumalloc, CudaResult};
use crate::{flag::AllocFlag, HasId, Id, PtrType, ShallowCopy};
use core::marker::PhantomData;

/// The pointer used for `CUDA` [`Buffer`](crate::Buffer)s
#[derive(Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion src/devices/cuda/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<Mods> crate::LazySetup for CUDA<Mods> {
#[cfg(test)]
mod tests {
use crate::{
AddOperation, ApplyFunction, AsNoId, Base, Buffer, Combiner, Device, HasId, Lazy, Retrieve,
AddOperation, ApplyFunction, Base, Buffer, Combiner, Device, HasId, Lazy, Retrieve,
Retriever, Run, CUDA,
};

Expand Down
6 changes: 3 additions & 3 deletions src/devices/cuda/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
bounds_to_range,
cuda::api::{cu_read_async, CUstreamCaptureStatus},
op_hint::unary,
pass_down_add_operation, pass_down_exec_now, AddOperation, ApplyFunction,
Buffer, CDatatype, ClearBuf, CopySlice, OnDropBuffer, Read, Resolve, Retrieve, Retriever,
SetOpHint, Shape, ToCLSource, ToMarker, UnaryGrad, Unit, WriteBuf, ZeroGrad, CUDA,
pass_down_add_operation, pass_down_exec_now, AddOperation, ApplyFunction, Buffer, CDatatype,
ClearBuf, CopySlice, OnDropBuffer, Read, Resolve, Retrieve, Retriever, SetOpHint, Shape,
ToCLSource, ToMarker, UnaryGrad, Unit, WriteBuf, ZeroGrad, CUDA,
};

use super::{
Expand Down
11 changes: 4 additions & 7 deletions src/devices/nnapi/nnapi_device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
cpu::{CPUPtr, DeallocWithLayout}, Alloc, AsOperandCode, Base, Buffer, Device, HasId, IsShapeIndep, Lazy, LazyRun,
LazySetup, Module, OnDropBuffer, PtrType, Retrieve, Retriever, Setup, Shape, Unit, WrappedData,
cpu::{CPUPtr, DeallocWithLayout},
Alloc, AsOperandCode, Base, Buffer, Device, HasId, IsShapeIndep, Lazy, LazyRun, LazySetup,
Module, OnDropBuffer, PtrType, Retrieve, Retriever, Setup, Shape, Unit, WrappedData,
};

use super::NnapiPtr;
Expand Down Expand Up @@ -215,11 +216,7 @@ impl<T, Mods: OnDropBuffer> NnapiDevice<T, Mods> {
fn set_input_ptrs(&self, run: &mut Execution) -> crate::Result<()> {
for (idx, (_id, input_ptr)) in self.input_ptrs.borrow().iter().enumerate() {
unsafe {
run.set_input_raw(
idx as i32,
input_ptr.ptr.cast(),
input_ptr.layout().size()
)
run.set_input_raw(idx as i32, input_ptr.ptr.cast(), input_ptr.layout().size())
}?
}
Ok(())
Expand Down
5 changes: 1 addition & 4 deletions src/devices/stack_array.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use core::{
ops::{Deref, DerefMut},
ptr::null_mut,
};
use core::ops::{Deref, DerefMut};

use crate::{shape::Shape, HasId, HostPtr, PtrType, ShallowCopy};

Expand Down
18 changes: 5 additions & 13 deletions src/devices/untyped/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<'a, T: Unit, S: crate::Shape> Buffer<'a, T, Untyped, S> {
// Safety: An Untyped device buffer is shape and data type independent!
// type Base<T, S: crate::Shape> = UntypedData; <- missing <T, S>
// type Data<T, S: crate::Shape> = UntypedData; <|
// storage type is also matched
// storage type is also matched
Some(unsafe { std::mem::transmute(self) })
}

Expand All @@ -47,9 +47,7 @@ impl<'a, T: Unit, S: crate::Shape> Buffer<'a, T, Untyped, S> {
{
self.data.matches_storage_type::<NT>().ok()?;

Some(unsafe {
&*(self as *const Self as *const Buffer<NT, Untyped, NS>)
})
Some(unsafe { &*(self as *const Self as *const Buffer<NT, Untyped, NS>) })
}

#[inline]
Expand All @@ -59,29 +57,23 @@ impl<'a, T: Unit, S: crate::Shape> Buffer<'a, T, Untyped, S> {
NS: crate::Shape,
{
self.data.matches_storage_type::<NT>().ok()?;
Some(unsafe {
&mut *(self as *mut Self as *mut Buffer<NT, Untyped, NS>)
})
Some(unsafe { &mut *(self as *mut Self as *mut Buffer<NT, Untyped, NS>) })
}

#[inline]
pub fn as_untyped(&self) -> &Buffer<'a, (), Untyped, ()> {
// Safety: An Untyped device buffer is shape and data type independent!
// type Base<T, S: crate::Shape> = UntypedData; <- missing <T, S>
// type Data<T, S: crate::Shape> = UntypedData; <|
unsafe {
&*(self as *const Self as *const Buffer<(), Untyped, ()>)
}
unsafe { &*(self as *const Self as *const Buffer<(), Untyped, ()>) }
}

#[inline]
pub fn as_untyped_mut(&mut self) -> &mut Buffer<'a, (), Untyped, ()> {
// Safety: An Untyped device buffer is shape and data type independent!
// type Base<T, S: crate::Shape> = UntypedData; <- missing <T, S>
// type Data<T, S: crate::Shape> = UntypedData; <|
unsafe {
&mut *(self as *mut Self as *mut Buffer<(), Untyped, ()>)
}
unsafe { &mut *(self as *mut Self as *mut Buffer<(), Untyped, ()>) }
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/devices/vulkan/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::fmt::Debug;

use crate::{
cpu_stack_ops::clear_slice, pass_down_add_operation, pass_down_exec_now, prelude::Number,
AddOperation, ApplyFunction, AsNoId, BufAsNoId, Buffer, CDatatype, ClearBuf, OnDropBuffer,
AddOperation, ApplyFunction, Buffer, CDatatype, ClearBuf, OnDropBuffer,
Read, Resolve, Retrieve, Retriever, Shape, ToCLSource, ToMarker, ToWgslSource, UnaryGrad, Unit,
UseGpuOrCpu, Vulkan, WriteBuf, ZeroGrad,
};
Expand Down
2 changes: 1 addition & 1 deletion src/devices/wgsl/ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
op_hint::unary, AddOperation, Alloc, ApplyFunction, AsNoId, OnDropBuffer, Read, Retrieve,
op_hint::unary, AddOperation, Alloc, ApplyFunction, OnDropBuffer, Read, Retrieve,
Retriever, SetOpHint, Shape, ToMarker, Unit,
};

Expand Down
1 change: 1 addition & 0 deletions src/exec_on_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ mod tests {
}

pub trait AddEw<T, D: crate::Device = Self>: crate::Device {
#[allow(dead_code)]
fn add(&self, lhs: &crate::Buffer<T, D>, rhs: &crate::Buffer<T, D>) -> crate::Buffer<T, D>;
}

Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
//! A lot more usage examples can be found in the [tests] and [examples] folder.
//!
//! [tests]: https://github.com/elftausend/custos/tree/main/tests
use core::ffi::c_void;

//pub use libs::*;
pub use buffer::*;
pub use devices::*;

Expand Down
8 changes: 4 additions & 4 deletions src/modules/autograd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ mod tests {
Ok(())
});

out.backward();
out.backward().unwrap();

assert_eq!(&***buf.grad(), [5.; 10]);
}
Expand Down Expand Up @@ -443,7 +443,7 @@ mod tests {
Ok(())
});

out.backward();
out.backward().unwrap();

assert_eq!(lhs.try_grad().unwrap().as_slice(), [4, 5, 6, 7]);
}
Expand All @@ -463,7 +463,7 @@ mod tests {
panic!("should not be called");
});

out.backward();
out.backward().unwrap();

assert!(lhs.try_grad().is_none());

Expand All @@ -475,7 +475,7 @@ mod tests {
Ok(())
});

out.backward();
out.backward().unwrap();

assert_eq!(lhs.try_grad().unwrap().as_slice(), [4, 5, 6, 7]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/autograd/tape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'t> Tape<'t> {
buf: &Buffer<'a, T, D, S>,
seed: &[T],
buffers: Option<&mut Buffers<Box<dyn BoxedShallowCopy>>>,
) -> crate::Result<()>
) -> crate::Result<()>
where
T: Unit + 'static,
D: Alloc<T> + ZeroGrad<T> + WriteBuf<T, S, D> + GradActions + AddOperation + 'static,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/autograd/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::marker::PhantomData;

use crate::{flag::AllocFlag, Autograd, HasId, PtrType, ShallowCopy, Shape, WrappedData};
use crate::{flag::AllocFlag, Autograd, HasId, PtrType, ShallowCopy, WrappedData};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ReqGradWrapper<Data, T> {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/lazy/lazy_graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
bounds_to_range, modules::lazy::exec_iter::ExecIter, op_hint::OpHint, AnyOp, BoxedShallowCopy, Buffers, Device, Downcast, Id, OperationFn, Parents
bounds_to_range, modules::lazy::exec_iter::ExecIter, op_hint::OpHint, AnyOp, BoxedShallowCopy,
Buffers, Device, Downcast, Id, OperationFn, Parents,
};
use core::ops::RangeBounds;
use std::collections::HashSet;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/lazy/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{
ops::{Deref, DerefMut},
};

use crate::{flag::AllocFlag, HasId, HostPtr, Id, Lazy, PtrType, ShallowCopy, Shape, WrappedData};
use crate::{flag::AllocFlag, HasId, HostPtr, Id, Lazy, PtrType, ShallowCopy, WrappedData};

#[derive(Debug, Default)]
pub struct LazyWrapper<Data, T> {
Expand Down
1 change: 0 additions & 1 deletion tests/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[cfg(feature = "wgpu")]
use custos::prelude::*;
#[cfg(feature = "wgpu")]
Expand Down
2 changes: 0 additions & 2 deletions tests/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use custos::prelude::*;


#[cfg(feature = "std")]
pub fn read<T: Unit, D: Alloc<T>>(device: &D, buf: &Buffer<T, D>) -> Vec<T>
where
Expand Down Expand Up @@ -194,7 +193,6 @@ fn test_deviceless_buf() {
Buffer::<u8, CPU>::deviceless(&device, 5)
};


for (idx, element) in buf.iter_mut().enumerate() {
*element = idx as u8;
}
Expand Down

0 comments on commit d97589e

Please sign in to comment.