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

#2154: Explore use of std::chrono for TimeType #2170

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/vt/elm/elm_lb_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void ElementLBData::stop(TimeType time) {
auto const started = cur_time_started_;
if (started) {
cur_time_started_ = false;
addTime(total_time.seconds());
addTime(total_time.seconds().count());
}

vt_debug_print(
Expand Down
2 changes: 1 addition & 1 deletion src/vt/elm/elm_lb_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct ElementLBData {

protected:
bool cur_time_started_ = false;
TimeType cur_time_ = TimeType{0.0};
TimeType cur_time_ = TimeType{};
PhaseType cur_phase_ = fst_lb_phase;
std::unordered_map<PhaseType, LoadType> phase_timings_ = {};
std::unordered_map<PhaseType, CommMapType> phase_comm_ = {};
Expand Down
2 changes: 1 addition & 1 deletion src/vt/event/event_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct EventRecord {

# if vt_check_enabled(diagnostics)
/// the time this event record was created
TimeType creation_time_stamp_ = TimeType{0.};
TimeType creation_time_stamp_ = TimeType{};
# endif
};

Expand Down
8 changes: 4 additions & 4 deletions src/vt/messaging/active.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ EventType ActiveMessenger::sendMsgMPI(
{
VT_ALLOW_MPI_CALLS;
#if vt_check_enabled(trace_enabled)
auto tr_begin = TimeType{0.};
auto tr_begin = TimeType{};
if (theConfig()->vt_trace_mpi) {
tr_begin = vt::timing::getCurrentTime();
}
Expand Down Expand Up @@ -581,7 +581,7 @@ std::tuple<EventType, int> ActiveMessenger::sendDataMPI(
);
{
#if vt_check_enabled(trace_enabled)
auto tr_begin = TimeType{0.};
auto tr_begin = TimeType{};
if (theConfig()->vt_trace_mpi) {
tr_begin = vt::timing::getCurrentTime();
}
Expand Down Expand Up @@ -771,7 +771,7 @@ void ActiveMessenger::recvDataDirect(
);

#if vt_check_enabled(trace_enabled)
auto tr_begin = TimeType{0.};
auto tr_begin = TimeType{};
if (theConfig()->vt_trace_mpi) {
tr_begin = vt::timing::getCurrentTime();
}
Expand Down Expand Up @@ -1008,7 +1008,7 @@ bool ActiveMessenger::tryProcessIncomingActiveMsg() {

{
#if vt_check_enabled(trace_enabled)
auto tr_begin = TimeType{0.};
auto tr_begin = TimeType{};
if (theConfig()->vt_trace_mpi) {
tr_begin = vt::timing::getCurrentTime();
}
Expand Down
2 changes: 1 addition & 1 deletion src/vt/messaging/request_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct RequestHolder {
bool testAll(Callable c, int& num_mpi_tests) {
# if vt_check_enabled(trace_enabled)
std::size_t const holder_size_start = holder_.size();
auto tr_begin = TimeType{0.0};
auto tr_begin = TimeType{};
if (theConfig()->vt_trace_irecv_polling) {
tr_begin = vt::timing::getCurrentTime();
}
Expand Down
4 changes: 2 additions & 2 deletions src/vt/runtime/component/diagnostic.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ meter::Timer<T> Diagnostic::registerTimerT(
) {
auto sum = registerDiagnostic<T>(
key + " [sum]", desc, DiagnosticUpdate::Sum, unit,
DiagnosticTypeEnum::PerformanceDiagnostic, T{0}
DiagnosticTypeEnum::PerformanceDiagnostic, T{}
);
auto min = registerDiagnostic<T>(
key + " [min]", desc, DiagnosticUpdate::Min, unit,
Expand All @@ -105,7 +105,7 @@ meter::Timer<T> Diagnostic::registerTimerT(
);
auto avg = registerDiagnostic<T>(
key + " [avg]", desc, DiagnosticUpdate::Avg, unit,
DiagnosticTypeEnum::PerformanceDiagnostic, T{0}
DiagnosticTypeEnum::PerformanceDiagnostic, T{}
);
return meter::Timer<T>{sum, avg, max, min};
}
Expand Down
11 changes: 8 additions & 3 deletions src/vt/runtime/component/diagnostic_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct DiagnosticValueWrapper {
*
* \return the max value
*/
T max() const { return N_ == 0 ? T{0} : max_; }
T max() const { return N_ == 0 ? T{} : max_; }

/**
* \internal \brief Get sum of values (use after reduction)
Expand All @@ -182,7 +182,7 @@ struct DiagnosticValueWrapper {
*
* \return the min value
*/
T min() const { return N_ == 0 ? T{0} : min_; }
T min() const { return N_ == 0 ? T{} : min_; }

/**
* \internal \brief Get the arithmetic mean value (use after reduction)
Expand Down Expand Up @@ -237,7 +237,12 @@ struct DiagnosticValueWrapper {
*/
T getComputedValue() const {
if (N_ > 0) {
return value_ / N_;
// silence nvcc warning
if constexpr (std::is_same_v<decltype(N_), T>) {
return value_ / N_;
} else {
return value_ / static_cast<T>(N_);
}
} else {
return value_;
}
Expand Down
6 changes: 3 additions & 3 deletions src/vt/runtime/component/meter/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ struct Timer : DiagnosticStatsPack<T> {
* \brief Stop the timer and record the interval
*/
void stop() {
if (start_time_ != TimeType{0.}) {
if (start_time_ != TimeType{}) {
update(start_time_, timing::getCurrentTime());
start_time_ = TimeType{0.};
start_time_ = TimeType{};
}
}

Expand All @@ -116,7 +116,7 @@ struct Timer : DiagnosticStatsPack<T> {
}

private:
T start_time_ = T{0};
T start_time_ = T{};
};

}}}} /* end namespace vt::runtime::component::meter */
Expand Down
4 changes: 2 additions & 2 deletions src/vt/scheduler/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ struct Scheduler : runtime::component::Component<Scheduler> {
*
* \param[in] current_time current time
*/
void runProgress(bool msg_only = false, TimeType current_time = TimeType{0.0} );
void runProgress(bool msg_only = false, TimeType current_time = TimeType{} );

/**
* \brief Runs the scheduler until a condition is met.
Expand Down Expand Up @@ -438,7 +438,7 @@ struct Scheduler : runtime::component::Component<Scheduler> {
EventTriggerContType event_triggers;
EventTriggerContType event_triggers_once;

TimeType last_progress_time_ = TimeType{0.0};
TimeType last_progress_time_ = TimeType{};
bool progress_time_enabled_ = false;
int32_t processed_after_last_progress_ = 0;

Expand Down
5 changes: 2 additions & 3 deletions src/vt/timetrigger/trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ struct Trigger {
* \return the next time this should be triggered
*/
TimeType nextTriggerTime() const {
return TimeType{
(last_trigger_time_.milliseconds() + period_.count()) / 1000.0};
return TimeType{last_trigger_time_.milliseconds() + period_};
}

/**
Expand Down Expand Up @@ -142,7 +141,7 @@ struct Trigger {
private:
std::chrono::milliseconds period_; /**< The trigger's period */
ActionType trigger_ = nullptr; /**< The action to trigger */
TimeType last_trigger_time_ = TimeType{0.}; /**< The last time it was triggered */
TimeType last_trigger_time_ = TimeType{}; /**< The last time it was triggered */
int id_ = -1; /**< The trigger's id */
};

Expand Down
2 changes: 1 addition & 1 deletion src/vt/timing/timing.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct formatter<::vt::TimeTypeWrapper> {
auto format(::vt::TimeTypeWrapper const& t, FormatContext& ctx) const {
return fmt::format_to(
ctx.out(), "{}",
to_engineering_string(t.seconds(), 5, eng_exponential, "s")
to_engineering_string(t.seconds().count(), 5, eng_exponential, "s")
);
}
};
Expand Down
124 changes: 49 additions & 75 deletions src/vt/timing/timing_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,42 @@
#if !defined INCLUDED_VT_TIMING_TIMING_TYPE_H
#define INCLUDED_VT_TIMING_TIMING_TYPE_H

#include <limits>
#include <chrono>
#include <algorithm>
#include <cmath>

namespace vt {

struct TimeTypeWrapper {
using TimeTypeInternal = double;
explicit constexpr TimeTypeWrapper(const TimeTypeInternal time = 0.0)
: time_(time) { }

explicit operator double() const { return time_; }
using Seconds = std::chrono::duration<TimeTypeInternal>;
using Milliseconds = std::chrono::duration<TimeTypeInternal, std::milli>;
using Microseconds = std::chrono::duration<TimeTypeInternal, std::micro>;

template <typename T>
explicit TimeTypeWrapper(
T time,
typename std::enable_if<
std::is_integral<T>::value || std::is_floating_point<T>::value>::type* =
nullptr)
: time_(Seconds(static_cast<TimeTypeInternal>(time))) { }

template <typename Rep = TimeTypeInternal>
explicit TimeTypeWrapper(
const std::chrono::duration<Rep, std::micro>& time =
std::chrono::duration<Rep, std::micro>(0))
: time_(std::chrono::duration_cast<Seconds>(time)) { }

template <typename Rep = TimeTypeInternal>
explicit TimeTypeWrapper(const std::chrono::duration<Rep, std::milli>& time)
: time_(std::chrono::duration_cast<Seconds>(time)) { }

template <typename Rep = TimeTypeInternal>
explicit TimeTypeWrapper(const Seconds& time)
: time_(std::chrono::duration_cast<Seconds>(time)) { }

explicit operator TimeTypeInternal() const { return time_.count(); }

TimeTypeWrapper& operator+=(const TimeTypeWrapper& other) {
time_ += other.time_;
Expand All @@ -67,14 +91,9 @@ struct TimeTypeWrapper {
return *this;
}

TimeTypeWrapper& operator*=(const double scalar) {
time_ *= scalar;
return *this;
}

TimeTypeWrapper& operator/=(const double scalar) {
time_ /= scalar;
return *this;
friend TimeTypeWrapper
operator/(const TimeTypeWrapper& lhs, const TimeTypeWrapper& rhs) {
return TimeTypeWrapper{lhs.time_ / rhs.time_};
}

friend TimeTypeWrapper
Expand All @@ -87,36 +106,6 @@ struct TimeTypeWrapper {
return TimeTypeWrapper(lhs.time_ - rhs.time_);
}

friend TimeTypeWrapper
operator*(const TimeTypeWrapper& lhs, const TimeTypeWrapper& rhs) {
return TimeTypeWrapper(lhs.time_ * rhs.time_);
}

friend TimeTypeWrapper
operator*(const TimeTypeWrapper& time, const double scalar) {
return TimeTypeWrapper(time.time_ * scalar);
}

friend TimeTypeWrapper
operator*(const double scalar, const TimeTypeWrapper& time) {
return TimeTypeWrapper(time.time_ * scalar);
}

friend TimeTypeWrapper
operator/(const double scalar, const TimeTypeWrapper& time) {
return TimeTypeWrapper(scalar / time.time_);
}

friend TimeTypeWrapper
operator/(const TimeTypeWrapper& time, const double scalar) {
return TimeTypeWrapper(time.time_ / scalar);
}

friend TimeTypeWrapper
operator/(const TimeTypeWrapper& lhs, const TimeTypeWrapper& rhs) {
return TimeTypeWrapper(lhs.time_ / rhs.time_);
}

friend bool
operator<(const TimeTypeWrapper& lhs, const TimeTypeWrapper& rhs) {
return lhs.time_ < rhs.time_;
Expand Down Expand Up @@ -147,51 +136,36 @@ struct TimeTypeWrapper {
return lhs.time_ != rhs.time_;
}

friend TimeTypeWrapper sqrt(const TimeTypeWrapper& time) {
return TimeTypeWrapper{std::sqrt(time.time_)};
template <typename Rep = TimeTypeInternal>
std::chrono::duration<Rep> seconds() const {
return std::chrono::duration_cast<std::chrono::duration<Rep>>(time_);
}

TimeTypeInternal seconds() const { return time_; }
TimeTypeInternal milliseconds() const { return time_ * 1000; }
TimeTypeInternal microseconds() const { return time_ * 1000000; }
template <typename Rep = TimeTypeInternal>
std::chrono::duration<Rep, std::milli> milliseconds() const {
return std::chrono::duration_cast<std::chrono::duration<Rep, std::milli>>(
time_);
}

template <typename Rep = TimeTypeInternal>
std::chrono::duration<Rep, std::micro> microseconds() const {
return std::chrono::duration_cast<std::chrono::duration<Rep, std::micro>>(
time_);
}

template <typename Serializer>
void serialize(Serializer& s) {
s | time_;
}

private:
TimeTypeInternal time_;
std::chrono::duration<TimeTypeInternal> time_;
};

static_assert(std::is_trivially_copyable_v<TimeTypeWrapper>, "TimeTypeWrapper must be trivially_copyable");

using TimeType = TimeTypeWrapper;

} /* end namespace vt */

namespace std {
template <>
class numeric_limits<vt::TimeTypeWrapper> {
using Type = typename vt::TimeTypeWrapper::TimeTypeInternal;

public:
static constexpr vt::TimeTypeWrapper max() noexcept {
return vt::TimeTypeWrapper(std::numeric_limits<Type>::max());
}

static constexpr vt::TimeTypeWrapper lowest() noexcept {
return vt::TimeTypeWrapper(std::numeric_limits<Type>::lowest());
}

inline vt::TimeTypeWrapper
min(const vt::TimeTypeWrapper& lhs, const vt::TimeTypeWrapper& rhs) {
return vt::TimeTypeWrapper(std::min(lhs.seconds(), rhs.seconds()));
}

inline vt::TimeTypeWrapper
max(const vt::TimeTypeWrapper& lhs, const vt::TimeTypeWrapper& rhs) {
return vt::TimeTypeWrapper(std::max(lhs.seconds(), rhs.seconds()));
}
};
} // namespace std

#endif /*INCLUDED_VT_TIMING_TIMING_TYPE_H*/
#endif // INCLUDED_VT_TIMING_TIMING_TYPE_H
4 changes: 2 additions & 2 deletions src/vt/trace/trace_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ struct TraceLite {
* \return time in microsecond as integer
*/
static inline TimeIntegerType timeToMicros(TimeType const time) {
return time.microseconds();
return time.microseconds().count();
}

/**
Expand Down Expand Up @@ -385,7 +385,7 @@ struct TraceLite {
TraceEventIDType cur_event_ = 1;
UserEventIDType flush_event_ = no_user_event_id;
bool enabled_ = true;
TimeType start_time_ = TimeType{0.0};
TimeType start_time_ = TimeType{};
std::string prog_name_ = "";
std::string trace_name_ = "";
std::string full_trace_name_ = "";
Expand Down
4 changes: 2 additions & 2 deletions src/vt/trace/trace_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,10 @@ struct Log final {
// Excluding sys/user-specific data, expected ~24 bytes

// Time of the event - all events need a time.
TimeType time = TimeType{0.0};
TimeType time = TimeType{};
// If a duration can be expressed in a single event.
// (Currently only for user-events.. could elim explicit end events.)
TimeType end_time = TimeType{0.0};
TimeType end_time = TimeType{};

TraceConstantsType type = TraceConstantsType::InvalidTraceType;
TraceEntryIDType ep = no_trace_entry_id;
Expand Down
Loading