Skip to content

Commit 0666dac

Browse files
authored
Make backends return errors (#38)
Annoyingly, Results don't cover all errors and we need this hacky error handler dance. See gfx-rs/wgpu#3767.
1 parent 84a238e commit 0666dac

File tree

10 files changed

+325
-133
lines changed

10 files changed

+325
-133
lines changed

blog/2024-11-21-optimizing-matrix-mul/code/Cargo.lock

+26-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blog/2024-11-21-optimizing-matrix-mul/code/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ spirv-std = { git = "https://github.com/rust-gpu/rust-gpu", rev = "0da80f8a61867
4848
futures = "0.3"
4949
glam = { version = "0.29.2", features = ["cuda", "bytemuck"] }
5050
tracing = "0.1.40"
51+
wgpu = { version = "23.0", features = ["spirv", "vulkan-portability"] }
5152

5253
# Enable incremental by default in release mode.
5354
[profile.release]

blog/2024-11-21-optimizing-matrix-mul/code/benches/gpu_bench.rs

+7-23
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ const SIZES: &[(u32, u32, u32)] = &[
2929
(64, 32, 128), // A: 64x32, B: 32x128, Result: 64x128
3030
(1024, 512, 2048), // A: 1024x512, B: 512x2048, Result: 1024x2048
3131
(2048, 1024, 4096), // A: 2048x1024, B: 1024x4096, Result: 2048x4096
32+
*/
3233
];
3334

3435
fn bench_all_variants(c: &mut Criterion) {
3536
// Initialize all variants outside the loop
36-
let multiplier_naive = matmul::naive::wgpu();
37-
let multiplier_workgroup_256 = matmul::workgroup_256::wgpu();
38-
let multiplier_workgroup_2d = matmul::workgroup_2d::wgpu();
39-
let multiplier_tiling_1d = matmul::tiling_1d::wgpu();
40-
let multiplier_tiling_1d_loop = matmul::tiling_1d_loop::wgpu();
41-
let multiplier_tiling_2d = matmul::tiling_2d::wgpu();
42-
let multiplier_isomorphic_gpu = matmul::isomorphic::wgpu();
37+
let multiplier_naive = matmul::naive::wgpu().unwrap();
38+
let multiplier_workgroup_256 = matmul::workgroup_256::wgpu().unwrap();
39+
let multiplier_workgroup_2d = matmul::workgroup_2d::wgpu().unwrap();
40+
let multiplier_tiling_1d = matmul::tiling_1d::wgpu().unwrap();
41+
let multiplier_tiling_1d_loop = matmul::tiling_1d_loop::wgpu().unwrap();
42+
let multiplier_tiling_2d = matmul::tiling_2d::wgpu().unwrap();
4343

4444
for &(m, k, n) in SIZES {
4545
// Calculate FLOPs for this size
@@ -134,22 +134,6 @@ fn bench_all_variants(c: &mut Criterion) {
134134
});
135135
},
136136
);
137-
138-
group.bench_with_input(
139-
BenchmarkId::new("isomorphic:wgpu", format!("{}x{}x{}", m, k, n)),
140-
&(m, k, n),
141-
|bench, &(m, k, n)| {
142-
bench.iter(|| {
143-
black_box(multiplier_isomorphic_gpu.multiply(
144-
black_box(&a),
145-
black_box(&b),
146-
m,
147-
k,
148-
n,
149-
))
150-
});
151-
},
152-
);
153137
}
154138
}
155139

blog/2024-11-21-optimizing-matrix-mul/code/benches/isomorphic_bench.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rand::Rng;
66
use std::time::Duration;
77

88
const WARMUP_TIME: Duration = Duration::from_secs(2);
9-
const MEASUREMENT_TIME: Duration = Duration::from_secs(5 * 60);
109
const SAMPLE_SIZE: usize = 10;
1110

1211
/// Matrix sizes to benchmark
@@ -34,19 +33,18 @@ const SIZES: &[(u32, u32, u32)] = &[
3433

3534
fn bench_isomorphic_variants(c: &mut Criterion) {
3635
// Initialize isomorphic variants
37-
let multiplier_isomorphic_gpu = matmul::isomorphic::wgpu();
38-
let multiplier_isomorphic_cpu_single = matmul::isomorphic::cpu::single_threaded();
39-
let multiplier_isomorphic_cpu_multi = matmul::isomorphic::cpu::multi_threaded();
36+
let multiplier_isomorphic_gpu = matmul::isomorphic::wgpu().unwrap();
37+
let multiplier_isomorphic_cpu_single = matmul::isomorphic::cpu::single_threaded().unwrap();
38+
let multiplier_isomorphic_cpu_multi = matmul::isomorphic::cpu::multi_threaded().unwrap();
4039

4140
for &(m, k, n) in SIZES {
42-
// Calculate FLOPs for this size
43-
let flops = 2.0 * (m as f64 * n as f64 * k as f64);
44-
45-
let mut group = c.benchmark_group(format!("isomorphic_matmul{}x{}x{}", m, k, n));
41+
let mut group = c.benchmark_group("isomorphic");
4642
group.sampling_mode(SamplingMode::Flat);
4743
group.warm_up_time(WARMUP_TIME);
48-
//group.measurement_time(MEASUREMENT_TIME);
4944
group.sample_size(SAMPLE_SIZE);
45+
46+
// Calculate FLOPs for this size
47+
let flops = 2.0 * (m as f64 * n as f64 * k as f64);
5048
group.throughput(Throughput::Elements(flops as u64));
5149

5250
// Create matrices for the given size

blog/2024-11-21-optimizing-matrix-mul/code/bin/blog/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ path = "src/bin.rs"
1010
[dependencies]
1111
matmul = { path = "../../crates/cpu/matmul" }
1212
settings = { path = "../../crates/shared/settings" }
13+
wgpu.workspace = true
1314
futures.workspace = true
1415
tracing.workspace = true
1516
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "std"] }

0 commit comments

Comments
 (0)