25 INTEGER :: i, nsamples, nargs, stat
27 REAL(kind=
dp) :: t, tend, tmax, tmin, tstart, tsum, tsum2
30 CHARACTER(len=32) :: arg
33 nargs = command_argument_count()
36 error stop
"Usage: parallel_rng_types_TEST [<int:nsamples>]"
40 CALL get_command_argument(1, arg)
41 READ (arg, *, iostat=stat) nsamples
43 error stop
"Usage: parallel_rng_types_TEST [<int:nsamples>]"
48 ionode = mpi_comm%is_source()
56 "Check distributions using", nsamples,
" random numbers:"
63 extended_precision=.true.)
79 IF (t > tmax) tmax = t
80 IF (t < tmin) tmin = t
89 "Average: ", tsum/real(nsamples, kind=
dp), &
90 "Variance:", tsum2/real(nsamples, kind=
dp), &
91 "Time [s]:", tend - tstart
96 rng_stream =
rng_stream_type(name=
"Test normal Gaussian distribution", &
98 extended_precision=.true.)
111 t = rng_stream%next()
114 IF (t > tmax) tmax = t
115 IF (t < tmin) tmin = t
124 "Average: ", tsum/real(nsamples, kind=
dp), &
125 "Variance:", tsum2/real(nsamples, kind=
dp), &
126 "Time [s]:", tend - tstart
142 CHARACTER(len=rng_record_length) :: rng_record
143 REAL(KIND=
dp),
DIMENSION(3, 2) :: ig, ig_orig, cg, cg_orig, bg, bg_orig
144 CHARACTER(len=rng_name_length) :: name, name_orig
145 CHARACTER(len=*),
PARAMETER :: serialized_string = &
146 "qtb_rng_gaussian 1 F T F 0.0000000000000000E+00&
155 "Checking dump and load round trip:"
157 rng_stream =
rng_stream_type(name=
"Roundtrip for normal Gaussian distrib", &
159 extended_precision=.true.)
161 CALL rng_stream%advance(7, 42)
162 CALL rng_stream%get(ig=ig_orig, cg=cg_orig, bg=bg_orig, name=name_orig)
163 CALL rng_stream%dump(rng_record)
166 CALL rng_stream%get(ig=ig, cg=cg, bg=bg, name=name)
168 IF (any(ig /= ig_orig) .OR. any(cg /= cg_orig) .OR. any(bg /= bg_orig) &
169 .OR. (name /= name_orig))
then
170 error stop
"Stream dump and load roundtrip failed"
174 "Roundtrip successful"
177 "Checking dumped format:"
182 extended_precision=.true., &
185 CALL rng_stream%dump(rng_record)
188 "EXPECTED:", serialized_string
191 "GENERATED:", rng_record
193 IF (rng_record /= serialized_string)
then
194 error stop
"Serialized record does not match the expected output"
198 "Serialized record matches the expected output"
205 SUBROUTINE shuffle_check()
208 INTEGER,
PARAMETER :: sz = 20
209 INTEGER,
DIMENSION(1:sz) :: arr, arr2, orig
210 LOGICAL,
DIMENSION(1:sz) :: mask
212 REAL(KIND=
dp),
DIMENSION(3, 2),
PARAMETER :: ig = 12.0_dp
218 orig = [(idx, idx=1, sz)]
221 CALL rng_stream%shuffle(arr)
223 IF (all(arr == orig))
then
224 error stop
"shuffle failed: array was left untouched"
228 IF (any(arr /= orig(arr)))
then
229 error stop
"shuffle failed: the shuffled original is not the shuffled original"
235 DO idx = 1,
size(orig)
236 IF (minval(arr, mask) /= orig(idx))
then
237 error stop
"shuffle failed: there is at least one unknown index"
239 mask(minloc(arr, mask)) = .false.
244 CALL rng_stream%reset()
245 CALL rng_stream%shuffle(arr2)
247 IF (any(arr2 /= arr))
then
248 error stop
"shuffle failed: array was shuffled differently with same rng state"
254 END SUBROUTINE shuffle_check
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()
Finalize the system default communicator and MPI when CP2K owns MPI.
Parallel (pseudo)random number generator (RNG) for multiple streams and substreams of random numbers.
subroutine advance(self, e, c)
Advance the state by n steps, i.e. jump n steps forward, if n > 0, or backward if n < 0.
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 dump_reload_check()
...
program parallel_rng_types_test