spirv tests: port to R32
this gives better debug info
This commit is contained in:
@@ -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<S: AbstractSim>(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<S: AbstractSim>(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<S: AbstractSim>(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<S: AbstractSim>(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<S: AbstractSim>(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<S: AbstractSim>(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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync>(real_state: &SpirvSim<f32, FullyGenericMaterial<f32>, B>, ref_state: &SimState) {
|
||||
let rel_threshold = 1e-2;
|
||||
fn assert_simstate_eq<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync>(real_state: &SpirvSim<R32, FullyGenericMaterial<R32>, 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::<R32>() - ref_cell.e();
|
||||
let diff_h = real_cell.h().cast::<R32>() - ref_cell.h();
|
||||
let diff_m = real_cell.m().cast::<R32>() - 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::<R32>(),
|
||||
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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_small<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_med_7bit<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_med128<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_med_23bit<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_med_0x800000_indexing<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_med_0x800000_address_space<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_large<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_smoke_not_multiple_of_4<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>(
|
||||
fn test_same_explicit<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>(
|
||||
mut ref_state: SimState,
|
||||
mut dut_state: SpirvSim<f32, FullyGenericMaterial<f32>, B>,
|
||||
mut dut_state: SpirvSim<R32, FullyGenericMaterial<R32>, B>,
|
||||
step_iters: u64,
|
||||
steps_per_iter: u32
|
||||
) {
|
||||
@@ -391,7 +394,7 @@ mod test {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_same<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>(seed: u64, step_iters: u64, steps_per_iter: u32, size: Index) {
|
||||
fn test_same<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_same_no_step_no_stim<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_same_1_step<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same::<B>(0x1234, 1, 1, Index::new(4, 4, 4));
|
||||
}
|
||||
|
||||
pub fn do_same_1000_step<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_same_1000_step<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same::<B>(0x1234, 1000, 1, Index::new(4, 4, 4));
|
||||
}
|
||||
|
||||
pub fn do_same_not_multiple_of_4<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_same_not_multiple_of_4<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same::<B>(0x1234, 100, 1, Index::new(3, 2, 5));
|
||||
}
|
||||
|
||||
pub fn do_same_100_steps_larger<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_same_100_steps_larger<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same::<B>(0x1234, 100, 1, Index::new(24, 20, 44));
|
||||
}
|
||||
|
||||
pub fn do_same_100_steps_of_10<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_same_100_steps_of_10<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same::<B>(0x1234, 100, 10, Index::new(24, 20, 44));
|
||||
}
|
||||
|
||||
fn test_same_conductor<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>(
|
||||
fn test_same_conductor<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_conductor_1_step<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same_conductor::<B>(0x1234, 1, 1, Index::new(4, 4, 4));
|
||||
}
|
||||
|
||||
pub fn do_conductor_many_steps_larger<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_conductor_many_steps_larger<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same_conductor::<B>(0x1234, 100, 10, Index::new(96, 16, 8));
|
||||
}
|
||||
|
||||
fn test_same_mb_ferromagnet<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>(
|
||||
fn test_same_mb_ferromagnet<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_mb_ferromagnet_1_step<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same_mb_ferromagnet::<B>(0x1234, 1, 1, Index::new(4, 4, 4));
|
||||
}
|
||||
|
||||
pub fn do_mb_ferromagnet_100_steps_larger<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_mb_ferromagnet_100_steps_larger<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_same_mb_ferromagnet::<B>(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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>(
|
||||
fn test_smoke_mh_ferromagnet<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_mh_ferromagnet_smoke_1_step<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_smoke_mh_ferromagnet::<B>(0x1234, 1, Index::new(4, 4, 4));
|
||||
}
|
||||
|
||||
pub fn do_mh_ferromagnet_smoke_100_steps_larger<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_mh_ferromagnet_smoke_100_steps_larger<B: SimBackend<R32, FullyGenericMaterial<R32>> + Send + Sync + Default>() {
|
||||
test_smoke_mh_ferromagnet::<B>(0x1234, 100, Index::new(328, 252, 160));
|
||||
}
|
||||
|
||||
pub fn do_step_multiple_with_stim<B: SimBackend<f32, FullyGenericMaterial<f32>> + Send + Sync + Default>() {
|
||||
pub fn do_step_multiple_with_stim<B: SimBackend<R32, FullyGenericMaterial<R32>> + 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());
|
||||
|
Reference in New Issue
Block a user