Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 130 lines (115 sloc) 4.339 kb
a182f13 @TeXitoi Relicense shootout-spectralnorm.rs to the shootout license
TeXitoi authored
1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
de64ff2 @Thiez Added missing copyright notice to shootout-spectralnorm.
Thiez authored
3 //
a182f13 @TeXitoi Relicense shootout-spectralnorm.rs to the shootout license
TeXitoi authored
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.
de64ff2 @Thiez Added missing copyright notice to shootout-spectralnorm.
Thiez authored
40
c956f76 @jbclements replaced ignore-pretty with no-pretty-expanded
jbclements authored
41 // no-pretty-expanded FIXME #15189
e880c42 @jbclements more loops to be ignored by pretty-rpass
jbclements authored
42
de7abd8 @P1start Unify non-snake-case lints and non-uppercase statics lints
P1start authored
43 #![allow(non_snake_case)]
a9fd41e @aturon Fallout: move from scoped to spawn
aturon authored
44 #![feature(unboxed_closures, core, os, scoped)]
44403f7 @brson test: xfail a bunch of tests that are incorrectly reading os::args()[1]
brson authored
45
97f24a8 @tbu- Make `sum` and `product` inherent methods on `Iterator`
tbu- authored
46 use std::iter::repeat;
d0de2b4 @aturon Fallout from stabilization
aturon authored
47 use std::thread;
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
48 use std::mem;
e965ba8 @bjz Remove lots of numeric traits from the preludes
bjz authored
49 use std::num::Float;
206ab89 @pcwalton librustc: Stop reexporting the standard modules from prelude.
pcwalton authored
50 use std::os;
220bf41 @nagisa Cleanup getenv from tests and benchmarks
nagisa authored
51 use std::env;
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
52 use std::raw::Repr;
53 use std::simd::f64x2;
d1affff @graydon Reliciense makefiles and testsuite. Yup.
graydon authored
54
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
55 fn main() {
7d941fa @nagisa Replace some uses of deprecated os functions
nagisa authored
56 let mut args = env::args();
220bf41 @nagisa Cleanup getenv from tests and benchmarks
nagisa authored
57 let answer = spectralnorm(if env::var_os("RUST_BENCH").is_some() {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
58 5500
59 } else if args.len() < 2 {
60 2000
61 } else {
7d941fa @nagisa Replace some uses of deprecated os functions
nagisa authored
62 args.nth(1).unwrap().parse().unwrap()
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
63 });
4af3494 @alexcrichton std: Stabilize std::fmt
alexcrichton authored
64 println!("{:.9}", answer);
9738c2a @pcwalton test: Rewrite nbody and spectralnorm shootout benchmarks
pcwalton authored
65 }
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
66
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
67 fn spectralnorm(n: usize) -> f64 {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
68 assert!(n % 2 == 0, "only even lengths are accepted");
7d8d06f @alexcrichton Remove deprecated functionality
alexcrichton authored
69 let mut u = repeat(1.0).take(n).collect::<Vec<_>>();
70 let mut v = u.clone();
71 let mut tmp = v.clone();
362d713 @nikomatsakis Fix remaining bench/debuginfo tests (and a few stragglers)
nikomatsakis authored
72 for _ in 0..10 {
17bc7d8 @japaric cleanup: replace `as[_mut]_slice()` calls with deref coercions
japaric authored
73 mult_AtAv(&u, &mut v, &mut tmp);
74 mult_AtAv(&v, &mut u, &mut tmp);
9738c2a @pcwalton test: Rewrite nbody and spectralnorm shootout benchmarks
pcwalton authored
75 }
17bc7d8 @japaric cleanup: replace `as[_mut]_slice()` calls with deref coercions
japaric authored
76 (dot(&u, &v) / dot(&v, &v)).sqrt()
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
77 }
78
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
79 fn mult_AtAv(v: &[f64], out: &mut [f64], tmp: &mut [f64]) {
80 mult_Av(v, tmp);
81 mult_Atv(tmp, out);
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
82 }
83
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
84 fn mult_Av(v: &[f64], out: &mut [f64]) {
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
85 parallel(out, |start, out| mult(v, out, start, |i, j| A(i, j)));
6bd2249 @TeXitoi shootout-spectralnorm resurection with parallelization
TeXitoi authored
86 }
87
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
88 fn mult_Atv(v: &[f64], out: &mut [f64]) {
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
89 parallel(out, |start, out| mult(v, out, start, |i, j| A(j, i)));
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
90 }
91
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
92 fn mult<F>(v: &[f64], out: &mut [f64], start: usize, a: F)
93 where F: Fn(usize, usize) -> f64 {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
94 for (i, slot) in out.iter_mut().enumerate().map(|(i, s)| (i + start, s)) {
95 let mut sum = f64x2(0.0, 0.0);
96 for (j, chunk) in v.chunks(2).enumerate().map(|(j, s)| (2 * j, s)) {
97 let top = f64x2(chunk[0], chunk[1]);
98 let bot = f64x2(a(i, j), a(i, j + 1));
99 sum += top / bot;
100 }
101 let f64x2(a, b) = sum;
102 *slot = a + b;
6bd2249 @TeXitoi shootout-spectralnorm resurection with parallelization
TeXitoi authored
103 }
104 }
105
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
106 fn A(i: usize, j: usize) -> f64 {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
107 ((i + j) * (i + j + 1) / 2 + i + 1) as f64
6bd2249 @TeXitoi shootout-spectralnorm resurection with parallelization
TeXitoi authored
108 }
109
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
110 fn dot(v: &[f64], u: &[f64]) -> f64 {
c9e6bda @bjz Revert the need for initial values with arithmetic iterators
bjz authored
111 v.iter().zip(u.iter()).map(|(a, b)| *a * *b).sum()
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
112 }
113
52072de @FlaPer87 Add a Racy type to bench tests
FlaPer87 authored
114
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
115 // Executes a closure in parallel over the given mutable slice. The closure `f`
116 // is run in parallel and yielded the starting index within `v` as well as a
117 // sub-slice of `v`.
7a14f49 @huonw Update tests for the Send - 'static change.
huonw authored
118 fn parallel<'a,T, F>(v: &mut [T], ref f: F)
119 where T: Send + Sync + 'a,
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
120 F: Fn(usize, &mut [T]) + Sync + 'a {
d4a2c94 @alexcrichton std: Clean out #[deprecated] APIs
alexcrichton authored
121 // FIXME: pick a more appropriate parallel factor
122 let parallelism = 4;
123 let size = v.len() / parallelism + 1;
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
124 v.chunks_mut(size).enumerate().map(|(i, chunk)| {
ba8ce4c @alexcrichton rollup merge of #22319: huonw/send-is-not-static
alexcrichton authored
125 thread::scoped(move|| {
7a14f49 @huonw Update tests for the Send - 'static change.
huonw authored
126 f(i * size, chunk)
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
127 })
128 }).collect::<Vec<_>>();
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
129 }
Something went wrong with that request. Please try again.