Skip to content
Snippets Groups Projects
Commit 463fa8c8 authored by Aaron Councilman's avatar Aaron Councilman
Browse files

Format rodinia

parent 40498a44
No related branches found
No related tags found
1 merge request!186Rodinia
Pipeline #201737 passed
......@@ -4,10 +4,10 @@ juno_build::juno!("backprop");
mod rust_backprop;
use hercules_rt::{runner, HerculesMutBox, HerculesImmBox, HerculesImmBoxTo, HerculesMutBoxTo};
use hercules_rt::{runner, HerculesImmBox, HerculesImmBoxTo, HerculesMutBox, HerculesMutBoxTo};
use rand::{Rng, SeedableRng};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use clap::Parser;
......@@ -37,27 +37,34 @@ fn run_backprop(
let mut hidden_prev_weights = HerculesMutBox::from(hidden_prev_weights.to_vec());
let mut runner = runner!(backprop);
let res = HerculesMutBox::from(
async_std::task::block_on(async {
runner.run(input_n,
hidden_n,
output_n,
input_vals.to(),
input_weights.to(),
hidden_weights.to(),
target.to(),
input_prev_weights.to(),
hidden_prev_weights.to(),
let res = HerculesMutBox::from(async_std::task::block_on(async {
runner
.run(
input_n,
hidden_n,
output_n,
input_vals.to(),
input_weights.to(),
hidden_weights.to(),
target.to(),
input_prev_weights.to(),
hidden_prev_weights.to(),
)
.await
})
).as_slice().to_vec();
}))
.as_slice()
.to_vec();
let out_err = res[0];
let hid_err = res[1];
(out_err, hid_err,
input_weights.as_slice().to_vec(), hidden_weights.as_slice().to_vec(),
input_prev_weights.as_slice().to_vec(), hidden_prev_weights.as_slice().to_vec())
(
out_err,
hid_err,
input_weights.as_slice().to_vec(),
hidden_weights.as_slice().to_vec(),
input_prev_weights.as_slice().to_vec(),
hidden_prev_weights.as_slice().to_vec(),
)
}
fn compare_float(x: f32, y: f32) -> bool {
......@@ -65,14 +72,11 @@ fn compare_float(x: f32, y: f32) -> bool {
}
fn compare_floats(xs: &[f32], ys: &[f32]) -> bool {
xs.len() == ys.len()
&& xs.iter().zip(ys.iter()).all(|(x, y)| compare_float(*x, *y))
xs.len() == ys.len() && xs.iter().zip(ys.iter()).all(|(x, y)| compare_float(*x, *y))
}
fn backprop_harness(args: BackpropInputs) {
let BackpropInputs {
layer_size,
} = args;
let BackpropInputs { layer_size } = args;
let mut rng = StdRng::seed_from_u64(7);
......@@ -86,16 +90,24 @@ fn backprop_harness(args: BackpropInputs) {
// For some reason the bpnn_randomize_row function used on target just sets it to 0.1
let target = vec![0.1; output_n + 1];
let input_weights = (0..(input_n+1)*(hidden_n+1)).map(|_| rng.random::<f32>()).collect::<Vec<_>>();
let hidden_weights = (0..(hidden_n+1)*(output_n+1)).map(|_| rng.random::<f32>()).collect::<Vec<_>>();
let input_weights = (0..(input_n + 1) * (hidden_n + 1))
.map(|_| rng.random::<f32>())
.collect::<Vec<_>>();
let hidden_weights = (0..(hidden_n + 1) * (output_n + 1))
.map(|_| rng.random::<f32>())
.collect::<Vec<_>>();
let input_prev_weights = vec![0.0; (input_n + 1) * (hidden_n + 1)];
let hidden_prev_weights = vec![0.0; (hidden_n + 1) * (output_n + 1)];
let (juno_out_err, juno_hid_err,
juno_input_weights, juno_hidden_weights,
juno_input_prev_weights, juno_hidden_prev_weights)
= run_backprop(
let (
juno_out_err,
juno_hid_err,
juno_input_weights,
juno_hidden_weights,
juno_input_prev_weights,
juno_hidden_prev_weights,
) = run_backprop(
input_n as u64,
hidden_n as u64,
output_n as u64,
......@@ -107,10 +119,14 @@ fn backprop_harness(args: BackpropInputs) {
&hidden_prev_weights,
);
let (rust_out_err, rust_hid_err,
rust_input_weights, rust_hidden_weights,
rust_input_prev_weights, rust_hidden_prev_weights)
= rust_backprop::backprop(
let (
rust_out_err,
rust_hid_err,
rust_input_weights,
rust_hidden_weights,
rust_input_prev_weights,
rust_hidden_prev_weights,
) = rust_backprop::backprop(
input_n,
hidden_n,
output_n,
......@@ -145,7 +161,5 @@ fn main() {
#[test]
fn backprop_test() {
backprop_harness(BackpropInputs {
layer_size: 65536,
});
backprop_harness(BackpropInputs { layer_size: 65536 });
}
fn layer_forward(n: usize, m: usize, vals: &[f32], weights: &[f32]) -> Vec<f32> {
let mut result = vec![0.0; m + 1];
result[0] = 1.0;
for j in 1..=m {
let mut sum = 0.0;
for k in 0..=n {
......@@ -27,7 +27,13 @@ fn output_error(n: usize, target: &[f32], actual: &[f32]) -> (f32, Vec<f32>) {
(error, result)
}
fn hidden_error(n: usize, m: usize, delta: &[f32], weights: &[f32], actual: &[f32]) -> (f32, Vec<f32>) {
fn hidden_error(
n: usize,
m: usize,
delta: &[f32],
weights: &[f32],
actual: &[f32],
) -> (f32, Vec<f32>) {
let mut result = vec![0.0; n + 1];
let mut error = 0.0;
......@@ -78,12 +84,37 @@ pub fn backprop(
let output_vals = layer_forward(hidden_n, output_n, &hidden_vals, &hidden_weights);
let (out_err, out_delta) = output_error(output_n, target, &output_vals);
let (hid_err, hid_delta) = hidden_error(hidden_n, output_n, &out_delta, &hidden_weights, &hidden_vals);
let (hid_err, hid_delta) = hidden_error(
hidden_n,
output_n,
&out_delta,
&hidden_weights,
&hidden_vals,
);
let (hidden_weights, hidden_prev_weights)
= adjust_weights(hidden_n, output_n, &out_delta, &hidden_vals, hidden_weights, hidden_prev_weights);
let (input_weights, input_prev_weights)
= adjust_weights(input_n, hidden_n, &hid_delta, &input_vals, input_weights, input_prev_weights);
let (hidden_weights, hidden_prev_weights) = adjust_weights(
hidden_n,
output_n,
&out_delta,
&hidden_vals,
hidden_weights,
hidden_prev_weights,
);
let (input_weights, input_prev_weights) = adjust_weights(
input_n,
hidden_n,
&hid_delta,
&input_vals,
input_weights,
input_prev_weights,
);
(out_err, hid_err, input_weights, hidden_weights, input_prev_weights, hidden_prev_weights)
(
out_err,
hid_err,
input_weights,
hidden_weights,
input_prev_weights,
hidden_prev_weights,
)
}
......@@ -14,7 +14,8 @@ pub struct Node {
pub fn parse_graph(file: String) -> (Vec<Node>, u32, Vec<u32>) {
let mut file = File::open(file).expect("Error opening input file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Error reading input file");
file.read_to_string(&mut contents)
.expect("Error reading input file");
let mut parser = nom::combinator::all_consuming(graph_parser);
let (_, result) = parser.parse(&contents).expect("Parser error");
......
......@@ -25,19 +25,15 @@ fn run_bfs(nodes: &[Node], source: u32, edges: &[u32]) -> Vec<i32> {
let mut runner = runner!(bfs);
HerculesMutBox::from(
async_std::task::block_on(async {
runner.run(n, m, nodes.to(), source, edges.to()).await
})
)
HerculesMutBox::from(async_std::task::block_on(async {
runner.run(n, m, nodes.to(), source, edges.to()).await
}))
.as_slice()
.to_vec()
}
fn bfs_harness(args: BFSInputs) {
let BFSInputs {
input,
} = args;
let BFSInputs { input } = args;
let (nodes, source, edges) = parse_graph(input);
......
......@@ -4,7 +4,7 @@ use std::collections::VecDeque;
pub fn bfs(graph_nodes: &[Node], source: u32, edges: &[u32]) -> Vec<i32> {
let mut explored = vec![false; graph_nodes.len()];
let mut costs = vec![-1; graph_nodes.len()];
let mut costs = vec![-1; graph_nodes.len()];
let mut worklist = VecDeque::new();
let source = source as usize;
......@@ -15,8 +15,8 @@ pub fn bfs(graph_nodes: &[Node], source: u32, edges: &[u32]) -> Vec<i32> {
while let Some(node) = worklist.pop_front() {
let edge_start = graph_nodes[node].edge_start;
let num_edges = graph_nodes[node].num_edges;
for edge in edge_start..edge_start+num_edges {
let num_edges = graph_nodes[node].num_edges;
for edge in edge_start..edge_start + num_edges {
let dst = edges[edge as usize] as usize;
if !explored[dst] {
explored[dst] = true;
......
#![feature(concat_idents)]
mod setup;
mod rust_cfd;
mod setup;
use clap::Parser;
use crate::setup::*;
use hercules_rt::{runner, HerculesImmBox, HerculesMutBox, HerculesImmBoxTo, HerculesMutBoxTo};
use hercules_rt::{runner, HerculesImmBox, HerculesImmBoxTo, HerculesMutBox, HerculesMutBoxTo};
juno_build::juno!("euler");
juno_build::juno!("pre_euler");
......@@ -41,7 +41,11 @@ fn run_euler(
let ff_variable = HerculesImmBox::from(ff_variable);
// TODO: Make hercules box handle structs, for now we'll copy into a vec
let ff_fc_density_energy = vec![ff_fc_density_energy.x, ff_fc_density_energy.y, ff_fc_density_energy.z];
let ff_fc_density_energy = vec![
ff_fc_density_energy.x,
ff_fc_density_energy.y,
ff_fc_density_energy.z,
];
let ff_fc_density_energy = HerculesImmBox::from(ff_fc_density_energy.as_slice());
let ff_fc_momentum_x = vec![ff_fc_momentum_x.x, ff_fc_momentum_x.y, ff_fc_momentum_x.z];
let ff_fc_momentum_x = HerculesImmBox::from(ff_fc_momentum_x.as_slice());
......@@ -52,9 +56,9 @@ fn run_euler(
let mut runner = runner!(euler);
HerculesMutBox::from(
async_std::task::block_on(async {
runner.run(
HerculesMutBox::from(async_std::task::block_on(async {
runner
.run(
nelr as u64,
iterations as u64,
variables.to(),
......@@ -68,8 +72,7 @@ fn run_euler(
ff_fc_momentum_z.to(),
)
.await
})
)
}))
.as_slice()
.to_vec()
}
......@@ -94,7 +97,11 @@ fn run_pre_euler(
let ff_variable = HerculesImmBox::from(ff_variable);
// TODO: Make hercules box handle structs, for now we'll copy into a vec
let ff_fc_density_energy = vec![ff_fc_density_energy.x, ff_fc_density_energy.y, ff_fc_density_energy.z];
let ff_fc_density_energy = vec![
ff_fc_density_energy.x,
ff_fc_density_energy.y,
ff_fc_density_energy.z,
];
let ff_fc_density_energy = HerculesImmBox::from(ff_fc_density_energy.as_slice());
let ff_fc_momentum_x = vec![ff_fc_momentum_x.x, ff_fc_momentum_x.y, ff_fc_momentum_x.z];
let ff_fc_momentum_x = HerculesImmBox::from(ff_fc_momentum_x.as_slice());
......@@ -107,9 +114,9 @@ fn run_pre_euler(
let variables = variables.to();
HerculesMutBox::from(
async_std::task::block_on(async {
runner.run(
HerculesMutBox::from(async_std::task::block_on(async {
runner
.run(
nelr as u64,
iterations as u64,
variables,
......@@ -123,8 +130,7 @@ fn run_pre_euler(
ff_fc_momentum_z.to(),
)
.await
})
)
}))
.as_slice()
.to_vec()
}
......@@ -134,8 +140,7 @@ fn compare_float(x: f32, y: f32) -> bool {
}
fn compare_floats(xs: &[f32], ys: &[f32]) -> bool {
xs.len() == ys.len()
&& xs.iter().zip(ys.iter()).all(|(x, y)| compare_float(*x, *y))
xs.len() == ys.len() && xs.iter().zip(ys.iter()).all(|(x, y)| compare_float(*x, *y))
}
fn cfd_harness(args: CFDInputs) {
......@@ -165,66 +170,64 @@ fn cfd_harness(args: CFDInputs) {
let variables = initialize_variables(nelr, ff_variable.as_slice());
let res_juno =
if pre_euler {
run_pre_euler(
nelr,
iterations,
variables.clone(),
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
} else {
run_euler(
nelr,
iterations,
variables.clone(),
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
};
let res_rust =
if pre_euler {
rust_cfd::pre_euler(
nelr,
iterations,
variables,
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
} else {
rust_cfd::euler(
nelr,
iterations,
variables,
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
};
let res_juno = if pre_euler {
run_pre_euler(
nelr,
iterations,
variables.clone(),
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
} else {
run_euler(
nelr,
iterations,
variables.clone(),
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
};
let res_rust = if pre_euler {
rust_cfd::pre_euler(
nelr,
iterations,
variables,
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
} else {
rust_cfd::euler(
nelr,
iterations,
variables,
areas.as_slice(),
elements_surrounding_elements.as_slice(),
normals.as_slice(),
ff_variable.as_slice(),
&ff_fc_density_energy,
&ff_fc_momentum_x,
&ff_fc_momentum_y,
&ff_fc_momentum_z,
)
};
if !compare_floats(&res_juno, res_rust.as_slice()) {
assert_eq!(res_juno.len(), res_rust.as_slice().len());
......@@ -239,24 +242,20 @@ fn main() {
#[test]
fn test_euler() {
cfd_harness(
CFDInputs {
data_file: "data/fvcorr.domn.097K".to_string(),
iterations: 1,
block_size: 8,
pre_euler: false,
}
);
cfd_harness(CFDInputs {
data_file: "data/fvcorr.domn.097K".to_string(),
iterations: 1,
block_size: 8,
pre_euler: false,
});
}
#[test]
fn test_pre_euler() {
cfd_harness(
CFDInputs {
data_file: "data/fvcorr.domn.097K".to_string(),
iterations: 1,
block_size: 8,
pre_euler: true,
}
);
cfd_harness(CFDInputs {
data_file: "data/fvcorr.domn.097K".to_string(),
iterations: 1,
block_size: 8,
pre_euler: true,
});
}
This diff is collapsed.
......@@ -9,16 +9,16 @@ use nom::Parser;
use crate::rust_cfd;
pub const GAMMA : f32 = 1.4;
pub const ff_mach : f32 = 1.2;
pub const NDIM : usize = 3;
pub const NNB : usize = 4;
pub const RK : usize = 3;
pub const VAR_DENSITY : usize = 0;
pub const VAR_MOMENTUM : usize = 1;
pub const VAR_DENSITY_ENERGY : usize = VAR_MOMENTUM + NDIM;
pub const NVAR : usize = VAR_DENSITY_ENERGY + 1;
pub const deg_angle_of_attack : f32 = 0.0;
pub const GAMMA: f32 = 1.4;
pub const ff_mach: f32 = 1.2;
pub const NDIM: usize = 3;
pub const NNB: usize = 4;
pub const RK: usize = 3;
pub const VAR_DENSITY: usize = 0;
pub const VAR_MOMENTUM: usize = 1;
pub const VAR_DENSITY_ENERGY: usize = VAR_MOMENTUM + NDIM;
pub const NVAR: usize = VAR_DENSITY_ENERGY + 1;
pub const deg_angle_of_attack: f32 = 0.0;
#[repr(align(32))]
struct Alignment([u8; 32]);
......@@ -39,8 +39,9 @@ where
// The maximum number of elements that may be waisted in getting the alignment we need is
// (32 - alignment of T) / size of T
let extra_elements = (32 - std::mem::align_of::<T>()) / std::mem::size_of::<T>();
let slice : Box<[T]>
= (0..len+extra_elements).map(|_| Default::default()).collect();
let slice: Box<[T]> = (0..len + extra_elements)
.map(|_| Default::default())
.collect();
let offset = unsafe { slice.align_to::<Alignment>().0.len() };
assert!(offset <= extra_elements);
......@@ -54,11 +55,11 @@ where
impl<T> AlignedSlice<T> {
pub fn as_slice(&self) -> &[T] {
&self.slice[self.offset..self.offset+self.length]
&self.slice[self.offset..self.offset + self.length]
}
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.slice[self.offset..self.offset+self.length]
&mut self.slice[self.offset..self.offset + self.length]
}
}
......@@ -90,7 +91,11 @@ where
}
#[repr(C)]
pub struct Float3 { pub x: f32, pub y: f32, pub z: f32 }
pub struct Float3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
pub struct FarFieldConditions {
pub ff_variable: AlignedSlice<f32>,
......@@ -101,7 +106,7 @@ pub struct FarFieldConditions {
}
pub fn set_far_field_conditions() -> FarFieldConditions {
let mut ff_variable : AlignedSlice<f32> = AlignedSlice::of_len(NVAR);
let mut ff_variable: AlignedSlice<f32> = AlignedSlice::of_len(NVAR);
let angle_of_attack = std::f32::consts::PI / 180.0 * deg_angle_of_attack;
......@@ -117,19 +122,20 @@ pub fn set_far_field_conditions() -> FarFieldConditions {
z: 0.0,
};
ff_variable[VAR_MOMENTUM+0] = ff_variable[VAR_DENSITY] * ff_velocity.x;
ff_variable[VAR_MOMENTUM+1] = ff_variable[VAR_DENSITY] * ff_velocity.y;
ff_variable[VAR_MOMENTUM+2] = ff_variable[VAR_DENSITY] * ff_velocity.z;
ff_variable[VAR_MOMENTUM + 0] = ff_variable[VAR_DENSITY] * ff_velocity.x;
ff_variable[VAR_MOMENTUM + 1] = ff_variable[VAR_DENSITY] * ff_velocity.y;
ff_variable[VAR_MOMENTUM + 2] = ff_variable[VAR_DENSITY] * ff_velocity.z;
ff_variable[VAR_DENSITY_ENERGY] = ff_variable[VAR_DENSITY] * (0.5 * (ff_speed * ff_speed)) + (ff_pressure / (GAMMA - 1.0));
ff_variable[VAR_DENSITY_ENERGY] =
ff_variable[VAR_DENSITY] * (0.5 * (ff_speed * ff_speed)) + (ff_pressure / (GAMMA - 1.0));
let ff_momentum = Float3 {
x: ff_variable[VAR_MOMENTUM+0],
y: ff_variable[VAR_MOMENTUM+1],
z: ff_variable[VAR_MOMENTUM+2],
x: ff_variable[VAR_MOMENTUM + 0],
y: ff_variable[VAR_MOMENTUM + 1],
z: ff_variable[VAR_MOMENTUM + 2],
};
let (ff_fc_momentum_x, ff_fc_momentum_y, ff_fc_momentum_z, ff_fc_density_energy)
= rust_cfd::compute_flux_contribution(
let (ff_fc_momentum_x, ff_fc_momentum_y, ff_fc_momentum_z, ff_fc_density_energy) =
rust_cfd::compute_flux_contribution(
ff_variable[VAR_DENSITY],
&ff_momentum,
ff_variable[VAR_DENSITY_ENERGY],
......@@ -155,11 +161,12 @@ pub struct GeometryData {
pub fn read_domain_geometry<P>(path: P, block_size: usize) -> GeometryData
where
P: AsRef<Path>
P: AsRef<Path>,
{
let mut file = File::open(path).expect("Error opening input file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Error reading input file");
file.read_to_string(&mut contents)
.expect("Error reading input file");
let mut parser = nom::combinator::all_consuming(|s| cfd_parser(block_size, s));
let (_, result) = parser.parse(&contents).expect("Parser error");
......@@ -174,9 +181,9 @@ fn cfd_parser<'a>(block_size: usize, text: &'a str) -> nom::IResult<&'a str, Geo
let nel = usize::from_str(nel).unwrap();
let nelr = block_size * ((nel / block_size) + usize::min(1, nel % block_size));
let mut areas : AlignedSlice<f32> = AlignedSlice::of_len(nelr);
let mut elements_surrounding_elements : AlignedSlice<i32> = AlignedSlice::of_len(nelr * NNB);
let mut normals : AlignedSlice<f32> = AlignedSlice::of_len(NDIM * NNB * nelr);
let mut areas: AlignedSlice<f32> = AlignedSlice::of_len(nelr);
let mut elements_surrounding_elements: AlignedSlice<i32> = AlignedSlice::of_len(nelr * NNB);
let mut normals: AlignedSlice<f32> = AlignedSlice::of_len(NDIM * NNB * nelr);
// read in data
let mut text = text;
......@@ -184,26 +191,24 @@ fn cfd_parser<'a>(block_size: usize, text: &'a str) -> nom::IResult<&'a str, Geo
let t = nom::character::complete::multispace0(text)?.0;
let (t, val) = nom::number::complete::float(t)?;
text = t;
areas[i] = val;
for j in 0..NNB {
let t = nom::character::complete::multispace0(text)?.0;
let (t, neg) =
if let Ok((t, _)) = nom::character::complete::char::<&str, nom::error::Error<&str>>('-')(t) {
(t, true)
} else {
(t, false)
};
let (t, neg) = if let Ok((t, _)) =
nom::character::complete::char::<&str, nom::error::Error<&str>>('-')(t)
{
(t, true)
} else {
(t, false)
};
let (t, val) = nom::character::complete::digit1(t)?;
text = t;
let val = i32::from_str(val).unwrap();
let val = if neg { - val } else { val };
elements_surrounding_elements[i + j * nelr] =
if val < 0 { -1 }
else { val }
- 1; // it's coming in with Fortran numbering
let val = if neg { -val } else { val };
elements_surrounding_elements[i + j * nelr] = if val < 0 { -1 } else { val } - 1; // it's coming in with Fortran numbering
for k in 0..NDIM {
let t = nom::character::complete::multispace0(text)?.0;
......@@ -220,7 +225,8 @@ fn cfd_parser<'a>(block_size: usize, text: &'a str) -> nom::IResult<&'a str, Geo
for i in nel..nelr {
areas[i] = areas[last];
for j in 0..NNB {
elements_surrounding_elements[i + j * nelr] = elements_surrounding_elements[last + j * nelr];
elements_surrounding_elements[i + j * nelr] =
elements_surrounding_elements[last + j * nelr];
for k in 0..NDIM {
normals[i + (j + k * NNB) * nelr] = normals[last + (j + k * NNB) * nelr];
}
......@@ -228,7 +234,15 @@ fn cfd_parser<'a>(block_size: usize, text: &'a str) -> nom::IResult<&'a str, Geo
}
let text = nom::character::complete::multispace0(text)?.0;
Ok((text, GeometryData { nelr, areas, elements_surrounding_elements, normals }))
Ok((
text,
GeometryData {
nelr,
areas,
elements_surrounding_elements,
normals,
},
))
}
pub fn initialize_variables(nelr: usize, ff_variable: &[f32]) -> AlignedSlice<f32> {
......
......@@ -7,18 +7,19 @@ use nom::Parser;
pub struct Image {
pub image: Vec<f32>,
pub max: f32,
pub max: f32,
pub rows: usize,
pub cols: usize,
}
pub fn read_graphics<P>(path: P) -> Image
where
P: AsRef<Path>
P: AsRef<Path>,
{
let mut file = File::open(path).expect("Error opening input file");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Error reading input file");
file.read_to_string(&mut contents)
.expect("Error reading input file");
let mut parser = nom::combinator::all_consuming(image_parser);
let (_, result) = parser.parse(&contents).expect("Parser error");
......@@ -28,7 +29,7 @@ where
pub fn write_graphics<P>(path: P, image: &[f32], rows: usize, cols: usize, max: f32)
where
P: AsRef<Path>
P: AsRef<Path>,
{
let mut file = File::create(path).expect("Error opening output file");
let f = &mut file;
......@@ -106,5 +107,13 @@ fn image_parser<'a>(text: &'a str) -> nom::IResult<&'a str, Image> {
}
}
Ok((text, Image { image, max, rows, cols }))
Ok((
text,
Image {
image,
max,
rows,
cols,
},
))
}
......@@ -6,7 +6,7 @@ use graphics::*;
use clap::Parser;
use hercules_rt::{runner, HerculesMutBox, HerculesImmBox, HerculesImmBoxTo, HerculesMutBoxTo};
use hercules_rt::{runner, HerculesImmBox, HerculesImmBoxTo, HerculesMutBox, HerculesMutBoxTo};
juno_build::juno!("srad");
......@@ -43,7 +43,7 @@ fn srad_harness(args: SRADInputs) {
image: image_ori,
max,
rows: image_ori_rows,
cols: image_ori_cols
cols: image_ori_cols,
} = read_graphics(image);
let image = resize(&image_ori, image_ori_rows, image_ori_cols, nrows, ncols);
let mut image_h = HerculesMutBox::from(image.clone());
......@@ -65,21 +65,24 @@ fn srad_harness(args: SRADInputs) {
let jE_h = HerculesImmBox::from(jE.as_slice());
let mut runner = runner!(srad);
let result : Vec<f32> = HerculesMutBox::from(
runner.run(
nrows as u64,
ncols as u64,
niter as u64,
image_h.to(),
iN_h.to(),
iS_h.to(),
jW_h.to(),
jE_h.to(),
max,
lambda,
).await)
.as_slice()
.to_vec();
let result: Vec<f32> = HerculesMutBox::from(
runner
.run(
nrows as u64,
ncols as u64,
niter as u64,
image_h.to(),
iN_h.to(),
iS_h.to(),
jW_h.to(),
jE_h.to(),
max,
lambda,
)
.await,
)
.as_slice()
.to_vec();
if let Some(output) = output {
write_graphics(output, &result, nrows, ncols, max);
......@@ -126,16 +129,14 @@ fn main() {
#[test]
fn srad_test() {
srad_harness(
SRADInputs {
niter: 100,
lambda: 0.5,
nrows: 502,
ncols: 458,
image: "data/image.pgm".to_string(),
output: None,
verify: true,
output_verify: None,
}
);
srad_harness(SRADInputs {
niter: 100,
lambda: 0.5,
nrows: 502,
ncols: 458,
image: "data/image.pgm".to_string(),
output: None,
verify: true,
output_verify: None,
});
}
......@@ -31,14 +31,14 @@ pub fn srad(
for i in 0..nrows {
for j in 0..ncols {
let tmp = image[i + nrows * j];
sum += tmp;
sum += tmp;
sum2 += tmp * tmp;
}
}
let meanROI = sum / nelems as f32;
let varROI = (sum2 / nelems as f32) - meanROI * meanROI;
let q0sqr = varROI / (meanROI * meanROI);
let varROI = (sum2 / nelems as f32) - meanROI * meanROI;
let q0sqr = varROI / (meanROI * meanROI);
for j in 0..ncols {
for i in 0..nrows {
......@@ -50,8 +50,8 @@ pub fn srad(
dW[k] = image[i + nrows * jW[j] as usize] - Jc;
dE[k] = image[i + nrows * jE[j] as usize] - Jc;
let G2 = (dN[k] * dN[k] + dS[k] * dS[k]
+ dW[k] * dW[k] + dE[k] * dE[k]) / (Jc * Jc);
let G2 =
(dN[k] * dN[k] + dS[k] * dS[k] + dW[k] * dW[k] + dE[k] * dE[k]) / (Jc * Jc);
let L = (dN[k] + dS[k] + dW[k] + dE[k]) / Jc;
let num = (0.5 * G2) - ((1.0 / 16.0) * (L * L));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment