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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
//! CUDA device memory allocation and transfer operations.
//!
//! Provides both **stream-ordered** (`*_async`) and **synchronous** (`*_sync`)
//! variants. The async functions enqueue the operation on a stream and return
//! immediately; the transfer completes in stream order. The sync variants block
//! the calling thread until the operation finishes.
//!
//! All functions in this module are `unsafe` because they operate on raw device
//! pointers whose validity cannot be checked at compile time.
use crate;
use CUdeviceptr;
use c_void;
use MaybeUninit;
/// Allocates `num_bytes` of device memory on `stream` using the stream-ordered
/// allocator (`cuMemAllocAsync`).
///
/// The returned pointer is usable by any kernel or memcpy enqueued on `stream`
/// after this call. Pair with [`free_async`] on a stream ordered after this
/// allocation and every use of the pointer.
///
/// # Safety
///
/// - A CUDA context must be bound to the calling thread.
/// - `stream` must be a valid `CUstream` from the current context.
/// - `num_bytes` must not exceed the device memory pool limits.
pub unsafe
/// Frees device memory previously allocated with [`malloc_async`] or
/// [`malloc_sync`].
///
/// The free is enqueued on `stream` and completes in stream order. The pointer
/// must not be accessed by any work enqueued after this call. If the pointer
/// came from [`malloc_async`], `stream` must be ordered after its allocation
/// stream. Every other stream using the pointer must also be ordered before
/// `stream`.
///
/// # Safety
///
/// - `dptr` must have been returned by [`malloc_async`] or [`malloc_sync`] and
/// not yet freed.
/// - `stream` must be a valid `CUstream` from the same context as the
/// allocation.
/// - All allocation and use work on other streams must happen before the free
/// on `stream`.
pub unsafe
/// Allocates `num_bytes` of device memory synchronously (`cuMemAlloc`).
///
/// Blocks the calling thread until the allocation completes. Pair with
/// [`free_sync`].
///
/// # Safety
///
/// - A CUDA context must be bound to the calling thread.
/// - `num_bytes` must not exceed available device memory.
pub unsafe
/// Frees device memory previously allocated with [`malloc_sync`].
///
/// Blocks the calling thread. All pending GPU work referencing `dptr` must have
/// completed before this call.
///
/// # Safety
///
/// - `dptr` must have been returned by [`malloc_sync`] and not yet freed.
/// - No in-flight GPU operations may reference `dptr`.
pub unsafe
/// Copies `num_bytes` from host memory at `src` to device memory at `dst`,
/// enqueued on `stream` (host-to-device, async).
///
/// The host buffer at `src` must remain valid and unmodified until the copy
/// completes (i.e., until a synchronization point on `stream`). For
/// guaranteed asynchronous behavior, `src` should point to page-locked
/// (pinned) host memory.
///
/// # Safety
///
/// - `dst` must be a valid device pointer with at least `num_bytes` allocated.
/// - `src` must point to at least `num_bytes` of readable host memory.
/// - `stream` must be a valid `CUstream` from the current context.
pub unsafe
/// Copies `num_bytes` from host memory at `src` to device memory at `dst`,
/// synchronously (host-to-device).
///
/// Blocks the calling thread until the copy is complete.
///
/// # Safety
///
/// - `dst` must be a valid device pointer with at least `num_bytes` allocated.
/// - `src` must point to at least `num_bytes` of readable host memory.
/// - The active CUDA context must own `dst` (caller binds the context).
pub unsafe
/// Copies `num_bytes` from device memory at `src` to host memory at `dst`,
/// enqueued on `stream` (device-to-host, async).
///
/// The host buffer at `dst` must not be read until the copy completes.
/// For guaranteed asynchronous behavior, `dst` should point to page-locked
/// (pinned) host memory.
///
/// # Safety
///
/// - `src` must be a valid device pointer with at least `num_bytes` accessible.
/// - `dst` must point to at least `num_bytes` of writable host memory.
/// - `stream` must be a valid `CUstream` from the current context.
pub unsafe
/// Copies `num_bytes` from device memory at `src` to device memory at `dst`,
/// enqueued on `stream` (device-to-device, async).
///
/// `src` and `dst` may reside on different devices if peer access is enabled.
///
/// # Safety
///
/// - Both `dst` and `src` must be valid device pointers with at least
/// `num_bytes` accessible.
/// - `stream` must be a valid `CUstream` from the current context.
/// - `dst` and `src` must not overlap unless they are identical.
pub unsafe
/// Sets `num_bytes` of device memory at `dptr` to `value`, enqueued on
/// `stream`.
///
/// Each byte in the range `[dptr, dptr + num_bytes)` is set to `value`.
///
/// # Safety
///
/// - `dptr` must be a valid device pointer with at least `num_bytes` allocated.
/// - `stream` must be a valid `CUstream` from the current context.
pub unsafe
/// Allocates `num_bytes` of page-locked host memory.
///
/// Pinned host memory can be used as a staging area for CUDA transfers that
/// need higher bandwidth, and is required for host-device copies that are
/// intended to overlap with GPU work. Pair with [`free_host`].
///
/// # Safety
///
/// - A CUDA context must be bound to the calling thread.
/// - `num_bytes` must not exceed the host memory available for page-locked
/// allocations. Passing zero bytes is not useful and the CUDA driver reports
/// it as an error.
pub unsafe
/// Frees page-locked host memory previously allocated with [`malloc_host`].
///
/// # Safety
///
/// - `ptr` must have been returned by [`malloc_host`] and not yet freed.
/// - No in-flight CUDA transfer or kernel may reference `ptr`.
pub unsafe