use std::os::raw::c_void;
use crate::error::{cuda_check, Error};
use crate::ffi;
use crate::Result;
pub fn device_count() -> Result<i32> {
let mut count = 0i32;
cuda_check(
unsafe { ffi::trt_cuda_device_count(&mut count) },
"cudaGetDeviceCount",
)?;
Ok(count)
}
pub fn set_device(device: i32) -> Result<()> {
cuda_check(unsafe { ffi::trt_cuda_set_device(device) }, "cudaSetDevice")
}
#[derive(Debug)]
pub struct Stream {
raw: *mut c_void,
owned: bool,
}
impl Stream {
pub fn new() -> Result<Self> {
Self::create(1)
}
pub fn blocking() -> Result<Self> {
Self::create(0)
}
fn create(non_blocking: i32) -> Result<Self> {
let mut raw: *mut c_void = std::ptr::null_mut();
cuda_check(
unsafe { ffi::trt_cuda_stream_create(&mut raw, non_blocking) },
"cudaStreamCreate",
)?;
Ok(Self { raw, owned: true })
}
pub const fn default_stream() -> Self {
Self {
raw: std::ptr::null_mut(),
owned: false,
}
}
pub fn as_raw(&self) -> *mut c_void {
self.raw
}
pub fn synchronize(&self) -> Result<()> {
cuda_check(
unsafe { ffi::trt_cuda_stream_sync(self.raw) },
"cudaStreamSynchronize",
)
}
}
impl Default for Stream {
fn default() -> Self {
Self::default_stream()
}
}
impl Drop for Stream {
fn drop(&mut self) {
if self.owned && !self.raw.is_null() {
let _ = cuda_check(
unsafe { ffi::trt_cuda_stream_destroy(self.raw) },
"cudaStreamDestroy",
);
}
}
}
unsafe impl Send for Stream {}
#[derive(Debug)]
pub struct DeviceBuffer {
ptr: *mut c_void,
bytes: usize,
}
impl DeviceBuffer {
pub fn new(bytes: usize) -> Result<Self> {
let mut ptr: *mut c_void = std::ptr::null_mut();
cuda_check(
unsafe { ffi::trt_cuda_malloc(&mut ptr, bytes) },
"cudaMalloc",
)?;
Ok(Self { ptr, bytes })
}
pub fn zeroed(bytes: usize, stream: &Stream) -> Result<Self> {
let buffer = Self::new(bytes)?;
cuda_check(
unsafe { ffi::trt_cuda_zero(buffer.ptr, bytes, stream.as_raw()) },
"cudaMemsetAsync",
)?;
stream.synchronize()?;
Ok(buffer)
}
pub fn size_bytes(&self) -> usize {
self.bytes
}
pub fn as_ptr(&self) -> *const c_void {
self.ptr
}
pub fn as_mut_ptr(&mut self) -> *mut c_void {
self.ptr
}
pub fn copy_from_host(&mut self, src: &[u8], stream: &Stream) -> Result<()> {
if src.len() > self.bytes {
return Err(Error::SizeMismatch {
tensor: "device buffer".to_string(),
expected: self.bytes,
found: src.len(),
});
}
cuda_check(
unsafe {
ffi::trt_cuda_copy_h2d(
self.ptr,
src.as_ptr() as *const c_void,
src.len(),
stream.as_raw(),
)
},
"cudaMemcpyAsync host to device",
)
}
pub fn copy_to_host_async(&self, dst: &mut [u8], stream: &Stream) -> Result<()> {
if dst.len() > self.bytes {
return Err(Error::SizeMismatch {
tensor: "device buffer".to_string(),
expected: self.bytes,
found: dst.len(),
});
}
cuda_check(
unsafe {
ffi::trt_cuda_copy_d2h(
dst.as_mut_ptr() as *mut c_void,
self.ptr,
dst.len(),
stream.as_raw(),
)
},
"cudaMemcpyAsync device to host",
)
}
pub fn copy_to_host(&self, dst: &mut [u8], stream: &Stream) -> Result<()> {
self.copy_to_host_async(dst, stream)?;
stream.synchronize()
}
}
impl Drop for DeviceBuffer {
fn drop(&mut self) {
let _ = cuda_check(unsafe { ffi::trt_cuda_free(self.ptr) }, "cudaFree");
}
}
unsafe impl Send for DeviceBuffer {}
#[derive(Debug)]
pub struct PinnedBuffer {
ptr: *mut c_void,
bytes: usize,
}
impl PinnedBuffer {
pub fn new(bytes: usize) -> Result<Self> {
let mut ptr: *mut c_void = std::ptr::null_mut();
cuda_check(
unsafe { ffi::trt_cuda_host_malloc(&mut ptr, bytes) },
"cudaHostAlloc",
)?;
Ok(Self { ptr, bytes })
}
pub fn size_bytes(&self) -> usize {
self.bytes
}
pub fn bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.ptr as *const u8, self.bytes) }
}
pub fn bytes_mut(&mut self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.ptr as *mut u8, self.bytes) }
}
pub fn f32s(&self) -> &[f32] {
assert_eq!(
self.bytes % std::mem::size_of::<f32>(),
0,
"buffer is not a whole number of f32"
);
unsafe {
std::slice::from_raw_parts(
self.ptr as *const f32,
self.bytes / std::mem::size_of::<f32>(),
)
}
}
pub fn f32s_mut(&mut self) -> &mut [f32] {
assert_eq!(
self.bytes % std::mem::size_of::<f32>(),
0,
"buffer is not a whole number of f32"
);
unsafe {
std::slice::from_raw_parts_mut(
self.ptr as *mut f32,
self.bytes / std::mem::size_of::<f32>(),
)
}
}
}
impl Drop for PinnedBuffer {
fn drop(&mut self) {
let _ = cuda_check(unsafe { ffi::trt_cuda_host_free(self.ptr) }, "cudaFreeHost");
}
}
unsafe impl Send for PinnedBuffer {}