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
//! Peer-to-peer (P2P) memory copy operations for multi-GPU workloads.
//!
//! This module provides functions to check, enable, and disable peer access
//! between CUDA devices, as well as copy data between device buffers on
//! different GPUs.
//!
//! Peer access enables direct GPU-to-GPU memory transfers over PCIe or
//! NVLink without staging through host memory, significantly improving
//! transfer bandwidth in multi-GPU configurations.
//!
//! # Platform note
//!
//! The underlying `cuDeviceCanAccessPeer`, `cuCtxEnablePeerAccess`,
//! `cuCtxDisablePeerAccess`, and `cuMemcpyPeer` driver functions are not
//! yet loaded by `oxicuda-driver`. All functions currently return
//! [`CudaError::NotSupported`] as placeholders. The API surface is
//! established here so that downstream crates can program against it.
//!
//! # Example
//!
//! ```rust,no_run
//! use oxicuda_driver::device::Device;
//! use oxicuda_memory::peer_copy;
//!
//! oxicuda_driver::init()?;
//! let dev0 = Device::get(0)?;
//! let dev1 = Device::get(1)?;
//!
//! if peer_copy::can_access_peer(&dev0, &dev1)? {
//! peer_copy::enable_peer_access(&dev0, &dev1)?;
//! // Now D2D copies between dev0 and dev1 can go over NVLink/PCIe
//! // peer_copy::copy_peer(&mut dst_buf, &dev1, &src_buf, &dev0)?;
//! }
//! # Ok::<(), oxicuda_driver::error::CudaError>(())
//! ```
use Device;
use ;
use Stream;
use crateDeviceBuffer;
/// Checks whether `device` can directly access memory on `peer`.
///
/// Returns `true` if peer access is supported between the two devices
/// (e.g., over NVLink or PCIe). Returns `false` if the devices are the
/// same or if the hardware topology does not support peer access.
///
/// # Errors
///
/// Currently returns [`CudaError::NotSupported`] because the underlying
/// driver function pointer (`cuDeviceCanAccessPeer`) is not yet loaded.
/// Enables peer access from the current context's device to `peer`.
///
/// After enabling, memory on `peer` can be directly accessed from
/// kernels and copy operations in the current context.
///
/// # Errors
///
/// * [`CudaError::PeerAccessAlreadyEnabled`] if peer access is already
/// enabled.
/// * [`CudaError::PeerAccessUnsupported`] if the hardware does not
/// support peer access.
/// * [`CudaError::NotSupported`] (current stub).
/// Disables peer access from the current context's device to `peer`.
///
/// # Errors
///
/// * [`CudaError::PeerAccessNotEnabled`] if peer access was not enabled.
/// * [`CudaError::NotSupported`] (current stub).
/// Copies data between device buffers on different GPUs (synchronous).
///
/// Both buffers must have the same length. Peer access should be enabled
/// between the source and destination devices before calling this function.
///
/// # Errors
///
/// * [`CudaError::InvalidValue`] if buffer lengths do not match.
/// * [`CudaError::PeerAccessNotEnabled`] if peer access has not been
/// enabled.
/// * [`CudaError::NotSupported`] (current stub).
/// Copies data between device buffers on different GPUs (asynchronous).
///
/// The copy is enqueued on `stream` and may not be complete when this
/// function returns. Both buffers must have the same length.
///
/// # Errors
///
/// * [`CudaError::InvalidValue`] if buffer lengths do not match.
/// * [`CudaError::NotSupported`] (current stub).
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------