summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKai Li <kaili_kloud@163.com>2014-02-20 19:42:57 +0800
committerKai Li <kaili_kloud@163.com>2014-03-04 10:02:00 +0800
commit263ceea2e7fa6614c7bd4b700771e11907925fba (patch)
treeb0814cf5555173f1cd14727b8598a8ef848c55a4 /src
parent8e580fd1e9a09d87754aa15dfd1968fe8df3d7fd (diff)
downloadcaffeonacl-263ceea2e7fa6614c7bd4b700771e11907925fba.tar.gz
caffeonacl-263ceea2e7fa6614c7bd4b700771e11907925fba.tar.bz2
caffeonacl-263ceea2e7fa6614c7bd4b700771e11907925fba.zip
Add Timer to wrap CPU clock_t and GPU cudaEvent_t based timing
Diffstat (limited to 'src')
-rw-r--r--src/caffe/util/benchmark.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/caffe/util/benchmark.cpp b/src/caffe/util/benchmark.cpp
new file mode 100644
index 00000000..19a036d9
--- /dev/null
+++ b/src/caffe/util/benchmark.cpp
@@ -0,0 +1,53 @@
+// Copyright 2014 kloud@github
+
+#include <ctime>
+#include <cuda_runtime.h>
+
+#include "caffe/common.hpp"
+#include "caffe/util/benchmark.hpp"
+
+namespace caffe {
+
+Timer::Timer() {
+ if (Caffe::mode() == Caffe::GPU) {
+ cudaEventCreate (&start_gpu_);
+ cudaEventCreate (&stop_gpu_);
+ }
+}
+
+Timer::~Timer() {
+ if (Caffe::mode() == Caffe::GPU) {
+ cudaEventDestroy (start_gpu_);
+ cudaEventDestroy (stop_gpu_);
+ }
+}
+
+void Timer::Start() {
+ if (Caffe::mode() == Caffe::GPU) {
+ cudaEventRecord(start_gpu_, 0);
+ } else {
+ start_cpu_ = clock();
+ }
+}
+
+void Timer::Stop() {
+ if (Caffe::mode() == Caffe::GPU) {
+ cudaEventRecord(stop_gpu_, 0);
+ } else {
+ stop_cpu_ = clock();
+ }
+}
+
+float Timer::ElapsedSeconds() {
+ float elapsed;
+ if (Caffe::mode() == Caffe::GPU) {
+ cudaEventSynchronize(stop_gpu_);
+ cudaEventElapsedTime(&elapsed, start_gpu_, stop_gpu_);
+ elapsed /= 1000.;
+ } else {
+ elapsed = float(stop_cpu_ - start_cpu_) / CLOCKS_PER_SEC;
+ }
+ return elapsed;
+}
+
+} // namespace caffe