fil_rustacuda/memory/device/mod.rs
1use crate::error::CudaResult;
2use crate::stream::Stream;
3
4mod device_box;
5mod device_buffer;
6mod device_slice;
7
8pub use self::device_box::*;
9pub use self::device_buffer::*;
10pub use self::device_slice::*;
11
12/// Sealed trait implemented by types which can be the source or destination when copying data
13/// to/from the device or from one device allocation to another.
14pub trait CopyDestination<O: ?Sized>: crate::private::Sealed {
15 /// Copy data from `source`. `source` must be the same size as `self`.
16 ///
17 /// # Errors
18 ///
19 /// If a CUDA error occurs, return the error.
20 fn copy_from(&mut self, source: &O) -> CudaResult<()>;
21
22 /// Copy data to `dest`. `dest` must be the same size as `self`.
23 ///
24 /// # Errors
25 ///
26 /// If a CUDA error occurs, return the error.
27 fn copy_to(&self, dest: &mut O) -> CudaResult<()>;
28}
29
30/// Sealed trait implemented by types which can be the source or destination when copying data
31/// asynchronously to/from the device or from one device allocation to another.
32///
33/// # Safety
34///
35/// The functions of this trait are unsafe because they return control to the calling code while
36/// the copy operation could still be occurring in the background. This could allow calling code
37/// to read, modify or deallocate the destination buffer, or to modify or deallocate the source
38/// buffer resulting in a data race and undefined behavior.
39///
40/// Thus to enforce safety, the following invariants must be upheld:
41/// * The source and destination are not deallocated
42/// * The source is not modified
43/// * The destination is not written or read by any other operation
44///
45/// These invariants must be preserved until the stream is synchronized or an event queued after
46/// the copy is triggered.
47///
48pub trait AsyncCopyDestination<O: ?Sized>: crate::private::Sealed {
49 /// Asynchronously copy data from `source`. `source` must be the same size as `self`.
50 ///
51 /// Host memory used as a source or destination must be page-locked.
52 ///
53 /// # Safety
54 ///
55 /// For why this function is unsafe, see [AsyncCopyDestination](trait.AsyncCopyDestination.html)
56 ///
57 /// # Errors
58 ///
59 /// If a CUDA error occurs, return the error.
60 unsafe fn async_copy_from(&mut self, source: &O, stream: &Stream) -> CudaResult<()>;
61
62 /// Asynchronously copy data to `dest`. `dest` must be the same size as `self`.
63 ///
64 /// Host memory used as a source or destination must be page-locked.
65 ///
66 /// # Safety
67 ///
68 /// For why this function is unsafe, see [AsyncCopyDestination](trait.AsyncCopyDestination.html)
69 ///
70 /// # Errors
71 ///
72 /// If a CUDA error occurs, return the error.
73 unsafe fn async_copy_to(&self, dest: &mut O, stream: &Stream) -> CudaResult<()>;
74}