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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use numa_gpu::runtime::allocator::{Allocator, MemType};
use numa_gpu::runtime::hw_info::NvidiaDriverInfo;
use numa_gpu::runtime::memory::{Mem, MemLock};
use numa_gpu::runtime::nvml::ThrottleReasons;
use numa_gpu::runtime::{cuda_wrapper, hw_info, linux_wrapper, numa};
#[cfg(not(target_arch = "aarch64"))]
use nvml_wrapper::{enum_wrappers::device::Clock, NVML};
#[cfg(target_arch = "aarch64")]
use numa_gpu::runtime::hw_info::CudaDeviceInfo;
use rustacuda::memory::DeviceBox;
use rustacuda::prelude::*;
use rustacuda::stream::{Stream, StreamFlags};
use rustacuda::{launch, CudaFlags};
use serde_derive::Serialize;
use std::convert::TryInto;
use std::ffi::CString;
use std::mem::size_of;
use std::ops::RangeInclusive;
use crate::types::*;
use crate::ArgPageType;
extern "C" {
pub fn cpu_stride(data: *const u32, iterations: u32) -> u64;
}
pub struct MemoryLatency;
impl MemoryLatency {
pub fn measure<W>(
device_id: DeviceId,
mem_type: MemType,
range: RangeInclusive<usize>,
stride: RangeInclusive<usize>,
repeat: u32,
writer: Option<&mut W>,
) where
W: std::io::Write,
{
if let (MemType::CudaDevMem, DeviceId::Cpu(_)) = (mem_type.clone(), &device_id) {
panic!("Cannot run benchmark on CPU with the given type of memory. Did you specify GPU device memory?");
}
let gpu_id = match device_id {
DeviceId::Gpu(id) => id,
_ => 0,
};
let (_context, device) = match rustacuda::init(CudaFlags::empty()) {
Ok(_) => {
let device = Device::get_device(gpu_id).expect("Couldn't set CUDA device");
let context = Context::create_and_push(
ContextFlags::MAP_HOST | ContextFlags::SCHED_AUTO,
device,
)
.expect("Couldn't create CUDA context");
(Some(context), Some(device))
}
Err(error) => {
eprintln!("Warning: {}", error);
(None, None)
}
};
numa::set_strict(true);
let buffer_bytes = *range.end() + 1;
let element_bytes = size_of::<u32>();
let buffer_len = buffer_bytes / element_bytes;
let hostname = hostname::get()
.expect("Couldn't get hostname")
.into_string()
.expect("Couldn't convert hostname into UTF-8 string");
let device_type = match device_id {
DeviceId::Cpu(_) => "CPU",
DeviceId::Gpu(_) => "GPU",
};
let cpu_node = match device_id {
DeviceId::Cpu(node) => Some(node),
_ => None,
};
let device_codename = match device_id {
DeviceId::Cpu(_) => Some(hw_info::cpu_codename().expect("Couldn't get CPU codename")),
DeviceId::Gpu(_) => device.map(|d| d.name().expect("Couldn't get device codename")),
};
let mem_type_description: MemTypeDescription = (&mem_type).into();
let template = DataPoint {
hostname: Some(hostname),
device_type: Some(device_type.to_string()),
device_codename,
cpu_node,
memory_node: mem_type_description.location,
memory_type: Some(mem_type_description.bare_mem_type),
page_type: Some(mem_type_description.page_type),
..Default::default()
};
let mnt = Measurement::new(range, stride, template);
let mut mem = Allocator::alloc_mem(mem_type, buffer_len);
mem.mlock().expect("Failed to mlock the memory");
if let Ok(slice) = (&mut mem).try_into() {
let _: &mut [_] = slice;
slice.iter_mut().by_ref().zip(0..).for_each(|(x, i)| *x = i);
}
let latencies = match device_id {
DeviceId::Cpu(did) => {
let ml = CpuMemoryLatency::new(did);
mnt.measure(
mem,
ml,
CpuMemoryLatency::prepare,
CpuMemoryLatency::run,
repeat,
)
}
DeviceId::Gpu(did) => {
let device = device.expect("No device found");
if let Ok(local_cpu_node) = device.numa_memory_affinity() {
linux_wrapper::numa_run_on_node(local_cpu_node).expect(&format!(
"Failed to bind main thread to CPU node {}",
local_cpu_node
));
} else {
eprintln!(
"Warning: Couldn't bind main thread to the CPU closest to GPU {}. This may
cause additional latency in measurements.",
did
);
}
let ml = GpuMemoryLatency::new(did);
let prepare = match mem {
Mem::CudaUniMem(_) => GpuMemoryLatency::prepare_prefetch,
_ => GpuMemoryLatency::prepare,
};
mnt.measure(mem, ml, prepare, GpuMemoryLatency::run, repeat)
}
};
if let Some(w) = writer {
let mut csv = csv::Writer::from_writer(w);
latencies
.iter()
.try_for_each(|row| csv.serialize(row))
.expect("Couldn't write serialized measurements")
}
}
}
#[derive(Clone, Debug, Default, Serialize)]
struct DataPoint {
pub hostname: Option<String>,
pub device_type: Option<String>,
pub device_codename: Option<String>,
pub cpu_node: Option<u16>,
pub memory_type: Option<BareMemType>,
pub memory_node: Option<u16>,
pub page_type: Option<ArgPageType>,
pub warm_up: bool,
pub range_bytes: usize,
pub stride_bytes: usize,
pub iterations: u32,
pub throttle_reasons: Option<String>,
pub clock_rate_mhz: Option<u32>,
pub cycles: u64,
pub ns: u64,
}
#[derive(Debug)]
struct Measurement {
stride: RangeInclusive<usize>,
range: RangeInclusive<usize>,
template: DataPoint,
}
#[derive(Debug)]
struct GpuMemoryLatency {
module: Module,
device_id: u32,
#[cfg(not(target_arch = "aarch64"))]
nvml: nvml_wrapper::NVML,
}
#[derive(Debug)]
struct CpuMemoryLatency;
#[derive(Debug)]
struct MeasurementParameters {
stride: usize,
iterations: u32,
}
impl Measurement {
fn new(
range: RangeInclusive<usize>,
stride: RangeInclusive<usize>,
template: DataPoint,
) -> Self {
Self {
stride,
range,
template,
}
}
fn measure<P, R, S>(
&self,
mut mem: Mem<u32>,
mut state: S,
prepare: P,
run: R,
repeat: u32,
) -> Vec<DataPoint>
where
P: Fn(&mut S, &mut Mem<u32>, &MeasurementParameters),
R: Fn(
&mut S,
&Mem<u32>,
&MeasurementParameters,
) -> (u32, Option<ThrottleReasons>, u64, u64),
{
let stride_iter = self.stride.clone();
let range_iter = self.range.clone();
let latencies = stride_iter
.filter(|stride| stride.is_power_of_two())
.flat_map(|stride| {
range_iter
.clone()
.filter(|range| range.is_power_of_two())
.zip(std::iter::repeat(stride))
.enumerate()
})
.flat_map(|(i, (range, stride))| {
let iterations = (range / stride) as u32;
let mut data_points: Vec<DataPoint> = Vec::with_capacity(repeat as usize + 1);
let mut warm_up = true;
let mp = MeasurementParameters { stride, iterations };
if i == 0 {
prepare(&mut state, &mut mem, &mp);
}
for _ in 0..repeat + 1 {
let (clock_rate_mhz, throttle_reasons, cycles, ns) = run(&mut state, &mem, &mp);
data_points.push(DataPoint {
warm_up,
range_bytes: range,
stride_bytes: stride,
iterations,
throttle_reasons: throttle_reasons.map(|r| r.to_string()),
clock_rate_mhz: Some(clock_rate_mhz),
cycles,
ns,
..self.template.clone()
});
warm_up = false;
}
data_points
})
.collect::<Vec<_>>();
latencies
}
}
impl GpuMemoryLatency {
#[cfg(not(target_arch = "aarch64"))]
fn new(device_id: u32) -> Self {
let module = Self::load_module();
let nvml = NVML::init().expect("Couldn't initialize NVML");
Self {
module,
device_id,
nvml,
}
}
#[cfg(target_arch = "aarch64")]
fn new(device_id: u32) -> Self {
let module = Self::load_module();
Self { module, device_id }
}
fn load_module() -> Module {
let module_path = CString::new(env!("CUDAUTILS_PATH"))
.expect("Failed to load CUDA module, check your CUDAUTILS_PATH");
let module = Module::load_from_file(&module_path).expect("Failed to load CUDA module");
module
}
fn prepare(_state: &mut Self, mem: &mut Mem<u32>, mp: &MeasurementParameters) {
let len = mem.len();
match mem.try_into() {
Ok(slice) => {
write_strides(slice, mp.stride);
}
Err((_, dev_slice)) => {
let mut host_mem = vec![0; len];
write_strides(&mut host_mem, mp.stride);
dev_slice
.copy_from(&host_mem)
.expect("Couldn't write strides data to device");
}
}
}
fn prepare_prefetch(_state: &mut Self, mem: &mut Mem<u32>, mp: &MeasurementParameters) {
let len = mem.len();
match mem.try_into() {
Ok(slice) => {
write_strides(slice, mp.stride);
}
Err((_, dev_slice)) => {
let mut host_mem = vec![0; len];
write_strides(&mut host_mem, mp.stride);
dev_slice
.copy_from(&host_mem)
.expect("Couldn't write strides data to device");
}
}
if let Mem::CudaUniMem(ref mut um) = mem {
let device_id = cuda_wrapper::current_device_id().expect("Couldn't get CUDA device id");
let stream =
Stream::new(StreamFlags::NON_BLOCKING, None).expect("Couldn't create CUDA stream");
cuda_wrapper::prefetch_async(um.as_unified_ptr(), um.len(), device_id, &stream)
.expect("Couldn't prefetch unified memory to device");
stream.synchronize().unwrap();
}
}
fn run(
_state: &mut Self,
mem: &Mem<u32>,
mp: &MeasurementParameters,
) -> (u32, Option<ThrottleReasons>, u64, u64) {
#[cfg(not(target_arch = "aarch64"))]
let clock_rate_mhz = _state
.nvml
.device_by_index(_state.device_id as u32)
.expect("Couldn't get NVML device")
.clock_info(Clock::SM)
.expect("Couldn't get clock rate with NVML");
#[cfg(target_arch = "aarch64")]
let clock_rate_mhz = CurrentContext::get_device()
.expect("Couldn't get CUDA device")
.clock_rate()
.expect("Couldn't get clock rate");
let mut dev_cycles = DeviceBox::new(&0_u64).expect("Couldn't allocate device memory");
let stream =
Stream::new(StreamFlags::NON_BLOCKING, None).expect("Failed to create CUDA stream");
unsafe {
let module = &_state.module;
launch!(module.gpu_stride<<<1, 1, 0, stream>>>(
mem.as_launchable_ptr(),
mp.iterations,
dev_cycles.as_device_ptr()
))
.expect("Failed to launch gpu_stride kernel");
};
stream
.synchronize()
.expect("Failed to synchronize CUDA stream");
#[cfg(not(target_arch = "aarch64"))]
let throttle_reasons: Option<ThrottleReasons> = Some(
_state
.nvml
.device_by_index(_state.device_id as u32)
.expect("Couldn't get NVML device")
.current_throttle_reasons()
.expect("Couldn't get current throttle reasons with NVML")
.into(),
);
#[cfg(target_arch = "aarch64")]
let throttle_reasons = None;
let mut cycles = 0;
dev_cycles
.copy_to(&mut cycles)
.expect("Couldn't copy result data from device");
let ns: u64 = cycles * 1000 / (clock_rate_mhz as u64);
(clock_rate_mhz, throttle_reasons, cycles, ns)
}
}
impl CpuMemoryLatency {
fn new(device_id: u16) -> Self {
numa::run_on_node(device_id).expect("Couldn't set NUMA node");
Self
}
fn run(
_state: &mut Self,
mem: &Mem<u32>,
mp: &MeasurementParameters,
) -> (u32, Option<ThrottleReasons>, u64, u64) {
let ns = if let Mem::CudaDevMem(_) = mem {
unreachable!();
} else {
unsafe { cpu_stride(mem.as_ptr(), mp.iterations) }
};
let cycles = 0;
let clock_rate_mhz = 0;
(clock_rate_mhz, None, cycles, ns)
}
fn prepare(_state: &mut Self, mem: &mut Mem<u32>, mp: &MeasurementParameters) {
if let Ok(slice) = mem.try_into() {
write_strides(slice, mp.stride);
} else {
unreachable!();
}
}
}
fn write_strides(data: &mut [u32], stride: usize) -> usize {
let element_bytes = size_of::<u32>();
let len = data.len();
let number_of_strides = data
.iter_mut()
.zip((stride / element_bytes)..)
.map(|(it, next)| *it = (next % len) as u32)
.count();
number_of_strides
}