gfortran 如何将 random_seed 增加 2^128
gfortran how do I increment random_seed by 2^128
gfortran page on random_seed 说当使用 OMP 线程时,每个线程将其种子递增 2^128。我想知道如何手动将种子增加 2^128。我写了一个小测试程序将主种子全部设置为 0,然后查看种子是什么,但我不明白我在看什么。我想知道的是我在子例程 increment_by_2_tothe_128
中放入的内容
program main
implicit none
character(len=32) :: arg
integer :: n
integer :: i
integer :: nthreads
integer, allocatable :: seed(:, :)
integer, allocatable :: master_seed(:)
real, allocatable :: rn(:)
call get_command_argument(1, arg)
read(arg, *) nthreads
call random_seed(size=n)
allocate(seed(n, nthreads))
allocate(master_seed(n))
allocate(rn(nthreads))
master_seed = 0
seed = 0
call random_seed(put=master_seed)
! call increment_by_2_tothe_128(n)
call omp_set_num_threads(nthreads)
!$OMP PARALLEL DO
do i=1,nthreads
call random_number(rn(i))
call random_seed(get=seed(:,i))
end do
do i=1,nthreads
print *, i
print *, rn(i)
print *, seed(:,i)
end do
end program main
subroutine increment_by_2_tothe_128(n)
implicit none
integer, intent(in) :: n
integer :: current_seed(n)
integer :: increment_seed(n)
call random_seed(get=current_seed)
! what goes here:
! incrememt_seed = current_seed + 2**128
call random_seed(put=increment_seed)
end subroutine increment_by_2_tothe_128
您不能手动执行此操作。您需要访问随机数生成器才能执行此操作,但内部结构不会向 Fortran 程序员公开。而且你显然不能调用生成器 2^128 次。
如果您需要进行移位,您需要使用一些伪随机数生成器,它确实公开了内部结构 和 同时允许这种移位。例如,它可以是 gfortran 内部使用的 xoroshiro PRNG family。这些生成器具有针对此转换的专门功能:
All generators, being based on linear recurrences, provide jump
functions that make it possible to simulate any number of calls to the
next-state function in constant time, once a suitable jump polynomial
has been computed. We provide ready-made jump functions for a number
of calls equal to the square root of the period, to make it easy
generating non-overlapping sequences for parallel computations, and
equal to the cube of the fourth root of the period, to make it
possible to generate independent sequences on different parallel
processors.
这些生成器通常用 C 实现,但 Fortran 实现 also exist(subroutine rng_jump
是跳转函数,免责声明:link 转到我的存储库,不保证质量)。
gfortran page on random_seed 说当使用 OMP 线程时,每个线程将其种子递增 2^128。我想知道如何手动将种子增加 2^128。我写了一个小测试程序将主种子全部设置为 0,然后查看种子是什么,但我不明白我在看什么。我想知道的是我在子例程 increment_by_2_tothe_128
program main
implicit none
character(len=32) :: arg
integer :: n
integer :: i
integer :: nthreads
integer, allocatable :: seed(:, :)
integer, allocatable :: master_seed(:)
real, allocatable :: rn(:)
call get_command_argument(1, arg)
read(arg, *) nthreads
call random_seed(size=n)
allocate(seed(n, nthreads))
allocate(master_seed(n))
allocate(rn(nthreads))
master_seed = 0
seed = 0
call random_seed(put=master_seed)
! call increment_by_2_tothe_128(n)
call omp_set_num_threads(nthreads)
!$OMP PARALLEL DO
do i=1,nthreads
call random_number(rn(i))
call random_seed(get=seed(:,i))
end do
do i=1,nthreads
print *, i
print *, rn(i)
print *, seed(:,i)
end do
end program main
subroutine increment_by_2_tothe_128(n)
implicit none
integer, intent(in) :: n
integer :: current_seed(n)
integer :: increment_seed(n)
call random_seed(get=current_seed)
! what goes here:
! incrememt_seed = current_seed + 2**128
call random_seed(put=increment_seed)
end subroutine increment_by_2_tothe_128
您不能手动执行此操作。您需要访问随机数生成器才能执行此操作,但内部结构不会向 Fortran 程序员公开。而且你显然不能调用生成器 2^128 次。
如果您需要进行移位,您需要使用一些伪随机数生成器,它确实公开了内部结构 和 同时允许这种移位。例如,它可以是 gfortran 内部使用的 xoroshiro PRNG family。这些生成器具有针对此转换的专门功能:
All generators, being based on linear recurrences, provide jump functions that make it possible to simulate any number of calls to the next-state function in constant time, once a suitable jump polynomial has been computed. We provide ready-made jump functions for a number of calls equal to the square root of the period, to make it easy generating non-overlapping sequences for parallel computations, and equal to the cube of the fourth root of the period, to make it possible to generate independent sequences on different parallel processors.
这些生成器通常用 C 实现,但 Fortran 实现 also exist(subroutine rng_jump
是跳转函数,免责声明:link 转到我的存储库,不保证质量)。