[flang-commits] [flang] [flang][runtime] Better non-repeatable RANDOM_INIT() (PR #67363)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Oct 16 10:20:56 PDT 2023


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/67363

>From ba441ce8f74d39b03cc1bfac0b46634c9babceed Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 25 Sep 2023 12:55:10 -0700
Subject: [PATCH] [flang][runtime] Better non-repeatable RANDOM_INIT()

Use a higher-frequency clock base when initializing the
pseudo-random number generator to implement
  CALL  RANDOM_INIT(REPEATABLE=.FALSE.)
---
 flang/runtime/random.cpp | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/flang/runtime/random.cpp b/flang/runtime/random.cpp
index b7175d6b63c35f8..8b00cfd1cac1950 100644
--- a/flang/runtime/random.cpp
+++ b/flang/runtime/random.cpp
@@ -20,10 +20,10 @@
 #include <algorithm>
 #include <cmath>
 #include <cstdint>
-#include <ctime>
 #include <limits>
 #include <memory>
 #include <random>
+#include <time.h>
 
 namespace Fortran::runtime {
 
@@ -100,7 +100,13 @@ void RTNAME(RandomInit)(bool repeatable, bool /*image_distinct*/) {
     if (repeatable) {
       generator.seed(0);
     } else {
-      generator.seed(std::time(nullptr));
+#ifdef CLOCK_REALTIME
+      timespec ts;
+      clock_gettime(CLOCK_REALTIME, &ts);
+      generator.seed(ts.tv_sec & ts.tv_nsec);
+#else
+      generator.seed(time(nullptr));
+#endif
     }
   }
 }



More information about the flang-commits mailing list