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.
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.
13 use std::from_str::FromStr;
17 use std::slice::from_elem;
20 fn A(i: uint, j: uint) -> f64 {
21 ((i + j) * (i + j + 1) / 2 + i + 1) as f64
24 fn dot(v: &[f64], u: &[f64]) -> f64 {
26 for (&v_i, &u_i) in v.iter().zip(u.iter()) {
32 fn mult(v: 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();
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;}
48 let out = out.clone();
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);
58 // wait until the channel hang up (every task finished)
60 for () in rx.iter() {}
63 fn mult_Av_impl(v: &Vec<f64> , i: uint) -> f64 {
65 for (j, &v_j) in v.iter().enumerate() {
71 fn mult_Av(v: RWArc<Vec<f64> >, out: RWArc<Vec<f64> >) {
72 mult(v, out, mult_Av_impl);
75 fn mult_Atv_impl(v: &Vec<f64> , i: uint) -> f64 {
77 for (j, &v_j) in v.iter().enumerate() {
83 fn mult_Atv(v: RWArc<Vec<f64> >, out: RWArc<Vec<f64> >) {
84 mult(v, out, mult_Atv_impl);
87 fn mult_AtAv(v: RWArc<Vec<f64> >, out: RWArc<Vec<f64> >, tmp: RWArc<Vec<f64> >) {
88 mult_Av(v, tmp.clone());
93 let args = os::args();
94 let n = if os::getenv("RUST_BENCH").is_some() {
96 } else if args.len() < 2 {
99 FromStr::from_str(args[1]).unwrap()
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());
109 u.read(|u| v.read(|v| {
110 println!("{:.9f}", (dot(*u, *v) / dot(*v, *v)).sqrt());