mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
[rs] Add overflow detection to hello-compute
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
use std::{borrow::Cow, convert::TryInto, str::FromStr};
|
use std::{borrow::Cow, convert::TryInto, str::FromStr};
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
|
|
||||||
|
// Indicates a u32 overflow in an intermediate Collatz value
|
||||||
|
const OVERFLOW: u32 = 0xffffffff;
|
||||||
|
|
||||||
async fn run() {
|
async fn run() {
|
||||||
let numbers = if std::env::args().len() <= 1 {
|
let numbers = if std::env::args().len() <= 1 {
|
||||||
let default = vec![1, 2, 3, 4];
|
let default = vec![1, 2, 3, 4];
|
||||||
@@ -13,10 +16,19 @@ async fn run() {
|
|||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
|
|
||||||
let times = execute_gpu(numbers).await;
|
let steps = execute_gpu(numbers).await;
|
||||||
println!("Times: {:?}", times);
|
|
||||||
|
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")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
log::info!("Times: {:?}", times);
|
log::info!("Steps: [{}]", disp_steps.join(", "));
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
|
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]));
|
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]
|
#[test]
|
||||||
fn test_multithreaded_compute() {
|
fn test_multithreaded_compute() {
|
||||||
use std::{sync::mpsc, thread, time::Duration};
|
use std::{sync::mpsc, thread, time::Duration};
|
||||||
|
|||||||
@@ -23,6 +23,11 @@ fn collatz_iterations(n_base: u32) -> u32{
|
|||||||
n = n / 2u;
|
n = n / 2u;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
// Overflow? (i.e. 3*n + 1 > 0xffffffffu?)
|
||||||
|
if (n >= 1431655765u) { // 0x55555555u
|
||||||
|
return 4294967295u; // 0xffffffffu
|
||||||
|
}
|
||||||
|
|
||||||
n = 3u * n + 1u;
|
n = 3u * n + 1u;
|
||||||
}
|
}
|
||||||
i = i + 1u;
|
i = i + 1u;
|
||||||
|
|||||||
Reference in New Issue
Block a user