From f76733711b38547b83782bc6e7c25afae700734a Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 11 Apr 2019 11:00:28 +0200 Subject: [PATCH] ENH: reorganize cpuTime into C++ and POSIX versions (#1238) --- src/OSspecific/POSIX/Make/files | 2 +- src/OSspecific/POSIX/cpuTime.H | 40 +++++++ .../cpuTime/{cpuTime.C => cpuTimePosix.C} | 19 +-- .../cpuTime/{cpuTime.H => cpuTimePosix.H} | 21 ++-- src/OSspecific/POSIX/cpuTimeFwd.H | 49 ++++++++ src/OpenFOAM/Make/files | 1 + src/OpenFOAM/global/cpuTime/cpuTimeCxx.C | 81 +++++++++++++ src/OpenFOAM/global/cpuTime/cpuTimeCxx.H | 111 ++++++++++++++++++ 8 files changed, 302 insertions(+), 22 deletions(-) create mode 100644 src/OSspecific/POSIX/cpuTime.H rename src/OSspecific/POSIX/cpuTime/{cpuTime.C => cpuTimePosix.C} (82%) rename src/OSspecific/POSIX/cpuTime/{cpuTime.H => cpuTimePosix.H} (89%) create mode 100644 src/OSspecific/POSIX/cpuTimeFwd.H create mode 100644 src/OpenFOAM/global/cpuTime/cpuTimeCxx.C create mode 100644 src/OpenFOAM/global/cpuTime/cpuTimeCxx.H diff --git a/src/OSspecific/POSIX/Make/files b/src/OSspecific/POSIX/Make/files index 7ce5e03c60..44ba1ec1bf 100644 --- a/src/OSspecific/POSIX/Make/files +++ b/src/OSspecific/POSIX/Make/files @@ -1,5 +1,5 @@ cpuInfo/cpuInfo.C -cpuTime/cpuTime.C +cpuTime/cpuTimePosix.C memInfo/memInfo.C signals/sigFpe.C diff --git a/src/OSspecific/POSIX/cpuTime.H b/src/OSspecific/POSIX/cpuTime.H new file mode 100644 index 0000000000..cc74eb60c9 --- /dev/null +++ b/src/OSspecific/POSIX/cpuTime.H @@ -0,0 +1,40 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Typedef + Foam::cpuTime + +Description + Selection of preferred clock mechanism for the elapsed cpu time. + +\*---------------------------------------------------------------------------*/ + +#ifndef cpuTime_H +#define cpuTime_H + +#include "cpuTimePosix.H" +#include "cpuTimeFwd.H" + +#endif + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.C b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.C similarity index 82% rename from src/OSspecific/POSIX/cpuTime/cpuTime.C rename to src/OSspecific/POSIX/cpuTime/cpuTimePosix.C index 8c7e5ef2d1..a215e5e4d6 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.C +++ b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.C @@ -25,17 +25,18 @@ License \*---------------------------------------------------------------------------*/ -#include "cpuTime.H" +#include "cpuTimePosix.H" #include // * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * // -const long Foam::cpuTime::clockTicks_(sysconf(_SC_CLK_TCK)); +// Clock-ticks per second +static const long clockTicks_(sysconf(_SC_CLK_TCK)); // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -double Foam::cpuTime::diff(const value_type& a, const value_type& b) +inline double Foam::cpuTimePosix::diff(const value_type& a, const value_type& b) { return ( @@ -47,13 +48,13 @@ double Foam::cpuTime::diff(const value_type& a, const value_type& b) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::cpuTime::value_type::value_type() +Foam::cpuTimePosix::value_type::value_type() { update(); } -Foam::cpuTime::cpuTime() +Foam::cpuTimePosix::cpuTimePosix() : start_(), last_(start_) @@ -62,27 +63,27 @@ Foam::cpuTime::cpuTime() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::cpuTime::value_type::update() +void Foam::cpuTimePosix::value_type::update() { ::times(this); } -void Foam::cpuTime::resetCpuTime() +void Foam::cpuTimePosix::resetCpuTime() { last_.update(); start_ = last_; } -double Foam::cpuTime::elapsedCpuTime() const +double Foam::cpuTimePosix::elapsedCpuTime() const { last_.update(); return diff(last_, start_); } -double Foam::cpuTime::cpuTimeIncrement() const +double Foam::cpuTimePosix::cpuTimeIncrement() const { const value_type prev(last_); last_.update(); diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.H b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.H similarity index 89% rename from src/OSspecific/POSIX/cpuTime/cpuTime.H rename to src/OSspecific/POSIX/cpuTime/cpuTimePosix.H index d4be85e7f0..1fe0413ba2 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.H +++ b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.H @@ -24,7 +24,7 @@ License along with OpenFOAM. If not, see . Class - Foam::cpuTime + Foam::cpuTimePosix Description Starts timing CPU usage and return elapsed time from start. @@ -33,12 +33,12 @@ See also clockTime SourceFiles - cpuTime.C + cpuTimePosix.C \*---------------------------------------------------------------------------*/ -#ifndef cpuTime_H -#define cpuTime_H +#ifndef cpuTimePosix_H +#define cpuTimePosix_H #include #include @@ -50,10 +50,10 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class cpuTime Declaration + Class cpuTimePosix Declaration \*---------------------------------------------------------------------------*/ -class cpuTime +class cpuTimePosix { //- Time structure used, with additional methods struct value_type : tms @@ -74,14 +74,11 @@ class cpuTime //- Last time when elapsedTime or timeIncrement was called mutable value_type last_; - //- Clock-ticks per second - static const long clockTicks_; - // Private Member Functions - //- Difference between two times (a - b) - static double diff(const value_type& a, const value_type& b); + //- Difference between two times (a - b) + inline static double diff(const value_type& a, const value_type& b); public: @@ -89,7 +86,7 @@ public: // Constructors //- Construct with the current clock time - cpuTime(); + cpuTimePosix(); // Member Functions diff --git a/src/OSspecific/POSIX/cpuTimeFwd.H b/src/OSspecific/POSIX/cpuTimeFwd.H new file mode 100644 index 0000000000..60acf4e842 --- /dev/null +++ b/src/OSspecific/POSIX/cpuTimeFwd.H @@ -0,0 +1,49 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Typedef + Foam::cpuTime + +Description + Selection of preferred clock mechanism for the elapsed cpu time. + +\*---------------------------------------------------------------------------*/ + +#ifndef cpuTimeFwd_H +#define cpuTimeFwd_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + class cpuTimeCxx; + class cpuTimePosix; + + typedef cpuTimePosix cpuTime; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 5a35b70613..8d53c6653a 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -6,6 +6,7 @@ global/argList/argListHelp.C global/clock/clock.C global/clockTime/clockTime.C global/clockValue/clockValue.C +global/cpuTime/cpuTimeCxx.C global/profiling/profiling.C global/profiling/profilingInformation.C global/profiling/profilingSysInfo.C diff --git a/src/OpenFOAM/global/cpuTime/cpuTimeCxx.C b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.C new file mode 100644 index 0000000000..11d28f07d9 --- /dev/null +++ b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.C @@ -0,0 +1,81 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "cpuTimeCxx.H" + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +inline double Foam::cpuTimeCxx::diff(const value_type& a, const value_type& b) +{ + return std::difftime(a.value, b.value) / CLOCKS_PER_SEC; +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::cpuTimeCxx::value_type::value_type() +{ + update(); +} + + +Foam::cpuTimeCxx::cpuTimeCxx() +: + start_(), + last_(start_) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void Foam::cpuTimeCxx::value_type::update() +{ + value = std::clock(); +} + + +void Foam::cpuTimeCxx::resetCpuTime() +{ + last_.update(); + start_ = last_; +} + + +double Foam::cpuTimeCxx::elapsedCpuTime() const +{ + last_.update(); + return diff(last_, start_); +} + + +double Foam::cpuTimeCxx::cpuTimeIncrement() const +{ + const value_type prev(last_); + last_.update(); + return diff(last_, prev); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/global/cpuTime/cpuTimeCxx.H b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.H new file mode 100644 index 0000000000..746141013d --- /dev/null +++ b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.H @@ -0,0 +1,111 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::cpuTimeCxx + +Description + Starts timing CPU usage and return elapsed time from start. + +See also + clockTime + +SourceFiles + cpuTimeCxx.C + +\*---------------------------------------------------------------------------*/ + +#ifndef cpuTimeCxx_H +#define cpuTimeCxx_H + +#include + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class cpuTimeCxx Declaration +\*---------------------------------------------------------------------------*/ + +class cpuTimeCxx +{ + //- Time structure used, with additional methods + struct value_type + { + std::clock_t value; + + //- Construct with the current clock time + value_type(); + + //- Update with the current clock time + void update(); + }; + + + // Private Data + + //- Start time, at the time of construction + value_type start_; + + //- Last time when elapsedTime or timeIncrement was called + mutable value_type last_; + + + // Private Member Functions + + //- Difference between two times (a - b) + static inline double diff(const value_type& a, const value_type& b); + + +public: + + // Constructors + + //- Construct with the current clock time + cpuTimeCxx(); + + + // Member Functions + + //- Reset to use the current time for the start time + void resetCpuTime(); + + //- Return CPU time (in seconds) from the start + double elapsedCpuTime() const; + + //- Return CPU time (in seconds) since last call to cpuTimeIncrement() + double cpuTimeIncrement() const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* //