Parallelism and SIMD boundaries
Quick answer
Parallel threads divide work among execution contexts. SIMD processes multiple data lanes in one instruction. Both can help suitable workloads, but overhead and hardware support determine the benefit.
Correctness across all input sizes
Choose independent partitions and combine partial results with an appropriate reduction. For vectorized code, handle the remainder when length is not divisible by the lane count. chunks_exact is a portable way to model this structure, not proof that the compiler emits SIMD. Architecture-specific intrinsics need target support and correct unsafe preconditions; use runtime detection and a scalar fallback where appropriate. Floating-point reductions can change rounding order.
fn sum(xs: &[u8]) -> u64 { let chunks = xs.chunks_exact(4); let tail: u64 = chunks.remainder().iter().map(|&x| u64::from(x)).sum(); chunks.flatten().map(|&x| u64::from(x)).sum::<u64>() + tail}fn main() { println!("{} {}", sum(&[1, 2, 3, 4, 5]), sum(&[])); }Expected stdout (not executed):
15 0The remainder preserves the fifth element.
fn main() { let data = [1u64, 2, 3, 4, 5]; let (a, b) = data.split_at(2); let sum = std::thread::scope(|s| { let left = s.spawn(|| a.iter().sum::<u64>()); let right = b.iter().sum::<u64>(); left.join().unwrap() + right }); println!("{sum}");}Expected stdout (not executed):
15The scope ensures borrowed slices remain valid; small inputs may be slower with threads.
Common mistake
Never execute an architecture-specific intrinsic on a CPU without its required features.
Try it yourself
Test lengths 0 through 9 and compare against a scalar sum before benchmarking.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Parallelism and SIMD boundaries — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
An optimized SIMD implementation uses a target-specific intrinsic. What is needed on unsupported CPUs?
Parallelism and SIMD boundaries — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn sum(xs: &[u8]) -> u64 { let chunks = xs.chunks_exact(4); let tail: u64 = chunks.remainder().iter().map(|&x| u64::from(x)).sum(); chunks.flatten().map(|&x| u64::from(x)).sum::<u64>() + tail } fn main() { println!("{} {}", sum(&[1, 2, 3, 4, 5]), sum(&[])); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Parallelism and SIMD boundaries — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust fn main() { let data = [1u64, 2, 3, 4, 5]; let (a, b) = data.split_at(2); let sum = std::thread::scope(|s| { let left = s.spawn(|| a.iter().sum::<u64>()); let right = b.iter().sum::<u64>(); left.join().unwrap() + right }); println!("{sum}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.