use std::sync::{Arc, RwLock, RwLockReadGuard, TryLockError};
use dlpk::sys::{DLDataType, DLDevice, DLPackVersion};
use dlpk::{DLPackPointerCast, DLPackTensor, GetDLPackDataType};
use ndarray::ArrayD;
use crate::error::ParseError;
pub trait Array: std::any::Any + Send + Sync {
fn as_any(&self) -> &dyn std::any::Any;
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
fn shape(&self) -> Vec<usize>;
fn dtype(&self) -> DLDataType;
fn device(&self) -> DLDevice;
fn as_dlpack(
&self,
device: DLDevice,
stream: Option<i64>,
max_version: DLPackVersion,
) -> Result<DLPackTensor, ParseError>;
fn copy(&self) -> Box<dyn Array>;
}
struct ReadLockedArray<T: 'static> {
_guard: RwLockReadGuard<'static, ArrayD<T>>,
_array: Arc<RwLock<ArrayD<T>>>,
shape: Vec<i64>,
strides: Vec<i64>,
}
unsafe extern "C" fn read_locked_deleter<T: 'static>(
managed: *mut dlpk::sys::DLManagedTensorVersioned,
) {
if managed.is_null() {
return;
}
unsafe {
let ctx = (*managed).manager_ctx;
if !ctx.is_null() {
drop(Box::from_raw(ctx.cast::<ReadLockedArray<T>>()));
(*managed).manager_ctx = std::ptr::null_mut();
}
}
}
impl<T> Array for Arc<RwLock<ArrayD<T>>>
where
T: 'static + Send + Sync + Clone + Default + GetDLPackDataType + DLPackPointerCast,
{
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn shape(&self) -> Vec<usize> {
match self.try_read() {
Ok(lock) => lock.shape().to_vec(),
Err(TryLockError::Poisoned(_)) => panic!("readcon-core array lock is poisoned"),
Err(TryLockError::WouldBlock) => panic!("readcon-core array is already locked"),
}
}
fn dtype(&self) -> DLDataType {
T::get_dlpack_data_type()
}
fn device(&self) -> DLDevice {
DLDevice::cpu()
}
fn as_dlpack(
&self,
device: DLDevice,
_stream: Option<i64>,
_max_version: DLPackVersion,
) -> Result<DLPackTensor, ParseError> {
if device != DLDevice::cpu() {
return Err(ParseError::ValidationError(format!(
"Arc<RwLock<ArrayD>> is CPU-only; requested device {device:?} unsupported"
)));
}
let array = Arc::clone(self);
let guard = match array.try_read() {
Ok(lock) => lock,
Err(TryLockError::Poisoned(_)) => {
return Err(ParseError::ValidationError(
"readcon-core array lock is poisoned".into(),
));
}
Err(TryLockError::WouldBlock) => {
return Err(ParseError::ValidationError(
"readcon-core array is already locked".into(),
));
}
};
let guard: RwLockReadGuard<'static, ArrayD<T>> = unsafe { std::mem::transmute(guard) };
let data = guard.as_ptr() as *mut std::ffi::c_void;
let mut ctx = Box::new(ReadLockedArray {
shape: guard.shape().iter().map(|&d| d as i64).collect(),
strides: guard.strides().iter().map(|&s| s as i64).collect(),
_guard: guard,
_array: array,
});
let dl_tensor = dlpk::sys::DLTensor {
data,
device: DLDevice::cpu(),
ndim: ctx.shape.len() as i32,
dtype: T::get_dlpack_data_type(),
shape: ctx.shape.as_mut_ptr(),
strides: ctx.strides.as_mut_ptr(),
byte_offset: 0,
};
let managed = dlpk::sys::DLManagedTensorVersioned {
version: DLPackVersion::current(),
manager_ctx: Box::into_raw(ctx).cast(),
deleter: Some(read_locked_deleter::<T>),
flags: dlpk::sys::DLPACK_FLAG_BITMASK_READ_ONLY,
dl_tensor,
};
Ok(unsafe { DLPackTensor::from_raw(managed) })
}
fn copy(&self) -> Box<dyn Array> {
Box::new(Arc::clone(self))
}
}
pub fn array_from_shape<T>(shape: &[usize]) -> Box<dyn Array>
where
T: 'static + Send + Sync + Clone + Default + GetDLPackDataType + DLPackPointerCast,
{
let arr: ArrayD<T> = ArrayD::default(ndarray::IxDyn(shape));
Box::new(Arc::new(RwLock::new(arr)))
}
pub fn allocate_array_on_device(
shape: &[usize],
device: DLDevice,
) -> Result<Box<dyn Array>, ParseError> {
if device == DLDevice::cpu() {
return Ok(array_from_shape::<f64>(shape));
}
#[cfg(feature = "cuda")]
{
use dlpk::sys::DLDeviceType;
if device.device_type == DLDeviceType::kDLCUDA {
return crate::cuda_array::allocate_cuda_f64(shape, device.device_id);
}
}
Err(ParseError::ValidationError(format!(
"no device allocator in this build for {device:?}; use caller-supplied device buffers via from_dlpack / array_from_host_f64_on_device, or build with `--features cuda` for CUDA devices"
)))
}
pub struct DeviceTaggedF64Array {
shape: Vec<usize>,
device: DLDevice,
data: Arc<Vec<f64>>,
}
impl DeviceTaggedF64Array {
pub fn new(shape: &[usize], data: Vec<f64>, device: DLDevice) -> Result<Self, ParseError> {
let n: usize = shape.iter().product();
if data.len() != n {
return Err(ParseError::ValidationError(format!(
"device-tagged array: expected {n} f64 values for shape {shape:?}, got {}",
data.len()
)));
}
Ok(Self {
shape: shape.to_vec(),
device,
data: Arc::new(data),
})
}
}
pub fn array_from_host_f64_on_device(
shape: &[usize],
data: Vec<f64>,
device: DLDevice,
) -> Result<Box<dyn Array>, ParseError> {
Ok(Box::new(DeviceTaggedF64Array::new(shape, data, device)?))
}
pub fn from_dlpack_f64(tensor: &DLPackTensor) -> Result<Box<dyn Array>, ParseError> {
let device = tensor.device();
let shape: Vec<usize> = tensor.shape().iter().map(|&d| d as usize).collect();
let n: usize = shape.iter().product();
let dtype = tensor.dtype();
if dtype.code != dlpk::sys::DLDataTypeCode::kDLFloat || dtype.bits != 64 {
return Err(ParseError::ValidationError(format!(
"from_dlpack_f64: expected f64, got dtype code={:?} bits={}",
dtype.code, dtype.bits
)));
}
let ptr = tensor
.data_ptr::<f64>()
.map_err(|e| ParseError::ValidationError(format!("from_dlpack_f64 data_ptr: {e}")))?;
let mut data = vec![0.0f64; n];
if n > 0 {
unsafe {
std::ptr::copy_nonoverlapping(ptr, data.as_mut_ptr(), n);
}
}
array_from_host_f64_on_device(&shape, data, device)
}
struct DeviceTaggedManager {
data: Arc<Vec<f64>>,
shape: Vec<i64>,
}
unsafe extern "C" fn device_tagged_deleter(managed: *mut dlpk::sys::DLManagedTensorVersioned) {
if managed.is_null() {
return;
}
unsafe {
let ctx = (*managed).manager_ctx;
if !ctx.is_null() {
let _ = Box::from_raw(ctx as *mut DeviceTaggedManager);
(*managed).manager_ctx = std::ptr::null_mut();
}
}
}
impl Array for DeviceTaggedF64Array {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn shape(&self) -> Vec<usize> {
self.shape.clone()
}
fn dtype(&self) -> DLDataType {
f64::get_dlpack_data_type()
}
fn device(&self) -> DLDevice {
self.device
}
fn as_dlpack(
&self,
device: DLDevice,
_stream: Option<i64>,
_max_version: DLPackVersion,
) -> Result<DLPackTensor, ParseError> {
if device != self.device {
return Err(ParseError::ValidationError(format!(
"device mismatch: array is on {:?}, requested {:?}",
self.device, device
)));
}
let manager = Box::new(DeviceTaggedManager {
data: Arc::clone(&self.data),
shape: self.shape.iter().map(|&d| d as i64).collect(),
});
let data_ptr = manager.data.as_ptr() as *mut std::ffi::c_void;
let shape_ptr = manager.shape.as_ptr() as *mut i64;
let mut managed = dlpk::sys::DLManagedTensorVersioned {
version: dlpk::sys::DLPackVersion {
major: dlpk::sys::DLPACK_MAJOR_VERSION,
minor: dlpk::sys::DLPACK_MINOR_VERSION,
},
manager_ctx: std::ptr::null_mut(),
deleter: Some(device_tagged_deleter),
dl_tensor: dlpk::sys::DLTensor {
data: data_ptr,
device: self.device,
ndim: self.shape.len() as i32,
dtype: f64::get_dlpack_data_type(),
shape: shape_ptr,
strides: std::ptr::null_mut(),
byte_offset: 0,
},
flags: dlpk::sys::DLPACK_FLAG_BITMASK_READ_ONLY,
};
managed.manager_ctx = Box::into_raw(manager) as *mut std::ffi::c_void;
Ok(unsafe { DLPackTensor::from_raw(managed) })
}
fn copy(&self) -> Box<dyn Array> {
Box::new(Self {
shape: self.shape.clone(),
device: self.device,
data: Arc::clone(&self.data),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn array_from_shape_reports_shape_and_dtype() {
let a: Box<dyn Array> = array_from_shape::<f64>(&[5, 3]);
assert_eq!(a.shape(), vec![5, 3]);
let dt = a.dtype();
assert_eq!(dt.code, dlpk::sys::DLDataTypeCode::kDLFloat);
assert_eq!(dt.bits, 64);
assert_eq!(dt.lanes, 1);
assert_eq!(a.device(), DLDevice::cpu());
}
#[test]
fn array_copy_shares_storage_via_arc() {
let a = array_from_shape::<f64>(&[2, 3]);
let b = a.copy();
assert_eq!(a.shape(), b.shape());
}
#[test]
fn array_dlpack_export_round_trip() {
let a = array_from_shape::<f64>(&[4, 3]);
let tensor = a
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.expect("DLPack export should succeed for CPU array");
assert_eq!(tensor.shape(), &[4, 3]);
}
fn shared_f64(values: Vec<f64>, shape: &[usize]) -> Arc<RwLock<ArrayD<f64>>> {
Arc::new(RwLock::new(
ArrayD::from_shape_vec(ndarray::IxDyn(shape), values).unwrap(),
))
}
#[test]
fn locked_array_exports_point_at_its_buffer() {
let array = shared_f64((0..6).map(f64::from).collect(), &[2, 3]);
let buffer = array.read().unwrap().as_ptr();
let first = array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap();
let second = array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap();
for tensor in [&first, &second] {
assert_eq!(tensor.data_ptr::<f64>().unwrap(), buffer);
assert_eq!(tensor.shape(), &[2, 3]);
assert_eq!(tensor.strides(), Some(&[3, 1][..]));
assert_ne!(tensor.flags() & dlpk::sys::DLPACK_FLAG_BITMASK_READ_ONLY, 0);
}
assert_eq!(array.shape(), vec![2, 3]);
}
#[test]
fn a_live_export_keeps_writers_out_until_it_is_deleted() {
let array = shared_f64(vec![1.0, 2.0, 3.0], &[3]);
let tensor = array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap();
assert!(matches!(array.try_write(), Err(TryLockError::WouldBlock)));
drop(array.try_read().expect("readers still get in"));
drop(tensor);
array
.try_write()
.expect("writers get in after the export is deleted")[0] = 9.0;
let again = array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap();
assert_eq!(unsafe { *again.data_ptr::<f64>().unwrap() }, 9.0);
}
#[test]
fn an_export_outlives_the_last_array_handle() {
let array = shared_f64(vec![4.0, 5.0], &[2]);
let tensor = array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap();
drop(array);
let values = unsafe { std::slice::from_raw_parts(tensor.data_ptr::<f64>().unwrap(), 2) };
assert_eq!(values, &[4.0, 5.0]);
}
#[test]
fn exporting_a_write_locked_array_is_an_error() {
let array = shared_f64(vec![1.0], &[1]);
let _writer = array.write().unwrap();
assert!(
array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.is_err()
);
}
#[cfg(target_os = "linux")]
#[test]
fn exports_fail_while_a_writer_waits_on_a_live_export() {
let array = shared_f64(vec![1.0], &[1]);
let held = array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap();
std::thread::scope(|scope| {
let writer = scope.spawn(|| array.write().unwrap()[0] = 2.0);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
while array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.is_ok()
{
assert!(
std::time::Instant::now() < deadline,
"exports kept succeeding while a writer waited"
);
std::thread::yield_now();
}
assert!(!writer.is_finished());
drop(held);
writer.join().unwrap();
});
assert_eq!(array.read().unwrap()[0], 2.0);
}
#[cfg(asan_run)]
#[test]
fn a_second_deleter_call_is_a_use_after_free() {
const CHILD: &str = "READCON_DOUBLE_DELETE_CHILD";
if std::env::var_os(CHILD).is_some() {
let array = shared_f64(vec![1.0], &[1]);
let tensor = array
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap()
.into_raw()
.as_ptr();
unsafe {
let deleter = (*tensor).deleter.unwrap();
deleter(tensor);
deleter(tensor);
}
return;
}
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"array::tests::a_second_deleter_call_is_a_use_after_free",
"--nocapture",
"--test-threads=1",
])
.env(CHILD, "1")
.output()
.unwrap();
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(!output.status.success(), "{stderr}");
assert!(stderr.contains("heap-use-after-free"), "{stderr}");
}
#[cfg(not(feature = "cuda"))]
#[test]
fn allocate_non_cpu_fails_clearly() {
match allocate_array_on_device(&[2, 3], DLDevice::cuda(0)) {
Ok(_) => panic!("non-CPU allocate must fail without --features cuda"),
Err(err) => {
let msg = format!("{err:?}");
assert!(
msg.contains("no device allocator") || msg.contains("allocator"),
"{msg}"
);
}
}
let cpu = allocate_array_on_device(&[2, 3], DLDevice::cpu()).unwrap();
assert_eq!(cpu.device(), DLDevice::cpu());
}
#[cfg(feature = "cuda")]
#[test]
fn allocate_cuda_succeeds_with_feature() {
if crate::cuda_array::cuda_device_count().is_err() {
return;
}
let a = allocate_array_on_device(&[2, 3], DLDevice::cuda(0))
.expect("CUDA allocate must succeed with --features cuda and a driver");
assert_eq!(a.device(), DLDevice::cuda(0));
assert_eq!(a.device().device_type, dlpk::sys::DLDeviceType::kDLCUDA);
let t = a
.as_dlpack(DLDevice::cuda(0), None, DLPackVersion::current())
.expect("matching as_dlpack");
assert_eq!(t.device().device_type, dlpk::sys::DLDeviceType::kDLCUDA);
let cpu = allocate_array_on_device(&[2, 3], DLDevice::cpu()).unwrap();
assert_eq!(cpu.device(), DLDevice::cpu());
}
#[test]
fn cuda_tagged_preserves_device_and_matching_as_dlpack() {
let data: Vec<f64> = (0..6).map(|i| i as f64).collect();
let a = array_from_host_f64_on_device(&[2, 3], data.clone(), DLDevice::cuda(0)).unwrap();
assert_eq!(a.device(), DLDevice::cuda(0));
assert_eq!(a.shape(), vec![2, 3]);
let mismatch = a
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap_err();
assert!(
format!("{mismatch:?}").contains("device mismatch"),
"{mismatch:?}"
);
let tensor = a
.as_dlpack(DLDevice::cuda(0), None, DLPackVersion::current())
.expect("matching CUDA device export");
assert_eq!(tensor.device(), DLDevice::cuda(0));
assert_eq!(tensor.shape(), &[2, 3]);
let back = from_dlpack_f64(&tensor).unwrap();
assert_eq!(back.device(), DLDevice::cuda(0));
assert_eq!(back.shape(), vec![2, 3]);
let again = back
.as_dlpack(DLDevice::cuda(0), None, DLPackVersion::current())
.unwrap();
assert_eq!(again.device(), DLDevice::cuda(0));
}
#[test]
fn cpu_tagged_path_unchanged() {
let a =
array_from_host_f64_on_device(&[1, 3], vec![1.0, 2.0, 3.0], DLDevice::cpu()).unwrap();
assert_eq!(a.device(), DLDevice::cpu());
let t = a
.as_dlpack(DLDevice::cpu(), None, DLPackVersion::current())
.unwrap();
assert_eq!(t.device(), DLDevice::cpu());
let back = from_dlpack_f64(&t).unwrap();
assert_eq!(back.device(), DLDevice::cpu());
}
}