Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
68a1a81
[flang-rt] Runtime implementation of extended intrinsic function SECN…
eugeneepshteyn Aug 4, 2025
716f82d
clang-format
eugeneepshteyn Aug 4, 2025
8bbe4eb
Fixed a typo
eugeneepshteyn Aug 4, 2025
5f671e7
Fixed init form
eugeneepshteyn Aug 4, 2025
9c53878
The failure code should be negative
eugeneepshteyn Aug 4, 2025
80b61ed
Introduce FAIL_TIME
eugeneepshteyn Aug 4, 2025
26b1265
Merge branch 'llvm:main' into secnds-runtime
eugeneepshteyn Aug 4, 2025
78a8d07
Code review: moved RTNAME(Secnds) to extensions.cpp. Added missing t…
eugeneepshteyn Aug 5, 2025
2f3a274
Attempt at using atomic operations in secnds_() to make it reentrant
eugeneepshteyn Aug 5, 2025
9940825
clang-format
eugeneepshteyn Aug 5, 2025
7268c8c
Fixed init
eugeneepshteyn Aug 5, 2025
812760f
Merge branch 'llvm:main' into secnds-runtime
eugeneepshteyn Aug 5, 2025
205818e
Reset startingPoint to 'uninitialized' on failure
eugeneepshteyn Aug 5, 2025
5945264
Changed currentStartingPoint initialization to use loop in all cases
eugeneepshteyn Aug 5, 2025
5cdb74d
clang-format
eugeneepshteyn Aug 5, 2025
599713e
Code review feedback
eugeneepshteyn Aug 5, 2025
4ae3cd3
clang-format
eugeneepshteyn Aug 5, 2025
fdd310a
Moved RTNAME(Secnds) declaration to extensions.h
eugeneepshteyn Aug 5, 2025
1cf3a46
Simplified the implementation based on Peter's suggestion
eugeneepshteyn Aug 6, 2025
aa3d3d4
clang-format
eugeneepshteyn Aug 6, 2025
0603b56
Merge branch 'llvm:main' into secnds-runtime
eugeneepshteyn Aug 6, 2025
fb75bee
Extracted common implementation into SecndsImpl(), which can then be …
eugeneepshteyn Aug 6, 2025
27ddc47
clang-format
eugeneepshteyn Aug 6, 2025
a61d035
Merge branch 'main' into secnds-runtime
eugeneepshteyn Aug 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions flang-rt/lib/runtime/extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "flang/Runtime/entry-names.h"
#include "flang/Runtime/io-api.h"
#include "flang/Runtime/iostat-consts.h"
#include <atomic>
#include <chrono>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -303,6 +304,75 @@ void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize,
// PERROR(STRING)
void RTNAME(Perror)(const char *str) { perror(str); }

// GNU extension function SECNDS(refTime)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready for review. I did some testing with multiple threads. Test program:

use omp_lib
external secnds
real sec_mid
real results(1000)
integer i, num_threads

results = 0.0
!$omp parallel do
do i=1, size(results)
  results(i) = secnds(0.0)
  num_threads = omp_get_num_threads()
end do
!$omp end parallel do

print *, "Number of threads in parallel region:", num_threads

sec_mid = minval(results)
print *, "Seconds from midnight: min:", sec_mid, ", max:", maxval(results) 

call sleep(2)

!$omp parallel do
do i=1, size(results)
  results(i) = secnds(sec_mid)
end do
!$omp end parallel do

print *, "Seconds from sec_mid: min:", minval(results), ", max:", maxval(results)
print *, "Seconds from midnight:", secnds(0.0)
end

Output:

$ ./a.out 
 Number of threads in parallel region: 256
 Seconds from midnight: min: 46489. , max: 46489.
 Seconds from sec_mid: min: 2. , max: 2.
 Seconds from midnight: 46491.

float FORTRAN_PROCEDURE_NAME(secnds)(float *refTime) {
constexpr float FAIL_SECNDS{-1.0f}; // Failure code for this function
// Failure code for time functions that return std::time_t
constexpr std::time_t FAIL_TIME{std::time_t{-1}};
constexpr std::time_t TIME_UNINITIALIZED{std::time_t{0}};
if (!refTime) {
return FAIL_SECNDS;
}
std::time_t now{std::time(nullptr)};
if (now == FAIL_TIME) {
return FAIL_SECNDS;
}
// In float result, we can only precisely store 2^24 seconds, which
// comes out to about 194 days. Thus, need to pick a starting point,
// which will allow us to keep the time diffs as precise as possible.
// Given the description of this function, midnight of the current
// day is the best starting point.
static std::atomic<std::time_t> startingPoint{TIME_UNINITIALIZED};
// "Acquire" will give us writes from other threads.
std::time_t localStartingPoint{startingPoint.load(std::memory_order_acquire)};
// Initialize startingPoint if we haven't initialized it yet or
// if we were passed 0.0f, which indicates to compute seconds from
// current day's midnight.
if (localStartingPoint == TIME_UNINITIALIZED || *refTime < 0.5f) {
// Compute midnight in the current timezone and try to initialize
// startingPoint with it. If there are any errors during computation,
// exit with error and hope that the other threads have better luck
// (or the user retries the call).
struct tm timeInfo;
#ifdef _WIN32
if (localtime_s(&timeInfo, &now)) {
#else
if (!localtime_r(&now, &timeInfo)) {
#endif
return FAIL_SECNDS;
}
// Back to midnight
timeInfo.tm_hour = 0;
timeInfo.tm_min = 0;
timeInfo.tm_sec = 0;
localStartingPoint = std::mktime(&timeInfo);
if (localStartingPoint == FAIL_TIME) {
return FAIL_SECNDS;
}
INTERNAL_CHECK(localStartingPoint > TIME_UNINITIALIZED);
// Attempt to atomically set startingPoint to localStartingPoint
std::time_t expected{TIME_UNINITIALIZED};
if (startingPoint.compare_exchange_strong(expected, localStartingPoint,
std::memory_order_acq_rel, // "Acquire and release" on success
std::memory_order_acquire)) { // "Acquire" on failure
// startingPoint was set to localStartingPoint
} else {
// startingPoint was already initialized and its value was loaded
// into `expected`. Discard our precomputed midnight value in favor
// of the one from startingPoint.
localStartingPoint = expected;
}
}
double diffStartingPoint{std::difftime(now, localStartingPoint)};
return static_cast<float>(diffStartingPoint) - *refTime;
}

float RTNAME(Secnds)(float *refTime, const char *sourceFile, int line) {
Terminator terminator{sourceFile, line};
RUNTIME_CHECK(terminator, refTime != nullptr);
return FORTRAN_PROCEDURE_NAME(secnds)(refTime);
}

// GNU extension function TIME()
std::int64_t RTNAME(time)() { return time(nullptr); }

Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Runtime/extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,9 @@ void RTNAME(Perror)(const char *str);
// MCLOCK -- returns accumulated time in ticks
int FORTRAN_PROCEDURE_NAME(mclock)();

// GNU extension subroutine SECNDS(refTime)
float FORTRAN_PROCEDURE_NAME(secnds)(float *refTime);
float RTNAME(Secnds)(float *refTime, const char *sourceFile, int line);

} // extern "C"
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_