]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-mandelbrot.rs
a5729c5b5bd4c1c02596d6a661418c8e8ec2379a
[rust.git] / src / test / bench / shootout-mandelbrot.rs
1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
3 //
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.
40
41 #![feature(simd)]
42
43 // ignore-pretty very bad with line comments
44
45 use std::old_io;
46 use std::os;
47 use std::simd::f64x2;
48 use std::sync::Arc;
49 use std::thread::Thread;
50
51 const ITER: usize = 50;
52 const LIMIT: f64 = 2.0;
53 const WORKERS: usize = 16;
54
55 #[inline(always)]
56 fn mandelbrot<W: old_io::Writer>(w: usize, mut out: W) -> old_io::IoResult<()> {
57     assert!(WORKERS % 2 == 0);
58
59     // Ensure w and h are multiples of 8.
60     let w = (w + 7) / 8 * 8;
61     let h = w;
62
63     let chunk_size = h / WORKERS;
64
65     // Account for remainders in workload division, e.g. 1000 / 16 = 62.5
66     let last_chunk_size = if h % WORKERS != 0 {
67         chunk_size + h % WORKERS
68     } else {
69         chunk_size
70     };
71
72     // precalc values
73     let inverse_w_doubled = 2.0 / w as f64;
74     let inverse_h_doubled = 2.0 / h as f64;
75     let v_inverses = f64x2(inverse_w_doubled, inverse_h_doubled);
76     let v_consts = f64x2(1.5, 1.0);
77
78     // A lot of this code assumes this (so do other lang benchmarks)
79     assert!(w == h);
80     let mut precalc_r = Vec::with_capacity(w);
81     let mut precalc_i = Vec::with_capacity(h);
82
83     let precalc_futures = (0..WORKERS).map(|i| {
84         Thread::scoped(move|| {
85             let mut rs = Vec::with_capacity(w / WORKERS);
86             let mut is = Vec::with_capacity(w / WORKERS);
87
88             let start = i * chunk_size;
89             let end = if i == (WORKERS - 1) {
90                 start + last_chunk_size
91             } else {
92                 (i + 1) * chunk_size
93             };
94
95             // This assumes w == h
96             for x in start..end {
97                 let xf = x as f64;
98                 let xy = f64x2(xf, xf);
99
100                 let f64x2(r, i) = xy * v_inverses - v_consts;
101                 rs.push(r);
102                 is.push(i);
103             }
104
105             (rs, is)
106         })
107     }).collect::<Vec<_>>();
108
109     for res in precalc_futures {
110         let (rs, is) = res.join().ok().unwrap();
111         precalc_r.extend(rs.into_iter());
112         precalc_i.extend(is.into_iter());
113     }
114
115     assert_eq!(precalc_r.len(), w);
116     assert_eq!(precalc_i.len(), h);
117
118     let arc_init_r = Arc::new(precalc_r);
119     let arc_init_i = Arc::new(precalc_i);
120
121     let data = (0..WORKERS).map(|i| {
122         let vec_init_r = arc_init_r.clone();
123         let vec_init_i = arc_init_i.clone();
124
125         Thread::scoped(move|| {
126             let mut res: Vec<u8> = Vec::with_capacity((chunk_size * w) / 8);
127             let init_r_slice = vec_init_r.as_slice();
128
129             let start = i * chunk_size;
130             let end = if i == (WORKERS - 1) {
131                 start + last_chunk_size
132             } else {
133                 (i + 1) * chunk_size
134             };
135
136             for &init_i in &vec_init_i[start..end] {
137                 write_line(init_i, init_r_slice, &mut res);
138             }
139
140             res
141         })
142     }).collect::<Vec<_>>();
143
144     try!(writeln!(&mut out as &mut Writer, "P4\n{} {}", w, h));
145     for res in data {
146         try!(out.write(res.join().ok().unwrap().as_slice()));
147     }
148     out.flush()
149 }
150
151 fn write_line(init_i: f64, vec_init_r: &[f64], res: &mut Vec<u8>) {
152     let v_init_i : f64x2 = f64x2(init_i, init_i);
153     let v_2 : f64x2 = f64x2(2.0, 2.0);
154     const LIMIT_SQUARED: f64 = LIMIT * LIMIT;
155
156     for chunk_init_r in vec_init_r.chunks(8) {
157         let mut cur_byte = 0xff;
158         let mut i = 0;
159
160         while i < 8 {
161             let v_init_r = f64x2(chunk_init_r[i], chunk_init_r[i + 1]);
162             let mut cur_r = v_init_r;
163             let mut cur_i = v_init_i;
164             let mut r_sq = v_init_r * v_init_r;
165             let mut i_sq = v_init_i * v_init_i;
166
167             let mut b = 0;
168             for _ in 0..ITER {
169                 let r = cur_r;
170                 let i = cur_i;
171
172                 cur_i = v_2 * r * i + v_init_i;
173                 cur_r = r_sq - i_sq + v_init_r;
174
175                 let f64x2(bit1, bit2) = r_sq + i_sq;
176
177                 if bit1 > LIMIT_SQUARED {
178                     b |= 2;
179                     if b == 3 { break; }
180                 }
181
182                 if bit2 > LIMIT_SQUARED {
183                     b |= 1;
184                     if b == 3 { break; }
185                 }
186
187                 r_sq = cur_r * cur_r;
188                 i_sq = cur_i * cur_i;
189             }
190
191             cur_byte = (cur_byte << 2) + b;
192             i += 2;
193         }
194
195         res.push(cur_byte^-1);
196     }
197 }
198
199 fn main() {
200     let args = os::args();
201     let res = if args.len() < 2 {
202         println!("Test mode: do not dump the image because it's not utf8, \
203                   which interferes with the test runner.");
204         mandelbrot(1000, old_io::util::NullWriter)
205     } else {
206         mandelbrot(args[1].parse().unwrap(), old_io::stdout())
207     };
208     res.unwrap();
209 }