Commit 85265a9c authored by Kenneth Moreland's avatar Kenneth Moreland

Add const correctness to Timer

It should be possible to query a vtkm::cont::Timer without modifying it.
As such, its query functions (such as Stopped and GetElapsedTime) should
be const.
parent 46550899
......@@ -573,11 +573,11 @@ public:
this->StopReady = true;
}
VTKM_CONT bool Started() { return this->StartReady; }
VTKM_CONT bool Started() const { return this->StartReady; }
VTKM_CONT bool Stopped() { return this->StopReady; }
VTKM_CONT bool Stopped() const { return this->StopReady; }
VTKM_CONT bool Ready() { return true; }
VTKM_CONT bool Ready() const { return true; }
/// Returns the elapsed time in seconds between the construction of this
/// class or the last call to Reset and the time this function is called. The
......@@ -585,7 +585,7 @@ public:
/// number of times to get the progressive time. This method synchronizes all
/// asynchronous operations.
///
VTKM_CONT vtkm::Float64 GetElapsedTime()
VTKM_CONT vtkm::Float64 GetElapsedTime() const
{
assert(this->StartReady);
if (!this->StartReady)
......@@ -595,23 +595,18 @@ public:
return 0;
}
bool manualStop = true;
if (!this->StopReady)
{
manualStop = false;
this->Stop();
}
TimeStamp startTime = this->StartTime;
TimeStamp stopTime = this->StopReady ? this->StopTime : this->GetCurrentTime();
vtkm::Float64 elapsedTime;
elapsedTime = vtkm::Float64(this->StopTime.Seconds - this->StartTime.Seconds);
elapsedTime += (vtkm::Float64(this->StopTime.Microseconds - this->StartTime.Microseconds) /
vtkm::Float64(1000000));
// Reset StopReady flag to its original state
this->StopReady = manualStop;
elapsedTime = vtkm::Float64(stopTime.Seconds - startTime.Seconds);
elapsedTime +=
(vtkm::Float64(stopTime.Microseconds - startTime.Microseconds) / vtkm::Float64(1000000));
return elapsedTime;
}
VTKM_CONT TimeStamp GetCurrentTime()
VTKM_CONT TimeStamp GetCurrentTime() const
{
vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapterTag>::Synchronize();
......
This diff is collapsed.
......@@ -26,15 +26,16 @@
#include <vtkm/cont/vtkm_cont_export.h>
#include <memory>
namespace vtkm
{
namespace cont
{
namespace detail
{
struct TimerFunctor;
}
class EnabledDeviceTimerImpls;
}
/// A class that can be used to time operations in VTK-m that might be occuring
/// in parallel. Users are recommended to provide a device adapter at construction
......@@ -53,8 +54,6 @@ class EnabledDeviceTimerImpls;
///
class VTKM_CONT_EXPORT Timer
{
friend struct detail::TimerFunctor;
public:
VTKM_CONT
Timer();
......@@ -74,17 +73,22 @@ public:
VTKM_CONT void Stop();
VTKM_CONT bool Started();
VTKM_CONT bool Started() const;
VTKM_CONT bool Stopped();
VTKM_CONT bool Stopped() const;
/// Used to check if Timer has finished the synchronization to get the result from the device.
VTKM_CONT bool Ready();
VTKM_CONT bool Ready() const;
/// Get the elapsed time measured by the given device adapter. If no device is
/// specified, the max time of all device measurements will be returned.
VTKM_CONT
vtkm::Float64 GetElapsedTime(DeviceAdapterId id = DeviceAdapterTagAny());
vtkm::Float64 GetElapsedTime(
vtkm::cont::DeviceAdapterId id = vtkm::cont::DeviceAdapterTagAny()) const;
/// Returns the device for which this timer is synchronized. If the device adapter has the same
/// id as DeviceAdapterTagAny, then the timer will synchronize all devices.
VTKM_CONT vtkm::cont::DeviceAdapterId GetDevice() const { return this->Device; }
private:
VTKM_CONT void Init();
......@@ -93,8 +97,7 @@ private:
VTKM_CONT void operator=(const Timer&) = delete;
DeviceAdapterId Device;
DeviceAdapterId DeviceForQuery;
EnabledDeviceTimerImpls* Internal;
std::unique_ptr<detail::EnabledDeviceTimerImpls> Internal;
};
}
} // namespace vtkm::cont
......
......@@ -67,12 +67,12 @@ void DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Stop()
this->StopReady = true;
}
bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Started()
bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Started() const
{
return this->StartReady;
}
bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Stopped()
bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Stopped() const
{
return this->StopReady;
}
......@@ -80,7 +80,7 @@ bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Stopped
// Callbacks without a mandated order(in independent streams) execute in undefined
// order and maybe serialized. So Instead CudaEventQuery is used here.
// Ref link: https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__STREAM.html
bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Ready()
bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Ready() const
{
if (cudaEventQuery(this->StopEvent) == cudaSuccess)
{
......@@ -91,6 +91,7 @@ bool DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::Ready()
vtkm::Float64 DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>::GetElapsedTime()
const
{
assert(this->StartReady);
if (!this->StartReady)
......@@ -99,18 +100,16 @@ vtkm::Float64 DeviceAdapterTimerImplementation<vtkm::cont::DeviceAdapterTagCuda>
"Start() function should be called first then trying to call GetElapsedTime().");
return 0;
}
bool manualStop = true;
if (!this->StopReady)
{
manualStop = false;
this->Stop();
// Stop was not called, so we have to insert a new event into the stream
VTKM_CUDA_CALL(cudaEventRecord(this->StopEvent, cudaStreamPerThread));
}
VTKM_CUDA_CALL(cudaEventSynchronize(this->StopEvent));
float elapsedTimeMilliseconds;
VTKM_CUDA_CALL(cudaEventElapsedTime(&elapsedTimeMilliseconds, this->StartEvent, this->StopEvent));
// Reset Stop flag to its original state
this->StopReady = manualStop;
return static_cast<vtkm::Float64>(0.001f * elapsedTimeMilliseconds);
}
}
......
......@@ -54,13 +54,13 @@ public:
VTKM_CONT void Stop();
VTKM_CONT bool Started();
VTKM_CONT bool Started() const;
VTKM_CONT bool Stopped();
VTKM_CONT bool Stopped() const;
VTKM_CONT bool Ready();
VTKM_CONT bool Ready() const;
VTKM_CONT vtkm::Float64 GetElapsedTime();
VTKM_CONT vtkm::Float64 GetElapsedTime() const;
private:
// Copying CUDA events is problematic.
......
......@@ -339,7 +339,6 @@ public:
VTKM_CONT void Reset()
{
vtkm::cont::DeviceAdapterAlgorithm<vtkm::cont::DeviceAdapterTagTBB>::Synchronize();
this->StartReady = false;
this->StopReady = false;
}
......@@ -347,23 +346,23 @@ public:
VTKM_CONT void Start()
{
this->Reset();
this->StartTime = ::tbb::tick_count::now();
this->StartTime = this->GetCurrentTime();
this->StartReady = true;
}
VTKM_CONT void Stop()
{
this->StopTime = ::tbb::tick_count::now();
this->StopTime = this->GetCurrentTime();
this->StopReady = true;
}
VTKM_CONT bool Started() { return this->StartReady; }
VTKM_CONT bool Started() const { return this->StartReady; }
VTKM_CONT bool Stopped() { return this->StopReady; }
VTKM_CONT bool Stopped() const { return this->StopReady; }
VTKM_CONT bool Ready() { return true; }
VTKM_CONT bool Ready() const { return true; }
VTKM_CONT vtkm::Float64 GetElapsedTime()
VTKM_CONT vtkm::Float64 GetElapsedTime() const
{
assert(this->StartReady);
if (!this->StartReady)
......@@ -373,21 +372,21 @@ public:
" GetElapsedTime().");
return 0;
}
bool manualStop = true;
if (!this->StopReady)
{
manualStop = false;
this->Stop();
}
vtkm::cont::DeviceAdapterAlgorithm<vtkm::cont::DeviceAdapterTagTBB>::Synchronize();
::tbb::tick_count::interval_t elapsedTime = this->StopTime - this->StartTime;
// Reset StopReady flag to its original state
this->StopReady = manualStop;
::tbb::tick_count startTime = this->StartTime;
::tbb::tick_count stopTime = this->StopReady ? this->StopTime : this->GetCurrentTime();
::tbb::tick_count::interval_t elapsedTime = stopTime - startTime;
return static_cast<vtkm::Float64>(elapsedTime.seconds());
}
VTKM_CONT::tbb::tick_count GetCurrentTime() const
{
vtkm::cont::DeviceAdapterAlgorithm<vtkm::cont::DeviceAdapterTagTBB>::Synchronize();
return ::tbb::tick_count::now();
}
private:
bool StartReady;
bool StopReady;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment