Improve PRNG jumping when using threads
authorJanne Blomqvist <jb@gcc.gnu.org>
Thu, 5 Sep 2019 06:59:55 +0000 (09:59 +0300)
committerJanne Blomqvist <jb@gcc.gnu.org>
Thu, 5 Sep 2019 06:59:55 +0000 (09:59 +0300)
commit0eaee4abb1d99f6165769a8ad2bfbf818860f40b
tree00b19c147fb98d452597b2fdbda9432eb4395450
parentf44526e367a99b8699fbdc31bb9bcc0618dbe072
Improve PRNG jumping when using threads

Currently, when a new thread needs to use the RANDOM_NUMBER intrinsic,
the per-thread PRNG state is initialized by copying the master state
and then jumping forwards N*2**128 entries in the stream so that the
PRNG streams for different threads don't alias each other, where N is
the number of threads that have so far initialized the PRNG.

With this patch the master state itself is jumped forwards once each
time a new thread initializes the PRNG, thus obviating the need to
jump through all the N-1 previous streams. Effectively turning an O(N)
algorithm into an O(1) one.

Regtested on x86_64-pc-linux-gnu.

libgfortran/ChangeLog:

2019-09-05  Janne Blomqvist  <jb@gcc.gnu.org>

* intrinsics/random.c (master_init): Replace with
master_state.init.
(njumps): Remove variable.
(master_state): Make instance of struct prng_state.
(init_rand_state): When jumping, update the master_state once
instead of keeping track of how many jumps need to be done.
(SZU64): Modify to handle new master_state.
(SZ): Likewise.
(random_seed_i4): Likewise.
(random_seed_i8): Likewise.

From-SVN: r275397
libgfortran/ChangeLog
libgfortran/intrinsics/random.c