performance measurements

Each table row shows performance measurements for this Rust program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
5000.070.08?1143  0% 0% 0% 100%
3,0002.362.364,9081143  2% 0% 1% 100%
5,5007.897.904,9121143  1% 1% 1% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read spectral-norm benchmark to see what this program should do.

 notes

rustc 1.0.0-alpha (44a287e6e 2015-01-08 17:03:40 -0800)

 spectral-norm Rust program source code

// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// contributed by the Rust Project Developers
// contributed by TeXitoi

#![allow(non_snake_case)]

use std::iter::{repeat, AdditiveIterator};
use std::thread::Thread;
use std::mem;
use std::num::Float;
use std::os;
use std::raw::Repr;
use std::simd::f64x2;

fn main() {
    let n = std::os::args().get(1).and_then(|n| n.parse()).unwrap_or(100);
    let answer = spectralnorm(n);
    println!("{:.9}", answer);
}

fn spectralnorm(n: usize) -> f64 {
    assert!(n % 2 == 0, "only even lengths are accepted");
    let mut u = repeat(1.0).take(n).collect::<Vec<_>>();
    let mut v = u.clone();
    let mut tmp = v.clone();
    for _ in 0..10 {
        mult_AtAv(&*u, v.as_mut_slice(), tmp.as_mut_slice());
        mult_AtAv(&*v, u.as_mut_slice(), tmp.as_mut_slice());
    }
    (dot(&*u, &*v) / dot(&*v, &*v)).sqrt()
}

fn mult_AtAv(v: &[f64], out: &mut [f64], tmp: &mut [f64]) {
    mult_Av(v, tmp);
    mult_Atv(tmp, out);
}

fn mult_Av(v: &[f64], out: &mut [f64]) {
    parallel(out, |start, out| mult(v, out, start, |i, j| A(i, j)));
}

fn mult_Atv(v: &[f64], out: &mut [f64]) {
    parallel(out, |start, out| mult(v, out, start, |i, j| A(j, i)));
}

fn mult<F>(v: &[f64], out: &mut [f64], start: usize, a: F)
           where F: Fn(usize, usize) -> f64 {
    for (i, slot) in out.iter_mut().enumerate().map(|(i, s)| (i + start, s)) {
        let mut sum = f64x2(0.0, 0.0);
        for (j, chunk) in v.chunks(2).enumerate().map(|(j, s)| (2 * j, s)) {
            let top = f64x2(chunk[0], chunk[1]);
            let bot = f64x2(a(i, j), a(i, j + 1));
            sum += top / bot;
        }
        let f64x2(a, b) = sum;
        *slot = a + b;
    }
}

fn A(i: usize, j: usize) -> f64 {
    ((i + j) * (i + j + 1) / 2 + i + 1) as f64
}

fn dot(v: &[f64], u: &[f64]) -> f64 {
    v.iter().zip(u.iter()).map(|(a, b)| *a * *b).sum()
}


struct Racy<T>(T);

unsafe impl<T: 'static> Send for Racy<T> {}

// Executes a closure in parallel over the given mutable slice. The closure `f`
// is run in parallel and yielded the starting index within `v` as well as a
// sub-slice of `v`.
fn parallel<T, F>(v: &mut [T], f: F)
                  where T: Send + Sync,
                        F: Fn(usize, &mut [T]) + Sync {
    let size = v.len() / os::num_cpus() + 1;

    v.chunks_mut(size).enumerate().map(|(i, chunk)| {
        // Need to convert `f` and `chunk` to something that can cross the task
        // boundary.
        let f = Racy(&f as *const _ as *const usize);
        let raw = Racy(chunk.repr());
        Thread::scoped(move|| {
            let f = f.0 as *const F;
            unsafe { (*f)(i * size, mem::transmute(raw.0)) }
        })
    }).collect::<Vec<_>>();
}

 make, command-line, and program output logs

Sat, 10 Jan 2015 20:08:39 GMT

MAKE:
/usr/local/src/rust/bin/rustc -C opt-level=3 -C target-cpu=core2 -C lto spectralnorm.rs -o spectralnorm.rust_run
spectralnorm.rs:18:13: 18:26 warning: use of unstable item
spectralnorm.rs:18     let n = std::os::args().get(1).and_then(|n| n.parse()).unwrap_or(100);
                               ^~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:18:51: 18:58 warning: use of unstable item: this method was just created
spectralnorm.rs:18     let n = std::os::args().get(1).and_then(|n| n.parse()).unwrap_or(100);
                                                                     ^~~~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:51:23: 51:28 warning: use of unstable item
spectralnorm.rs:51         let mut sum = f64x2(0.0, 0.0);
                                         ^~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:53:23: 53:28 warning: use of unstable item
spectralnorm.rs:53             let top = f64x2(chunk[0], chunk[1]);
                                         ^~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:54:23: 54:28 warning: use of unstable item
spectralnorm.rs:54             let bot = f64x2(a(i, j), a(i, j + 1));
                                         ^~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:67:50: 67:55 warning: use of unstable item: needs to be re-evaluated as part of numerics reform
spectralnorm.rs:67     v.iter().zip(u.iter()).map(|(a, b)| *a * *b).sum()
                                                                    ^~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:73:25: 73:29 warning: use of unstable item: will be overhauled with new lifetime rules; see RFC 458
spectralnorm.rs:73 unsafe impl<T: 'static> Send for Racy<T> {}
                                           ^~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:81:26: 81:38 warning: use of unstable item
spectralnorm.rs:81     let size = v.len() / os::num_cpus() + 1;
                                            ^~~~~~~~~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:87:30: 87:36 warning: use of unstable item
spectralnorm.rs:87         let raw = Racy(chunk.repr());
                                                ^~~~~~
note: this feature may not be used in the beta release channel
spectralnorm.rs:88:9: 88:23 warning: use of unstable item: may change with specifics of new Send semantics
spectralnorm.rs:88         Thread::scoped(move|| {
                           ^~~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
rm spectralnorm.rs
9.73s to complete and log all make actions

COMMAND LINE:
./spectralnorm.rust_run 5500

PROGRAM OUTPUT:
1.274224153

Revised BSD license

  Home   Conclusions   License   Play