forked from BVLC/caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request BVLC#136 from kloudkl/cuda_timing
Add Timer class unifying CPU and GPU timer and use it in net_speed_benchmark
- Loading branch information
Showing
4 changed files
with
314 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2014 kloud@github | ||
|
||
#ifndef CAFFE_UTIL_BENCHMARK_H_ | ||
#define CAFFE_UTIL_BENCHMARK_H_ | ||
|
||
#include <boost/date_time/posix_time/posix_time.hpp> | ||
#include <cuda_runtime.h> | ||
|
||
namespace caffe { | ||
|
||
class Timer { | ||
public: | ||
Timer(); | ||
virtual ~Timer(); | ||
void Start(); | ||
void Stop(); | ||
float MilliSeconds(); | ||
float Seconds(); | ||
|
||
inline bool initted() { return initted_; } | ||
inline bool running() { return running_; } | ||
inline bool has_run_at_least_once() { return has_run_at_least_once_; } | ||
|
||
protected: | ||
void Init(); | ||
|
||
bool initted_; | ||
bool running_; | ||
bool has_run_at_least_once_; | ||
cudaEvent_t start_gpu_; | ||
cudaEvent_t stop_gpu_; | ||
boost::posix_time::ptime start_cpu_; | ||
boost::posix_time::ptime stop_cpu_; | ||
float elapsed_milliseconds_; | ||
}; | ||
|
||
} // namespace caffe | ||
|
||
#endif // CAFFE_UTIL_BENCHMARK_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Copyright 2014 kloud@github | ||
|
||
#include <unistd.h> // for usleep | ||
#include <cuda_runtime.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "caffe/common.hpp" | ||
#include "caffe/util/benchmark.hpp" | ||
#include "caffe/test/test_caffe_main.hpp" | ||
|
||
namespace caffe { | ||
|
||
extern cudaDeviceProp CAFFE_TEST_CUDA_PROP; | ||
|
||
class BenchmarkTest : public ::testing::Test {}; | ||
|
||
TEST_F(BenchmarkTest, TestTimerConstructorCPU) { | ||
Caffe::set_mode(Caffe::CPU); | ||
Timer timer; | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerConstructorGPU) { | ||
Caffe::set_mode(Caffe::GPU); | ||
Timer timer; | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerStartCPU) { | ||
Caffe::set_mode(Caffe::CPU); | ||
Timer timer; | ||
timer.Start(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_TRUE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_TRUE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
timer.Stop(); | ||
timer.Start(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_TRUE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerStartGPU) { | ||
Caffe::set_mode(Caffe::GPU); | ||
Timer timer; | ||
timer.Start(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_TRUE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
timer.Stop(); | ||
timer.Start(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_TRUE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_TRUE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerStopCPU) { | ||
Caffe::set_mode(Caffe::CPU); | ||
Timer timer; | ||
timer.Stop(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
timer.Stop(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
timer.Stop(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerStopGPU) { | ||
Caffe::set_mode(Caffe::GPU); | ||
Timer timer; | ||
timer.Stop(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
timer.Stop(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
timer.Stop(); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerMilliSecondsCPU) { | ||
Caffe::set_mode(Caffe::CPU); | ||
Timer timer; | ||
CHECK_EQ(timer.MilliSeconds(), 0); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
usleep(300 * 1000); | ||
CHECK_GE(timer.MilliSeconds(), 299); | ||
CHECK_LE(timer.MilliSeconds(), 301); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerMilliSecondsGPU) { | ||
Caffe::set_mode(Caffe::GPU); | ||
Timer timer; | ||
CHECK_EQ(timer.MilliSeconds(), 0); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
usleep(300 * 1000); | ||
CHECK_GE(timer.MilliSeconds(), 299); | ||
CHECK_LE(timer.MilliSeconds(), 301); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerSecondsCPU) { | ||
Caffe::set_mode(Caffe::CPU); | ||
Timer timer; | ||
CHECK_EQ(timer.Seconds(), 0); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
usleep(300 * 1000); | ||
CHECK_GE(timer.Seconds(), 0.299); | ||
CHECK_LE(timer.Seconds(), 0.301); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
TEST_F(BenchmarkTest, TestTimerSecondsGPU) { | ||
Caffe::set_mode(Caffe::GPU); | ||
Timer timer; | ||
CHECK_EQ(timer.Seconds(), 0); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_FALSE(timer.has_run_at_least_once()); | ||
timer.Start(); | ||
usleep(300 * 1000); | ||
CHECK_GE(timer.Seconds(), 0.299); | ||
CHECK_LE(timer.Seconds(), 0.301); | ||
EXPECT_TRUE(timer.initted()); | ||
EXPECT_FALSE(timer.running()); | ||
EXPECT_TRUE(timer.has_run_at_least_once()); | ||
} | ||
|
||
} // namespace caffe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2014 kloud@github | ||
|
||
#include <boost/date_time/posix_time/posix_time.hpp> | ||
#include <cuda_runtime.h> | ||
|
||
#include "caffe/common.hpp" | ||
#include "caffe/util/benchmark.hpp" | ||
|
||
namespace caffe { | ||
|
||
Timer::Timer() | ||
: initted_(false), | ||
running_(false), | ||
has_run_at_least_once_(false) { | ||
Init(); | ||
} | ||
|
||
Timer::~Timer() { | ||
if (Caffe::mode() == Caffe::GPU) { | ||
CUDA_CHECK(cudaEventDestroy(start_gpu_)); | ||
CUDA_CHECK(cudaEventDestroy(stop_gpu_)); | ||
} | ||
} | ||
|
||
void Timer::Start() { | ||
if (!running()) { | ||
if (Caffe::mode() == Caffe::GPU) { | ||
CUDA_CHECK(cudaEventRecord(start_gpu_, 0)); | ||
} else { | ||
start_cpu_ = boost::posix_time::microsec_clock::local_time(); | ||
} | ||
running_ = true; | ||
has_run_at_least_once_ = true; | ||
} | ||
} | ||
|
||
void Timer::Stop() { | ||
if (running()) { | ||
if (Caffe::mode() == Caffe::GPU) { | ||
CUDA_CHECK(cudaEventRecord(stop_gpu_, 0)); | ||
CUDA_CHECK(cudaEventSynchronize(stop_gpu_)); | ||
} else { | ||
stop_cpu_ = boost::posix_time::microsec_clock::local_time(); | ||
} | ||
running_ = false; | ||
} | ||
} | ||
|
||
float Timer::MilliSeconds() { | ||
if (!has_run_at_least_once()) { | ||
LOG(WARNING) << "Timer has never been run before reading time."; | ||
return 0; | ||
} | ||
if (running()) { | ||
Stop(); | ||
} | ||
if (Caffe::mode() == Caffe::GPU) { | ||
CUDA_CHECK(cudaEventElapsedTime(&elapsed_milliseconds_, start_gpu_, | ||
stop_gpu_)); | ||
} else { | ||
elapsed_milliseconds_ = (stop_cpu_ - start_cpu_).total_milliseconds(); | ||
} | ||
return elapsed_milliseconds_; | ||
} | ||
|
||
float Timer::Seconds() { | ||
return MilliSeconds() / 1000.; | ||
} | ||
|
||
void Timer::Init() { | ||
if (!initted()) { | ||
if (Caffe::mode() == Caffe::GPU) { | ||
CUDA_CHECK(cudaEventCreate(&start_gpu_)); | ||
CUDA_CHECK(cudaEventCreate(&stop_gpu_)); | ||
} | ||
initted_ = true; | ||
} | ||
} | ||
|
||
} // namespace caffe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters