Fix template argument deduction for Timer, remove ThreadRegionTimer

This commit is contained in:
Matthias Hochsteger 2021-06-11 12:01:38 +02:00
parent 6f7543c7dc
commit 1de1a1800e

View File

@ -148,15 +148,25 @@ namespace ngcore
}; };
struct TNoTracing{ static constexpr bool do_tracing=false; };
struct TTracing{ static constexpr bool do_tracing=true; };
struct TNoTiming{ static constexpr bool do_timing=false; };
struct TTiming{ static constexpr bool do_timing=true; };
namespace detail { namespace detail {
struct NoTracing_t{};
struct NoTiming_t{}; template<typename T>
constexpr bool is_tracing_type_v = std::is_same_v<T, TNoTracing> || std::is_same_v<T, TTracing>;
template<typename T>
constexpr bool is_timing_type_v = std::is_same_v<T, TNoTiming> || std::is_same_v<T, TTiming>;
} }
static detail::NoTracing_t NoTracing; static TNoTracing NoTracing;
static detail::NoTiming_t NoTiming; static TNoTiming NoTiming;
template<bool DO_TRACING=true, bool DO_TIMING=true> template<typename TTracing=TTracing, typename TTiming=TTiming>
class Timer class Timer
{ {
int timernr; int timernr;
@ -165,74 +175,68 @@ namespace ngcore
return NgProfiler::CreateTimer (name); return NgProfiler::CreateTimer (name);
} }
public: public:
Timer (const std::string & name) : timernr(Init(name)) static constexpr bool do_tracing = TTracing::do_tracing;
{ } static constexpr bool do_timing = TTiming::do_timing;
template<typename= std::enable_if<!DO_TRACING>> Timer (const std::string & name) : timernr(Init(name)) { }
Timer( const std::string & name, detail::NoTracing_t ) : timernr(Init(name))
{ }
template<typename= std::enable_if<!DO_TIMING>> template<std::enable_if_t< detail::is_tracing_type_v<TTracing>, bool> = false>
Timer( const std::string & name, detail::NoTiming_t ) : timernr(Init(name)) Timer( const std::string & name, TTracing ) : timernr(Init(name)) { }
{ }
template<typename= std::enable_if<!DO_TRACING&&!DO_TIMING>> template<std::enable_if_t< detail::is_timing_type_v<TTiming>, bool> = false>
Timer( const std::string & name, detail::NoTracing_t, detail::NoTiming_t ) : timernr(Init(name)) Timer( const std::string & name, TTiming ) : timernr(Init(name)) { }
{ }
template<typename= std::enable_if<!DO_TRACING&&!DO_TIMING>> Timer( const std::string & name, TTracing, TTiming ) : timernr(Init(name)) { }
Timer( const std::string & name, detail::NoTiming_t, detail::NoTracing_t ) : timernr(Init(name))
{ }
void SetName (const std::string & name) void SetName (const std::string & name)
{ {
NgProfiler::SetName (timernr, name); NgProfiler::SetName (timernr, name);
} }
void Start () void Start () const
{ {
Start(TaskManager::GetThreadId()); Start(TaskManager::GetThreadId());
} }
void Stop () void Stop () const
{ {
Stop(TaskManager::GetThreadId()); Stop(TaskManager::GetThreadId());
} }
void Start (int tid) void Start (int tid) const
{ {
if(tid==0) if(tid==0)
{ {
if constexpr(DO_TIMING) if constexpr(do_timing)
NgProfiler::StartTimer (timernr); NgProfiler::StartTimer (timernr);
if constexpr(DO_TRACING) if constexpr(do_tracing)
if(trace) trace->StartTimer(timernr); if(trace) trace->StartTimer(timernr);
} }
else else
{ {
if constexpr(DO_TIMING) if constexpr(do_timing)
NgProfiler::StartThreadTimer(timernr, tid); NgProfiler::StartThreadTimer(timernr, tid);
if constexpr(DO_TRACING) if constexpr(do_tracing)
trace->StartTask (tid, timernr, PajeTrace::Task::ID_TIMER); if(trace) trace->StartTask (tid, timernr, PajeTrace::Task::ID_TIMER);
} }
} }
void Stop (int tid) void Stop (int tid) const
{ {
if(tid==0) if(tid==0)
{ {
if constexpr(DO_TIMING) if constexpr(do_timing)
NgProfiler::StopTimer (timernr); NgProfiler::StopTimer (timernr);
if constexpr(DO_TRACING) if constexpr(do_tracing)
if(trace) trace->StopTimer(timernr); if(trace) trace->StopTimer(timernr);
} }
else else
{ {
if constexpr(DO_TIMING) if constexpr(do_timing)
NgProfiler::StopThreadTimer(timernr, tid); NgProfiler::StopThreadTimer(timernr, tid);
if constexpr(DO_TRACING) if constexpr(do_tracing)
trace->StopTask (tid, timernr, PajeTrace::Task::ID_TIMER); if(trace) trace->StopTask (tid, timernr, PajeTrace::Task::ID_TIMER);
} }
} }
void AddFlops (double aflops) void AddFlops (double aflops)
{ {
if constexpr(DO_TIMING) if constexpr(do_timing)
NgProfiler::AddFlops (timernr, aflops); NgProfiler::AddFlops (timernr, aflops);
} }
@ -249,14 +253,14 @@ namespace ngcore
Timer object. Timer object.
Start / stop timer at constructor / destructor. Start / stop timer at constructor / destructor.
*/ */
template<bool DO_TRACING, bool DO_TIMING> template<typename TTimer>
class RegionTimer class RegionTimer
{ {
Timer<DO_TRACING, DO_TIMING> & timer; const TTimer & timer;
int tid; int tid;
public: public:
/// start timer /// start timer
RegionTimer (Timer<DO_TRACING, DO_TIMING> & atimer) : timer(atimer) RegionTimer (const TTimer & atimer) : timer(atimer)
{ {
tid = TaskManager::GetThreadId(); tid = TaskManager::GetThreadId();
timer.Start(tid); timer.Start(tid);
@ -272,25 +276,6 @@ namespace ngcore
void operator=(RegionTimer &&) = delete; void operator=(RegionTimer &&) = delete;
}; };
class ThreadRegionTimer
{
size_t nr;
size_t tid;
public:
/// start timer
ThreadRegionTimer (size_t _nr, size_t _tid) : nr(_nr), tid(_tid)
{ NgProfiler::StartThreadTimer(nr, tid); }
/// stop timer
~ThreadRegionTimer ()
{ NgProfiler::StopThreadTimer(nr, tid); }
ThreadRegionTimer() = delete;
ThreadRegionTimer(ThreadRegionTimer &&) = delete;
ThreadRegionTimer(const ThreadRegionTimer &) = delete;
void operator=(const ThreadRegionTimer &) = delete;
void operator=(ThreadRegionTimer &&) = delete;
};
class RegionTracer class RegionTracer
{ {
int nr; int nr;
@ -317,8 +302,8 @@ namespace ngcore
nr = region_id; nr = region_id;
} }
/// start trace with timer /// start trace with timer
template<bool DO_TRACING, bool DO_TIMING> template<typename TTimer>
RegionTracer (int athread_id, Timer<DO_TRACING, DO_TIMING> & timer, int additional_value = -1 ) RegionTracer (int athread_id, TTimer & timer, int additional_value = -1 )
: thread_id(athread_id) : thread_id(athread_id)
{ {
nr = timer; nr = timer;