1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
4 // contributed by the Rust Project Developers
6 // Copyright (c) 2012-2014 The Rust Project Developers
8 // All rights reserved.
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
14 // - Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
17 // - Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in
19 // the documentation and/or other materials provided with the
22 // - Neither the name of "The Computer Language Benchmarks Game" nor
23 // the name of "The Computer Language Shootout Benchmarks" nor the
24 // names of its contributors may be used to endorse or promote
25 // products derived from this software without specific prior
26 // written permission.
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 // OF THE POSSIBILITY OF SUCH DAMAGE.
41 // no-pretty-expanded FIXME #15189
43 #![allow(non_snake_case)]
44 #![feature(unboxed_closures)]
46 use std::iter::{repeat, AdditiveIterator};
47 use std::thread::Thread;
55 let args = os::args();
56 let answer = spectralnorm(if os::getenv("RUST_BENCH").is_some() {
58 } else if args.len() < 2 {
61 args[1].parse().unwrap()
63 println!("{:.9}", answer);
66 fn spectralnorm(n: uint) -> f64 {
67 assert!(n % 2 == 0, "only even lengths are accepted");
68 let mut u = repeat(1.0).take(n).collect::<Vec<_>>();
69 let mut v = u.clone();
70 let mut tmp = v.clone();
71 for _ in range(0u, 10) {
72 mult_AtAv(u.as_slice(), v.as_mut_slice(), tmp.as_mut_slice());
73 mult_AtAv(v.as_slice(), u.as_mut_slice(), tmp.as_mut_slice());
75 (dot(u.as_slice(), v.as_slice()) / dot(v.as_slice(), v.as_slice())).sqrt()
78 fn mult_AtAv(v: &[f64], out: &mut [f64], tmp: &mut [f64]) {
83 fn mult_Av(v: &[f64], out: &mut [f64]) {
84 parallel(out, |start, out| mult(v, out, start, |i, j| A(i, j)));
87 fn mult_Atv(v: &[f64], out: &mut [f64]) {
88 parallel(out, |start, out| mult(v, out, start, |i, j| A(j, i)));
91 fn mult<F>(v: &[f64], out: &mut [f64], start: uint, a: F)
92 where F: Fn(uint, uint) -> f64 {
93 for (i, slot) in out.iter_mut().enumerate().map(|(i, s)| (i + start, s)) {
94 let mut sum = f64x2(0.0, 0.0);
95 for (j, chunk) in v.chunks(2).enumerate().map(|(j, s)| (2 * j, s)) {
96 let top = f64x2(chunk[0], chunk[1]);
97 let bot = f64x2(a(i, j), a(i, j + 1));
100 let f64x2(a, b) = sum;
105 fn A(i: uint, j: uint) -> f64 {
106 ((i + j) * (i + j + 1) / 2 + i + 1) as f64
109 fn dot(v: &[f64], u: &[f64]) -> f64 {
110 v.iter().zip(u.iter()).map(|(a, b)| *a * *b).sum()
116 unsafe impl<T: 'static> Send for Racy<T> {}
118 // Executes a closure in parallel over the given mutable slice. The closure `f`
119 // is run in parallel and yielded the starting index within `v` as well as a
121 fn parallel<T, F>(v: &mut [T], f: F)
122 where T: Send + Sync,
123 F: Fn(uint, &mut [T]) + Sync {
124 let size = v.len() / os::num_cpus() + 1;
126 v.chunks_mut(size).enumerate().map(|(i, chunk)| {
127 // Need to convert `f` and `chunk` to something that can cross the task
129 let f = Racy(&f as *const _ as *const uint);
130 let raw = Racy(chunk.repr());
131 Thread::spawn(move|| {
132 let f = f.0 as *const F;
133 unsafe { (*f)(i * size, mem::transmute(raw.0)) }
135 }).collect::<Vec<_>>();