Skip to content

Commit 4c56754

Browse files
committed
Fix crash - initialize TRNG during __init__()
1 parent 4124419 commit 4c56754

File tree

3 files changed

+218
-249
lines changed

3 files changed

+218
-249
lines changed

src/RayTracingWeekend.jl

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +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,
711
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
10-
11-
export TRNG
14+
export TRNG
1215

1316
const Vec3{T<:AbstractFloat} = SVector{3, T}
1417

@@ -43,21 +46,31 @@ end
4346
(one(T)-t)*white + t*skyblue
4447
end
4548

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

50-
reseed!() = for (i,rng) in enumerate(TRNG) Random.seed!(rng, i) end # reset the seed
51-
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
5261

53-
@inline function trand() # thread-local rand()
54-
@inbounds rng = TRNG[Threads.threadid()]
55-
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)
5669
end
5770

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

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

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+
274292
@inline @fastmath function get_ray(c::Camera{T}, s::T, t::T) where T
275293
rd = SVector{2,T}(c.lens_radius * random_vec2_in_disk(T))
276294
offset = c.u * rd.x + c.v * rd.y #offset = c.u * rd.x + c.v * rd.y
@@ -330,8 +348,7 @@ function render(scene::HittableList, cam::Camera{T}, image_width=400,
330348
# Makes comparing performance more accurate.
331349
reseed!()
332350

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

src/old_proto.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)