184: added test to hello-compute example r=kvark a=emmetthebarnraiser

I wasn't exactly sure of the proper way to test async stuff, but this is a strategy that seemed to work for me. Let me know if there is anything you would like me to change, or if this isn't something that you would like to add to the examples!

Co-authored-by: Brian Frazho <befrazho@gmail.com>
This commit is contained in:
bors[bot]andBrian Frazho authored and GitHub committed 2020-03-01 02:43:59 +00:00
commit e09b8d58a7
1 file changed
+29 -5
+29 -5
View File
@@ -13,6 +13,10 @@ async fn run() {
.map(|s| u32::from_str(&s).expect("You must pass a list of positive integers!"))
.collect();
println!("Times: {:?}", execute_gpu(numbers).await);
}
async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
let slice_size = numbers.len() * std::mem::size_of::<u32>();
let size = slice_size as wgpu::BufferAddress;
@@ -92,18 +96,38 @@ async fn run() {
encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size);
queue.submit(&[encoder.finish()]);
if let Ok(mapping) = staging_buffer.map_read(0u64, size).await {
let times : Box<[u32]> = mapping
mapping
.as_slice()
.chunks_exact(4)
.map(|b| u32::from_ne_bytes(b.try_into().unwrap()))
.collect();
println!("Times: {:?}", times);
.collect()
} else {
panic!("failed to run compute on gpu!")
}
}
fn main() {
futures::executor::block_on(run());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compute_1(){
let input = vec!(1, 2, 3, 4);
futures::executor::block_on(assert_execute_gpu(input, vec!(0, 1, 7, 2)));
}
#[test]
fn test_compute_2(){
let input = vec!(5, 23, 10, 9);
futures::executor::block_on(assert_execute_gpu(input, vec!(5, 15, 6, 19)));
}
async fn assert_execute_gpu(input: Vec<u32>, expected: Vec<u32>){
assert_eq!(execute_gpu(input).await, expected);
}
}