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
use std::{mem::MaybeUninit, result::Result as StdResult, sync::Arc};
use crate::{
context::Context,
error::Result,
graph::{GraphBuffer, RetainedAllocation},
memory::{DeviceAllocation, DeviceMemory, transfer},
view::{
AnyBitPatternDeviceRepr, DeviceOutputView, DeviceRepr, DeviceSlice, DeviceView,
DeviceViewMut, NoUninitDeviceRepr,
},
};
impl<T> GraphBuffer<T>
where
T: DeviceRepr,
{
pub(crate) fn from_memory(memory: DeviceMemory<T>) -> Self {
Self { memory }
}
pub(crate) fn retained(&self) -> RetainedAllocation {
let memory = Arc::clone(self.memory.allocation());
RetainedAllocation(memory)
}
/// Returns the number of exposed elements.
#[must_use]
pub fn len(&self) -> usize {
self.memory.len()
}
/// Returns whether the exposed element range is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.memory.is_empty()
}
/// Returns the byte length of the exposed element range.
#[must_use]
pub fn byte_len(&self) -> usize {
self.memory.byte_len()
}
/// Returns the context retained by the buffer allocation.
#[must_use]
pub fn context(&self) -> &Arc<Context> {
self.memory.context()
}
/// Returns the CUDA-accessible element pointer without synchronizing graph work.
#[must_use]
pub fn as_ptr(&self) -> *const T {
self.memory.as_ptr()
}
/// Returns the CUDA-accessible mutable element pointer.
///
/// The caller must order raw accesses against graph launches using this allocation.
pub fn as_mut_ptr(&mut self) -> *mut T {
self.memory.as_mut_ptr()
}
/// Copies a host slice into this graph-retained device buffer.
///
/// This updates the stable allocation used by graph-buffer node APIs.
/// The caller is still responsible for ordering this copy against graph launches that read or write the same allocation.
///
/// # Errors
///
/// Returns an error if `host_slice` does not have the same length as this buffer or if CUDA rejects the copy.
pub fn copy_from_host(&mut self, host_slice: &[T]) -> Result<()> {
transfer::copy_from_host(self, host_slice)
}
/// Copies this graph-retained device buffer into a host slice.
///
/// # Errors
///
/// Returns an error if `host_slice` does not have the same length as this buffer or if CUDA rejects the copy.
pub fn copy_to_host(&self, host_slice: &mut [T]) -> Result<()>
where
T: AnyBitPatternDeviceRepr,
{
let () = T::LAYOUT;
// SAFETY: `T: AnyBitPatternDeviceRepr`, so any device bytes are valid `T` values.
unsafe { self.copy_to_host_unchecked(host_slice) }
}
/// Copies graph-buffer values whose validity follows from the graph's kernels.
///
/// # Safety
///
/// Every copied element must be initialized and valid for `T`.
/// The copy must be ordered after all graph writes to this buffer.
///
/// # Errors
///
/// Returns an error for unequal element counts or byte-size overflow.
/// Also propagates context-binding, stream-capture, CUDA copy, and stream synchronization errors.
pub unsafe fn copy_to_host_unchecked(&self, host_slice: &mut [T]) -> Result<()> {
// SAFETY: the caller guarantees every element is initialized, valid for `T`, and written before this copy, which is all the owned memory's copy requires.
unsafe { self.memory.copy_to_host_unchecked(host_slice) }
}
/// Copies another graph-retained buffer into this buffer.
///
/// # Errors
///
/// Returns an error if the buffers have different lengths or if CUDA rejects the copy.
pub fn copy_from_buffer<S: DeviceSlice<T> + ?Sized>(&mut self, src: &S) -> Result<()> {
transfer::copy_device(self, src)
}
/// Copies every element into a new host vector and waits for completion.
///
/// # Errors
///
/// Propagates byte-size, context-binding, stream-capture, CUDA copy, and stream synchronization errors.
/// No initialized vector is returned unless the transfer completes.
pub fn copy_to_host_vec(&self) -> Result<Vec<T>>
where
T: AnyBitPatternDeviceRepr,
{
self.memory.copy_to_host_vec()
}
/// Reads graph output values whose validity follows from the producing kernels.
///
/// # Safety
///
/// All elements must be initialized and valid for `T`, and graph writes must finish before this readback.
///
/// # Errors
///
/// Propagates byte-size, context-binding, stream-capture, CUDA copy, and stream synchronization errors.
/// No initialized vector is returned unless the transfer completes.
pub unsafe fn copy_to_host_vec_unchecked(&self) -> Result<Vec<T>> {
// SAFETY: the caller guarantees every element is initialized, valid for `T`, and written before this readback.
unsafe { self.memory.copy_to_host_vec_unchecked() }
}
/// Returns the allocation retained by graph definitions, executables, and foreign owners.
#[must_use]
pub fn allocation(&self) -> &Arc<DeviceAllocation> {
self.memory.allocation()
}
/// Borrows the initialized buffer as a shared device view.
#[must_use]
pub fn view(&self) -> DeviceView<'_, T> {
DeviceView::from_buffer(self)
}
/// Borrows the initialized buffer as an exclusive device view.
pub fn view_mut(&mut self) -> DeviceViewMut<'_, T> {
DeviceViewMut::from_buffer(self)
}
/// Reinterprets initialized bytes without changing graph-retained allocation ownership.
///
/// # Errors
///
/// Returns the original buffer if the destination type is zero-sized, its size does not divide the exposed byte length, or a nonempty range is not aligned for the destination type.
pub fn cast<U: AnyBitPatternDeviceRepr>(self) -> StdResult<GraphBuffer<U>, Self>
where
T: NoUninitDeviceRepr,
{
self.memory
.cast()
.map(GraphBuffer::from_memory)
.map_err(Self::from_memory)
}
/// Erases initialization while preserving graph-retained allocation ownership.
#[must_use]
pub fn into_uninit(self) -> GraphBuffer<MaybeUninit<T>> {
GraphBuffer::from_memory(self.memory.into_uninit())
}
}
impl<T: DeviceRepr> GraphBuffer<MaybeUninit<T>> {
/// Borrows the uninitialized buffer as a write-only destination of `T` values.
pub fn output(&mut self) -> DeviceOutputView<'_, T> {
DeviceOutputView::from_output(self)
}
/// Marks all values initialized after their producing work completes.
///
/// # Safety
///
/// Every element must be valid, and producing work must complete or be ordered before every consumer.
/// Recording a graph does not establish initialization; only its successful execution can do so.
#[must_use]
pub unsafe fn assume_init(self) -> GraphBuffer<T> {
// SAFETY: the caller guarantees every element is valid and its producing work is ordered before every consumer.
GraphBuffer::from_memory(unsafe { self.memory.assume_init() })
}
/// Exposes only a produced prefix while retaining the full graph allocation.
///
/// # Safety
///
/// Every prefix element must be valid, with producer completion and ordering satisfying `assume_init`.
///
/// # Errors
///
/// Returns an error if `length` exceeds the currently exposed element count.
pub unsafe fn assume_init_prefix(self, length: usize) -> Result<GraphBuffer<T>> {
// SAFETY: the caller guarantees the first `length` elements are valid and ordered as `assume_init` requires; the length itself is checked by the callee.
unsafe { self.memory.assume_init_prefix(length) }.map(GraphBuffer::from_memory)
}
}