Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 124 lines (109 sloc) 4.248 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)]
ce1a965 @alexcrichton Fallout in tests and docs from feature renamings
alexcrichton authored
44 #![feature(unboxed_closures, iter_arith, core_simd, scoped)]
44403f7 @brson test: xfail a bunch of tests that are incorrectly reading os::args()[1]
brson authored
45
d0de2b4 @aturon Fallout from stabilization
aturon authored
46 use std::thread;
220bf41 @nagisa Cleanup getenv from tests and benchmarks
nagisa authored
47 use std::env;
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
48 use std::simd::f64x2;
d1affff @graydon Reliciense makefiles and testsuite. Yup.
graydon authored
49
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
50 fn main() {
7d941fa @nagisa Replace some uses of deprecated os functions
nagisa authored
51 let mut args = env::args();
220bf41 @nagisa Cleanup getenv from tests and benchmarks
nagisa authored
52 let answer = spectralnorm(if env::var_os("RUST_BENCH").is_some() {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
53 5500
54 } else if args.len() < 2 {
55 2000
56 } else {
7d941fa @nagisa Replace some uses of deprecated os functions
nagisa authored
57 args.nth(1).unwrap().parse().unwrap()
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
58 });
4af3494 @alexcrichton std: Stabilize std::fmt
alexcrichton authored
59 println!("{:.9}", answer);
9738c2a @pcwalton test: Rewrite nbody and spectralnorm shootout benchmarks
pcwalton authored
60 }
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
61
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
62 fn spectralnorm(n: usize) -> f64 {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
63 assert!(n % 2 == 0, "only even lengths are accepted");
836f32e @bluss Use vec![elt; n] where possible
bluss authored
64 let mut u = vec![1.0; n];
7d8d06f @alexcrichton Remove deprecated functionality
alexcrichton authored
65 let mut v = u.clone();
66 let mut tmp = v.clone();
362d713 @nikomatsakis Fix remaining bench/debuginfo tests (and a few stragglers)
nikomatsakis authored
67 for _ in 0..10 {
17bc7d8 @japaric cleanup: replace `as[_mut]_slice()` calls with deref coercions
japaric authored
68 mult_AtAv(&u, &mut v, &mut tmp);
69 mult_AtAv(&v, &mut u, &mut tmp);
9738c2a @pcwalton test: Rewrite nbody and spectralnorm shootout benchmarks
pcwalton authored
70 }
17bc7d8 @japaric cleanup: replace `as[_mut]_slice()` calls with deref coercions
japaric authored
71 (dot(&u, &v) / dot(&v, &v)).sqrt()
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
72 }
73
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
74 fn mult_AtAv(v: &[f64], out: &mut [f64], tmp: &mut [f64]) {
75 mult_Av(v, tmp);
76 mult_Atv(tmp, out);
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
77 }
78
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
79 fn mult_Av(v: &[f64], out: &mut [f64]) {
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
80 parallel(out, |start, out| mult(v, out, start, |i, j| A(i, j)));
6bd2249 @TeXitoi shootout-spectralnorm resurection with parallelization
TeXitoi authored
81 }
82
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
83 fn mult_Atv(v: &[f64], out: &mut [f64]) {
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
84 parallel(out, |start, out| mult(v, out, start, |i, j| A(j, i)));
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
85 }
86
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
87 fn mult<F>(v: &[f64], out: &mut [f64], start: usize, a: F)
88 where F: Fn(usize, usize) -> f64 {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
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));
d792925 @huonw Shim some of the old std::simd functionality.
huonw authored
94 sum = sum + top / bot;
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
95 }
96 let f64x2(a, b) = sum;
97 *slot = a + b;
6bd2249 @TeXitoi shootout-spectralnorm resurection with parallelization
TeXitoi authored
98 }
99 }
100
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
101 fn A(i: usize, j: usize) -> f64 {
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
102 ((i + j) * (i + j + 1) / 2 + i + 1) as f64
6bd2249 @TeXitoi shootout-spectralnorm resurection with parallelization
TeXitoi authored
103 }
104
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
105 fn dot(v: &[f64], u: &[f64]) -> f64 {
ca7418b @Veedrac Removed many pointless calls to *iter() and iter_mut()
Veedrac authored
106 v.iter().zip(u).map(|(a, b)| *a * *b).sum()
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
107 }
108
52072de @FlaPer87 Add a Racy type to bench tests
FlaPer87 authored
109
f7b5470 @alexcrichton bench: Improve the spectralnorm shootout benchmark
alexcrichton authored
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`.
7a14f49 @huonw Update tests for the Send - 'static change.
huonw authored
113 fn parallel<'a,T, F>(v: &mut [T], ref f: F)
114 where T: Send + Sync + 'a,
43bfaa4 @alexcrichton Mass rename uint/int to usize/isize
alexcrichton authored
115 F: Fn(usize, &mut [T]) + Sync + 'a {
d4a2c94 @alexcrichton std: Clean out #[deprecated] APIs
alexcrichton authored
116 // FIXME: pick a more appropriate parallel factor
8d90d3f @alexcrichton Remove all unstable deprecated functionality
alexcrichton authored
117 // FIXME: replace with thread::scoped when it exists again
d4a2c94 @alexcrichton std: Clean out #[deprecated] APIs
alexcrichton authored
118 let parallelism = 4;
119 let size = v.len() / parallelism + 1;
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
120 v.chunks_mut(size).enumerate().map(|(i, chunk)| {
8d90d3f @alexcrichton Remove all unstable deprecated functionality
alexcrichton authored
121 f(i * size, chunk)
56290a0 @alexcrichton std: Stabilize the prelude module
alexcrichton authored
122 }).collect::<Vec<_>>();
7c8d128 @brson bench: Add shootout-spectralnorm
brson authored
123 }
Something went wrong with that request. Please try again.