beast2 icon indicating copy to clipboard operation
beast2 copied to clipboard

Randomizer per thread for deterministic behaviour when using threads

Open rbouckaert opened this issue 1 year ago • 0 comments
trafficstars

BEAST is usually deterministic, so starting with the same seed twice results in exactly the same output.

Multi-threaded algorithms that use the Randomizer class in different threads does not show this behaviour, since it depends on how much time the operating system assigns to each thread on when threads access Randomizer. This hinders debugging of for example automatic stopping MCMC algorithms, which use multiple MCMC chains in parallel, all accessing the same Randomizer object.

A solution that has minimal impact on other code would be for the Randomizer to check the thread name using Thread.currentThread().getName(). If the name is not set, the default Randomizer.random object can be used to generate random numbers. If the thread name is set, a new MersenneTwisterFast can be associate with the name, so that the order in which threads obtain random numbers does not matter.

This is somewhat fragile in that the programmer has to set the thread name (using Thread.setName()). Also, it may have performance impact on programs accessing many random numbers. On the other hand, it does not require any existing code to be updated, so can remain in a v2.7 release.

rbouckaert avatar Mar 11 '24 02:03 rbouckaert