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
use crate::error::{Error, ErrorKind, Result, ToResult};
use crate::runtime::memory::LaunchableMutSlice;
use cuda_driver_sys::{
cuCtxGetDevice, cuMemAdvise, cuMemGetInfo_v2, cuMemHostRegister_v2, cuMemHostUnregister,
cuMemPrefetchAsync, cuMemcpyAsync, cuMemsetD32Async, CUdevice, CUstream,
CU_MEMHOSTREGISTER_DEVICEMAP, CU_MEMHOSTREGISTER_PORTABLE,
};
use rustacuda::memory::{DeviceCopy, UnifiedPointer};
use rustacuda::stream::Stream;
use std::mem::{size_of, transmute_copy, zeroed};
use std::os::raw::{c_uint, c_void};
pub use cuda_driver_sys::CUmem_advise_enum as MemAdviseFlags;
pub struct CudaMemInfo {
pub free: usize,
pub total: usize,
}
pub fn mem_info() -> Result<CudaMemInfo> {
let mut free: usize = 0;
let mut total: usize = 0;
unsafe {
cuMemGetInfo_v2(&mut free, &mut total)
.to_result()
.map_err(|e| {
Error::with_chain::<Error, _>(e.into(), "Failed to get memory information")
})?;
}
Ok(CudaMemInfo { free, total })
}
pub unsafe fn host_register<T>(mem: &[T]) -> Result<()> {
cuMemHostRegister_v2(
mem.as_ptr() as *mut c_void,
mem.len() * size_of::<T>(),
CU_MEMHOSTREGISTER_PORTABLE | CU_MEMHOSTREGISTER_DEVICEMAP,
)
.to_result()
.map_err(|e| Error::with_chain::<Error, _>(e.into(), "Failed to dynamically pin memory"))
}
pub unsafe fn host_unregister<T>(mem: &[T]) -> Result<()> {
cuMemHostUnregister(mem.as_ptr() as *mut c_void)
.to_result()
.map_err(|e| {
Error::with_chain::<Error, _>(
e.into(),
"Failed to unregister dynamically pinned memory",
)
})
}
pub const CPU_DEVICE_ID: CUdevice = -1;
pub fn current_device_id() -> Result<CUdevice> {
unsafe {
let mut cu_device: CUdevice = zeroed();
cuCtxGetDevice(&mut cu_device).to_result().map_err(|e| {
Error::with_chain::<Error, _>(e.into(), "Failed to get current CUDA device ID")
})?;
Ok(cu_device)
}
}
pub fn prefetch_async<T: DeviceCopy>(
mem: UnifiedPointer<T>,
len: usize,
destination_device: CUdevice,
stream: &Stream,
) -> Result<()> {
unsafe {
let cu_stream = transmute_copy::<Stream, CUstream>(stream);
cuMemPrefetchAsync(
mem.as_raw() as *const c_void as u64,
len * size_of::<T>(),
destination_device,
cu_stream,
)
.to_result()
.map_err(|e| {
Error::with_chain::<Error, _>(
e.into(),
format!(
"Failed to prefetch unified memory to device {}",
destination_device
),
)
})?;
}
Ok(())
}
pub fn mem_advise<T: DeviceCopy>(
mem: UnifiedPointer<T>,
len: usize,
advice: MemAdviseFlags,
device: CUdevice,
) -> Result<()> {
unsafe {
cuMemAdvise(
mem.as_raw() as *const c_void as u64,
len * size_of::<T>(),
advice,
device,
)
.to_result()
.map_err(|e| {
Error::with_chain::<Error, _>(e.into(), format!("Failed to advise memory location"))
})?;
}
Ok(())
}
pub fn async_copy<T: DeviceCopy>(dst: &mut [T], src: &[T], stream: &Stream) -> Result<()> {
assert!(
src.len() == dst.len(),
"Source and destination slices have different lengths"
);
unsafe {
let cu_stream = transmute_copy::<Stream, CUstream>(stream);
let bytes = size_of::<T>() * src.len();
if bytes != 0 {
cuMemcpyAsync(
dst.as_mut_ptr() as u64,
src.as_ptr() as u64,
bytes,
cu_stream,
)
.to_result()?
}
}
Ok(())
}
pub fn memset_async<T: DeviceCopy>(
mem: LaunchableMutSlice<T>,
value: i32,
stream: &Stream,
) -> Result<()> {
assert!(
size_of::<T>() >= size_of::<i32>(),
"Size of type T must be larger than i32"
);
assert!(
size_of::<T>() % size_of::<i32>() == 0,
"Size of type T must be divisible by i32"
);
unsafe {
let cu_stream = transmute_copy::<Stream, CUstream>(&stream);
cuMemsetD32Async(
mem.as_ptr() as u64,
value as c_uint,
mem.len()
.checked_mul(size_of::<T>() / size_of::<c_uint>())
.ok_or_else(|| {
ErrorKind::IntegerOverflow("Failed to compute memset length".to_string())
})?,
cu_stream,
)
.to_result()
.map_err(|e| {
Error::with_chain::<Error, _>(e.into(), format!("Failed to schedule memset"))
})?;
}
Ok(())
}