Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GPUArrays"
uuid = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
version = "12.3.0"
version = "12.3.1"

[workspace]
projects = ["lib/GPUArraysCore", "lib/JLArrays", "test", "docs"]
Expand Down
35 changes: 22 additions & 13 deletions src/host/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ end
philox4x32_10((ctr0%UInt32, (ctr0>>32)%UInt32, ctr1%UInt32, (ctr1>>32)%UInt32), (key%UInt32, (key>>32)%UInt32))
end

## Float conversions: unsigned integer → uniform float, strictly positive
## (can round up to exactly 1.0)
## Float conversions

# `rand` must return values in [0, 1), so it uses the same fixed grids as Random's Xoshiro:
# 11, 24 and 53 random bits, each output equally likely, never 1.0.
@inline closeopen01(::Type{Float16}, u::UInt32) = Float16(u >> 21) * Float16(0x1.0p-11)
@inline closeopen01(::Type{Float32}, u::UInt32) = Float32(u >> 8) * Float32(0x1.0p-24)
@inline closeopen01(::Type{Float64}, u::UInt64) = Float64(u >> 11) * 0x1.0p-53

# Box-Muller needs a strictly positive input for its logarithm, and benefits from the finer
# resolution near zero, so it uses Random123's conversion. It can round up to exactly 1.0.

"""
u01(F, u::Union{UInt32, UInt64})::F
Expand Down Expand Up @@ -334,17 +342,18 @@ end

# Convert Philox UInt32 outputs to N values of type T
@inline philox_to_vals(::Type{Float16}, a1, a2, a3, a4) =
(Float16(u01(Float32, a1)), Float16(u01(Float32, a2)),
Float16(u01(Float32, a3)), Float16(u01(Float32, a4)))
(closeopen01(Float16, a1), closeopen01(Float16, a2),
closeopen01(Float16, a3), closeopen01(Float16, a4))
@inline philox_to_vals(::Type{Float32}, a1, a2, a3, a4) =
(u01(Float32, a1), u01(Float32, a2), u01(Float32, a3), u01(Float32, a4))
(closeopen01(Float32, a1), closeopen01(Float32, a2),
closeopen01(Float32, a3), closeopen01(Float32, a4))
for T in (UInt8, UInt16, UInt32, Int8, Int16, Int32, Bool)
@eval @inline philox_to_vals(::Type{$T}, a1, a2, a3, a4) =
(a1 % $T, a2 % $T, a3 % $T, a4 % $T)
end
@inline philox_to_vals(::Type{Float64}, a1, a2, a3, a4) =
(u01(Float64, UInt64(a1) | UInt64(a2) << 32),
u01(Float64, UInt64(a3) | UInt64(a4) << 32))
(closeopen01(Float64, UInt64(a1) | UInt64(a2) << 32),
closeopen01(Float64, UInt64(a3) | UInt64(a4) << 32))
for T in (UInt64, Int64)
@eval @inline philox_to_vals(::Type{$T}, a1, a2, a3, a4) =
((UInt64(a1) | UInt64(a2) << 32) % $T,
Expand All @@ -355,14 +364,14 @@ for T in (UInt128, Int128)
((UInt128(a1) | UInt128(a2) << 32 | UInt128(a3) << 64 | UInt128(a4) << 96) % $T,)
end
@inline philox_to_vals(::Type{Complex{Float16}}, a1, a2, a3, a4) =
(complex(Float16(u01(Float32, a1)), Float16(u01(Float32, a2))),
complex(Float16(u01(Float32, a3)), Float16(u01(Float32, a4))))
(complex(closeopen01(Float16, a1), closeopen01(Float16, a2)),
complex(closeopen01(Float16, a3), closeopen01(Float16, a4)))
@inline philox_to_vals(::Type{Complex{Float32}}, a1, a2, a3, a4) =
(complex(u01(Float32, a1), u01(Float32, a2)),
complex(u01(Float32, a3), u01(Float32, a4)))
(complex(closeopen01(Float32, a1), closeopen01(Float32, a2)),
complex(closeopen01(Float32, a3), closeopen01(Float32, a4)))
@inline philox_to_vals(::Type{Complex{Float64}}, a1, a2, a3, a4) =
(complex(u01(Float64, UInt64(a1) | UInt64(a2) << 32),
u01(Float64, UInt64(a3) | UInt64(a4) << 32)),)
(complex(closeopen01(Float64, UInt64(a1) | UInt64(a2) << 32),
closeopen01(Float64, UInt64(a3) | UInt64(a4) << 32)),)

# Number of output values per Philox4x32 call
vals_per_call(::Type{T}) where T = sizeof(T) <= 4 ? 4 : sizeof(T) <= 8 ? 2 : 1
Expand Down
23 changes: 22 additions & 1 deletion test/testsuite/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ Random.rand(rng::AbstractRNG, ::Random.SamplerType{RGBTriplet}) =
@test Array(A) != Array(B)
end

# uniform floats are in [0, 1). Raw words at the top of the range used to round up
# to 1.0: about one in 4000 Float16 draws, and one in 2^25 Float32 draws.
in01(x::Real) = 0 <= x < 1
in01(x::Complex) = in01(real(x)) && in01(imag(x))
for T in (Float16, Float32, Float64, ComplexF16, ComplexF32, ComplexF64)
for words in (ntuple(_ -> zero(UInt32), 4), ntuple(_ -> typemax(UInt32), 4),
(0xffefff7f, 0xffefff80, 0xffffff7f, 0xffffff80),
(0xfffffbff, 0xffffffff, 0xfffffc00, 0xffffffff))
@test all(in01, GPUArrays.philox_to_vals(T, words...))
end
top = prevfloat(one(real(T)))
top = T <: Complex ? T(top, top) : top
@test all(==(zero(T)), GPUArrays.philox_to_vals(T, ntuple(_ -> zero(UInt32), 4)...))
@test all(==(top), GPUArrays.philox_to_vals(T, ntuple(_ -> typemax(UInt32), 4)...))
end
if AT <: AbstractGPUArray
for T in filter(t -> t in (Float16, ComplexF16), eltypes)
@test all(in01, Array(rand!(rng, AT{T}(undef, 2^20))))
end
end

# empty arrays
B = AT{Float32}(undef, 0)
rand!(rng, B)
Expand Down Expand Up @@ -147,7 +168,7 @@ Random.rand(rng::AbstractRNG, ::Random.SamplerType{RGBTriplet}) =
A = AT{RGBTriplet}(undef, 1024)
rand!(rng, A)
h = Array(A)
@test all(t -> 0f0 <= t.r <= 1f0 && 0f0 <= t.g <= 1f0 && 0f0 <= t.b <= 1f0, h)
@test all(t -> 0f0 <= t.r < 1f0 && 0f0 <= t.g < 1f0 && 0f0 <= t.b < 1f0, h)
@test any(t -> t.r != h[1].r, h)
end

Expand Down
Loading