]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-spectralnorm.rs
test: Automatically remove all `~[T]` from tests.
[rust.git] / src / test / bench / shootout-spectralnorm.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 extern crate sync;
12
13 use std::from_str::FromStr;
14 use std::iter::count;
15 use std::cmp::min;
16 use std::os;
17 use std::slice::from_elem;
18 use sync::RWArc;
19
20 fn A(i: uint, j: uint) -> f64 {
21     ((i + j) * (i + j + 1) / 2 + i + 1) as f64
22 }
23
24 fn dot(v: &[f64], u: &[f64]) -> f64 {
25     let mut sum = 0.0;
26     for (&v_i, &u_i) in v.iter().zip(u.iter()) {
27         sum += v_i * u_i;
28     }
29     sum
30 }
31
32 fn mult(v: RWArc<Vec<f64>>,
33         out: RWArc<Vec<f64>>,
34         f: fn(&Vec<f64>, uint) -> f64) {
35     // We launch in different tasks the work to be done.  To finish
36     // this fuction, we need to wait for the completion of every
37     // tasks.  To do that, we give to each tasks a wait_chan that we
38     // drop at the end of the work.  At the end of this function, we
39     // wait until the channel hang up.
40     let (tx, rx) = channel();
41
42     let len = out.read(|out| out.len());
43     let chunk = len / 100 + 1;
44     for chk in count(0, chunk) {
45         if chk >= len {break;}
46         let tx = tx.clone();
47         let v = v.clone();
48         let out = out.clone();
49         spawn(proc() {
50             for i in range(chk, min(len, chk + chunk)) {
51                 let val = v.read(|v| f(v, i));
52                 out.write(|out| out[i] = val);
53             }
54             drop(tx)
55         });
56     }
57
58     // wait until the channel hang up (every task finished)
59     drop(tx);
60     for () in rx.iter() {}
61 }
62
63 fn mult_Av_impl(v: &Vec<f64> , i: uint) -> f64 {
64     let mut sum = 0.;
65     for (j, &v_j) in v.iter().enumerate() {
66         sum += v_j / A(i, j);
67     }
68     sum
69 }
70
71 fn mult_Av(v: RWArc<Vec<f64> >, out: RWArc<Vec<f64> >) {
72     mult(v, out, mult_Av_impl);
73 }
74
75 fn mult_Atv_impl(v: &Vec<f64> , i: uint) -> f64 {
76     let mut sum = 0.;
77     for (j, &v_j) in v.iter().enumerate() {
78         sum += v_j / A(j, i);
79     }
80     sum
81 }
82
83 fn mult_Atv(v: RWArc<Vec<f64> >, out: RWArc<Vec<f64> >) {
84     mult(v, out, mult_Atv_impl);
85 }
86
87 fn mult_AtAv(v: RWArc<Vec<f64> >, out: RWArc<Vec<f64> >, tmp: RWArc<Vec<f64> >) {
88     mult_Av(v, tmp.clone());
89     mult_Atv(tmp, out);
90 }
91
92 fn main() {
93     let args = os::args();
94     let n = if os::getenv("RUST_BENCH").is_some() {
95         5500
96     } else if args.len() < 2 {
97         2000
98     } else {
99         FromStr::from_str(args[1]).unwrap()
100     };
101     let u = RWArc::new(from_elem(n, 1.));
102     let v = RWArc::new(from_elem(n, 1.));
103     let tmp = RWArc::new(from_elem(n, 1.));
104     for _ in range(0, 10) {
105         mult_AtAv(u.clone(), v.clone(), tmp.clone());
106         mult_AtAv(v.clone(), u.clone(), tmp.clone());
107     }
108
109     u.read(|u| v.read(|v| {
110         println!("{:.9f}", (dot(*u, *v) / dot(*v, *v)).sqrt());
111     }))
112 }