1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::{
error::CudaResult,
memory::{
array::{ArrayObject, ArrayPrimitive},
DeviceBox, DeviceCopy, UnifiedBuffer,
},
prelude::DeviceBuffer,
surface::Surface,
texture::Texture,
};
pub trait DeviceCopyExt: DeviceCopy {
fn as_dbox(&self) -> CudaResult<DeviceBox<Self>> {
DeviceBox::new(self)
}
}
impl<T: DeviceCopy> DeviceCopyExt for T {}
pub trait SliceExt<T: DeviceCopy> {
fn as_dbuf(&self) -> CudaResult<DeviceBuffer<T>>;
fn as_unified_buf(&self) -> CudaResult<UnifiedBuffer<T>>;
fn as_1d_array(&self) -> CudaResult<ArrayObject>
where
T: ArrayPrimitive;
fn as_2d_array(&self, width: usize, height: usize) -> CudaResult<ArrayObject>
where
T: ArrayPrimitive;
fn as_1d_texture(&self) -> CudaResult<Texture>
where
T: ArrayPrimitive,
{
Texture::from_array(self.as_1d_array()?)
}
fn as_2d_texture(&self, width: usize, height: usize) -> CudaResult<Texture>
where
T: ArrayPrimitive,
{
Texture::from_array(self.as_2d_array(width, height)?)
}
fn as_1d_surface(&self) -> CudaResult<Surface>
where
T: ArrayPrimitive,
{
Surface::from_array(self.as_1d_array()?)
}
fn as_2d_surface(&self, width: usize, height: usize) -> CudaResult<Surface>
where
T: ArrayPrimitive,
{
Surface::from_array(self.as_2d_array(width, height)?)
}
}
impl<T: DeviceCopy> SliceExt<T> for &[T] {
fn as_dbuf(&self) -> CudaResult<DeviceBuffer<T>> {
DeviceBuffer::from_slice(*self)
}
fn as_unified_buf(&self) -> CudaResult<UnifiedBuffer<T>> {
UnifiedBuffer::from_slice(*self)
}
fn as_1d_array(&self) -> CudaResult<ArrayObject>
where
T: ArrayPrimitive,
{
let mut arr = ArrayObject::new_1d(self.len(), T::array_format(), 1)?;
arr.copy_from(self)?;
Ok(arr)
}
fn as_2d_array(&self, width: usize, height: usize) -> CudaResult<ArrayObject>
where
T: ArrayPrimitive,
{
let mut arr = ArrayObject::new_2d([width, height], T::array_format(), 1)?;
arr.copy_from(self)?;
Ok(arr)
}
}
impl<T: DeviceCopy, const N: usize> SliceExt<T> for [T; N] {
fn as_dbuf(&self) -> CudaResult<DeviceBuffer<T>> {
DeviceBuffer::from_slice(self)
}
fn as_unified_buf(&self) -> CudaResult<UnifiedBuffer<T>> {
UnifiedBuffer::from_slice(self)
}
fn as_1d_array(&self) -> CudaResult<ArrayObject>
where
T: ArrayPrimitive,
{
let mut arr = ArrayObject::new_1d(self.len(), T::array_format(), 1)?;
arr.copy_from(self)?;
Ok(arr)
}
fn as_2d_array(&self, width: usize, height: usize) -> CudaResult<ArrayObject>
where
T: ArrayPrimitive,
{
let mut arr = ArrayObject::new_2d([width, height], T::array_format(), 1)?;
arr.copy_from(self)?;
Ok(arr)
}
}