From c02e5427d4bc1c43d115992e97a78545c341dc05 Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 21 Aug 2022 20:46:57 -0700 Subject: [PATCH] spirv tests: port to R32 this gives better debug info --- crates/coremem/src/sim/spirv/mod.rs | 107 +++++++++++++++------------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/crates/coremem/src/sim/spirv/mod.rs b/crates/coremem/src/sim/spirv/mod.rs index cc1c44e..f07aeb7 100644 --- a/crates/coremem/src/sim/spirv/mod.rs +++ b/crates/coremem/src/sim/spirv/mod.rs @@ -275,24 +275,27 @@ mod test { use crate::real::{R32, ToFloat as _}; use crate::sim::legacy::{self, SimState}; use crate::mat::{self, AnisomorphicConductor}; - fn mean_magnitude_e(sim: &S) -> f32 { - (sim.map_sum_enumerated(|_pos: Index, cell| { - cell.e().mag().to_f64() - })/((sim.width() * sim.height() * sim.depth()) as f64)).to_f32() + fn mean_magnitude_e(sim: &S) -> R32 { + let m = sim.map_sum_enumerated(|_pos: Index, cell| { + cell.e().mag().to_r64() + })/(sim.width() * sim.height() * sim.depth()).to_r64(); + m.cast() } - fn mean_magnitude_h(sim: &S) -> f32 { - (sim.map_sum_enumerated(|_pos: Index, cell| { - cell.h().mag().to_f64() - })/((sim.width() * sim.height() * sim.depth()) as f64)).to_f32() + fn mean_magnitude_h(sim: &S) -> R32 { + let m = sim.map_sum_enumerated(|_pos: Index, cell| { + cell.h().mag().to_r64() + })/(sim.width() * sim.height() * sim.depth()).to_r64(); + m.cast() } - fn mean_magnitude_m(sim: &S) -> f32 { - (sim.map_sum_enumerated(|_pos: Index, cell| { - cell.m().mag().to_f64() - })/((sim.width() * sim.height() * sim.depth()) as f64)).to_f32() + fn mean_magnitude_m(sim: &S) -> R32 { + let m = sim.map_sum_enumerated(|_pos: Index, cell| { + cell.m().mag().to_r64() + })/(sim.width() * sim.height() * sim.depth()).to_r64(); + m.cast() } - fn assert_simstate_eq> + Send + Sync>(real_state: &SpirvSim, B>, ref_state: &SimState) { - let rel_threshold = 1e-2; + fn assert_simstate_eq> + Send + Sync>(real_state: &SpirvSim, B>, ref_state: &SimState) { + let rel_threshold = 1e-2f32.to_r32(); let mean_e = mean_magnitude_e(ref_state); let mean_h = mean_magnitude_h(ref_state); let mean_m = mean_magnitude_m(ref_state); @@ -305,9 +308,9 @@ mod test { let real_cell = real_state.sample(idx); let ref_cell = ref_state.sample(idx); - let diff_e = real_cell.e().cast::() - ref_cell.e(); - let diff_h = real_cell.h().cast::() - ref_cell.h(); - let diff_m = real_cell.m().cast::() - ref_cell.m(); + let diff_e = real_cell.e() - ref_cell.e(); + let diff_h = real_cell.h() - ref_cell.h(); + let diff_m = real_cell.m() - ref_cell.m(); // if x == 1 && y == 1 && z == 0 { // if ref_cell.m() != Vec3::zero() { @@ -317,17 +320,17 @@ mod test { // println!("diff_e: {}; diff_h: {}, diff_m: {}", diff_e, diff_h, diff_m); // } assert!( - diff_e.mag().to_f32() <= mean_e * rel_threshold, + diff_e.mag() <= mean_e * rel_threshold, "idx.e: {:?}. {:?} v.s. {:?}", idx, real_cell.e(), ref_cell.e() ); assert!( - diff_h.mag().to_f32() <= mean_h * rel_threshold, + diff_h.mag() <= mean_h * rel_threshold, "idx.h: {:?}. {:?} v.s. {:?}", idx, real_cell.h(), ref_cell.h() ); assert!( - diff_m.mag().to_f32() <= mean_m * rel_threshold || diff_m.mag() <= ref_cell.m().mag() * rel_threshold.cast::(), + diff_m.mag() <= mean_m * rel_threshold || diff_m.mag() <= ref_cell.m().mag(), "idx.m: {:?}. {:?} v.s. {:?}", idx, real_cell.m(), ref_cell.m() ); } @@ -338,49 +341,49 @@ mod test { mod backend_agnostic { use super::*; use crate::stim::{Fields, NoopStimulus, RngStimulus}; - pub fn do_smoke_small> + Send + Sync + Default>() { + pub fn do_smoke_small> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(8, 8, 8), 1e-3, B::default()); state.step(); } - pub fn do_smoke_med_7bit> + Send + Sync + Default>() { + pub fn do_smoke_med_7bit> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(124, 124, 124), 1e-3, B::default()); state.step(); } - pub fn do_smoke_med128> + Send + Sync + Default>() { + pub fn do_smoke_med128> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(128, 128, 128), 1e-3, B::default()); state.step(); } - pub fn do_smoke_med_23bit> + Send + Sync + Default>() { + pub fn do_smoke_med_23bit> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(127, 256, 256), 1e-3, B::default()); state.step(); } - pub fn do_smoke_med_0x800000_indexing> + Send + Sync + Default>() { + pub fn do_smoke_med_0x800000_indexing> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(128, 256, 256), 1e-3, B::default()); state.step(); } - pub fn do_smoke_med_0x800000_address_space> + Send + Sync + Default>() { + pub fn do_smoke_med_0x800000_address_space> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(170, 256, 256), 1e-3, B::default()); state.step(); } - pub fn do_smoke_large> + Send + Sync + Default>() { + pub fn do_smoke_large> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(326, 252, 160), 1e-3, B::default()); state.step(); } - pub fn do_smoke_not_multiple_of_4> + Send + Sync + Default>() { + pub fn do_smoke_not_multiple_of_4> + Send + Sync + Default>() { let mut state = SpirvSim::new_with_backend(Index::new(3, 2, 5), 1e-3, B::default()); state.step(); } - fn test_same_explicit> + Send + Sync + Default>( + fn test_same_explicit> + Send + Sync + Default>( mut ref_state: SimState, - mut dut_state: SpirvSim, B>, + mut dut_state: SpirvSim, B>, step_iters: u64, steps_per_iter: u32 ) { @@ -391,7 +394,7 @@ mod test { } } - fn test_same> + Send + Sync + Default>(seed: u64, step_iters: u64, steps_per_iter: u32, size: Index) { + fn test_same> + Send + Sync + Default>(seed: u64, step_iters: u64, steps_per_iter: u32, size: Index) { let mut cpu_state = SimState::new(size, 1e-3); cpu_state.apply_stimulus(&RngStimulus::new(seed)); @@ -401,33 +404,33 @@ mod test { test_same_explicit(cpu_state, spirv_state, step_iters, steps_per_iter); } - pub fn do_same_no_step_no_stim> + Send + Sync + Default>() { + pub fn do_same_no_step_no_stim> + Send + Sync + Default>() { let ref_state = SimState::new(Index::new(8, 8, 8), 1e-3); let dut_state = SpirvSim::new_with_backend(Index::new(8, 8, 8), 1e-3, B::default()); test_same_explicit(ref_state, dut_state, 1, 1); } - pub fn do_same_1_step> + Send + Sync + Default>() { + pub fn do_same_1_step> + Send + Sync + Default>() { test_same::(0x1234, 1, 1, Index::new(4, 4, 4)); } - pub fn do_same_1000_step> + Send + Sync + Default>() { + pub fn do_same_1000_step> + Send + Sync + Default>() { test_same::(0x1234, 1000, 1, Index::new(4, 4, 4)); } - pub fn do_same_not_multiple_of_4> + Send + Sync + Default>() { + pub fn do_same_not_multiple_of_4> + Send + Sync + Default>() { test_same::(0x1234, 100, 1, Index::new(3, 2, 5)); } - pub fn do_same_100_steps_larger> + Send + Sync + Default>() { + pub fn do_same_100_steps_larger> + Send + Sync + Default>() { test_same::(0x1234, 100, 1, Index::new(24, 20, 44)); } - pub fn do_same_100_steps_of_10> + Send + Sync + Default>() { + pub fn do_same_100_steps_of_10> + Send + Sync + Default>() { test_same::(0x1234, 100, 10, Index::new(24, 20, 44)); } - fn test_same_conductor> + Send + Sync + Default>( + fn test_same_conductor> + Send + Sync + Default>( seed: u64, step_iters: u64, steps_per_iter: u32, size: Index ) { use rand::{Rng as _, SeedableRng as _}; @@ -455,15 +458,15 @@ mod test { test_same_explicit(ref_state, dut_state, step_iters, steps_per_iter); } - pub fn do_conductor_1_step> + Send + Sync + Default>() { + pub fn do_conductor_1_step> + Send + Sync + Default>() { test_same_conductor::(0x1234, 1, 1, Index::new(4, 4, 4)); } - pub fn do_conductor_many_steps_larger> + Send + Sync + Default>() { + pub fn do_conductor_many_steps_larger> + Send + Sync + Default>() { test_same_conductor::(0x1234, 100, 10, Index::new(96, 16, 8)); } - fn test_same_mb_ferromagnet> + Send + Sync + Default>( + fn test_same_mb_ferromagnet> + Send + Sync + Default>( seed: u64, step_iters: u64, steps_per_iter: u32, size: Index ) { use rand::{Rng as _, SeedableRng as _}; @@ -488,7 +491,7 @@ mod test { b_start.cast(), b_end.cast(), m_max.cast() )); dut_state.put_material(Index::new(x, y, z), coremem_cross::mat::MBPgram::new( - b_start, b_end, m_max + b_start.cast(), b_end.cast(), m_max.cast() )); } } @@ -503,11 +506,11 @@ mod test { test_same_explicit(ref_state, dut_state, step_iters, steps_per_iter); } - pub fn do_mb_ferromagnet_1_step> + Send + Sync + Default>() { + pub fn do_mb_ferromagnet_1_step> + Send + Sync + Default>() { test_same_mb_ferromagnet::(0x1234, 1, 1, Index::new(4, 4, 4)); } - pub fn do_mb_ferromagnet_100_steps_larger> + Send + Sync + Default>() { + pub fn do_mb_ferromagnet_100_steps_larger> + Send + Sync + Default>() { test_same_mb_ferromagnet::(0x1234, 10, 10, Index::new(96, 16, 8)); } @@ -548,7 +551,7 @@ mod test { // test_same_explicit(ref_state, dut_state, steps); // } - fn test_smoke_mh_ferromagnet> + Send + Sync + Default>( + fn test_smoke_mh_ferromagnet> + Send + Sync + Default>( seed: u64, steps: u64, size: Index ) { // XXX This doesn't do anything, except make sure we don't crash! @@ -563,9 +566,13 @@ mod test { let h_start = rng.gen_range(1.0..1000.0); let mu_r = rng.gen_range(1.1..100000.0); let m_max = rng.gen_range(0.0..1_000_000.0); - dut_state.put_material(Index::new(x, y, z), mat::MHPgram::new(h_start, mu_r, m_max)); + dut_state.put_material(Index::new(x, y, z), mat::MHPgram::new( + h_start.cast(), mu_r.cast(), m_max.cast() + )); } else if rng.gen_range(0.0..1.0) < 0.5 { - dut_state.put_material(Index::new(x, y, z), mat::IsomorphicConductor::new(rng.gen_range(0.0..1e7))); + dut_state.put_material(Index::new(x, y, z), mat::IsomorphicConductor::new( + rng.gen_range(0.0..1e7).cast() + )); } } } @@ -578,15 +585,15 @@ mod test { } } - pub fn do_mh_ferromagnet_smoke_1_step> + Send + Sync + Default>() { + pub fn do_mh_ferromagnet_smoke_1_step> + Send + Sync + Default>() { test_smoke_mh_ferromagnet::(0x1234, 1, Index::new(4, 4, 4)); } - pub fn do_mh_ferromagnet_smoke_100_steps_larger> + Send + Sync + Default>() { + pub fn do_mh_ferromagnet_smoke_100_steps_larger> + Send + Sync + Default>() { test_smoke_mh_ferromagnet::(0x1234, 100, Index::new(328, 252, 160)); } - pub fn do_step_multiple_with_stim> + Send + Sync + Default>() { + pub fn do_step_multiple_with_stim> + Send + Sync + Default>() { let size = Index::new(4, 12, 8); let mut ref_state = SimState::new(size, 1e-3); let mut dut_state = SpirvSim::new_with_backend(size, 1e-3, B::default());