Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function barrier in rand! #1350

Merged
merged 1 commit into from
Jun 24, 2021
Merged
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
13 changes: 9 additions & 4 deletions src/univariates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,20 @@ end

# multiple univariate, must allocate array
rand(rng::AbstractRNG, s::Sampleable{Univariate}, dims::Dims) =
rand!(rng, sampler(s), Array{eltype(s)}(undef, dims))
rand!(rng, s, Array{eltype(s)}(undef, dims))
rand(rng::AbstractRNG, s::Sampleable{Univariate,Continuous}, dims::Dims) =
rand!(rng, sampler(s), Array{float(eltype(s))}(undef, dims))
rand!(rng, s, Array{float(eltype(s))}(undef, dims))

# multiple univariate with pre-allocated array
# we use a function barrier since for some distributions `sampler(s)` is not type-stable:
# https://github.com/JuliaStats/Distributions.jl/pull/1281
function rand!(rng::AbstractRNG, s::Sampleable{Univariate}, A::AbstractArray)
smp = sampler(s)
return _rand_loops!(rng, sampler(s), A)
end

function _rand_loops!(rng::AbstractRNG, sampler::Sampleable{Univariate}, A::AbstractArray)
for i in eachindex(A)
@inbounds A[i] = rand(rng, smp)
@inbounds A[i] = rand(rng, sampler)
end
return A
end
Expand Down