From 4485afc9283237d45eb079719063f4496da2acab Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Mon, 28 Aug 2023 14:31:12 -0700 Subject: [PATCH] UniqueString tweak Previously UniqueString used MPI_Wtime, which could have a very low resolution (1e-5s) on some machines. In this PR, we switch to use amrex::second(), which uses the highest resolution steady clock (usually 1.e-9s). We have also changed a hardwired number according to resolution of the clock so that the digits are not wasted on trailing zeros. --- Src/Base/AMReX_Utility.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Src/Base/AMReX_Utility.cpp b/Src/Base/AMReX_Utility.cpp index b9cc90df5ae..11da03e844f 100644 --- a/Src/Base/AMReX_Utility.cpp +++ b/Src/Base/AMReX_Utility.cpp @@ -183,10 +183,17 @@ amrex::FileExists(const std::string &filename) std::string amrex::UniqueString() { - std::stringstream tempstring; - tempstring << std::setprecision(11) << std::fixed << ParallelDescriptor::second(); - auto const tsl = tempstring.str().length(); - return(tempstring.str().substr(tsl/2, tsl)); + constexpr int len = 7; + static const auto n = std::max + (len, + static_cast( + std::round(std::log10(double(MaxResSteadyClock::period::den) + /double(MaxResSteadyClock::period::num))))); + std::stringstream tempstring; + tempstring << std::setprecision(n) << std::fixed << amrex::second()+100.0; + auto const ts = tempstring.str(); + auto const tsl = ts.length(); + return ts.substr(tsl-len,tsl); // tsl-len >= 0 } void