mirror of
https://github.com/gfx-rs/wgpu.git
synced 2025-02-17 01:12:41 +00:00
[rs] Add overflow detection to hello-compute
This commit is contained in:
parent
d18cc67131
commit
985c1feb4e
@ -1,6 +1,9 @@
|
||||
use std::{borrow::Cow, convert::TryInto, str::FromStr};
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
// Indicates a u32 overflow in an intermediate Collatz value
|
||||
const OVERFLOW: u32 = 0xffffffff;
|
||||
|
||||
async fn run() {
|
||||
let numbers = if std::env::args().len() <= 1 {
|
||||
let default = vec![1, 2, 3, 4];
|
||||
@ -13,10 +16,19 @@ async fn run() {
|
||||
.collect()
|
||||
};
|
||||
|
||||
let times = execute_gpu(numbers).await;
|
||||
println!("Times: {:?}", times);
|
||||
let steps = execute_gpu(numbers).await;
|
||||
|
||||
let disp_steps: Vec<String> = steps
|
||||
.iter()
|
||||
.map(|&n| match n {
|
||||
OVERFLOW => "OVERFLOW".to_string(),
|
||||
_ => n.to_string(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
println!("Steps: [{}]", disp_steps.join(", "));
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
log::info!("Times: {:?}", times);
|
||||
log::info!("Steps: [{}]", disp_steps.join(", "));
|
||||
}
|
||||
|
||||
async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
|
||||
@ -194,6 +206,15 @@ mod tests {
|
||||
pollster::block_on(assert_execute_gpu(input, vec![5, 15, 6, 19]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compute_overflow() {
|
||||
let input = vec![77031, 837799, 8400511, 63728127];
|
||||
pollster::block_on(assert_execute_gpu(
|
||||
input,
|
||||
vec![350, 524, OVERFLOW, OVERFLOW],
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multithreaded_compute() {
|
||||
use std::{sync::mpsc, thread, time::Duration};
|
||||
|
@ -23,6 +23,11 @@ fn collatz_iterations(n_base: u32) -> u32{
|
||||
n = n / 2u;
|
||||
}
|
||||
else {
|
||||
// Overflow? (i.e. 3*n + 1 > 0xffffffffu?)
|
||||
if (n >= 1431655765u) { // 0x55555555u
|
||||
return 4294967295u; // 0xffffffffu
|
||||
}
|
||||
|
||||
n = 3u * n + 1u;
|
||||
}
|
||||
i = i + 1u;
|
||||
|
Loading…
Reference in New Issue
Block a user