Skip to content

Commit 177d7c8

Browse files
authored
Merge pull request #10 from claforte/claforte/threads_crash2
Claforte/threads crash2
2 parents 6b53e54 + 4c56754 commit 177d7c8

File tree

4 files changed

+595
-83
lines changed

4 files changed

+595
-83
lines changed

src/RayTracingWeekend.jl

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
module RayTracingWeekend
22

3-
using Images, LinearAlgebra, Random, RandomNumbers.Xorshifts, StaticArrays
3+
using Images
4+
using LinearAlgebra
5+
using Random
6+
using RandomNumbers.Xorshifts
7+
using StaticArrays
48

59
export color_vec3_in_rgb, default_camera, get_ray, hit, near_zero, point, random_between, random_vec2,
610
random_vec2_in_disk, random_vec3, random_vec3_in_sphere, random_vec3_on_sphere, ray_color, ray_to_HitRecord, reflect,
7-
reflectance, refract, render, reseed!, rgb, rgb_gamma2, skycolor, squared_length, trand
11+
reflectance, refract, render, reseed!, rgb, rgb_gamma2, scatter, skycolor, squared_length, trand
812
export Camera, Dielectric, Hittable, HittableList, HitRecord, Lambertian, Material, Metal, Ray, Scatter, Sphere, Vec3
913
export scene_2_spheres, scene_4_spheres, scene_blue_red_spheres, scene_diel_spheres, scene_random_spheres
14+
export TRNG
1015

1116
const Vec3{T<:AbstractFloat} = SVector{3, T}
1217

@@ -41,21 +46,31 @@ end
4146
(one(T)-t)*white + t*skyblue
4247
end
4348

44-
# Instantiate 1 RNG (Random Number Generator) per thread, for performance
45-
# Fix the random seeds, to make it easier to benchmark changes.
46-
const TRNG = [Xoroshiro128Plus(i) for i = 1:Threads.nthreads()]
49+
# Per-thread Random Number Generator. Initialized later...
50+
const TRNG = Xoroshiro128Plus[]
4751

48-
reseed!() = for (i,rng) in enumerate(TRNG) Random.seed!(rng, i) end # reset the seed
49-
reseed!()
52+
function __init__()
53+
# Instantiate 1 RNG (Random Number Generator) per thread, for performance.
54+
# This can't be done during precompilation since the number of threads isn't known then.
55+
resize!(TRNG, Threads.nthreads())
56+
for i in 1:Threads.nthreads()
57+
TRNG[i] = Xoroshiro128Plus(i)
58+
end
59+
nothing
60+
end
5061

51-
@inline function trand() # thread-local rand()
52-
@inbounds rng = TRNG[Threads.threadid()]
53-
rand(rng)
62+
# Reset the per-thread random seeds to make results reproducible
63+
reseed!() = for i in 1:Threads.nthreads() Random.seed!(TRNG[i], i) end
64+
65+
"Per-thread rand()"
66+
@inline function trand()
67+
@inbounds rng = TRNG[Threads.threadid()]
68+
rand(rng)
5469
end
5570

56-
@inline function trand(::Type{T}) where T # thread-local rand()
57-
@inbounds rng = TRNG[Threads.threadid()]
58-
rand(rng, T)
71+
@inline function trand(::Type{T}) where T
72+
@inbounds rng = TRNG[Threads.threadid()]
73+
rand(rng, T)
5974
end
6075

6176
@inline function random_vec3_in_sphere(::Type{T}) where T # equiv to random_in_unit_sphere()
@@ -269,6 +284,11 @@ function default_camera(lookfrom::Vec3{T}=(SA{T}[0,0,0]),
269284
Camera{T}(origin, lower_left_corner, horizontal, vertical, u, v, w, lens_radius)
270285
end
271286

287+
default_camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, focus_dist; elem_type::Type{T}) where T =
288+
default_camera(Vec3{T}(lookfrom), Vec3{T}(lookat), Vec3{T}(vup),
289+
T(vfov), T(aspect_ratio), T(aperture), T(focus_dist)
290+
)
291+
272292
@inline @fastmath function get_ray(c::Camera{T}, s::T, t::T) where T
273293
rd = SVector{2,T}(c.lens_radius * random_vec2_in_disk(T))
274294
offset = c.u * rd.x + c.v * rd.y #offset = c.u * rd.x + c.v * rd.y
@@ -328,8 +348,7 @@ function render(scene::HittableList, cam::Camera{T}, image_width=400,
328348
# Makes comparing performance more accurate.
329349
reseed!()
330350

331-
#Threads.@threads # claforte: uncomment for CRASH?!
332-
for i in 1:image_height
351+
Threads.@threads for i in 1:image_height
333352
@inbounds for j in 1:image_width # iterate over each row (FASTER?!)
334353
accum_color = SA{T}[0,0,0]
335354
u = convert(T, j/image_width)

src/old_proto.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using BenchmarkTools, Images, InteractiveUtils, LinearAlgebra, StaticArrays
66
using LoopVectorization: @turbo
7-
using SIMD: @simd
7+
#using SIMD: @simd
88

99
Threads.nthreads()
1010

@@ -957,7 +957,6 @@ t_cam1 = default_camera([13,2,3], [0,0,0], [0,1,0], 20, 16/9, 0.1, 10.0; elem_ty
957957
# Extract the scene creation from the render() call:
958958
# 300.344 ms (1883484 allocations: 144.21 MiB)
959959
print("render(scene_random_spheres(; elem_type=ELEM_TYPE), t_cam1, 200, 32):")
960-
reseed!()
961960
_scene_random_spheres = scene_random_spheres(; elem_type=ELEM_TYPE)
962961
@btime render($_scene_random_spheres, $t_cam1, 200, 32)
963962

0 commit comments

Comments
 (0)