From 74f80bcf9be51cd497804a77b70c1b8b91d232e8 Mon Sep 17 00:00:00 2001 From: Clemens-Dautermann Date: Mon, 22 Mar 2021 14:41:56 +0100 Subject: [PATCH] parallelization --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/main.rs | 44 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef39b4c..7e21df5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -219,6 +219,7 @@ dependencies = [ "image", "indicatif", "num-complex", + "rayon", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b3271ff..8262232 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ edition = "2018" image = "0.23.14" num-complex = "0.4.0" # uuid = { version = "0.8", features = ["v4"] } -indicatif = "0.15.0" \ No newline at end of file +indicatif = "0.15.0" +rayon = "1.5.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 41435c7..f56d776 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,16 @@ use image::{RgbImage, ImageBuffer, Rgb}; use std::fmt::Error; use num_complex::Complex; use indicatif::ProgressBar; +use rayon::prelude::*; const THRESHHOLD: f64 = 200_i32.pow(2) as f64; +struct PixelInfo { + x: u32, + y: u32, + color: Rgb, +} + fn calculate_color(number: Complex, iterations: u32) -> Result, Error> { let mut z: Complex = Complex::new(0.0, 0.0); let mut cycle = Vec::with_capacity(iterations as usize); @@ -67,8 +74,12 @@ fn generate_mandelbrot(width: u32, let bar = ProgressBar::new(width as u64); - for x in 0..width { + let mut all_pixels: Vec> = vec![]; + + (0..width).into_par_iter().map(|x| { bar.inc(1); + let mut row: Vec = vec![]; + for y in 0..height { let x_offset: f64 = (x as f64 / width as f64) * section_width; let y_offset: f64 = (y as f64 / width as f64) * section_height; @@ -76,9 +87,22 @@ fn generate_mandelbrot(width: u32, let color = calculate_color( Complex::new(upper_left.re + x_offset, upper_left.im + y_offset), iterations).unwrap(); - out_image.put_pixel(x, y, color); + row.push(PixelInfo { + x, + y, + color, + }); + } + row + }).collect_into_vec(&mut all_pixels); + + for vector in all_pixels { + for pixel in vector { + out_image.put_pixel(pixel.x, pixel.y, pixel.color); } } + + bar.finish_with_message("Rendered frame."); Ok(out_image) } @@ -100,16 +124,17 @@ fn render_around_point(image_size: u32, lower_right) } -fn render_sequence(num_frames: u32, +fn render_sequence(end_frame: u32, + start_frame: u32, image_size: u32, iterations: u32, initial_size: f64, zoom_point: Complex, scale_factor: f32, path: String) { - let mut scale = initial_size; + let mut scale = initial_size * (1.0 - scale_factor).powi(start_frame as i32) as f64; - for frame in 0..num_frames { + for frame in start_frame..end_frame { let fractal: RgbImage = render_around_point(image_size, iterations, scale, @@ -125,12 +150,13 @@ fn render_sequence(num_frames: u32, } fn main() { - render_sequence(10, + render_sequence(240, + 0, 500, 200, - 1.0, - Complex::new(-0.5, -0.5), + 2.0, + Complex::new(-0.761574, -0.0847596), 1.0 / 10.0, "./out/".parse().unwrap(), - ) + ); }