New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New mandelbrot.rs #2
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// The Computer Language Benchmarks Game | ||
// http://benchmarksgame.alioth.debian.org/ | ||
// | ||
// contributed by the Rust Project Developers | ||
// contributed by TeXitoi | ||
// contributed by Matt Watson | ||
use std::io::Write; | ||
use std::io; | ||
use std::thread; | ||
const THREADS: usize = 8; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably needs attention as at present size must be an even multiple of THREADS |
||
const MAX_ITER: usize = 50; | ||
const DX: f64 = -1.5; | ||
const DY: f64 = -1.0; | ||
pub fn mbrotpt(x: f64, y: f64) -> usize { | ||
let mut z = (0.0, 0.0); | ||
for _ in 0..MAX_ITER { | ||
z = (z.0 * z.0 - z.1 * z.1 + x, | ||
2.0 * z.0 * z.1 + y); | ||
if z.0 * z.0 + z.1 * z.1 >= 4.0 { | ||
return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
|
||
fn mbrot8(x: usize, y: usize, inv: f64) -> u8 { | ||
let mut result = 0 as usize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer minimize type conversions: here, you can use directly u8, and it will be automatically inferred. You also must change the return type of mbrotpt to u8 for this to work. |
||
let mut i = 0; | ||
while i < 8 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use |
||
result = result << 1; | ||
result = result | mbrotpt((x + i) as f64 * inv + DX, | ||
y as f64 * inv + DY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can be faster: the argument of mbrotpt are calculated for each point, so square size. But each value only depend on x or y. Thus, the values can be precalculated, and precious floating point instruction can be removed. You can read at some benchmark implementation, including the old rust one to see examples. I will not require this optimization to be done to merge your pull request. |
||
i += 1; | ||
} | ||
result as u8 | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange empty line. |
||
fn main() { | ||
let size = std::env::args_os().nth(1) | ||
.and_then(|s| s.into_string().ok()) | ||
.and_then(|n| n.parse().ok()) | ||
.unwrap_or(200); | ||
let inv = 2.0 / size as f64; | ||
println!("P4"); | ||
println!("{} {}",size, size); | ||
let workers: Vec<usize> = (0..THREADS).collect();; | ||
let handles: Vec<_> = workers.into_iter().map(|t| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better: let handles: Vec<_> = (0..THREADS).map(|t| { |
||
thread::spawn(move || { | ||
let mut rows = vec![vec![0 as u8; 8 * size / 64]; size / THREADS]; | ||
for z in 0..size / THREADS { | ||
let mut row = vec![0; size / 8]; | ||
for x in 0..size / 8 { | ||
row[x] = mbrot8(x * 8,t * (size / THREADS) + z, inv); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally, idiomatic rust avoid using [] as it can fail and the bound checking can have a cost. We prefer iteration. You can then rewrite that as : for (x, elt) in row.iter_mut().enumerate() {
*elt = mbrot8(x * 8, t * (size / THREADS) + z, inv);
} The same can be done for rows. |
||
rows[z] = row.to_vec(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This to_vec() is useless, no? You can just move the value directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, you can directly use the rows[z] instead of creating a row and then affecting it. |
||
} | ||
rows | ||
}) | ||
}).collect(); | ||
|
||
for h in handles { | ||
let rows = h.join().unwrap(); | ||
for i in 0..size / THREADS { | ||
std::io::stdout().write(&rows[i]).ok().expect("Could not write to stdout"); | ||
} | ||
} | ||
io::stdout().flush().ok().expect("Could not flush stdout"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stdout will be executed and locked several times. Even if it must not be the bottleneck, we can avoid it. Calling unwrap on a result is most of the time as good as calling ok().unwrap(). And this loop can be written to avoid array indexing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let stdout_unlocked = std::io::stdout();
let mut stdout = stdout_unlocked.lock();
for row in handles.into_iter().flat_map(|h| h.join().unwrap().into_iter()) {
stdout.write_all(&row).unwrap();
}
stdout.flush().unwrap(); |
||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do not use the original version, you can remove the rust project line.
My contribution must be the last one as, according to the rules of the benchmark game, the last contributor must be the person that submit the benchmark. Or you can keep this order, but then you must propose yourself the benchmark.