]> git.proxmox.com Git - rustc.git/blame - src/test/bench/shootout-spectralnorm.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / test / bench / shootout-spectralnorm.rs
CommitLineData
1a4d82fc
JJ
1// The Computer Language Benchmarks Game
2// http://benchmarksgame.alioth.debian.org/
223e47cc 3//
1a4d82fc
JJ
4// contributed by the Rust Project Developers
5
6// Copyright (c) 2012-2014 The Rust Project Developers
7//
8// All rights reserved.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions
12// are met:
13//
14// - Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
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
20// distribution.
21//
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.
27//
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.
40
41// no-pretty-expanded FIXME #15189
42
43#![allow(non_snake_case)]
62682a34 44#![feature(unboxed_closures, iter_arith, core_simd, scoped)]
1a4d82fc 45
85aaf69f 46use std::thread;
85aaf69f 47use std::env;
1a4d82fc 48use std::simd::f64x2;
223e47cc 49
1a4d82fc 50fn main() {
85aaf69f
SL
51 let mut args = env::args();
52 let answer = spectralnorm(if env::var_os("RUST_BENCH").is_some() {
1a4d82fc
JJ
53 5500
54 } else if args.len() < 2 {
55 2000
56 } else {
85aaf69f 57 args.nth(1).unwrap().parse().unwrap()
1a4d82fc
JJ
58 });
59 println!("{:.9}", answer);
970d7e83 60}
223e47cc 61
c34b1796 62fn spectralnorm(n: usize) -> f64 {
1a4d82fc 63 assert!(n % 2 == 0, "only even lengths are accepted");
c1a9b12d 64 let mut u = vec![1.0; n];
1a4d82fc
JJ
65 let mut v = u.clone();
66 let mut tmp = v.clone();
85aaf69f
SL
67 for _ in 0..10 {
68 mult_AtAv(&u, &mut v, &mut tmp);
69 mult_AtAv(&v, &mut u, &mut tmp);
970d7e83 70 }
85aaf69f 71 (dot(&u, &v) / dot(&v, &v)).sqrt()
223e47cc
LB
72}
73
1a4d82fc
JJ
74fn mult_AtAv(v: &[f64], out: &mut [f64], tmp: &mut [f64]) {
75 mult_Av(v, tmp);
76 mult_Atv(tmp, out);
77}
78
79fn mult_Av(v: &[f64], out: &mut [f64]) {
80 parallel(out, |start, out| mult(v, out, start, |i, j| A(i, j)));
81}
82
83fn mult_Atv(v: &[f64], out: &mut [f64]) {
84 parallel(out, |start, out| mult(v, out, start, |i, j| A(j, i)));
223e47cc
LB
85}
86
c34b1796
AL
87fn mult<F>(v: &[f64], out: &mut [f64], start: usize, a: F)
88 where F: Fn(usize, usize) -> f64 {
1a4d82fc
JJ
89 for (i, slot) in out.iter_mut().enumerate().map(|(i, s)| (i + start, s)) {
90 let mut sum = f64x2(0.0, 0.0);
91 for (j, chunk) in v.chunks(2).enumerate().map(|(j, s)| (2 * j, s)) {
92 let top = f64x2(chunk[0], chunk[1]);
93 let bot = f64x2(a(i, j), a(i, j + 1));
94 sum += top / bot;
223e47cc 95 }
1a4d82fc
JJ
96 let f64x2(a, b) = sum;
97 *slot = a + b;
223e47cc
LB
98 }
99}
100
c34b1796 101fn A(i: usize, j: usize) -> f64 {
1a4d82fc 102 ((i + j) * (i + j + 1) / 2 + i + 1) as f64
223e47cc
LB
103}
104
1a4d82fc 105fn dot(v: &[f64], u: &[f64]) -> f64 {
62682a34 106 v.iter().zip(u).map(|(a, b)| *a * *b).sum()
1a4d82fc
JJ
107}
108
109
1a4d82fc
JJ
110// Executes a closure in parallel over the given mutable slice. The closure `f`
111// is run in parallel and yielded the starting index within `v` as well as a
112// sub-slice of `v`.
85aaf69f
SL
113fn parallel<'a,T, F>(v: &mut [T], ref f: F)
114 where T: Send + Sync + 'a,
c34b1796
AL
115 F: Fn(usize, &mut [T]) + Sync + 'a {
116 // FIXME: pick a more appropriate parallel factor
117 let parallelism = 4;
118 let size = v.len() / parallelism + 1;
1a4d82fc 119 v.chunks_mut(size).enumerate().map(|(i, chunk)| {
85aaf69f
SL
120 thread::scoped(move|| {
121 f(i * size, chunk)
1a4d82fc
JJ
122 })
123 }).collect::<Vec<_>>();
223e47cc 124}