D3DEnergetic / FIDASIM

A Neutral Beam and Fast-ion Diagnostic Modeling Suite
http://d3denergetic.github.io/FIDASIM/
Other
29 stars 19 forks source link

A compatability fix proposed for 2.0.0-pre to avoid /dev/urandom #199

Open zhubr opened 4 years ago

zhubr commented 4 years ago

Hello, I'd propose to (conditionally) replace direct use of /dev/urandom with RANDOM_INIT/RANDOM_NUMBER builtins at least in case of GNU fortran, because it is more standard and portable method and internally employs /dev/urandom if available, with some fallbacks and alternative OS-specific mechanisms of obtaining random values. Please note that libgfortran has been using /dev/urandom internally since SVN r239356 (2016-08-11), whereas RANDOM_INIT was introduced in 2018, therefore the patch proposed below has no way to silently pick up some ancient dumb impementation (It would then fail to compile instead).

--- src/utilities.f90.orig  Mon Jan 13 21:57:14 2020
+++ src/utilities.f90   Sun Jul 12 12:48:08 2020
@@ -214,9 +214,16 @@
     integer(Int32) :: seed
         !+ Seed value

+#ifdef __GFORTRAN__
+    real(8) :: temp_r
+    call random_init(.false., .true.)
+    call random_number(temp_r)
+    seed = FLOOR(2147483647*temp_r)
+#else
     open(89, file="/dev/urandom", access="stream", form="unformatted", action="read", status="old")
     read(89) seed
     close(89)
+#endif
     seed = abs(seed)

 end function rng_seed
lstagner commented 4 years ago

From what I have read support for random_init was introduced in gfortran 9. Because of this, at this time, we cannot implement this feature since most fusion clusters are hopelessly out of date. Your suggestion is a good one though and when gfortran 9 becomes common enough we will change it.

There has been a push for the use of Singularity containers in the future so this fix may happen sooner rather than later.