Skip to content

Commit

Permalink
Parallelize code
Browse files Browse the repository at this point in the history
  • Loading branch information
resttime committed Jan 19, 2020
1 parent dd1b211 commit 99eacd9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ fn main() {

for j in (0..ny).rev() {
for i in 0..nx {
let mut col = Vec3::default();
for _ in 0..ns {
let u: f32 = (i as f32 + rand_float()) / nx as f32;
let v: f32 = (j as f32 + rand_float()) / ny as f32;
let r = cam.get_ray(u, v);
col += color(r, &world, 0);
}
let mut col: Vec3 = (0..ns)
.into_par_iter()
.map(|_| {
let u: f32 = (i as f32 + rand_float()) / nx as f32;
let v: f32 = (j as f32 + rand_float()) / ny as f32;
let r = cam.get_ray(u, v);
color(r, &world, 0)
})
.sum();
col /= ns as f32;
col = Vec3::new(col[0].sqrt(), col[1].sqrt(), col[2].sqrt());

Expand Down

0 comments on commit 99eacd9

Please sign in to comment.