Skip to content
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

Merged
merged 3 commits into from
May 31, 2017

Conversation

budziq
Copy link
Collaborator

@budziq budziq commented May 29, 2017

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?
output

Copy link
Member

@dtolnay dtolnay left a 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.
Copy link
Member

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.


// Function converting intensity values to RGB
// Based on http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
fn wavelength_to_rgb(wavelength: u32) -> Rgb<u8> {
Copy link
Member

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.

}

// Normalizes color intensity values within RGB range
fn normalize(color: f32, factor: f32) -> u8 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide this too.

}

// 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide this too.


let iterations = 300;
// precomputed pixel colors from possible intensity range
let color_lut: Vec<_> = (0..iterations)
Copy link
Member

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.

let pixel = color_lut[i as usize];
img.put_pixel(x, y, pixel);
}
let _ = img.save("output.png");
Copy link
Member

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.

}

fn run() -> Result<()> {
// resultant image size
Copy link
Member

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`
@budziq
Copy link
Collaborator Author

budziq commented May 31, 2017

Rebased and fixed :)

Don't know how I've missed that Result on ImageBuffer::save. Maby we should have ![deny(warnings)] somewhere in skeptic config (in such way we could be also asked to fix snippets on depreciations)

Also I did not know a better way to handle the thumbnail except for inline html pointing to github static content.

@dtolnay dtolnay merged commit 827b8ae into rust-lang-nursery:master May 31, 2017
@budziq budziq deleted the fractal branch May 31, 2017 16:00
@budziq budziq restored the fractal branch June 7, 2017 09:54
@budziq budziq deleted the fractal branch June 7, 2017 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants