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

Document Thread Safety #1829

Open
adomasbaliuka opened this issue Jan 23, 2024 · 1 comment
Open

Document Thread Safety #1829

adomasbaliuka opened this issue Jan 23, 2024 · 1 comment

Comments

@adomasbaliuka
Copy link

adomasbaliuka commented Jan 23, 2024

In which cases is sampling thread-safe? It seems that (at least) sampling from Hypergeometric apparently isn't.

This might be due to this C-code in Rmath-julia. It should be documented if sampling from distributions is thread-safe.

Below example runs fine without the @threads macro. With the macro, it seemingly runs forever and prints warnings to the console (which seem to come from that C library Rmath-julia).

using Random, Distributions

Random.seed!(42)

function sample_KkC(n; N, Q)
    total_errors = Distributions.Binomial(N, Q)
    K = rand(total_errors)
    d_hyper = Hypergeometric(K, N - K, n)
    k = rand(d_hyper)

    return k
end

function main()
    function f(Q)
        objective(n) = [sample_KkC(n; N = 819_200, Q) for _ = 1:100]
        vals = [10, 100]
        objective.(vals)
    end

    Qs = [0.05, 0.055, 0.1, 0.2, 0.3]

    Threads.@threads for i in eachindex(Qs)
        f(Qs[i])
    end
end

main()

output

rhyper.c: afc(i), i=-15 < 0 -- SHOULD NOT HAPPEN!
rhyper(*, n1=-262145576, n2=100, k=10000): branch III: giving up after 1384304160 rejections
rhyper(*, n1=-287340072, n2=100, k=10000): branch III: giving up after 1384304160 rejections

System Info

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, tigerlake)
  Threads: 11 on 8 virtual cores

(jl_Lv9tPw) pkg> st
Status `/tmp/jl_Lv9tPw/Project.toml`
  [31c24e10] Distributions v0.25.107
@adomasbaliuka
Copy link
Author

Should be fixed by JuliaStats/Rmath-julia#46 but does not yet work correctly on Distributions v0.25.110. Will check when updated further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant