diff --git a/juno_samples/rodinia/backprop/src/main.rs b/juno_samples/rodinia/backprop/src/main.rs index 36d5da262bb7dd5413932a335e6cc12ee611c2c1..848b0abb02cdca6d26d9aa2961e3e0206844fa19 100644 --- a/juno_samples/rodinia/backprop/src/main.rs +++ b/juno_samples/rodinia/backprop/src/main.rs @@ -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 }); } diff --git a/juno_samples/rodinia/backprop/src/rust_backprop.rs b/juno_samples/rodinia/backprop/src/rust_backprop.rs index be1aafea36de7169fc757b831f2dab9edbbea0b8..c5f23021b72fbe0f52b0f537318402259b7cf1ba 100644 --- a/juno_samples/rodinia/backprop/src/rust_backprop.rs +++ b/juno_samples/rodinia/backprop/src/rust_backprop.rs @@ -1,7 +1,7 @@ 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, + ) } diff --git a/juno_samples/rodinia/bfs/src/graph_parser.rs b/juno_samples/rodinia/bfs/src/graph_parser.rs index 7b54a96d5769b9bff7e3a700f003b13bb5eadef0..fecd2a3ed86c23aa64511febb5fd7a5ece8fe0f7 100644 --- a/juno_samples/rodinia/bfs/src/graph_parser.rs +++ b/juno_samples/rodinia/bfs/src/graph_parser.rs @@ -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"); diff --git a/juno_samples/rodinia/bfs/src/main.rs b/juno_samples/rodinia/bfs/src/main.rs index 95e3541097cf66eeb35386017c6c2139c36e9780..21e48c35e5dbd33875dccb5767d644d6ea2bca7e 100644 --- a/juno_samples/rodinia/bfs/src/main.rs +++ b/juno_samples/rodinia/bfs/src/main.rs @@ -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); diff --git a/juno_samples/rodinia/bfs/src/rust_bfs.rs b/juno_samples/rodinia/bfs/src/rust_bfs.rs index f38effff40fa617224685fb1dea42e09d8c58044..f90dcfafee3b8ac3d471c35ccfc7bd98e38259c0 100644 --- a/juno_samples/rodinia/bfs/src/rust_bfs.rs +++ b/juno_samples/rodinia/bfs/src/rust_bfs.rs @@ -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; diff --git a/juno_samples/rodinia/cfd/src/main.rs b/juno_samples/rodinia/cfd/src/main.rs index 9a51ddfafefb2c525410b87752be6822b611d167..1ce6b89ae370eede1e0ca88c20aeff0f1261b66f 100644 --- a/juno_samples/rodinia/cfd/src/main.rs +++ b/juno_samples/rodinia/cfd/src/main.rs @@ -1,12 +1,12 @@ #![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, + }); } diff --git a/juno_samples/rodinia/cfd/src/rust_cfd.rs b/juno_samples/rodinia/cfd/src/rust_cfd.rs index c02482aee6a1dbc53b799f92c721c09ad4316f56..42479df8eb2cd5cf86e70cafd212050a07d729b4 100644 --- a/juno_samples/rodinia/cfd/src/rust_cfd.rs +++ b/juno_samples/rodinia/cfd/src/rust_cfd.rs @@ -30,7 +30,12 @@ pub fn compute_flux_contribution( z: velocity.z * de_p, }; - (fc_momentum_x, fc_momentum_y, fc_momentum_z, fc_density_energy) + ( + fc_momentum_x, + fc_momentum_y, + fc_momentum_z, + fc_density_energy, + ) } fn compute_velocity(density: f32, momentum: &Float3) -> Float3 { @@ -53,20 +58,16 @@ fn compute_speed_of_sound(density: f32, pressure: f32) -> f32 { (GAMMA * pressure / density).sqrt() } -fn compute_step_factor( - nelr: usize, - variables: &[f32], - areas: &[f32] -) -> Vec<f32> { +fn compute_step_factor(nelr: usize, variables: &[f32], areas: &[f32]) -> Vec<f32> { let mut step_factors = vec![0.0; nelr]; for i in 0..nelr { let density = variables[i + VAR_DENSITY * nelr]; let momentum = Float3 { - x: variables[i + (VAR_MOMENTUM+0) * nelr], - y: variables[i + (VAR_MOMENTUM+1) * nelr], - z: variables[i + (VAR_MOMENTUM+2) * nelr], + x: variables[i + (VAR_MOMENTUM + 0) * nelr], + y: variables[i + (VAR_MOMENTUM + 1) * nelr], + z: variables[i + (VAR_MOMENTUM + 2) * nelr], }; let density_energy = variables[i + VAR_DENSITY_ENERGY * nelr]; @@ -93,16 +94,16 @@ fn compute_flux_euler( ff_fc_momentum_z: &Float3, ff_fc_density_energy: &Float3, ) -> Vec<f32> { - let smoothing_coefficient : f32 = 0.2; + let smoothing_coefficient: f32 = 0.2; let mut fluxes = vec![0.0; nelr * NVAR]; for i in 0..nelr { let density_i = variables[i + VAR_DENSITY * nelr]; let momentum_i = Float3 { - x: variables[i + (VAR_MOMENTUM+0) * nelr], - y: variables[i + (VAR_MOMENTUM+1) * nelr], - z: variables[i + (VAR_MOMENTUM+2) * nelr], + x: variables[i + (VAR_MOMENTUM + 0) * nelr], + y: variables[i + (VAR_MOMENTUM + 1) * nelr], + z: variables[i + (VAR_MOMENTUM + 2) * nelr], }; let density_energy_i = variables[i + VAR_DENSITY_ENERGY * nelr]; @@ -113,12 +114,22 @@ fn compute_flux_euler( let pressure_i = compute_pressure(density_i, density_energy_i, speed_sqd_i); let speed_of_sound_i = compute_speed_of_sound(density_i, pressure_i); - let (fc_i_momentum_x, fc_i_momentum_y, fc_i_momentum_z, fc_i_density_energy) - = compute_flux_contribution(density_i, &momentum_i, density_energy_i, pressure_i, &velocity_i); + let (fc_i_momentum_x, fc_i_momentum_y, fc_i_momentum_z, fc_i_density_energy) = + compute_flux_contribution( + density_i, + &momentum_i, + density_energy_i, + pressure_i, + &velocity_i, + ); - let mut flux_i_density : f32 = 0.0; - let mut flux_i_momentum = Float3 { x: 0.0, y: 0.0, z: 0.0 }; - let mut flux_i_density_energy : f32 = 0.0; + let mut flux_i_density: f32 = 0.0; + let mut flux_i_momentum = Float3 { + x: 0.0, + y: 0.0, + z: 0.0, + }; + let mut flux_i_density_energy: f32 = 0.0; for j in 0..NNB { let nb = elements_surrounding_elements[i + j * nelr]; @@ -127,15 +138,17 @@ fn compute_flux_euler( y: normals[i + (j + 1 * NNB) * nelr], z: normals[i + (j + 2 * NNB) * nelr], }; - let normal_len = (normal.x * normal.x + normal.y * normal.y + normal.z * normal.z).sqrt(); + let normal_len = + (normal.x * normal.x + normal.y * normal.y + normal.z * normal.z).sqrt(); - if nb >= 0 { // legitimate neighbor + if nb >= 0 { + // legitimate neighbor let nb = nb as usize; let density_nb = variables[nb + VAR_DENSITY * nelr]; let momentum_nb = Float3 { - x: variables[nb + (VAR_MOMENTUM+0) * nelr], - y: variables[nb + (VAR_MOMENTUM+1) * nelr], - z: variables[nb + (VAR_MOMENTUM+2) * nelr], + x: variables[nb + (VAR_MOMENTUM + 0) * nelr], + y: variables[nb + (VAR_MOMENTUM + 1) * nelr], + z: variables[nb + (VAR_MOMENTUM + 2) * nelr], }; let density_energy_nb = variables[nb + VAR_DENSITY_ENERGY * nelr]; let velocity_nb = compute_velocity(density_nb, &momentum_nb); @@ -143,11 +156,20 @@ fn compute_flux_euler( let pressure_nb = compute_pressure(density_nb, density_energy_nb, speed_sqd_nb); let speed_of_sound_nb = compute_speed_of_sound(density_nb, pressure_nb); - let (fc_nb_momentum_x, fc_nb_momentum_y, fc_nb_momentum_z, fc_nb_density_energy) - = compute_flux_contribution(density_nb, &momentum_nb, density_energy_nb, pressure_nb, &velocity_nb); + let (fc_nb_momentum_x, fc_nb_momentum_y, fc_nb_momentum_z, fc_nb_density_energy) = + compute_flux_contribution( + density_nb, + &momentum_nb, + density_energy_nb, + pressure_nb, + &velocity_nb, + ); // artificial viscosity - let factor = -normal_len * smoothing_coefficient * 0.5 * (speed_i + speed_sqd_nb.sqrt() + speed_of_sound_i + speed_of_sound_nb); + let factor = -normal_len + * smoothing_coefficient + * 0.5 + * (speed_i + speed_sqd_nb.sqrt() + speed_of_sound_i + speed_of_sound_nb); flux_i_density += factor * (density_i - density_nb); flux_i_density_energy += factor * (density_energy_i - density_energy_nb); flux_i_momentum.x += factor * (momentum_i.x - momentum_nb.x); @@ -175,27 +197,29 @@ fn compute_flux_euler( flux_i_momentum.x += factor * (fc_nb_momentum_x.z + fc_i_momentum_x.z); flux_i_momentum.y += factor * (fc_nb_momentum_y.z + fc_i_momentum_y.z); flux_i_momentum.z += factor * (fc_nb_momentum_z.z + fc_i_momentum_z.z); - } else if nb == -1 { // a wing boundary + } else if nb == -1 { + // a wing boundary flux_i_momentum.x += normal.x * pressure_i; flux_i_momentum.y += normal.y * pressure_i; flux_i_momentum.z += normal.z * pressure_i; - } else if nb == -2 { // a far field boundary + } else if nb == -2 { + // a far field boundary let factor = 0.5 * normal.x; - flux_i_density += factor * (ff_variable[VAR_MOMENTUM+0] + momentum_i.x); + flux_i_density += factor * (ff_variable[VAR_MOMENTUM + 0] + momentum_i.x); flux_i_density_energy += factor * (ff_fc_density_energy.x + fc_i_density_energy.x); flux_i_momentum.x += factor * (ff_fc_momentum_x.x + fc_i_momentum_x.x); flux_i_momentum.y += factor * (ff_fc_momentum_y.x + fc_i_momentum_y.x); flux_i_momentum.z += factor * (ff_fc_momentum_z.x + fc_i_momentum_z.x); let factor = 0.5 * normal.y; - flux_i_density += factor * (ff_variable[VAR_MOMENTUM+1] + momentum_i.y); + flux_i_density += factor * (ff_variable[VAR_MOMENTUM + 1] + momentum_i.y); flux_i_density_energy += factor * (ff_fc_density_energy.y + fc_i_density_energy.y); flux_i_momentum.x += factor * (ff_fc_momentum_x.y + fc_i_momentum_x.y); flux_i_momentum.y += factor * (ff_fc_momentum_y.y + fc_i_momentum_y.y); flux_i_momentum.z += factor * (ff_fc_momentum_z.y + fc_i_momentum_z.y); let factor = 0.5 * normal.z; - flux_i_density += factor * (ff_variable[VAR_MOMENTUM+2] + momentum_i.z); + flux_i_density += factor * (ff_variable[VAR_MOMENTUM + 2] + momentum_i.z); flux_i_density_energy += factor * (ff_fc_density_energy.z + fc_i_density_energy.z); flux_i_momentum.x += factor * (ff_fc_momentum_x.z + fc_i_momentum_x.z); flux_i_momentum.y += factor * (ff_fc_momentum_y.z + fc_i_momentum_y.z); @@ -204,9 +228,9 @@ fn compute_flux_euler( } fluxes[i + VAR_DENSITY * nelr] = flux_i_density; - fluxes[i + (VAR_MOMENTUM+0) * nelr] = flux_i_momentum.x; - fluxes[i + (VAR_MOMENTUM+1) * nelr] = flux_i_momentum.y; - fluxes[i + (VAR_MOMENTUM+2) * nelr] = flux_i_momentum.z; + fluxes[i + (VAR_MOMENTUM + 0) * nelr] = flux_i_momentum.x; + fluxes[i + (VAR_MOMENTUM + 1) * nelr] = flux_i_momentum.y; + fluxes[i + (VAR_MOMENTUM + 2) * nelr] = flux_i_momentum.z; fluxes[i + VAR_DENSITY_ENERGY * nelr] = flux_i_density_energy; } @@ -225,9 +249,9 @@ fn compute_flux_contributions( for i in 0..nelr { let density_i = variables[i + VAR_DENSITY * nelr]; let momentum_i = Float3 { - x: variables[i + (VAR_MOMENTUM+0) * nelr], - y: variables[i + (VAR_MOMENTUM+1) * nelr], - z: variables[i + (VAR_MOMENTUM+2) * nelr], + x: variables[i + (VAR_MOMENTUM + 0) * nelr], + y: variables[i + (VAR_MOMENTUM + 1) * nelr], + z: variables[i + (VAR_MOMENTUM + 2) * nelr], }; let density_energy_i = variables[i + VAR_DENSITY_ENERGY * nelr]; @@ -238,8 +262,14 @@ fn compute_flux_contributions( let pressure_i = compute_pressure(density_i, density_energy_i, speed_sqd_i); let speed_of_sound_i = compute_speed_of_sound(density_i, pressure_i); - let (fc_i_momentum_x, fc_i_momentum_y, fc_i_momentum_z, fc_i_density_energy) - = compute_flux_contribution(density_i, &momentum_i, density_energy_i, pressure_i, &velocity_i); + let (fc_i_momentum_x, fc_i_momentum_y, fc_i_momentum_z, fc_i_density_energy) = + compute_flux_contribution( + density_i, + &momentum_i, + density_energy_i, + pressure_i, + &velocity_i, + ); fc_momentum_x[i + 0 * nelr] = fc_i_momentum_x.x; fc_momentum_x[i + 1 * nelr] = fc_i_momentum_x.y; @@ -258,7 +288,12 @@ fn compute_flux_contributions( fc_density_energy[i + 2 * nelr] = fc_i_density_energy.z; } - (fc_momentum_x, fc_momentum_y, fc_momentum_z, fc_density_energy) + ( + fc_momentum_x, + fc_momentum_y, + fc_momentum_z, + fc_density_energy, + ) } fn compute_flux_pre_euler( @@ -276,16 +311,16 @@ fn compute_flux_pre_euler( ff_fc_momentum_z: &Float3, ff_fc_density_energy: &Float3, ) -> Vec<f32> { - let smoothing_coefficient : f32 = 0.2; + let smoothing_coefficient: f32 = 0.2; let mut fluxes = vec![0.0; nelr * NVAR]; for i in 0..nelr { let density_i = variables[i + VAR_DENSITY * nelr]; let momentum_i = Float3 { - x: variables[i + (VAR_MOMENTUM+0) * nelr], - y: variables[i + (VAR_MOMENTUM+1) * nelr], - z: variables[i + (VAR_MOMENTUM+2) * nelr], + x: variables[i + (VAR_MOMENTUM + 0) * nelr], + y: variables[i + (VAR_MOMENTUM + 1) * nelr], + z: variables[i + (VAR_MOMENTUM + 2) * nelr], }; let density_energy_i = variables[i + VAR_DENSITY_ENERGY * nelr]; @@ -297,29 +332,33 @@ fn compute_flux_pre_euler( let speed_of_sound_i = compute_speed_of_sound(density_i, pressure_i); let fc_i_momentum_x = Float3 { - x: fc_momentum_x[i + 0*nelr], - y: fc_momentum_x[i + 1*nelr], - z: fc_momentum_x[i + 2*nelr], + x: fc_momentum_x[i + 0 * nelr], + y: fc_momentum_x[i + 1 * nelr], + z: fc_momentum_x[i + 2 * nelr], }; let fc_i_momentum_y = Float3 { - x: fc_momentum_y[i + 0*nelr], - y: fc_momentum_y[i + 1*nelr], - z: fc_momentum_y[i + 2*nelr], + x: fc_momentum_y[i + 0 * nelr], + y: fc_momentum_y[i + 1 * nelr], + z: fc_momentum_y[i + 2 * nelr], }; let fc_i_momentum_z = Float3 { - x: fc_momentum_z[i + 0*nelr], - y: fc_momentum_z[i + 1*nelr], - z: fc_momentum_z[i + 2*nelr], + x: fc_momentum_z[i + 0 * nelr], + y: fc_momentum_z[i + 1 * nelr], + z: fc_momentum_z[i + 2 * nelr], }; let fc_i_density_energy = Float3 { - x: fc_density_energy[i + 0*nelr], - y: fc_density_energy[i + 1*nelr], - z: fc_density_energy[i + 2*nelr], + x: fc_density_energy[i + 0 * nelr], + y: fc_density_energy[i + 1 * nelr], + z: fc_density_energy[i + 2 * nelr], }; - let mut flux_i_density : f32 = 0.0; - let mut flux_i_momentum = Float3 { x: 0.0, y: 0.0, z: 0.0 }; - let mut flux_i_density_energy : f32 = 0.0; + let mut flux_i_density: f32 = 0.0; + let mut flux_i_momentum = Float3 { + x: 0.0, + y: 0.0, + z: 0.0, + }; + let mut flux_i_density_energy: f32 = 0.0; for j in 0..NNB { let nb = elements_surrounding_elements[i + j * nelr]; @@ -328,15 +367,16 @@ fn compute_flux_pre_euler( y: normals[i + (j + 1 * NNB) * nelr], z: normals[i + (j + 2 * NNB) * nelr], }; - let normal_len = (normal.x * normal.x + normal.y * normal.y + normal.z * normal.z).sqrt(); + let normal_len = + (normal.x * normal.x + normal.y * normal.y + normal.z * normal.z).sqrt(); if nb >= 0 { let nb = nb as usize; let density_nb = variables[nb + VAR_DENSITY * nelr]; let momentum_nb = Float3 { - x: variables[nb + (VAR_MOMENTUM+0) * nelr], - y: variables[nb + (VAR_MOMENTUM+1) * nelr], - z: variables[nb + (VAR_MOMENTUM+2) * nelr], + x: variables[nb + (VAR_MOMENTUM + 0) * nelr], + y: variables[nb + (VAR_MOMENTUM + 1) * nelr], + z: variables[nb + (VAR_MOMENTUM + 2) * nelr], }; let density_energy_nb = variables[nb + VAR_DENSITY_ENERGY * nelr]; let velocity_nb = compute_velocity(density_nb, &momentum_nb); @@ -345,28 +385,31 @@ fn compute_flux_pre_euler( let speed_of_sound_nb = compute_speed_of_sound(density_nb, pressure_nb); let fc_nb_momentum_x = Float3 { - x: fc_momentum_x[nb + 0*nelr], - y: fc_momentum_x[nb + 1*nelr], - z: fc_momentum_x[nb + 2*nelr], + x: fc_momentum_x[nb + 0 * nelr], + y: fc_momentum_x[nb + 1 * nelr], + z: fc_momentum_x[nb + 2 * nelr], }; let fc_nb_momentum_y = Float3 { - x: fc_momentum_y[nb + 0*nelr], - y: fc_momentum_y[nb + 1*nelr], - z: fc_momentum_y[nb + 2*nelr], + x: fc_momentum_y[nb + 0 * nelr], + y: fc_momentum_y[nb + 1 * nelr], + z: fc_momentum_y[nb + 2 * nelr], }; let fc_nb_momentum_z = Float3 { - x: fc_momentum_z[nb + 0*nelr], - y: fc_momentum_z[nb + 1*nelr], - z: fc_momentum_z[nb + 2*nelr], + x: fc_momentum_z[nb + 0 * nelr], + y: fc_momentum_z[nb + 1 * nelr], + z: fc_momentum_z[nb + 2 * nelr], }; let fc_nb_density_energy = Float3 { - x: fc_density_energy[nb + 0*nelr], - y: fc_density_energy[nb + 1*nelr], - z: fc_density_energy[nb + 2*nelr], + x: fc_density_energy[nb + 0 * nelr], + y: fc_density_energy[nb + 1 * nelr], + z: fc_density_energy[nb + 2 * nelr], }; // artificial viscosity - let factor = -normal_len * smoothing_coefficient * 0.5 * (speed_i + speed_sqd_nb.sqrt() + speed_of_sound_i + speed_of_sound_nb); + let factor = -normal_len + * smoothing_coefficient + * 0.5 + * (speed_i + speed_sqd_nb.sqrt() + speed_of_sound_i + speed_of_sound_nb); flux_i_density += factor * (density_i - density_nb); flux_i_density_energy += factor * (density_energy_i - density_energy_nb); flux_i_momentum.x += factor * (momentum_i.x - momentum_nb.x); @@ -400,21 +443,21 @@ fn compute_flux_pre_euler( flux_i_momentum.z += normal.z * pressure_i; } else if nb == -2 { let factor = 0.5 * normal.x; - flux_i_density += factor * (ff_variable[VAR_MOMENTUM+0] + momentum_i.x); + flux_i_density += factor * (ff_variable[VAR_MOMENTUM + 0] + momentum_i.x); flux_i_density_energy += factor * (ff_fc_density_energy.x + fc_i_density_energy.x); flux_i_momentum.x += factor * (ff_fc_momentum_x.x + fc_i_momentum_x.x); flux_i_momentum.y += factor * (ff_fc_momentum_y.x + fc_i_momentum_y.x); flux_i_momentum.z += factor * (ff_fc_momentum_z.x + fc_i_momentum_z.x); let factor = 0.5 * normal.y; - flux_i_density += factor * (ff_variable[VAR_MOMENTUM+1] + momentum_i.y); + flux_i_density += factor * (ff_variable[VAR_MOMENTUM + 1] + momentum_i.y); flux_i_density_energy += factor * (ff_fc_density_energy.y + fc_i_density_energy.y); flux_i_momentum.x += factor * (ff_fc_momentum_x.y + fc_i_momentum_x.y); flux_i_momentum.y += factor * (ff_fc_momentum_y.y + fc_i_momentum_y.y); flux_i_momentum.z += factor * (ff_fc_momentum_z.y + fc_i_momentum_z.y); let factor = 0.5 * normal.z; - flux_i_density += factor * (ff_variable[VAR_MOMENTUM+2] + momentum_i.z); + flux_i_density += factor * (ff_variable[VAR_MOMENTUM + 2] + momentum_i.z); flux_i_density_energy += factor * (ff_fc_density_energy.z + fc_i_density_energy.z); flux_i_momentum.x += factor * (ff_fc_momentum_x.z + fc_i_momentum_x.z); flux_i_momentum.y += factor * (ff_fc_momentum_y.z + fc_i_momentum_y.z); @@ -423,9 +466,9 @@ fn compute_flux_pre_euler( } fluxes[i + VAR_DENSITY * nelr] = flux_i_density; - fluxes[i + (VAR_MOMENTUM+0) * nelr] = flux_i_momentum.x; - fluxes[i + (VAR_MOMENTUM+1) * nelr] = flux_i_momentum.y; - fluxes[i + (VAR_MOMENTUM+2) * nelr] = flux_i_momentum.z; + fluxes[i + (VAR_MOMENTUM + 0) * nelr] = flux_i_momentum.x; + fluxes[i + (VAR_MOMENTUM + 1) * nelr] = flux_i_momentum.y; + fluxes[i + (VAR_MOMENTUM + 2) * nelr] = flux_i_momentum.z; fluxes[i + VAR_DENSITY_ENERGY * nelr] = flux_i_density_energy; } @@ -438,15 +481,20 @@ fn time_step( old_variables: &[f32], variables: &mut AlignedSlice<f32>, step_factors: &[f32], - fluxes: &[f32] + fluxes: &[f32], ) { for i in 0..nelr { let factor = step_factors[i] / (RK + 1 - j) as f32; - variables[i + VAR_DENSITY*nelr] = old_variables[i + VAR_DENSITY*nelr] + factor * fluxes[i + VAR_DENSITY * nelr]; - variables[i + (VAR_MOMENTUM+0)*nelr] = old_variables[i + (VAR_MOMENTUM+0)*nelr] + factor * fluxes[i + (VAR_MOMENTUM+0)*nelr]; - variables[i + (VAR_MOMENTUM+1)*nelr] = old_variables[i + (VAR_MOMENTUM+1)*nelr] + factor * fluxes[i + (VAR_MOMENTUM+1)*nelr]; - variables[i + (VAR_MOMENTUM+2)*nelr] = old_variables[i + (VAR_MOMENTUM+2)*nelr] + factor * fluxes[i + (VAR_MOMENTUM+2)*nelr]; - variables[i + VAR_DENSITY_ENERGY*nelr] = old_variables[i + VAR_DENSITY_ENERGY*nelr] + factor * fluxes[i + VAR_DENSITY_ENERGY*nelr]; + variables[i + VAR_DENSITY * nelr] = + old_variables[i + VAR_DENSITY * nelr] + factor * fluxes[i + VAR_DENSITY * nelr]; + variables[i + (VAR_MOMENTUM + 0) * nelr] = old_variables[i + (VAR_MOMENTUM + 0) * nelr] + + factor * fluxes[i + (VAR_MOMENTUM + 0) * nelr]; + variables[i + (VAR_MOMENTUM + 1) * nelr] = old_variables[i + (VAR_MOMENTUM + 1) * nelr] + + factor * fluxes[i + (VAR_MOMENTUM + 1) * nelr]; + variables[i + (VAR_MOMENTUM + 2) * nelr] = old_variables[i + (VAR_MOMENTUM + 2) * nelr] + + factor * fluxes[i + (VAR_MOMENTUM + 2) * nelr]; + variables[i + VAR_DENSITY_ENERGY * nelr] = old_variables[i + VAR_DENSITY_ENERGY * nelr] + + factor * fluxes[i + VAR_DENSITY_ENERGY * nelr]; } } @@ -479,7 +527,14 @@ pub fn euler( ff_fc_momentum_z, ff_fc_density_energy, ); - time_step(j, nelr, old_variables.as_slice(), &mut variables, &step_factors, &fluxes); + time_step( + j, + nelr, + old_variables.as_slice(), + &mut variables, + &step_factors, + &fluxes, + ); } } @@ -504,11 +559,8 @@ pub fn pre_euler( let step_factors = compute_step_factor(nelr, variables.as_slice(), areas); for j in 0..RK { - let (fc_momentum_x, fc_momentum_y, fc_momentum_z, fc_density_energy) - = compute_flux_contributions( - nelr, - variables.as_slice(), - ); + let (fc_momentum_x, fc_momentum_y, fc_momentum_z, fc_density_energy) = + compute_flux_contributions(nelr, variables.as_slice()); let fluxes = compute_flux_pre_euler( nelr, variables.as_slice(), @@ -524,7 +576,14 @@ pub fn pre_euler( ff_fc_momentum_z, ff_fc_density_energy, ); - time_step(j, nelr, old_variables.as_slice(), &mut variables, &step_factors, &fluxes); + time_step( + j, + nelr, + old_variables.as_slice(), + &mut variables, + &step_factors, + &fluxes, + ); } } diff --git a/juno_samples/rodinia/cfd/src/setup.rs b/juno_samples/rodinia/cfd/src/setup.rs index e005fccf98e2556c6fc17147a10591f6334d9dca..ad2ee9612b9959783878386d8de2bb74a270c7f6 100644 --- a/juno_samples/rodinia/cfd/src/setup.rs +++ b/juno_samples/rodinia/cfd/src/setup.rs @@ -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> { diff --git a/juno_samples/rodinia/srad/src/graphics.rs b/juno_samples/rodinia/srad/src/graphics.rs index 9b93e98732ec1246a359cba1526856806baa6389..be34ef7afea20b2aafe09a7d7805ba9bf7b05ec1 100644 --- a/juno_samples/rodinia/srad/src/graphics.rs +++ b/juno_samples/rodinia/srad/src/graphics.rs @@ -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, + }, + )) } diff --git a/juno_samples/rodinia/srad/src/main.rs b/juno_samples/rodinia/srad/src/main.rs index 1bbb9d473b19f1ac9035dc14c2a276a0e2a2060b..1b99b41aada5341fb3157cfb0d97be3a99e73796 100644 --- a/juno_samples/rodinia/srad/src/main.rs +++ b/juno_samples/rodinia/srad/src/main.rs @@ -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, + }); } diff --git a/juno_samples/rodinia/srad/src/rust_srad.rs b/juno_samples/rodinia/srad/src/rust_srad.rs index 6799659978d7bd14de29412400e737190679faa3..3226e35faf2410f94ab887019d54abafe0219454 100644 --- a/juno_samples/rodinia/srad/src/rust_srad.rs +++ b/juno_samples/rodinia/srad/src/rust_srad.rs @@ -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));