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

Random: support threads for GLOBAL_RNG access #32407

Merged
merged 1 commit into from
Jul 19, 2019
Merged

Random: support threads for GLOBAL_RNG access #32407

merged 1 commit into from
Jul 19, 2019

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Jun 24, 2019

No description provided.

@JeffBezanson JeffBezanson added multithreading Base.Threads and related functionality randomness Random number generation and the Random stdlib labels Jun 24, 2019
@vtjnash vtjnash added the triage This should be discussed on a triage call label Jun 27, 2019
@JeffBezanson
Copy link
Member

From triage: make default global RNG per-thread for now. Document that explicit RNG objects are not thread-safe.

@StefanKarpinski
Copy link
Member

General thoughts from triage:

  • calling rand() should be thread-safe even if there's a performance hit
  • people are comfortable with changing the default rng to PCG64/Xorshift128whatever
    • including this with multithreading might help with popularity since it combines good with bad
  • using a PRNG with a small state might help since we can use atomics
  • explicit RNG objects are not thread-safe, if you want to use them from multiple threads, use locks

@vtjnash vtjnash removed the triage This should be discussed on a triage call label Jul 8, 2019
@vtjnash vtjnash marked this pull request as ready for review July 12, 2019 23:06
@StefanKarpinski
Copy link
Member

Calling the function to get the global RNG get_local_rng is a bit confusing. I know it's thread-local, but it's global in the sense of scope. Maybe get_thread_rng?

@StefanKarpinski
Copy link
Member

Or maybe just default_rng()?

@JeffBezanson JeffBezanson changed the title RFC: Random: support threads for GLOBAL_RNG access Random: support threads for GLOBAL_RNG access Jul 17, 2019
@JeffBezanson
Copy link
Member

Rebased to pick up #32604.

I think we can merge this, and think about the naming convention for functions like this. I know people might start using it, but it's not exported and we'll have time to rename it if we want before 1.3.

#RNG = get(tls, :RNG, nothing)
#RNG isa MersenneTwister && return RNG
if length(THREAD_RNGs) < tid
resize!(THREAD_RNGs, Threads.nthreads())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether this is thread safe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I still need to push my local fork which fixes some stuff like this. Was just waiting on the outcome of the naming discussion.

The `Random.GLOBAL_RNG` is now a singleton placeholder object
which implements the prior `Random` public API for MersenneTwister
as a shim to support existing clients until Julia v2.0.
@rfourquet
Copy link
Member

Or maybe just default_rng()

I agree with this suggestion, and had the same idea when reading this PR the first time. I find both get_local_rng() and get_global_rng() confusing, while default_rng() conveys the practical meaning without implementation details (if I may!) considerations (default_rng() naming works independantly of the threading abilities of the runtime, and in fact there was at one point an unexported function defaultRNG() = GLOBAL_RNG).

@testset "GLOBAL_RNG" begin
local GLOBAL_RNG = Random.GLOBAL_RNG
local LOCAL_RNG = Random.get_local_rng()
@test VERSION < v"2" # deprecate this in v2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _GLOBAL_RNG thing is rather clever! Do I get it right that it's only to avoid breaking code and that it's intended to be removed in v2?

@mauro3
Copy link
Contributor

mauro3 commented Jan 9, 2020

What is the reason that in this PR separate RNG stream are created (link to code) instead of using the "trick" advocated in the docs of using just one chain but sampling from different locations (implemented using the Future.randjump)?

Wikipedia (fourth bullet) suggests that at least for MCMC the randjump strategy should be used. Or are the seeds of the separate streams chosen such that inter-stream numbers are non-correlated too? This line suggest that no such approach is taken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
multithreading Base.Threads and related functionality randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants