Skip to content

Commit 99662f2

Browse files
committed
Improve the documentation of the compute shader example.
Also make it include `1` in the output sequence, because that is the first number according to https://oeis.org/A006877.
1 parent 19d23e5 commit 99662f2

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

examples/runners/wgpu/src/compute.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//! This crate contains code to print the integer sequence at <https://oeis.org/A006877>:
2+
//!
3+
//! > In the '3x+1' problem, these values for the starting value set new records for number of
4+
//! > steps to reach 1.
5+
//!
6+
//! each starting value is paired with the number of steps to reach 1 (a.k.a the "Collatz
7+
//! sequence length").
8+
//!
9+
//! The individual Collatz sequence lengths are computed via a computer shader on the GPU.
10+
111
use crate::{CompiledShaderModules, Options, maybe_watch};
212

313
use std::time::Duration;
@@ -252,14 +262,16 @@ async fn start_internal(options: &Options, compiled_shader_modules: CompiledShad
252262
drop(data);
253263
readback_buffer.unmap();
254264

265+
// Our `max` computation excludes `1`, so print that manually as the first number in the
266+
// sequence.
267+
println!("1: 0");
255268
let mut max = 0;
256269
for (src, out) in src_range.zip(result.iter().copied()) {
257270
if out == u32::MAX {
258271
println!("{src}: overflowed");
259272
break;
260273
} else if out > max {
261274
max = out;
262-
// Should produce <https://oeis.org/A006877>
263275
println!("{src}: {out}");
264276
}
265277
}

examples/shaders/compute-shader/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ use spirv_std::{glam, spirv};
77

88
// Adapted from the wgpu hello-compute example
99

10+
/// Returns the length of the Collatz sequence (excluding the starting number) for `n`. Returns
11+
/// `None` if (a) `n` is zero, or (b) a number in the sequence overflows a `u32`.
12+
///
13+
/// # Examples
14+
///
15+
/// The sequence for 3 (excluding the starting number) is `[10, 5, 16, 8, 4, 2, 1]`, which has
16+
/// length 7.
17+
/// ```
18+
/// # use compute_shader::collatz;
19+
/// assert_eq!(collatz(3), Some(7));
20+
/// ```
1021
pub fn collatz(mut n: u32) -> Option<u32> {
1122
let mut i = 0;
1223
if n == 0 {
@@ -20,7 +31,8 @@ pub fn collatz(mut n: u32) -> Option<u32> {
2031
if n >= 0x5555_5555 {
2132
return None;
2233
}
23-
// TODO: Use this instead when/if checked add/mul can work: n.checked_mul(3)?.checked_add(1)?
34+
// TODO: Use this instead when/if checked add/mul can work:
35+
// n.checked_mul(3)?.checked_add(1)?
2436
3 * n + 1
2537
};
2638
i += 1;

examples/shaders/compute-shader/src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Code for running the Collatz problem on the CPU. See the wgpu compute runner
2+
//! for the equivalent GPU code.
3+
//!
4+
//! Currently it's not actually built or tested, and is just here for reference.
5+
16
use std::time::Instant;
27

38
use compute_shader::collatz;
@@ -13,15 +18,15 @@ fn main() {
1318
.map(collatz)
1419
.collect::<Vec<_>>();
1520
let took = start.elapsed();
21+
println!("1: 0");
1622
let mut max = 0;
1723
for (src, out) in src_range.zip(result.iter().copied()) {
1824
match out {
1925
Some(out) if out > max => {
2026
max = out;
21-
// Should produce <https://oeis.org/A006877>
2227
println!("{src}: {out}");
2328
}
24-
Some(_) => (),
29+
Some(_) => {}
2530
None => {
2631
println!("{src}: overflowed");
2732
break;

0 commit comments

Comments
 (0)