Skip to content

Commit

Permalink
bench: Add shootout-spectralnorm
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Feb 8, 2012
1 parent 708f7b9 commit 7c8d128
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/test/bench/shootout-spectralnorm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Based on spectalnorm.gcc by Sebastien Loisel

use std;

fn eval_A(i: uint, j: uint) -> float {
1.0/(((i+j)*(i+j+1u)/2u+i+1u) as float)
}

fn eval_A_times_u(u: [const float], Au: [mutable float]) {
let N = vec::len(u);
let i = 0u;
while i < N {
Au[i] = 0.0;
let j = 0u;
while j < N {
Au[i] += eval_A(i, j) * u[j];
j += 1u;
}
i += 1u;
}
}

fn eval_At_times_u(u: [const float], Au: [mutable float]) {
let N = vec::len(u);
let i = 0u;
while i < N {
Au[i] = 0.0;
let j = 0u;
while j < N {
Au[i] += eval_A(j, i) * u[j];
j += 1u;
}
i += 1u;
}
}

fn eval_AtA_times_u(u: [const float], AtAu: [mutable float]) {
let v = vec::init_elt_mut(vec::len(u), 0.0);
eval_A_times_u(u, v);
eval_At_times_u(v, AtAu);
}

fn main(args: [str]) {

let N = if vec::len(args) == 2u {
uint::from_str(args[1])
} else {
1000u
};

let u = vec::init_elt_mut(N, 1.0);
let v = vec::init_elt_mut(N, 0.0);
let i = 0u;
while i < 10u {
eval_AtA_times_u(u, v);
eval_AtA_times_u(v, u);
i += 1u;
}

let vBv = 0.0;
let vv = 0.0;
let i = 0u;
while i < N {
vBv += u[i] * v[i];
vv += v[i] * v[i];
i += 1u;
}

std::io::println(#fmt("%0.9f\n", float::sqrt(vBv / vv)));
}

0 comments on commit 7c8d128

Please sign in to comment.