Working multithreadding with Counter

This commit is contained in:
Jonathan Flueren 2022-06-01 20:13:55 +02:00
parent ac4eca7e95
commit 47158af5ee
5 changed files with 49 additions and 50 deletions

View file

@ -1,16 +1,4 @@
use super::{utility, Color, Rgb, RgbImage}; use super::{utility, Color, Rgb, RgbImage};
use std::io::Write;
pub fn write_color(out: &mut impl Write, pixel_color: &Color) {
write!(
out,
"{} {} {}\n",
(255.999 * pixel_color.x()) as u32,
(255.999 * pixel_color.y()) as u32,
(255.999 * pixel_color.z()) as u32
)
.unwrap();
}
pub fn put_color(img: &mut RgbImage, pixel_color: &Color, x: u32, y: u32, samples_per_pixel: u32) { pub fn put_color(img: &mut RgbImage, pixel_color: &Color, x: u32, y: u32, samples_per_pixel: u32) {
let mut r = pixel_color.x(); let mut r = pixel_color.x();

View file

@ -13,9 +13,11 @@ impl HittableList {
} }
} }
/*
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.objects.clear(); self.objects.clear();
} }
*/
pub fn add(&mut self, obj: Box<dyn Hittable>) { pub fn add(&mut self, obj: Box<dyn Hittable>) {
self.objects.push(obj); self.objects.push(obj);

View file

@ -8,18 +8,16 @@ mod ray;
mod utility; mod utility;
mod vec3; mod vec3;
use std::env;
use std::sync::atomic::AtomicU32;
//use std::fs::File;
//use std::io::BufWriter;
//use std::io::Write;
use camera::Camera; use camera::Camera;
use hittable::{HitRecord, Hittable, Sphere}; use hittable::{HitRecord, Hittable, Sphere};
use hittable_list::HittableList; use hittable_list::HittableList;
use image::{Rgb, RgbImage}; use image::{Rgb, RgbImage};
use ray::Ray; use ray::Ray;
use vec3::{Color, Point3, Vec3};
use rayon::prelude::*; use rayon::prelude::*;
use std::env;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use vec3::{Color, Point3, Vec3};
fn ray_color(r: &Ray, world: &HittableList, depth: u32) -> Color { fn ray_color(r: &Ray, world: &HittableList, depth: u32) -> Color {
let mut rec = HitRecord::empty(); let mut rec = HitRecord::empty();
@ -74,7 +72,7 @@ fn main() {
/* /*
for i in -15..15 { for i in -15..15 {
for j in -15..15 { for j in -15..15 {
world.add(Box::<Sphere>::new(Sphere::new(Point3::new(j as f64/6.0 as f64, i as f64/6.0 as f64, -1.5), 0.05))); world.add(Box::<Sphere>::new(Sphere::new(Point3::new(j as f64/5.0 as f64, i as f64/5.0 as f64, -1.5), 0.08)));
} }
} }
*/ */
@ -90,12 +88,14 @@ fn main() {
let mut image = RgbImage::new(image_width, image_height); let mut image = RgbImage::new(image_width, image_height);
//let mut file = BufWriter::new(File::create(default_file).unwrap()); let atomic_counter = Arc::new(AtomicU32::new(0));
//writeln!(&mut file, "P3\n{image_width} {image_height}\n255\n").unwrap(); let color_lines: Vec<_> = (0..image_height)
.into_par_iter()
.rev()
.map(|j| {
let v = atomic_counter.fetch_add(1, Ordering::Relaxed);
eprint!("\rScanlines remaining: {:5}", image_height - v);
let mut atomic_counter = AtomicU32::new(image_height);
let color_lines: Vec<_> = (0..image_height).into_par_iter().rev().map(|j| {
eprint!("\rScanlines remaining: {:5}", *atomic_counter.get_mut());
let mut colors = Vec::new(); let mut colors = Vec::new();
for i in 0..image_width { for i in 0..image_width {
let mut pixel_color = Color::new(0.0, 0.0, 0.0); let mut pixel_color = Color::new(0.0, 0.0, 0.0);
@ -107,14 +107,13 @@ fn main() {
} }
colors.push(pixel_color); colors.push(pixel_color);
} }
*atomic_counter.get_mut() -= 1;
return colors; return colors;
} ).collect(); })
.collect();
eprint!("\rScanlines remaining: {:5}", 0);
(0..image_height).into_iter().rev().for_each(|j| { (0..image_height).into_iter().rev().for_each(|j| {
(0..image_width).into_iter().for_each(|i| { (0..image_width).into_iter().for_each(|i| {
//color::write_color(&mut file, &pixel_color);
color::put_color( color::put_color(
&mut image, &mut image,
&color_lines[(image_height - j - 1) as usize][i as usize], &color_lines[(image_height - j - 1) as usize][i as usize],

View file

@ -1,5 +1,5 @@
use std::ops::*;
use super::utility; use super::utility;
use std::ops::*;
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct Vec3 { pub struct Vec3 {
@ -39,17 +39,27 @@ impl Vec3 {
} }
pub fn random_f64() -> Vec3 { pub fn random_f64() -> Vec3 {
Vec3::new(utility::random_f64(),utility::random_f64(),utility::random_f64()) Vec3::new(
utility::random_f64(),
utility::random_f64(),
utility::random_f64(),
)
} }
pub fn random_rng(min: f64, max: f64) -> Self { pub fn random_rng(min: f64, max: f64) -> Self {
Self::new(utility::random_rng(min,max), utility::random_rng(min,max), utility::random_rng(min,max)) Self::new(
utility::random_rng(min, max),
utility::random_rng(min, max),
utility::random_rng(min, max),
)
} }
pub fn random_in_unit_sphere() -> Self { pub fn random_in_unit_sphere() -> Self {
loop { loop {
let p = Vec3::random_rng(-1.0, 1.0); let p = Vec3::random_rng(-1.0, 1.0);
if p.length_squared() >= 1.0 { continue }; if p.length_squared() >= 1.0 {
continue;
};
return p; return p;
} }
} }