#[derive(Clone, Debug)]
pub struct DeviceBuffer<T> {
host_shadow: Vec<T>,
}
impl<T> DeviceBuffer<T> {
pub const fn from_host_shadow(host_shadow: Vec<T>) -> Self {
Self { host_shadow }
}
pub const fn len(&self) -> usize {
self.host_shadow.len()
}
pub const fn is_empty(&self) -> bool {
self.host_shadow.len() == 0
}
pub fn host_shadow(&self) -> &[T] {
&self.host_shadow
}
}
#[derive(Clone, Debug)]
pub struct DeviceVector {
pub len: usize,
pub data: DeviceBuffer<f64>,
}
#[derive(Clone, Debug)]
pub struct DeviceMatrix {
pub rows: usize,
pub cols: usize,
pub data: DeviceBuffer<f64>,
pub column_major: bool,
}
impl DeviceMatrix {
pub const fn bytes(&self) -> usize {
self.rows
.saturating_mul(self.cols)
.saturating_mul(std::mem::size_of::<f64>())
}
}
#[derive(Clone, Debug)]
pub struct DeviceCsrMatrix {
pub rows: usize,
pub cols: usize,
pub rowptr: DeviceBuffer<i32>,
pub colidx: DeviceBuffer<i32>,
pub values: DeviceBuffer<f64>,
}
impl DeviceCsrMatrix {
pub fn new(
rows: usize,
cols: usize,
rowptr: DeviceBuffer<i32>,
colidx: DeviceBuffer<i32>,
values: DeviceBuffer<f64>,
) -> Self {
let expected = rows + 1;
let mut ptr = rowptr.host_shadow().to_vec();
if ptr.len() != expected {
let fill = ptr.last().copied().unwrap_or(0);
ptr.resize(expected, fill);
}
Self {
rows,
cols,
rowptr: DeviceBuffer::from_host_shadow(ptr),
colidx,
values,
}
}
pub const fn nnz(&self) -> usize {
self.values.len()
}
}