JuliaSIMD / VectorizedRNG.jl

Vectorized uniform and normal random samplers.
MIT License
33 stars 7 forks source link

DomainError when sampling multithreaded from distribution #14

Closed pcjentsch closed 3 years ago

pcjentsch commented 3 years ago

Am I using the multithreaded functionality incorrectly? Here is a MWE.

using VectorizedRNG, Distributions

d = Geometric(0.7) #for example

@Threads.threads for i in 1:10000
       rand(local_rng(),d,100)
end

Error:

ERROR: TaskFailedException
Stacktrace:
 [1] wait
   @ ./task.jl:322 [inlined]
 [2] threading_run(func::Function)
   @ Base.Threads ./threadingconstructs.jl:34
 [3] top-level scope
   @ ./threadingconstructs.jl:93

    nested task error: DomainError with -9.726969444577471e172:
    log will only return a complex result if called with a complex argument. Try log(Complex(x)).
    Stacktrace:
      [1] throw_complex_domainerror(f::Symbol, x::Float64)
        @ Base.Math ./math.jl:33
      [2] log(x::Float64)
        @ Base.Math ./special/log.jl:285
      [3] randexp_unlikely(rng::VectorizedRNG.Xoshift{2}, idx::UInt64, x::Float64)
        @ Random /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Random/src/normal.jl:133
      [4] _randexp(rng::VectorizedRNG.Xoshift{2}, ri::UInt64)
        @ Random /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Random/src/normal.jl:127
      [5] randexp
        @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Random/src/normal.jl:119 [inlined]
      [6] rand(rng::VectorizedRNG.Xoshift{2}, d::Geometric{Float64})
        @ Distributions ~/.julia/packages/Distributions/Xrm9e/src/univariate/discrete/geometric.jl:143
      [7] rand!
        @ ~/.julia/packages/Distributions/Xrm9e/src/univariates.jl:167 [inlined]
      [8] rand(rng::VectorizedRNG.Xoshift{2}, s::Geometric{Float64}, dims::Tuple{Int64})
        @ Distributions ~/.julia/packages/Distributions/Xrm9e/src/univariates.jl:158
      [9] rand(::VectorizedRNG.Xoshift{2}, ::Geometric{Float64}, ::Int64)
        @ Distributions ~/.julia/packages/Distributions/Xrm9e/src/genericrand.jl:26
     [10] macro expansion
        @ ./REPL[5]:2 [inlined]
     [11] (::var"#18#threadsfor_fun#1"{UnitRange{Int64}})(onethread::Bool)
        @ Main ./threadingconstructs.jl:81
     [12] (::var"#18#threadsfor_fun#1"{UnitRange{Int64}})()
        @ Main ./threadingconstructs.jl:48

Occurs both on the latest release and the main branch, with any number of threads. Also happens with Poisson, and even Normal. But randn works fine.

The following code produces this error only sometimes:

@Threads.threads for i in 1:10000
              rand(local_rng(),d)
end
chriselrod commented 3 years ago

Thanks for the issue. I've fixed this and will tag a new release soon.