Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random_seed intrinsic errors for certain seed values #691

Closed
kiranchandramohan opened this issue Mar 22, 2019 · 0 comments · Fixed by #702
Closed

random_seed intrinsic errors for certain seed values #691

kiranchandramohan opened this issue Mar 22, 2019 · 0 comments · Fixed by #702

Comments

@kiranchandramohan
Copy link
Collaborator

kiranchandramohan commented Mar 22, 2019

We observed recently that the random_seed intrinsic function errors out when certain seed values are used. By looking at the relevant code it was found that only the least significant 23 bits of the seed is used and if it happens to be 0 then an Error is generated.

This restriction of using only the least significant 23 bits is not mentioned in the PGI compiler's documentation, nor is it mentioned in the Error produced. Browsing through the old PGI fortran documentation it was observed that the following statement was present till the 2011 release but dropped from the 2012 release. It specifically talks about using the least significant 23 bits. Also, there is a mention about integer sizes of some platforms which are probably not valid for Flang.
https://www.pgroup.com/doc/pgi11fortref.pdf
https://www.pgroup.com/doc/pgi12fortref.pdf
"The random number intrinsic generates a 46 bit lagged fibonacci pseudo-random sequence with a short lag of 5 and a long lag of 17. For a given seed, including the default seed, the sequence generated is independent of the platform and number of processors. Due to limitations of some platforms' default integer type, the seed vector is of size 34. Only the least significant 23 bits of each element of the seed array are used, thus a seed array returned or used is portable between platforms. For non-degenerate seed arrays, the period of this generator is (217 - 1) * 245. If all the odd elements of the seed array are even, the period will be shorter."

  1. I think this is a bug in Flang since the platforms on which Flang runs do not have an integer size restriction.
  2. Another important point to note is that the random_seed does not honour the i8 flag. The first reason is due to the fact that it uses only the least 23 bits. But also some intermediate values used like the array list[i][j] and vlo, vhi are of type "int" and not "__INT_T" in runtime/flang/rnum.c.

Reproducer and output observed are given below.

Reproducer

program rand
   integer :: my_seed_sz
   integer, allocatable :: my_seed_arr(:)
   real :: my_rand

   call random_seed(size=my_seed_sz)
   print *, my_seed_sz

   allocate(my_seed_arr(my_seed_sz))
   my_seed_arr = 0
   my_seed_arr(1) = Z'800000'

   call random_seed(put=my_seed_arr)
   call random_number(my_rand)
   print *, "Random = ", my_rand

   deallocate(my_seed_arr)
end program rand

Output

./a.out
           34
0: random_seed:  input seed must have at least one nonzero value
kiranchandramohan added a commit to kiranchandramohan/flang that referenced this issue Apr 5, 2019
This patch fixes setting random seeds when seeds are large
and has zeroes in their least significant 23 bits.
Fixes flang-compiler#691

Change-Id: If532da3159997a4ddbecd20f388a5e6280bc1012
kiranchandramohan added a commit to kiranchandramohan/flang that referenced this issue Apr 5, 2019
This patch fixes setting random seeds when seeds are large
and has zeroes in their least significant 23 bits.
Fixes flang-compiler#691

Change-Id: If532da3159997a4ddbecd20f388a5e6280bc1012
kiranchandramohan added a commit to kiranchandramohan/flang that referenced this issue Apr 5, 2019
This patch fixes setting random seeds when seeds are large
and has zeros in their least significant 23 bits.
Fixes flang-compiler#691

Change-Id: If532da3159997a4ddbecd20f388a5e6280bc1012
kiranchandramohan added a commit to kiranchandramohan/flang that referenced this issue Jun 14, 2022
This patch fixes setting random seeds when seeds are large
and has zeros in their least significant 23 bits.
Fixes flang-compiler#691

Change-Id: If532da3159997a4ddbecd20f388a5e6280bc1012
kiranchandramohan added a commit to kiranchandramohan/flang that referenced this issue Jun 15, 2022
This patch fixes setting random seeds when seeds are large
and has zeros in their least significant 23 bits.
Fixes flang-compiler#691

Change-Id: If532da3159997a4ddbecd20f388a5e6280bc1012
pawosm-arm pushed a commit that referenced this issue Jun 15, 2022
This patch fixes setting random seeds when seeds are large
and has zeros in their least significant 23 bits.
Fixes #691

Change-Id: If532da3159997a4ddbecd20f388a5e6280bc1012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant