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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use crate::ffi;
use crate::memory::DeviceBuffer;
use crate::runtime::Future;
use crate::stream::Stream;

type Result<T> = std::result::Result<T, crate::error::Error>;

/// A host buffer.
///
/// # Performance
///
/// Host buffers are managed by CUDA and can be used for pinned memory transfer. Pinned memory
/// transfer speeds are usually higher compared to paged memory transfers. Pinned memory buffers are
/// especially important for this crate because the runtime thread must do the least amount of CPU
/// work possible. Paged transfers do require the host to move data into a CUDA managed buffer first
/// (an extra memory copy) whilst pinned transfers do not.
pub struct HostBuffer<T: Copy + 'static> {
    inner: ffi::memory::HostBuffer<T>,
}

impl<T: Copy + 'static> HostBuffer<T> {
    /// Allocates memory on the host. This creates a pinned buffer. Any transfers to and from this
    /// buffer automatically become pinned transfers, and will be much faster.
    ///
    /// [CUDA documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1g32bd7a39135594788a542ae72217775c)
    ///
    /// # Arguments
    ///
    /// * `num_elements` - Number of elements to allocate.
    pub async fn new(num_elements: usize) -> Self {
        let inner = Future::new(move || ffi::memory::HostBuffer::<T>::new(num_elements)).await;
        Self { inner }
    }

    /// Allocates memory on the host and copies the provided data into it.
    ///
    /// This creates a pinned buffer. Any transfers to and from this buffer automatically become
    /// pinned transfers, and will be much faster.
    ///
    /// This is a convenience function that allows the caller to quickly put data into a host
    /// buffer. It is roughly similar to `buffer.copy_from_slice(slice)`.
    ///
    /// # Arguments
    ///
    /// * `slice` - Data to copy into the new host buffer.
    pub async fn from_slice(slice: &[T]) -> Self {
        let mut this = Self::new(slice.len()).await;
        this.copy_from_slice(slice);
        this
    }

    /// Allocates memory on the host and copies the provided array into it.
    ///
    /// This creates a pinned buffer. Any transfers to and from this buffer automatically become
    /// pinned transfers, and will be much faster.
    ///
    /// This is a convenience function that allows the caller to quickly put data into a host
    /// buffer. It is roughly similar to `buffer.copy_from_array(slice)`.
    ///
    /// # Arguments
    ///
    /// * `array` - Array to copy into the new host buffer.
    #[cfg(feature = "ndarray")]
    pub async fn from_array<D: ndarray::Dimension>(array: &ndarray::ArrayView<'_, T, D>) -> Self {
        let mut this = Self::new(array.len()).await;
        this.copy_from_array(array);
        this
    }

    /// Copies memory from the provided device buffer to this buffer.
    ///
    /// This function synchronizes the stream implicitly.
    ///
    /// [CUDA documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1g85073372f776b4c4d5f89f7124b7bf79)
    ///
    /// # Pinned transfer
    ///
    /// This function is guaranteed to produce a pinned transfer on the runtime thread.
    ///
    /// # Stream ordered semantics
    ///
    /// This function uses stream ordered semantics. It can only be guaranteed to complete
    /// sequentially relative to operations scheduled on the same stream or the default stream.
    ///
    /// # Arguments
    ///
    /// * `other` - Device buffer to copy from.
    /// * `stream` - Stream to use.
    #[inline(always)]
    pub async fn copy_from(&mut self, other: &DeviceBuffer<T>, stream: &Stream) -> Result<()> {
        other.copy_to(self, stream).await
    }

    /// Copies memory from the provided device buffer to this buffer.
    ///
    /// [CUDA documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1g85073372f776b4c4d5f89f7124b7bf79)
    ///
    /// # Pinned transfer
    ///
    /// This function is guaranteed to produce a pinned transfer on the runtime thread.
    ///
    /// # Stream ordered semantics
    ///
    /// This function uses stream ordered semantics. It can only be guaranteed to complete
    /// sequentially relative to operations scheduled on the same stream or the default stream.
    ///
    /// # Safety
    ///
    /// This function is unsafe because the operation might not have completed when the function
    /// returns, and thus the state of the buffer is undefined.
    ///
    /// # Arguments
    ///
    /// * `other` - Device buffer to copy from.
    /// * `stream` - Stream to use.
    #[inline(always)]
    pub async unsafe fn copy_from_async(
        &mut self,
        other: &DeviceBuffer<T>,
        stream: &Stream,
    ) -> Result<()> {
        other.copy_to_async(self, stream).await
    }

    /// Copies memory from this buffer to the provided device buffer.
    ///
    /// This function synchronizes the stream implicitly.
    ///
    /// [CUDA documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1g85073372f776b4c4d5f89f7124b7bf79)
    ///
    /// # Pinned transfer
    ///
    /// This function is guaranteed to produce a pinned transfer on the runtime thread.
    ///
    /// # Stream ordered semantics
    ///
    /// This function uses stream ordered semantics. It can only be guaranteed to complete
    /// sequentially relative to operations scheduled on the same stream or the default stream.
    ///
    /// # Arguments
    ///
    /// * `other` - Device buffer to copy to.
    /// * `stream` - Stream to use.
    #[inline(always)]
    pub async fn copy_to(&self, other: &mut DeviceBuffer<T>, stream: &Stream) -> Result<()> {
        other.copy_from(self, stream).await
    }

    /// Copies memory from this buffer to the provided device buffer.
    ///
    /// [CUDA documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html#group__CUDART__MEMORY_1g85073372f776b4c4d5f89f7124b7bf79)
    ///
    /// # Pinned transfer
    ///
    /// This function is guaranteed to produce a pinned transfer on the runtime thread.
    ///
    /// # Stream ordered semantics
    ///
    /// This function uses stream ordered semantics. It can only be guaranteed to complete
    /// sequentially relative to operations scheduled on the same stream or the default stream.
    ///
    /// # Safety
    ///
    /// This function is unsafe because the operation might not have completed when the function
    /// returns, and thus the state of the buffer is undefined.
    ///
    /// # Arguments
    ///
    /// * `other` - Device buffer to copy to.
    /// * `stream` - Stream to use.
    #[inline(always)]
    pub async unsafe fn copy_to_async(
        &self,
        other: &mut DeviceBuffer<T>,
        stream: &Stream,
    ) -> Result<()> {
        other.copy_from_async(self, stream).await
    }

    /// Copy data into the host buffer from a slice.
    ///
    /// # Synchronization safety
    ///
    /// This call is only synchronization-safe if all streams that have previously been used for
    /// copy operations either from or to this host buffer have been synchronized, and no operations
    /// have been scheduled since.
    ///
    /// # Arguments
    ///
    /// * `slice` - Data to copy into the new host buffer.
    ///
    /// # Example
    ///
    /// ```
    /// # use async_cuda_core::HostBuffer;
    /// # tokio_test::block_on(async {
    /// let mut host_buffer = HostBuffer::<u8>::new(100).await;
    /// let some_data = vec![10; 100];
    /// host_buffer.copy_from_slice(&some_data);
    /// # })
    /// ```
    #[inline(always)]
    pub fn copy_from_slice(&mut self, slice: &[T]) {
        self.inner.copy_from_slice(slice);
    }

    /// Copy array into the host buffer from a slice.
    ///
    /// # Synchronization safety
    ///
    /// This call is only synchronization-safe if all streams that have previously been used for
    /// copy operations either from or to this host buffer have been synchronized, and no operations
    /// have been scheduled since.
    ///
    /// # Arguments
    ///
    /// * `array` - Array to copy into the new host buffer.
    #[cfg(feature = "ndarray")]
    #[inline(always)]
    pub fn copy_from_array<D: ndarray::Dimension>(&mut self, array: &ndarray::ArrayView<T, D>) {
        self.inner.copy_from_array(array)
    }

    /// Copy the data to a [`Vec`] and return it.
    #[inline(always)]
    pub fn to_vec(&self) -> Vec<T> {
        self.inner.to_vec()
    }

    /// Copy the data to an [`ndarray::Array`] and return it.
    ///
    /// Function panics if provided shape does not match size of array.
    ///
    /// # Arguments
    ///
    /// * `shape` - Shape for array.
    #[cfg(feature = "ndarray")]
    #[inline(always)]
    pub fn to_array_with_shape<D: ndarray::Dimension>(
        &self,
        shape: impl Into<ndarray::StrideShape<D>>,
    ) -> ndarray::Array<T, D> {
        self.inner.to_array_with_shape::<D>(shape)
    }

    /// Get number of elements in buffer.
    #[inline(always)]
    pub fn num_elements(&self) -> usize {
        self.inner.num_elements
    }

    /// Access the inner synchronous implementation of [`HostBuffer`].
    #[inline(always)]
    pub fn inner(&self) -> &ffi::memory::HostBuffer<T> {
        &self.inner
    }

    /// Access the inner synchronous implementation of [`HostBuffer`].
    #[inline(always)]
    pub fn inner_mut(&mut self) -> &mut ffi::memory::HostBuffer<T> {
        &mut self.inner
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_new() {
        let buffer = HostBuffer::<u32>::new(100).await;
        assert_eq!(buffer.num_elements(), 100);
        assert_eq!(buffer.to_vec().len(), 100);
    }

    #[tokio::test]
    async fn test_from_slice() {
        let all_ones = vec![1_u32; 200];
        let buffer = HostBuffer::from_slice(all_ones.as_slice()).await;
        assert_eq!(buffer.num_elements(), 200);
        let data = buffer.to_vec();
        assert_eq!(data.len(), 200);
        assert!(data.into_iter().all(|v| v == 1_u32));
    }

    #[tokio::test]
    async fn test_copy() {
        let stream = Stream::new().await.unwrap();
        let all_ones = vec![1_u32; 100];
        let host_buffer = HostBuffer::from_slice(all_ones.as_slice()).await;

        let mut device_buffer = DeviceBuffer::<u32>::new(100, &stream).await;
        unsafe {
            host_buffer
                .copy_to_async(&mut device_buffer, &stream)
                .await
                .unwrap();
        }

        let mut return_host_buffer = HostBuffer::<u32>::new(100).await;
        unsafe {
            return_host_buffer
                .copy_from_async(&device_buffer, &stream)
                .await
                .unwrap();
        }

        stream.synchronize().await.unwrap();

        assert_eq!(return_host_buffer.num_elements(), 100);
        let return_data = return_host_buffer.to_vec();
        assert_eq!(return_data.len(), 100);
        assert!(return_data.into_iter().all(|v| v == 1_u32));
    }

    #[tokio::test]
    #[should_panic]
    async fn test_it_panics_when_copying_invalid_size() {
        let stream = Stream::new().await.unwrap();
        let host_buffer = HostBuffer::<u32>::new(100).await;
        let mut device_buffer = DeviceBuffer::<u32>::new(101, &Stream::null()).await;
        let _ = unsafe { host_buffer.copy_to_async(&mut device_buffer, &stream).await };
    }
}