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

Use rust-style loops in rodinia

parent 872b3103
No related branches found
No related tags found
1 merge request!186Rodinia
Pipeline #201732 passed
......@@ -129,7 +129,7 @@ _ "_"
0x[0-9a-fA-F]+ "HEX_INT"
0b[0-1]+ "BIN_INT"
0o[0-7]+ "OCT_INT"
[0-9]+\.[0-9]*(|e[0-9]+) "FLOAT_LIT"
[0-9]+\.[0-9]+(|e[0-9]+) "FLOAT_LIT"
@[a-zA-Z0-9_]+ "LABEL"
. "UNMATCHED"
......
......@@ -302,7 +302,7 @@ Stmt -> Result<Stmt, ()>
inclusive: false, step: None, body: Box::new($7?) }) }
| 'for' VarBind 'in' NonStructExpr '..' '=' NonStructExpr Stmts
{ Ok(Stmt::ForStmt{ span: $span, var: $2?, init: $4?, bound: $7?,
inclusive: false, step: None, body: Box::new($8?) }) }
inclusive: true, step: None, body: Box::new($8?) }) }
| 'while' NonStructExpr Stmts
{ Ok(Stmt::WhileStmt{ span : $span, cond : $2?, body : Box::new($3?) }) }
| 'return' ';'
......
......@@ -7,12 +7,12 @@ fn layer_forward<n, m: usize>(vals: f32[n + 1], weights: f32[n + 1, m + 1]) -> f
let result : f32[m + 1];
result[0] = 1.0;
for j = 0 to m {
for j in 1..=m {
let sum = 0.0;
for k = 0 to n+1 {
sum += weights[k, j+1] * vals[k];
for k in 0..=n {
sum += weights[k, j] * vals[k];
}
result[j + 1] = squash(sum);
result[j] = squash(sum);
}
return result;
......@@ -22,11 +22,11 @@ fn output_error<n: usize>(target: f32[n + 1], actual: f32[n + 1]) -> (f32, f32[n
let errsum = 0.0;
let delta : f32[n + 1];
for j = 0 to n {
let a = actual[j + 1];
let t = target[j + 1];
delta[j + 1] = a * (1.0 - a) * (t - a);
errsum += abs!(delta[j + 1]);
for j in 1..=n {
let a = actual[j];
let t = target[j];
delta[j] = a * (1.0 - a) * (t - a);
errsum += abs!(delta[j]);
}
return (errsum, delta);
......@@ -40,16 +40,16 @@ fn hidden_error<hidden_n, output_n: usize>(
let errsum = 0.0;
let delta : f32[hidden_n + 1];
for j = 0 to hidden_n {
let h = hidden_vals[j + 1];
for j in 1..=hidden_n {
let h = hidden_vals[j];
let sum = 0.0;
for k = 0 to output_n {
sum += out_delta[k + 1] * hidden_weights[j + 1, k + 1];
for k in 1..=output_n {
sum += out_delta[k] * hidden_weights[j, k];
}
delta[j + 1] = h * (1.0 - h) * sum;
errsum += abs!(delta[j + 1]);
delta[j] = h * (1.0 - h) * sum;
errsum += abs!(delta[j]);
}
return (errsum, delta);
......@@ -64,11 +64,11 @@ fn adjust_weights<n, m: usize>(
weights: f32[n + 1, m + 1],
prev_weights: f32[n + 1, m + 1]
) -> (f32[n + 1, m + 1], f32[n + 1, m + 1]) {
for j = 0 to m {
for k = 0 to n+1 {
let new_dw = ETA * delta[j+1] * vals[k] + MOMENTUM * prev_weights[k, j+1];
weights[k, j+1] += new_dw;
prev_weights[k, j+1] = new_dw;
for j in 1..=m {
for k in 0..=n {
let new_dw = ETA * delta[j] * vals[k] + MOMENTUM * prev_weights[k, j];
weights[k, j] += new_dw;
prev_weights[k, j] = new_dw;
}
}
......
......@@ -14,7 +14,7 @@ fn bfs<n, m: usize>(graph_nodes: Node[n], source: u32, edges: u32[m]) -> i32[n]
visited[source as u64] = true;
let cost: i32[n];
for i = 0 to n {
for i in 0..n {
cost[i] = -1;
}
cost[source as u64] = 0;
......@@ -25,14 +25,14 @@ fn bfs<n, m: usize>(graph_nodes: Node[n], source: u32, edges: u32[m]) -> i32[n]
while !stop {
stop = true;
for i = 0 to n {
for i in 0..n {
if mask[i] {
mask[i] = false;
let edge_start = graph_nodes[i].edge_start as u64;
let num_edges = graph_nodes[i].num_edges as u64;
for edge = edge_start to edge_start + num_edges {
for edge in edge_start..edge_start + num_edges {
let id = edges[edge] as u64;
if !visited[id] {
cost[id] = cost[i] + 1;
......@@ -42,7 +42,7 @@ fn bfs<n, m: usize>(graph_nodes: Node[n], source: u32, edges: u32[m]) -> i32[n]
}
}
for i = 0 to n {
for i in 0..n {
if updated[i] {
mask[i] = true;
visited[i] = true;
......
......@@ -49,7 +49,7 @@ fn compute_speed_of_sound(density: f32, pressure: f32) -> f32 {
fn compute_step_factor<nelr: usize>(variables: Variables::<nelr>, areas: f32[nelr]) -> f32[nelr] {
let step_factors : f32[nelr];
for i = 0 to nelr {
for i in 0..nelr {
let density = variables.density[i];
let momentum : float3;
......@@ -108,7 +108,7 @@ fn compute_flux<nelr: usize>(
const smoothing_coefficient : f32 = 0.2;
let fluxes: Variables::<nelr>;
for i = 0 to nelr {
for i in 0..nelr {
let density_i = variables.density[i];
let momentum_i = float3 { x: variables.momentum.x[i],
......@@ -131,7 +131,7 @@ fn compute_flux<nelr: usize>(
let flux_i_momentum = float3 { x: 0.0, y: 0.0, z: 0.0 };
let flux_i_density_energy : f32 = 0.0;
for j = 0 to NNB {
for j in 0..NNB {
let nb = elements_surrounding_elements[j, i];
let normal = float3 {
x: normals.x[j, i],
......@@ -235,7 +235,7 @@ fn time_step<nelr: usize>(
step_factors: f32[nelr],
fluxes: Variables::<nelr>,
) -> Variables::<nelr> {
for i = 0 to nelr {
for i in 0..nelr {
let factor = step_factors[i] / (RK + 1 - j) as f32;
variables.density[i] = old_variables.density[i] + factor * fluxes.density[i];
......@@ -251,7 +251,7 @@ fn time_step<nelr: usize>(
fn copy_vars<nelr: usize>(variables: Variables::<nelr>) -> Variables::<nelr> {
let result : Variables::<nelr>;
for i = 0 to nelr {
for i in 0..nelr {
result.density[i] = variables.density[i];
result.momentum.x[i] = variables.momentum.x[i];
result.momentum.y[i] = variables.momentum.y[i];
......@@ -275,11 +275,11 @@ fn euler<nelr: usize>(
ff_flux_contribution_momentum_y: float3,
ff_flux_contribution_momentum_z: float3,
) -> Variables::<nelr> {
for i = 0 to iterations {
for i in 0..iterations {
let old_variables = copy_vars::<nelr>(variables);
let step_factors = compute_step_factor::<nelr>(variables, areas);
for j = 0 to RK {
for j in 0..RK {
let fluxes = compute_flux::<nelr>(variables, elements_surrounding_elements,
normals, ff_variable, ff_flux_contribution_density_energy,
ff_flux_contribution_momentum_x,
......
......@@ -60,7 +60,7 @@ fn compute_speed_of_sound(density: f32, pressure: f32) -> f32 {
fn compute_step_factor<nelr: usize>(variables: Variables::<nelr>, areas: f32[nelr]) -> f32[nelr] {
let step_factors : f32[nelr];
for i = 0 to nelr {
for i in 0..nelr {
let density = variables.density[i];
let momentum : float3;
......@@ -114,7 +114,7 @@ fn compute_flux_contributions<nelr: usize>(
let fc_momentum_z: Momentum::<nelr>;
let fc_density_energy: Momentum::<nelr>;
for i = 0 to nelr {
for i in 0..nelr {
let density_i = variables.density[i];
let momentum_i = float3 { x: variables.momentum.x[i],
......@@ -169,7 +169,7 @@ fn compute_flux<nelr: usize>(
const smoothing_coefficient : f32 = 0.2;
let fluxes: Variables::<nelr>;
for i = 0 to nelr {
for i in 0..nelr {
let density_i = variables.density[i];
let momentum_i = float3 { x: variables.momentum.x[i],
......@@ -201,7 +201,7 @@ fn compute_flux<nelr: usize>(
let flux_i_momentum = float3 { x: 0.0, y: 0.0, z: 0.0 };
let flux_i_density_energy : f32 = 0.0;
for j = 0 to NNB {
for j in 0..NNB {
let nb = elements_surrounding_elements[j, i];
let normal = float3 {
x: normals.x[j, i],
......@@ -314,7 +314,7 @@ fn time_step<nelr: usize>(
step_factors: f32[nelr],
fluxes: Variables::<nelr>,
) -> Variables::<nelr> {
for i = 0 to nelr {
for i in 0..nelr {
let factor = step_factors[i] / (RK + 1 - j) as f32;
variables.density[i] = old_variables.density[i] + factor * fluxes.density[i];
......@@ -330,7 +330,7 @@ fn time_step<nelr: usize>(
fn copy_vars<nelr: usize>(variables: Variables::<nelr>) -> Variables::<nelr> {
let result : Variables::<nelr>;
for i = 0 to nelr {
for i in 0..nelr {
result.density[i] = variables.density[i];
result.momentum.x[i] = variables.momentum.x[i];
result.momentum.y[i] = variables.momentum.y[i];
......@@ -354,11 +354,11 @@ fn pre_euler<nelr: usize>(
ff_fc_momentum_y: float3,
ff_fc_momentum_z: float3,
) -> Variables::<nelr> {
for i = 0 to iterations {
for i in 0..iterations {
let old_variables = copy_vars::<nelr>(variables);
let step_factors = compute_step_factor::<nelr>(variables, areas);
for j = 0 to RK {
for j in 0..RK {
let (fc_momentum_x, fc_momentum_y, fc_momentum_z, fc_density_energy)
= compute_flux_contributions::<nelr>(variables);
let fluxes = compute_flux::<nelr>(variables, elements_surrounding_elements,
......
// HPVM put the extract and compress kernels in the host code, but the CUDA and
// OpenCL versions of Rodinia put them on the device
fn extract<nrows, ncols: usize>(inout image: f32[ncols, nrows], max: f32) {
for j = 0 to ncols {
for i = 0 to nrows {
for j in 0..ncols {
for i in 0..nrows {
image[j, i] = exp!(image[j, i] / max);
}
}
}
fn compress<nrows, ncols: usize>(inout image: f32[ncols, nrows], max: f32) {
for j = 0 to ncols {
for i = 0 to nrows {
for j in 0..ncols {
for i in 0..nrows {
image[j, i] = ln!(image[j, i]) * max;
}
}
......@@ -32,14 +32,14 @@ fn srad<nrows, ncols: usize>(
extract::<nrows, ncols>(&image, max);
for iter = 0 to niter {
for iter in 0..niter {
let sum = 0;
let sum2 = 0;
// These loops should really be interchanged, but they aren't in the
// Rodinia source (though they are in the HPVM source)
for i = 0 to nrows {
for j = 0 to ncols {
for i in 0..nrows {
for j in 0..ncols {
let tmp = image[j, i];
sum += tmp;
sum2 += tmp * tmp;
......@@ -57,8 +57,8 @@ fn srad<nrows, ncols: usize>(
let c : f32[ncols, nrows];
for j = 0 to ncols {
for i = 0 to nrows {
for j in 0..ncols {
for i in 0..nrows {
let Jc = image[j, i];
dN[j, i] = image[j, iN[i] as u64] - Jc;
dS[j, i] = image[j, iS[i] as u64] - Jc;
......@@ -82,8 +82,8 @@ fn srad<nrows, ncols: usize>(
}
}
for j = 0 to ncols {
for i = 0 to nrows {
for j in 0..ncols {
for i in 0..nrows {
let cN = c[j, i];
let cS = c[j, iS[i] as u64];
let cW = c[j, i];
......
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