follow nx/ny conventions

This commit is contained in:
Magnus Ulimoen 2020-01-29 22:01:12 +01:00
parent fc5565dbcd
commit 4ac1ad0d2c
5 changed files with 21 additions and 28 deletions

View File

@ -23,7 +23,7 @@ fn performance_benchmark(c: &mut Criterion) {
let x = ndarray::Array2::from_shape_fn((h, w), |(_, i)| i as f32 / (w - 1) as f32); let x = ndarray::Array2::from_shape_fn((h, w), |(_, i)| i as f32 / (w - 1) as f32);
let y = ndarray::Array2::from_shape_fn((h, w), |(j, _)| j as f32 / (h - 1) as f32); let y = ndarray::Array2::from_shape_fn((h, w), |(j, _)| j as f32 / (h - 1) as f32);
let mut universe = System::<Upwind4>::new(w, h, x.as_slice().unwrap(), y.as_slice().unwrap()); let mut universe = System::<Upwind4>::new(x.clone(), y.clone());
group.bench_function("advance", |b| { group.bench_function("advance", |b| {
b.iter(|| { b.iter(|| {
universe.set_gaussian(0.5, 0.5); universe.set_gaussian(0.5, 0.5);
@ -31,7 +31,7 @@ fn performance_benchmark(c: &mut Criterion) {
}) })
}); });
let mut universe = System::<Upwind4>::new(w, h, x.as_slice().unwrap(), y.as_slice().unwrap()); let mut universe = System::<Upwind4>::new(x.clone(), y.clone());
group.bench_function("advance_upwind", |b| { group.bench_function("advance_upwind", |b| {
b.iter(|| { b.iter(|| {
universe.set_gaussian(0.5, 0.5); universe.set_gaussian(0.5, 0.5);
@ -39,7 +39,7 @@ fn performance_benchmark(c: &mut Criterion) {
}) })
}); });
let mut universe = System::<SBP4>::new(w, h, x.as_slice().unwrap(), y.as_slice().unwrap()); let mut universe = System::<SBP4>::new(x, y);
group.bench_function("advance_trad4", |b| { group.bench_function("advance_trad4", |b| {
b.iter(|| { b.iter(|| {
universe.set_gaussian(0.5, 0.5); universe.set_gaussian(0.5, 0.5);

View File

@ -66,17 +66,6 @@ impl<SBP: super::operators::SbpOperator> Grid<SBP> {
operator: std::marker::PhantomData, operator: std::marker::PhantomData,
}) })
} }
pub fn new_from_slice(
ny: usize,
nx: usize,
x: &[f32],
y: &[f32],
) -> Result<Self, ndarray::ShapeError> {
let x = Array2::from_shape_vec((ny, nx), x.to_vec())?;
let y = Array2::from_shape_vec((ny, nx), y.to_vec())?;
Self::new(x, y)
}
pub fn nx(&self) -> usize { pub fn nx(&self) -> usize {
self.x.shape()[1] self.x.shape()[1]
} }

View File

@ -23,8 +23,10 @@ pub struct MaxwellUniverse(maxwell::System<operators::Upwind4>);
#[wasm_bindgen] #[wasm_bindgen]
impl MaxwellUniverse { impl MaxwellUniverse {
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
pub fn new(width: usize, height: usize, x: &[f32], y: &[f32]) -> Self { pub fn new(height: usize, width: usize, x: &[f32], y: &[f32]) -> Self {
Self(maxwell::System::new(width as usize, height as usize, x, y)) let x = ndarray::Array2::from_shape_vec((height, width), x.to_vec()).unwrap();
let y = ndarray::Array2::from_shape_vec((height, width), y.to_vec()).unwrap();
Self(maxwell::System::new(x, y))
} }
pub fn init(&mut self, x0: f32, y0: f32) { pub fn init(&mut self, x0: f32, y0: f32) {

View File

@ -21,7 +21,7 @@ impl std::ops::DerefMut for Field {
} }
impl Field { impl Field {
pub fn new(width: usize, height: usize) -> Self { pub fn new(height: usize, width: usize) -> Self {
let field = Array3::zeros((3, height, width)); let field = Array3::zeros((3, height, width));
Self(field) Self(field)
@ -68,6 +68,7 @@ impl Field {
} }
} }
#[derive(Debug, Clone)]
pub struct System<SBP: SbpOperator> { pub struct System<SBP: SbpOperator> {
sys: (Field, Field), sys: (Field, Field),
wb: WorkBuffers, wb: WorkBuffers,
@ -75,17 +76,17 @@ pub struct System<SBP: SbpOperator> {
} }
impl<SBP: SbpOperator> System<SBP> { impl<SBP: SbpOperator> System<SBP> {
pub fn new(width: usize, height: usize, x: &[f32], y: &[f32]) -> Self { pub fn new(x: Array2<f32>, y: Array2<f32>) -> Self {
assert_eq!((width * height), x.len()); assert_eq!(x.shape(), y.shape());
assert_eq!((width * height), y.len()); let ny = x.shape()[0];
let nx = x.shape()[1];
let grid = Grid::new(x, y).unwrap();
let grid = Grid::new_from_slice(height, width, x, y).expect(
"Could not create grid. Different number of elements compared to width*height?",
);
Self { Self {
sys: (Field::new(width, height), Field::new(width, height)), sys: (Field::new(ny, nx), Field::new(ny, nx)),
grid, grid,
wb: WorkBuffers::new(width, height), wb: WorkBuffers::new(ny, nx),
} }
} }
@ -556,15 +557,16 @@ fn SAT_characteristics<SBP: SbpOperator>(
} }
} }
#[derive(Clone, Debug)]
pub struct WorkBuffers { pub struct WorkBuffers {
k: [Field; 4], k: [Field; 4],
tmp: (Array2<f32>, Array2<f32>, Array2<f32>, Array2<f32>), tmp: (Array2<f32>, Array2<f32>, Array2<f32>, Array2<f32>),
} }
impl WorkBuffers { impl WorkBuffers {
pub fn new(nx: usize, ny: usize) -> Self { pub fn new(ny: usize, nx: usize) -> Self {
let arr2 = Array2::zeros((ny, nx)); let arr2 = Array2::zeros((ny, nx));
let arr3 = Field::new(nx, ny); let arr3 = Field::new(ny, nx);
Self { Self {
k: [arr3.clone(), arr3.clone(), arr3.clone(), arr3], k: [arr3.clone(), arr3.clone(), arr3.clone(), arr3],
tmp: (arr2.clone(), arr2.clone(), arr2.clone(), arr2), tmp: (arr2.clone(), arr2.clone(), arr2.clone(), arr2),

View File

@ -116,7 +116,7 @@ import { MaxwellUniverse, default as init, set_panic_hook as setPanicHook } from
} }
} }
const universe = new MaxwellUniverse(width, height, x, y); const universe = new MaxwellUniverse(height, width, x, y);
// Transfer x, y to cpu, prepare fBuffer // Transfer x, y to cpu, prepare fBuffer