New random_unit_interval implementation
Re-wrote the random_unit_interval
function to generate doubles with 48 bits of random mantissa (3.5e-15), as opposed to the current 32-bit version (2.3e-10).
This implementation uses rand_r
(to blend the initial state bytes), and erand48
to generate the result over the initial state bytes. The implementation of both functions is inlined to avoid relatively expensive function calls.
This change passes both the testRandom
and testRandomSpacing
tests.
Merge request reports
Activity
@matthieu, @folkert,
testRandomSpacing
hasn't finished quite yet, but the results look decent:[02973.3] main: Categories | 1e-05 1e-06 1e-07 1e-08 1e-09 1e-10 [02973.3] main: total = 52344913920 | 522609 52521 5165 517 50 7 [02973.3] main: expected | 523449 52344 5234 523 52 5 [02973.3] main: Difference | 840 177 69 6 2 2 [02973.3] main: 5 sigma difference | 3615 1140 360 110 35 10
Speed-wise, the new function is ~1.5x slower than the original (configured with
--enable-optimization
).What do you think?
Cheers, Pedro
added 1 commit
- ddc2a6fd - remove Random123 sub-directory, not needed in this branch.
@folkert, cool! If we're going with this branch, can you update the descriptions in
doc/RTD/
? Cheers!mentioned in merge request !862 (closed)
@matthieu, the code was taken from the
glibc
implementation, so it's essentially the same code we get when we call the function directly. The implementations also don't have anything platform-specific about them. For safety/sanity, I could replace theshort int
andunsigned int
types withint16_t
anduint32_t
respectively, just to be sure we don't break anywhere.@nnrw56 I was just wondering whether
glibc
would use a different implementation on a different architecture.@nnrw56, is it easy in this RNG to extend the number of different random number types? Or should we test a few different numbers using
testRandom
and if they work add them?@folkert, I don't understand the question :) What do you mean by different number types?
We want to have different random numbers for the same particle and the same time step, currently we do that by changing the number type like for example
random_number_stellar_feedback
andrandom_number_star_formation
, we know that we need more types than are currently available, so we also need to extend this by a few more types and potentially add a few spare ones such that we do not need to think about it in the future.We have multiple physical processes in the simulations that require some level of stochasticity. At a given time-step (ti_current) and for a given particle (a given ID), we do need different uncorrelated numbers. Essentially, using additional salt, which is what the last argument (the enum) to the function call is.
@folkert beat me to it with a better answer...
49 50 random_number_BH_swallow = 4947009007LL 50 51 }; 51 52 53 #include <errno.h> 54 #include <ieee754.h> 55 #include <limits.h> 56 #include <sys/types.h> 57 58 /* Inline the default RNG functions to avoid costly function calls. */ 59 60 INLINE static int inl_rand_r(unsigned int *seed) { changed this line in version 3 of the diff
- Resolved by Matthieu Schaller