[flang-commits] [flang] [flang][runtime] Better non-repeatable RANDOM_INIT() (PR #67363)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Mon Sep 25 13:18:45 PDT 2023
https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/67363
>From 850aedb355a4d4320f3d96203eb62004f469e2c7 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 1/2] [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 | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/flang/runtime/random.cpp b/flang/runtime/random.cpp
index b7175d6b63c35f8..93113657bdecbca 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,9 @@ void RTNAME(RandomInit)(bool repeatable, bool /*image_distinct*/) {
if (repeatable) {
generator.seed(0);
} else {
- generator.seed(std::time(nullptr));
+ timespec ts;
+ clock_gettime(CLOCK_REALTIME, &ts);
+ generator.seed(ts.tv_sec & ts.tv_nsec);
}
}
}
>From 7b600203f3f76eab3b6b20620e5df7e75e4f7f22 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 25 Sep 2023 13:18:35 -0700
Subject: [PATCH 2/2] patch for Windows
---
flang/runtime/random.cpp | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/flang/runtime/random.cpp b/flang/runtime/random.cpp
index 93113657bdecbca..8b00cfd1cac1950 100644
--- a/flang/runtime/random.cpp
+++ b/flang/runtime/random.cpp
@@ -100,9 +100,13 @@ void RTNAME(RandomInit)(bool repeatable, bool /*image_distinct*/) {
if (repeatable) {
generator.seed(0);
} else {
+#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