2013-02-19 09:53:03 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 19:32:48 -06: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.
|
|
|
|
|
2012-02-03 19:46:17 -06:00
|
|
|
// based on:
|
2012-02-03 21:50:32 -06:00
|
|
|
// http://shootout.alioth.debian.org/
|
|
|
|
// u64q/program.php?test=mandelbrot&lang=python3&id=2
|
2012-02-03 19:46:17 -06:00
|
|
|
//
|
2013-02-17 23:48:06 -06:00
|
|
|
// takes 3 optional args:
|
2012-02-03 22:43:48 -06:00
|
|
|
// square image size, defaults to 80_u
|
|
|
|
// output path, default is "" (no output), "-" means stdout
|
2013-02-17 23:48:06 -06:00
|
|
|
// depth (max iterations per pixel), defaults to 50_u
|
2012-02-03 22:43:48 -06:00
|
|
|
//
|
2013-02-17 23:48:06 -06:00
|
|
|
// in the shootout, they use 16000 as image size, 50 as depth,
|
|
|
|
// and write to stdout:
|
|
|
|
//
|
|
|
|
// ./shootout_mandelbrot 16000 "-" 50 > /tmp/mandel.pbm
|
2012-02-03 19:46:17 -06:00
|
|
|
//
|
2012-02-03 22:43:48 -06:00
|
|
|
// writes pbm image to output path
|
2012-02-03 19:46:17 -06:00
|
|
|
|
2013-03-01 17:55:31 -06:00
|
|
|
use core::io::WriterUtil;
|
2013-04-03 08:28:36 -05:00
|
|
|
use core::hashmap::HashMap;
|
2012-02-03 19:46:17 -06:00
|
|
|
|
2012-07-27 21:32:42 -05:00
|
|
|
struct cmplx {
|
2012-09-07 16:50:47 -05:00
|
|
|
re: f64,
|
|
|
|
im: f64
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ops::Mul<cmplx,cmplx> for cmplx {
|
2013-03-22 13:23:21 -05:00
|
|
|
fn mul(&self, x: &cmplx) -> cmplx {
|
2012-09-19 20:00:26 -05:00
|
|
|
cmplx {
|
|
|
|
re: self.re*(*x).re - self.im*(*x).im,
|
|
|
|
im: self.re*(*x).im + self.im*(*x).re
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:46:17 -06:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ops::Add<cmplx,cmplx> for cmplx {
|
2013-03-22 13:23:21 -05:00
|
|
|
fn add(&self, x: &cmplx) -> cmplx {
|
2012-09-19 20:00:26 -05:00
|
|
|
cmplx {
|
|
|
|
re: self.re + (*x).re,
|
|
|
|
im: self.im + (*x).im
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 19:46:17 -06:00
|
|
|
|
2012-12-20 11:35:12 -06:00
|
|
|
struct Line {i: uint, b: ~[u8]}
|
2012-07-27 21:32:42 -05:00
|
|
|
|
2013-03-22 13:23:21 -05:00
|
|
|
fn cabs(x: cmplx) -> f64
|
2012-02-03 19:46:17 -06:00
|
|
|
{
|
2012-02-03 21:50:32 -06:00
|
|
|
x.re*x.re + x.im*x.im
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
2013-02-17 23:48:06 -06:00
|
|
|
fn mb(x: cmplx, depth: uint) -> bool
|
2012-02-03 19:46:17 -06:00
|
|
|
{
|
2013-02-17 23:48:06 -06:00
|
|
|
let mut z = x;
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0;
|
2013-02-17 23:48:06 -06:00
|
|
|
while i < depth {
|
|
|
|
if cabs(z) >= 4_f64 {
|
|
|
|
return false;
|
2012-02-03 21:50:32 -06:00
|
|
|
}
|
2013-02-17 23:48:06 -06:00
|
|
|
z = z*z + x;
|
2012-02-03 21:50:32 -06:00
|
|
|
i += 1;
|
|
|
|
}
|
2013-02-17 23:48:06 -06:00
|
|
|
true
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
2013-02-17 23:48:06 -06:00
|
|
|
fn fillbyte(x: cmplx, incr: f64, depth: uint) -> u8 {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut rv = 0_u8;
|
|
|
|
let mut i = 0_u8;
|
2012-02-03 21:50:32 -06:00
|
|
|
while i < 8_u8 {
|
2012-07-27 21:32:42 -05:00
|
|
|
let z = cmplx {re: x.re + (i as f64)*incr, im: x.im};
|
2013-02-17 23:48:06 -06:00
|
|
|
if mb(z, depth) {
|
2012-02-03 21:50:32 -06:00
|
|
|
rv += 1_u8 << (7_u8 - i);
|
|
|
|
}
|
|
|
|
i += 1_u8;
|
|
|
|
}
|
|
|
|
rv
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
2013-02-17 23:48:06 -06:00
|
|
|
fn chanmb(i: uint, size: uint, depth: uint) -> Line
|
2012-02-03 19:46:17 -06:00
|
|
|
{
|
2013-02-17 23:48:06 -06:00
|
|
|
let bsize = size/8_u;
|
|
|
|
let mut crv = vec::with_capacity(bsize);
|
|
|
|
let incr = 2_f64/(size as f64);
|
|
|
|
let y = incr*(i as f64) - 1_f64;
|
|
|
|
let xincr = 8_f64*incr;
|
|
|
|
for uint::range(0_u, bsize) |j| {
|
|
|
|
let x = cmplx {re: xincr*(j as f64) - 1.5_f64, im: y};
|
|
|
|
crv.push(fillbyte(x, incr, depth));
|
2012-02-03 21:50:32 -06:00
|
|
|
};
|
2012-12-27 13:42:50 -06:00
|
|
|
Line {i:i, b:crv}
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
2012-12-30 11:54:44 -06:00
|
|
|
struct Devnull();
|
2012-02-03 22:43:48 -06:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl io::Writer for Devnull {
|
2013-03-22 19:58:50 -05:00
|
|
|
fn write(&self, _b: &const [u8]) {}
|
2012-12-24 15:29:36 -06:00
|
|
|
fn seek(&self, _i: int, _s: io::SeekStyle) {}
|
|
|
|
fn tell(&self) -> uint {0_u}
|
|
|
|
fn flush(&self) -> int {0}
|
|
|
|
fn get_type(&self) -> io::WriterType { io::File }
|
2012-02-03 22:43:48 -06:00
|
|
|
}
|
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
fn writer(path: ~str, pport: comm::Port<Line>, size: uint)
|
2012-02-03 19:46:17 -06:00
|
|
|
{
|
2013-03-14 13:22:14 -05:00
|
|
|
let cout: @io::Writer = match path {
|
2012-08-03 21:59:04 -05:00
|
|
|
~"" => {
|
2013-02-26 20:42:00 -06:00
|
|
|
@Devnull as @io::Writer
|
2012-02-03 22:43:48 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
~"-" => {
|
2012-03-12 22:04:27 -05:00
|
|
|
io::stdout()
|
2012-02-03 22:43:48 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-02-03 22:43:48 -06:00
|
|
|
result::get(
|
2012-09-25 18:23:04 -05:00
|
|
|
&io::file_writer(&Path(path),
|
2012-08-14 15:38:35 -05:00
|
|
|
~[io::Create, io::Truncate]))
|
2012-02-03 22:43:48 -06:00
|
|
|
}
|
|
|
|
};
|
2013-02-17 23:48:06 -06:00
|
|
|
cout.write_line("P4");
|
2012-08-22 19:24:52 -05:00
|
|
|
cout.write_line(fmt!("%u %u", size, size));
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut lines: HashMap<uint, Line> = HashMap::new();
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut done = 0_u;
|
|
|
|
let mut i = 0_u;
|
2012-02-03 21:50:32 -06:00
|
|
|
while i < size {
|
2012-12-27 13:42:50 -06:00
|
|
|
let aline = pport.recv();
|
2012-02-03 21:50:32 -06:00
|
|
|
if aline.i == done {
|
2013-02-17 23:48:06 -06:00
|
|
|
debug!("W %u", done);
|
2012-02-03 21:50:32 -06:00
|
|
|
cout.write(aline.b);
|
|
|
|
done += 1_u;
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut prev = done;
|
2012-02-03 21:50:32 -06:00
|
|
|
while prev <= i {
|
2013-02-17 23:48:06 -06:00
|
|
|
match lines.pop(&prev) {
|
|
|
|
Some(pl) => {
|
|
|
|
debug!("WS %u", prev);
|
|
|
|
cout.write(pl.b);
|
|
|
|
done += 1_u;
|
|
|
|
prev += 1_u;
|
|
|
|
}
|
|
|
|
None => break
|
|
|
|
};
|
2012-02-03 21:50:32 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("S %u", aline.i);
|
2013-02-17 23:48:06 -06:00
|
|
|
lines.insert(aline.i, aline);
|
2012-02-03 21:50:32 -06:00
|
|
|
};
|
|
|
|
i += 1_u;
|
|
|
|
}
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
2013-02-17 23:48:06 -06:00
|
|
|
~[~"", ~"4000", ~"50"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else {
|
|
|
|
args
|
|
|
|
};
|
|
|
|
|
2013-02-17 23:48:06 -06:00
|
|
|
let depth = if vec::len(args) < 4_u { 50_u }
|
|
|
|
else { uint::from_str(args[3]).get() };
|
|
|
|
|
2012-12-20 11:35:12 -06:00
|
|
|
let path = if vec::len(args) < 3_u { ~"" }
|
|
|
|
else { copy args[2] }; // FIXME: bad for perf
|
2012-06-03 09:45:23 -05:00
|
|
|
|
|
|
|
let size = if vec::len(args) < 2_u { 80_u }
|
|
|
|
else { uint::from_str(args[1]).get() };
|
2012-05-24 00:53:50 -05:00
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
let (pport, pchan) = comm::stream();
|
2013-04-17 01:45:29 -05:00
|
|
|
let pchan = comm::SharedChan::new(pchan);
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0_u, size) |j| {
|
2012-12-27 13:42:50 -06:00
|
|
|
let cchan = pchan.clone();
|
2013-02-17 23:48:06 -06:00
|
|
|
do task::spawn { cchan.send(chanmb(j, size, depth)) };
|
2012-02-03 21:50:32 -06:00
|
|
|
};
|
2012-12-27 13:42:50 -06:00
|
|
|
writer(path, pport, size);
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|