-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfng.f90
450 lines (309 loc) · 9.87 KB
/
rfng.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
! This is a Fortran module for Mersenne twister (MT) pseudo-random number
! generation (PRNG)
!
! MT is *not* cryptographically secure. From wikipedia:
!
! [MT] is not cryptographically secure. The reason is that observing a
! sufficient number of iterations (624 in the case of MT19937, since this is
! the size of the state vector from which future iterations are produced)
! allows one to predict all future iterations
!===============================================================================
#define int32_t integer(kind = int32)
#define int64_t integer(kind = int64)
module rng_m
use iso_c_binding
use iso_fortran_env
implicit none
public :: rng_t
private
! TODO: add 64-bit version. From wikipedia it seems that the difference is
! just a few constants
int32_t, parameter :: n32 = 624
!********
public :: rng_state_t
type, bind(c) :: rng_state_t
! C struct interface
integer(kind = c_int32_t) :: mt(0: n32-1) ! state vector
integer(kind = c_int32_t) :: index_ = n32 + 1
end type rng_state_t
!********
type :: rng_t
! Type with bound procedures cannot bind(c), so this type just wraps the
! C interface `rng_state_t` and adds convenience procedures for Fortran
type(rng_state_t) :: s
contains
procedure :: &
seed => seed_mt19937, &
int32 => int32_mt19937, &
uint32 => uint32_mt19937
end type rng_t
!===============================================================================
contains
!===============================================================================
function seed_array(seed_, len_) bind(c, name = "seed_array_fort") result(rng)
! This is not public but C++ dgaf
!
! TODO: DRY with seed()
integer(c_int32_t), intent(in) :: seed_(len_)
integer(c_int32_t), intent(in) :: len_
type(rng_state_t) :: rng
!********
int32_t, parameter :: &
w = 32, &
f = 1812433253
int32_t :: i
!print *, "len_ = ", len_
!print *, "seed_(1) = ", seed_(1)
!print *, "seed_ = ", seed_
rng%index_ = n32
!rng%mt(0) = seed_(1)
rng%mt(0: min(len_, n32) - 1) = seed_(1: min(len_, n32))
! Reference C implementation expands keys differently but YOLO
do i = min(len_, n32), n32 - 1
rng%mt(i) = f * ieor(rng%mt(i-1), shiftr(rng%mt(i-1), w-2)) + i
end do
!print *, "rng%mt = ", rng%mt
!print *, "rng%mt = ", rng%mt(0: 10)
end function seed_array
!===============================================================================
function seed(seed_) bind(c, name = "seed_fort") result(rng)
integer(c_int32_t), intent(in) :: seed_
type(rng_state_t) :: rng
!********
int32_t, parameter :: &
w = 32, &
f = 1812433253
int32_t :: i
!print *, "seed_ = ", seed_
rng%index_ = n32
rng%mt(0) = seed_
! Honestly it's a lot easier and more compact to write bit-fiddling
! operations like this in C instead of Fortran
do i = 1, n32 - 1
rng%mt(i) = f * ieor(rng%mt(i-1), shiftr(rng%mt(i-1), w-2)) + i
end do
!print *, "rng%mt = ", rng%mt
!print *, "rng%mt = ", rng%mt(0: 4)
end function seed
!===============================================================================
subroutine seed_mt19937(rng, seed_)
! Fortran convenience interface to seed()
class(rng_t) :: rng
int32_t, intent(in) :: seed_
rng%s = seed(seed_)
end subroutine seed_mt19937
!===============================================================================
function get_int32(rng) bind(c, name = "int32_fort") result(num)
type(rng_state_t) :: rng
integer(c_int32_t) :: num
!********
int32_t, parameter :: &
b = int(z"9d2c5680", int32), &
c = int(z"efc60000", int32), &
d = int(z"ffffffff", int32), &
l = 18, &
s = 7, &
t = 15, &
u = 11
int32_t :: y
if (rng%index_ >= n32) then
if (rng%index_ > n32) rng = seed(5489)
call twist_mt19937(rng)
end if
y = rng%mt(rng%index_)
y = ieor(y, iand(shiftr(y, u), d))
y = ieor(y, iand(shiftl(y, s), b))
y = ieor(y, iand(shiftl(y, t), c))
y = ieor(y, shiftr(y, l))
rng%index_ = rng%index_ + 1
num = y
end function get_int32
!===============================================================================
function get_uint32(rng) bind(c, name = "uint32_fort") result(num)
type(rng_state_t) :: rng
integer(c_int64_t) :: num
!********
!int64_t :: num
num = iand( &
!int(rng%s%int32(), int64), &
int(get_int32(rng), int64), &
int(z"ffffffff" , int64))
!print *, "num = ", num
end function get_uint32
!===============================================================================
function int32_mt19937(rng) result(num)
class(rng_t) :: rng
int32_t :: num
num = get_int32(rng%s)
end function int32_mt19937
!===============================================================================
function uint32_mt19937(rng) result(num)
! Extract an unsigned 32 bit int but return a signed 64 bit int because
! that's all that Fortran has :(
class(rng_t) :: rng
int64_t :: num
num = iand( &
int(rng%int32(), int64), &
int(z"ffffffff", int64))
end function uint32_mt19937
!===============================================================================
subroutine twist_mt19937(rng)
type(rng_state_t) :: rng
!********
int32_t, parameter :: &
a = int(z"9908b0df", int32), &
m = 397, &
r = 31
int32_t, parameter :: &
lower_mask = shiftl(1, r) - 1, &
upper_mask = not(lower_mask)
int32_t :: i, x, xa
!print "(a,b0.32)", "lower_mask = ", lower_mask
!print "(a,b0.32)", "upper_mask = ", upper_mask
!! Only using error unit to debug without causing recursive IO (e.g. if
!! caller is calling int32() within a print statement)
!write(error_unit,*) "twist_mt19937()"
! Fortran `mod()` is consistent with the C `%` operator. Fortran `modulo()`
! works differently for negative args
do i = 0, n32 - 1
x = ior(iand(rng%mt(i) , upper_mask), &
iand(rng%mt(mod(i+1, n32)), lower_mask))
xa = shiftr(x, 1)
if (mod(x, 2) /= 0) xa = ieor(xa, a)
rng%mt(i) = ieor(rng%mt(mod(i + m, n32)), xa)
end do
rng%index_ = 0
end subroutine twist_mt19937
!===============================================================================
end module rng_m
!===============================================================================
#ifdef RNG_FORT_TEST
module rng_test_m
use rng_m
implicit none
integer :: nfail_glbl = 0, npass_glbl = 0
contains
!********
function to_str(i) result(str_)
integer, intent(in) :: i
character(len = :), allocatable :: str_
character :: buffer*16
write(buffer, "(i0)") i
str_ = trim(buffer)
end function to_str
!********
! Might be better if we take expected vs actual values as args, then we can
! print them instead of just having a binary pass/fail
#define TEST_(x) call test_((x), __FILE__, __LINE__)
subroutine test_(test, file_, line)
logical, intent(in) :: test
character(len = *), intent(in) :: file_
integer, intent(in) :: line
if (test) then
npass_glbl = npass_glbl + 1
else
nfail_glbl = nfail_glbl + 1
write(*,*) "Error: assertion failed in """//file_ &
//""" at line "//to_str(line)
end if
end subroutine test_
!********
subroutine rng_test()
integer :: i, hist_min, hist_max, mod_, rand_
integer(kind = 8) :: dummy
!integer :: fid
integer, allocatable :: hist(:)
type(rng_t) :: rng, ra, rb
! To compare results with reference C implementation, call init_genrand(0)
! instead of init_by_array() as in the original. The reference C
! implementation is here:
!
! http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/MT2002/CODES/mt19937ar.c
!
! It can be easier to print hex values instead of decimal-formatted ints
! because of signed vs unsigned differences with Fortran
!print *, rng%int32(), rng%int32(), rng%int32(), rng%int32(), rng%int32()
! Test default (not explicitly seeded) seed
TEST_(rng%uint32() == 3499211612)
! Test explicit seed
call rng%seed(0)
TEST_(rng%uint32() == 2357136044)
TEST_(rng%uint32() == 2546248239)
! Test re-seeding. Should get same number as before
call rng%seed(0)
TEST_(rng%uint32() == 2357136044)
! Test > 624 calls. This will trigger another twist_mt19937() call
call rng%seed(0)
do i = 1, 997
dummy = rng%uint32()
end do
TEST_(rng%uint32() == 3814118674)
TEST_(rng%uint32() == 2172679577)
TEST_(rng%uint32() == 3043451800)
! Test multiple RNGs running concurrently
call ra%seed(0)
call rb%seed(0)
TEST_(ra%uint32() == 2357136044)
TEST_(rb%uint32() == 2357136044)
TEST_(ra%uint32() == 2546248239)
TEST_(rb%uint32() == 2546248239)
TEST_(ra%uint32() == 3071714933)
TEST_(rb%uint32() == 3071714933)
! Test (signed) int32 generation
call rng%seed(0)
TEST_(rng%int32() == -1937831252)
TEST_(rng%int32() == -1748719057)
TEST_(rng%int32() == -1223252363)
TEST_(rng%int32() == -668873536)
TEST_(rng%int32() == -1706118333)
!********
write(*,*) to_str(npass_glbl)//" / "//to_str(npass_glbl + nfail_glbl) &
//" tests passed"
write(*,*) to_str(nfail_glbl)//" tests failed"
write(*,*)
! TODO: histogram test
return
do i = 1, 1000
!print "(a,z0.8)", "rng = ", rng%int32()
print *, "rng = ", rng%uint32()
!print "(a,z0.8)", "rng = ", rng%uint32()
!print *, "rng mod = ", modulo(rng%int32(), 10001)
end do
!********
! Check some results by making a histogram that counts the number of each
! random value
mod_ = 5
hist_min = 0
hist_max = mod_ - 1
allocate(hist(hist_min: hist_max))
hist = 0
call rng%seed(0)
do i = 1, 1000000
rand_ = modulo(rng%int32(), mod_)
hist(rand_) = hist(rand_) + 1
end do
write(*,*) "Histogram ="
write(*,*) "["
write(*,"(i12)") hist
write(*,*) "]"
!open(newunit = fid, file = "rng-out-1.csv")
!do i = 1, 1000
! write(fid, "(i0)") modulo(rng%int32(), 10)
!end do
!close(fid)
end subroutine rng_test
end module rng_test_m
!===============================================================================
program main
use rng_m
use rng_test_m
implicit none
write(*,*) "starting rng main"
write(*,*)
call rng_test()
write(*,*) "ending rng main"
write(*,*)
call exit(nfail_glbl)
end program main
#endif
!===============================================================================