25 INTEGER :: i, nsamples, nargs, stat
27 REAL(kind=
dp) :: t, tend, tmax, tmin, tstart, tsum, tsum2
28 TYPE(mp_comm_type) :: mpi_comm
29 TYPE(rng_stream_type) :: rng_stream
30 CHARACTER(len=32) :: arg
33 nargs = command_argument_count()
36 error stop
"Usage: parallel_rng_types_TEST [<int:nsamples>]"
39 CALL get_command_argument(1, arg)
40 READ (arg, *, iostat=stat) nsamples
42 error stop
"Usage: parallel_rng_types_TEST [<int:nsamples>]"
46 ionode = mpi_comm%is_source()
54 "Check distributions using", nsamples,
" random numbers:"
59 rng_stream = rng_stream_type(name=
"Test uniform distribution [0,1]", &
61 extended_precision=.true.)
76 IF (t > tmax) tmax = t
77 IF (t < tmin) tmin = t
86 "Average: ", tsum/real(nsamples, kind=
dp), &
87 "Variance:", tsum2/real(nsamples, kind=
dp), &
88 "Time [s]:", tend - tstart
93 rng_stream = rng_stream_type(name=
"Test normal Gaussian distribution", &
95 extended_precision=.true.)
107 t = rng_stream%next()
110 IF (t > tmax) tmax = t
111 IF (t < tmin) tmin = t
120 "Average: ", tsum/real(nsamples, kind=
dp), &
121 "Variance:", tsum2/real(nsamples, kind=
dp), &
122 "Time [s]:", tend - tstart
137 TYPE(rng_stream_type) :: rng_stream
138 CHARACTER(len=rng_record_length) :: rng_record
139 REAL(KIND=
dp),
DIMENSION(3, 2) :: ig, ig_orig, cg, cg_orig, bg, bg_orig
140 CHARACTER(len=rng_name_length) :: name, name_orig
141 CHARACTER(len=*),
PARAMETER :: serialized_string = &
142 "qtb_rng_gaussian 1 F T F 0.0000000000000000E+00&
151 "Checking dump and load round trip:"
153 rng_stream = rng_stream_type(name=
"Roundtrip for normal Gaussian distrib", &
155 extended_precision=.true.)
157 CALL rng_stream%advance(7, 42)
158 CALL rng_stream%get(ig=ig_orig, cg=cg_orig, bg=bg_orig, name=name_orig)
159 CALL rng_stream%dump(rng_record)
162 CALL rng_stream%get(ig=ig, cg=cg, bg=bg, name=name)
164 IF (any(ig /= ig_orig) .OR. any(cg /= cg_orig) .OR. any(bg /= bg_orig) &
165 .OR. (name /= name_orig)) &
166 error stop
"Stream dump and load roundtrip failed"
169 "Roundtrip successful"
172 "Checking dumped format:"
175 rng_stream = rng_stream_type(name=
"qtb_rng_gaussian", &
177 extended_precision=.true., &
180 CALL rng_stream%dump(rng_record)
183 "EXPECTED:", serialized_string
186 "GENERATED:", rng_record
188 IF (rng_record /= serialized_string) &
189 error stop
"Serialized record does not match the expected output"
192 "Serialized record matches the expected output"
200 TYPE(rng_stream_type) :: rng_stream
202 INTEGER,
PARAMETER :: sz = 20
203 INTEGER,
DIMENSION(1:sz) :: arr, arr2, orig
204 LOGICAL,
DIMENSION(1:sz) :: mask
206 REAL(KIND=
dp),
DIMENSION(3, 2),
PARAMETER :: ig = 12.0_dp
211 rng_stream = rng_stream_type(name=
"shuffle() check", seed=ig)
212 orig = [(idx, idx=1, sz)]
215 CALL rng_stream%shuffle(arr)
217 IF (all(arr == orig)) &
218 error stop
"shuffle failed: array was left untouched"
221 IF (any(arr /= orig(arr))) &
222 error stop
"shuffle failed: the shuffled original is not the shuffled original"
227 DO idx = 1,
size(orig)
228 IF (minval(arr, mask) /= orig(idx)) &
229 error stop
"shuffle failed: there is at least one unknown index"
230 mask(minloc(arr, mask)) = .false.
235 CALL rng_stream%reset()
236 CALL rng_stream%shuffle(arr2)
238 IF (any(arr2 /= arr)) &
239 error stop
"shuffle failed: array was shuffled differently with same rng state"
Defines the basic variable types.
integer, parameter, public dp
Machine interface based on Fortran 2003 and POSIX.
integer, parameter, public default_output_unit
real(kind=dp) function, public m_walltime()
returns time from a real-time clock, protected against rolling early/easily
Interface to the message passing library MPI.
subroutine, public mp_world_init(mp_comm)
initializes the system default communicator
subroutine, public mp_world_finalize()
finalizes the system default communicator
Parallel (pseudo)random number generator (RNG) for multiple streams and substreams of random numbers.
type(rng_stream_type) function, public rng_stream_type_from_record(rng_record)
Create a RNG stream from a record given as an internal file (string).
integer, parameter, public rng_name_length
integer, parameter, public rng_record_length
integer, parameter, public uniform
subroutine, public check_rng(output_unit, ionode)
...
integer, parameter, public gaussian
subroutine shuffle_check()
...
subroutine dump_reload_check()
...
program parallel_rng_types_test