-
Notifications
You must be signed in to change notification settings - Fork 287
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
Added "Draw fractal dispatching work to a thread pool" example #176
Conversation
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.
This is a great example of threadpool! We just need to pare it down to the most relevant code so the reader isn't seeing 100 lines of mostly unimportant code.
|
||
[![threadpool-badge]][threadpool] [![num-badge]][num] [![num_cpus-badge]][num_cpus] [![image-badge]][image] [![cat-concurrency-badge]][cat-concurrency][![cat-science-badge]][cat-science][![cat-rendering-badge]][cat-rendering] | ||
|
||
Draws a fractal from [Julia set] to an image utilizing a thread pool for computation. |
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.
Add a thumbnail here with a link to the full sized image.
src/concurrency.md
Outdated
|
||
// Function converting intensity values to RGB | ||
// Based on http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm | ||
fn wavelength_to_rgb(wavelength: u32) -> Rgb<u8> { |
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.
Hide this like we hide all the error handling code. This is not important to understanding threadpool.
src/concurrency.md
Outdated
} | ||
|
||
// Normalizes color intensity values within RGB range | ||
fn normalize(color: f32, factor: f32) -> u8 { |
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.
Hide this too.
src/concurrency.md
Outdated
} | ||
|
||
// Maps Julia set distance estimation to intensity values | ||
fn julia(c: Complex<f32>, x: u32, y: u32, width: u32, height: u32, max_iter: u32) -> u32 { |
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.
Hide this too.
src/concurrency.md
Outdated
|
||
let iterations = 300; | ||
// precomputed pixel colors from possible intensity range | ||
let color_lut: Vec<_> = (0..iterations) |
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.
Remove this and inline the computation below. This seems like a performance optimization that has nothing to do with threadpool so it doesn't provide value to the example.
src/concurrency.md
Outdated
let pixel = color_lut[i as usize]; | ||
img.put_pixel(x, y, pixel); | ||
} | ||
let _ = img.save("output.png"); |
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.
Show how to handle this error.
src/concurrency.md
Outdated
} | ||
|
||
fn run() -> Result<()> { | ||
// resultant image size |
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.
Remove this comment. I think the names width
and height
and the next line are self-explanatory.
Also added thumbnail showing the final output. Added missing error handling for`ImageBuffer::save`
Rebased and fixed :) Don't know how I've missed that Result on Also I did not know a better way to handle the thumbnail except for inline html pointing to github static content. |
fixes: https://github.com/brson/rust-cookbook/issues/102
Currently the example is marked as "no_run" as it leaves
output.png
in rust-cookbook cwd due to https://github.com/brson/rust-skeptic/issues/23 (there is a PR for it https://github.com/brson/rust-skeptic/pull/26)Also on weaker machines the example in debug build might take 1-2s and I would not like to make the test even slower.
Also maybe we could add additional thumbnail for the output.png?