Skip to content

Commit 9d40b8a

Browse files
Add batch memcpy benchmark
1 parent 8268b30 commit 9d40b8a

2 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎sycl/sycl-rs/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pin-project = "1.1.13"
2929
thiserror = "2.0.18"
3030

3131
[dev-dependencies]
32-
criterion = "0.7.0"
32+
criterion = { version = "0.7.0", features = ["html_reports"] }
3333
half = { version = "2.7.1", features = ["bytemuck"] }
3434
tokio = { version = "1.52.3", features = ["macros", "rt", "rt-multi-thread"] }
3535

‎sycl/sycl-rs/benches/batch_memcpy.rs‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,85 @@
1-
use criterion::{criterion_group, criterion_main, Criterion};
1+
use std::{fmt::Display, time::Duration};
2+
3+
use bytemuck::Pod;
4+
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
5+
use sycl_rs::{prelude::*, usm::UsmAlloc};
26

37
fn criterion_benchmark(c: &mut Criterion) {
4-
c.bench_function("test", |b| b.iter(|| 2 + 3));
8+
memcpy_benchmark(c, 10).unwrap();
9+
}
10+
11+
fn memcpy_benchmark(c: &mut Criterion, test_count: u32) -> sycl_rs::Result<()> {
12+
static KB: usize = 1024;
13+
14+
let used_memory = 4_usize.pow(test_count) * KB;
15+
16+
let mut group = c.benchmark_group("memcpy_test");
17+
group.measurement_time(Duration::from_secs(30));
18+
19+
for i in 1..test_count {
20+
let buf_count = 4_usize.pow(i);
21+
let buf_size = used_memory / buf_count;
22+
let copy_count = buf_count / 2;
23+
24+
let mut queue = Queue::new();
25+
let mut host_buffers = Vec::<HostUsmBox<u32>>::new();
26+
let mut device_buffers = Vec::<DeviceUsmBox<u32>>::new();
27+
28+
for _ in 1..copy_count {
29+
let host_buffer = unsafe { queue.alloc_uninit_host::<u32>(buf_size) };
30+
let device_buffer = unsafe { queue.alloc_uninit_device::<u32>(buf_size) };
31+
32+
host_buffers.push(host_buffer);
33+
device_buffers.push(device_buffer);
34+
}
35+
36+
queue.wait()?;
37+
38+
group.bench_function(
39+
BenchmarkId::new(
40+
"h2d copy",
41+
BenchmarkParams {
42+
copy_count,
43+
buf_size,
44+
},
45+
),
46+
|b| {
47+
b.iter(|| batch_mempcy(&mut queue, &host_buffers, &mut device_buffers));
48+
},
49+
);
50+
}
51+
52+
group.finish();
53+
54+
Ok(())
55+
}
56+
57+
fn batch_mempcy<T, A1, A2>(
58+
queue: &mut Queue,
59+
sources: &[UsmBox<T, A1>],
60+
destinations: &mut [UsmBox<T, A2>],
61+
) -> sycl_rs::Result<()>
62+
where
63+
T: Pod,
64+
A1: UsmAlloc,
65+
A2: UsmAlloc,
66+
{
67+
for (src, dst) in sources.iter().zip(destinations) {
68+
queue.copy(src, dst)?;
69+
}
70+
71+
queue.wait()
72+
}
73+
74+
struct BenchmarkParams {
75+
copy_count: usize,
76+
buf_size: usize,
77+
}
78+
79+
impl Display for BenchmarkParams {
80+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81+
write!(f, "{} copies of size {}", self.copy_count, self.buf_size)
82+
}
583
}
684

785
criterion_group!(benches, criterion_benchmark);

0 commit comments

Comments
 (0)