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

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

Merged
merged 1 commit into from
Oct 16, 2023

Conversation

klausler
Copy link
Contributor

Use a higher-frequency clock base when initializing the pseudo-random number generator to implement
CALL RANDOM_INIT(REPEATABLE=.FALSE.)

@klausler klausler requested a review from vdonaldson September 25, 2023 19:57
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Sep 25, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2023

@llvm/pr-subscribers-flang-runtime

Changes

Use a higher-frequency clock base when initializing the pseudo-random number generator to implement
CALL RANDOM_INIT(REPEATABLE=.FALSE.)


Full diff: https://github.com/llvm/llvm-project/pull/67363.diff

1 Files Affected:

  • (modified) flang/runtime/random.cpp (+4-2)
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);
     }
   }
 }

@vdonaldson
Copy link
Contributor

Ok, modulo the windows issue

@klausler klausler force-pushed the bug1380 branch 2 times, most recently from db4765e to 930c117 Compare September 29, 2023 21:18
Use a higher-frequency clock base when initializing the
pseudo-random number generator to implement
  CALL  RANDOM_INIT(REPEATABLE=.FALSE.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants