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 = "11.5.8"
version = "11.5.9"

[workspace]
projects = ["lib/GPUArraysCore", "lib/JLArrays", "test", "docs"]
Expand Down
5 changes: 3 additions & 2 deletions src/host/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ end
end
end

function Random.rand!(rng::RNG, A::AnyGPUArray{T}) where T <: Number
function Random.rand!(rng::RNG, A::AnyGPUArray{T}) where T
isempty(A) && return A
rand_generic_kernel!(get_backend(A))(rng.seed, rng.counter, A; ndrange=length(A))
advance_counter!(rng)
Expand Down Expand Up @@ -586,7 +586,8 @@ function Random.rand!(rng::RNG{AT}, A::AbstractArray{T}) where {AT, T}
Random.rand!(rng, B)
copyto!(A, B)
end
function Random.randn!(rng::RNG{AT}, A::AbstractArray{T}) where {AT, T}
function Random.randn!(rng::RNG{AT}, A::AbstractArray{T}) where {AT, T<:Union{AbstractFloat,
Complex{<:AbstractFloat}}}
isempty(A) && return A
B = similar(AT{T}, size(A))
Random.randn!(rng, B)
Expand Down
28 changes: 28 additions & 0 deletions test/testsuite/random.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# A non-`Number` isbits eltype with a custom sampler, mirroring how ColorTypes
# hooks colorants into the Random API. Exercises the generic element-wise rand!
# path; regression cover for JuliaGPU/CUDA.jl#3179.
struct RGBTriplet
r::Float32
g::Float32
b::Float32
end
Random.rand(rng::AbstractRNG, ::Random.SamplerType{RGBTriplet}) =
RGBTriplet(rand(rng, Float32), rand(rng, Float32), rand(rng, Float32))

@testsuite "random" (AT, eltypes)->begin
rng = if AT <: AbstractGPUArray
GPUArrays.RNG{AT}()
Expand Down Expand Up @@ -88,4 +99,21 @@
@test (randn!(cpu_rng, A); true)
end
end

# non-`Number` eltypes with a custom sampler must generate element-wise
# rather than hit an ambiguous fallback (JuliaGPU/CUDA.jl#3179)
if AT <: AbstractGPUArray
@testset "non-Number eltype" begin
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 any(t -> t.r != h[1].r, h)
end

# randn! on a non-float eltype must error cleanly, not recurse forever
@testset "randn! rejects non-float" begin
@test_throws MethodError randn!(rng, AT{Int32}(undef, 4))
end
end
end
Loading