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
121
122
123
124
125
126
127
128
129
130
use crate::{Alloc, Buffer, Device, Read};
use super::{static_cpu, StaticDevice};
impl<'a, T: Clone> Buffer<'a, T> {
/// Moves the buffer [`Buffer`] to a static device.<br>
/// This device is chosen via the type parameter D -> [`OpenCL`](crate::OpenCL), [`CUDA`](crate::CUDA).<br>
/// It is recommended to use the to_gpu() method of [`Buffer`].
///
/// Example
#[cfg_attr(not(feature = "opencl"), doc = "```ignore")]
#[cfg_attr(feature = "opencl", doc = "```")]
/// use custos::prelude::*;
///
/// let cpu_buffer = Buffer::from(&[1., 2., 3.]);
///
/// let cl_buf = cpu_buffer.to_dev::<OpenCL>();
/// assert_eq!(cl_buf.read(), vec![1., 2., 3.]);
/// ```
#[inline]
pub fn to_dev<D>(self) -> Buffer<'static, T, D, ()>
where
D: StaticDevice + Alloc<'static, T>,
{
Buffer::from((D::as_static(), self.as_slice()))
}
/// Moves a [`Buffer`] to a [`CUDA`] device.<br>
/// It is recommended to use the to_gpu() method of [Buffer].
///
/// Example
/// ```
/// use custos::prelude::*;
///
/// let cpu_buffer = Buffer::from(&[1., 2., 3.]);
///
/// let cu_buf = cpu_buffer.to_cuda();
/// assert_eq!(cu_buf.read(), vec![1., 2., 3.]);
/// ```
#[cfg(feature = "cuda")]
#[inline]
pub fn to_cuda(self) -> Buffer<'a, T, crate::CUDA> {
self.to_dev::<crate::CUDA>()
}
/// Converts a [Buffer] to an OpenCL device buffer.<br>
/// It is recommended to use the to_gpu() method of [Buffer].
///
/// Example
/// ```
/// use custos::prelude::*;
///
/// let cpu_buffer = Buffer::from(&[1., 2., 3.]);
///
/// let cl_buf = cpu_buffer.to_cl();
/// assert_eq!(cl_buf.read(), vec![1., 2., 3.]);
/// ```
#[cfg(feature = "opencl")]
#[inline]
pub fn to_cl(self) -> Buffer<'a, T, crate::OpenCL> {
self.to_dev::<crate::OpenCL>()
}
/// Converts a [Buffer] to an OpenCL device buffer.
///
/// This method depends on the feature configuration.<br>
/// If the 'cuda' feature is enabled, this function will return a CUDA device buffer.
///
/// # Example
/// ```
/// use custos::prelude::*;
///
/// let cpu_buf = Buffer::from(&[4., 3., 5.]);
/// let cl_buf = cpu_buf.to_gpu();
///
/// assert_eq!(cl_buf.read(), vec![4., 3., 5.]);
/// ```
#[cfg(feature = "opencl")]
#[cfg(not(feature = "cuda"))]
#[inline]
pub fn to_gpu(self) -> Buffer<'a, T, crate::OpenCL> {
self.to_cl()
}
/// Converts a [Buffer] to a CUDA device buffer.
///
/// This method depends on the feature configuration.<br>
/// If the 'cuda' feature is disabled, this function will return an OpenCL device buffer.
///
/// # Example
/// ```
/// use custos::prelude::*;
///
/// let cpu_buf = Buffer::from(&[4., 3., 5.]);
/// let cuda_buf = cpu_buf.to_gpu();
///
/// assert_eq!(cuda_buf.read(), vec![4., 3., 5.]);
/// ```
#[cfg(feature = "cuda")]
#[inline]
pub fn to_gpu(self) -> Buffer<'a, T, crate::CUDA> {
self.to_cuda()
}
}
impl<'a, T, D> Buffer<'a, T, D>
where
T: Clone + Default,
D: Device + Read<T>,
{
/// Moves the [`Buffer`] back to a CPU buffer.
///
/// Example
///
#[cfg_attr(not(any(feature = "opencl", feature = "cuda")), doc = "```ignore")]
#[cfg_attr(any(feature = "opencl", feature = "cuda"), doc = "```")]
/// use custos::prelude::*;
///
/// let gpu_buf = Buffer::from(&[1, 2, 3]).to_gpu();
///
/// // ... some operations ...
///
/// let cpu_buf = gpu_buf.to_cpu();
/// assert_eq!(cpu_buf.as_slice(), &[1, 2, 3]);
/// ```
#[inline]
pub fn to_cpu(self) -> Buffer<'a, T> {
Buffer::from((static_cpu(), self.read_to_vec()))
}
}