2014-02-07 13:08:32 -06:00
|
|
|
// Copyright 2012-2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-06-16 15:54:08 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-07 13:08:32 -06:00
|
|
|
// ignore-test arcs no longer unwrap
|
2013-12-18 11:57:58 -06:00
|
|
|
|
2014-01-30 14:04:47 -06:00
|
|
|
extern mod sync;
|
2013-08-05 23:11:25 -05:00
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::from_str::FromStr;
|
2013-11-28 13:46:59 -06:00
|
|
|
use std::iter::count;
|
2014-02-06 01:34:33 -06:00
|
|
|
use std::cmp::min;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::os;
|
2013-11-28 13:46:59 -06:00
|
|
|
use std::vec::from_elem;
|
2014-01-30 14:04:47 -06:00
|
|
|
use sync::Arc;
|
|
|
|
use sync::RWArc;
|
2012-12-10 19:32:48 -06:00
|
|
|
|
2013-11-28 13:46:59 -06:00
|
|
|
fn A(i: uint, j: uint) -> f64 {
|
|
|
|
((i + j) * (i + j + 1) / 2 + i + 1) as f64
|
2013-04-16 18:11:16 -05:00
|
|
|
}
|
2012-02-08 03:05:53 -06:00
|
|
|
|
2013-04-16 18:11:16 -05:00
|
|
|
fn dot(v: &[f64], u: &[f64]) -> f64 {
|
|
|
|
let mut sum = 0.0;
|
2013-11-28 13:46:59 -06:00
|
|
|
for (&v_i, &u_i) in v.iter().zip(u.iter()) {
|
|
|
|
sum += v_i * u_i;
|
2013-04-16 18:11:16 -05:00
|
|
|
}
|
|
|
|
sum
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|
|
|
|
|
2013-11-28 13:46:59 -06:00
|
|
|
fn mult(v: RWArc<~[f64]>, out: RWArc<~[f64]>, f: fn(&~[f64], uint) -> f64) {
|
|
|
|
let wait = Arc::new(());
|
|
|
|
let len = out.read(|out| out.len());
|
|
|
|
let chunk = len / 100 + 1;
|
|
|
|
for chk in count(0, chunk) {
|
|
|
|
if chk >= len {break;}
|
|
|
|
let w = wait.clone();
|
|
|
|
let v = v.clone();
|
|
|
|
let out = out.clone();
|
2014-01-27 17:29:50 -06:00
|
|
|
spawn(proc() {
|
2013-11-28 13:46:59 -06:00
|
|
|
for i in range(chk, min(len, chk + chunk)) {
|
|
|
|
let val = v.read(|v| f(v, i));
|
|
|
|
out.write(|out| out[i] = val);
|
|
|
|
}
|
|
|
|
let _ = w;
|
2014-01-27 17:29:50 -06:00
|
|
|
});
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|
2013-11-28 13:46:59 -06:00
|
|
|
let _ = wait.unwrap();
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|
|
|
|
|
2013-11-28 13:46:59 -06:00
|
|
|
fn mult_Av_impl(v: &~[f64], i: uint) -> f64 {
|
|
|
|
let mut sum = 0.;
|
|
|
|
for (j, &v_j) in v.iter().enumerate() {
|
|
|
|
sum += v_j / A(i, j);
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|
2013-11-28 13:46:59 -06:00
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mult_Av(v: RWArc<~[f64]>, out: RWArc<~[f64]>) {
|
|
|
|
mult(v, out, mult_Av_impl);
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|
|
|
|
|
2013-11-28 13:46:59 -06:00
|
|
|
fn mult_Atv_impl(v: &~[f64], i: uint) -> f64 {
|
|
|
|
let mut sum = 0.;
|
|
|
|
for (j, &v_j) in v.iter().enumerate() {
|
|
|
|
sum += v_j / A(j, i);
|
|
|
|
}
|
|
|
|
sum
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mult_Atv(v: RWArc<~[f64]>, out: RWArc<~[f64]>) {
|
|
|
|
mult(v, out, mult_Atv_impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mult_AtAv(v: RWArc<~[f64]>, out: RWArc<~[f64]>, tmp: RWArc<~[f64]>) {
|
|
|
|
mult_Av(v, tmp.clone());
|
2013-04-16 18:11:16 -05:00
|
|
|
mult_Atv(tmp, out);
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
2013-11-28 13:46:59 -06:00
|
|
|
let args = os::args();
|
|
|
|
let n = if os::getenv("RUST_BENCH").is_some() {
|
|
|
|
5500
|
|
|
|
} else if args.len() < 2 {
|
|
|
|
2000
|
|
|
|
} else {
|
|
|
|
FromStr::from_str(args[1]).unwrap()
|
|
|
|
};
|
|
|
|
let u = RWArc::new(from_elem(n, 1.));
|
|
|
|
let v = RWArc::new(from_elem(n, 1.));
|
|
|
|
let tmp = RWArc::new(from_elem(n, 1.));
|
|
|
|
for _ in range(0, 10) {
|
|
|
|
mult_AtAv(u.clone(), v.clone(), tmp.clone());
|
|
|
|
mult_AtAv(v.clone(), u.clone(), tmp.clone());
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|
2013-11-28 13:46:59 -06:00
|
|
|
let u = u.unwrap();
|
|
|
|
let v = v.unwrap();
|
|
|
|
println!("{:.9f}", (dot(u,v) / dot(v,v)).sqrt());
|
2012-02-08 03:05:53 -06:00
|
|
|
}
|