SINGA-165 Add cross-platform timer API to singa Update cmake files and Timer class.
TODO, make some dependent libs (e.g., glog and protobuf) as external cmake projects, which can be downloaded and compiled automatically. Ref to http://www.kaizou.org/2014/11/gtest-cmake/ Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/bdbffdc8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/bdbffdc8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/bdbffdc8 Branch: refs/heads/dev Commit: bdbffdc88a590bdb4a7b963dddcc003e27443dfc Parents: 193e9df Author: wangwei <[email protected]> Authored: Sat May 7 00:13:55 2016 +0800 Committer: Wei Wang <[email protected]> Committed: Sat May 7 17:02:08 2016 +0800 ---------------------------------------------------------------------- CMakeLists.txt | 16 ++++++++-------- include/singa/utils/timer.h | 39 ++++++++++++++++++++++++++++++--------- src/CMakeLists.txt | 14 ++++++++++++++ test/CMakeLists.txt | 8 ++++++++ test/gtest/CMakeLists.txt | 0 test/singa/CMakeLists.txt | 4 ---- test/singa/test_timer.cc | 20 ++++++++------------ 7 files changed, 68 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/bdbffdc8/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/CMakeLists.txt b/CMakeLists.txt index 1daebd5..c1cc4f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,12 @@ -PROJECT(timer) CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -MESSAGE(STATUS "flags: " ${CMAKE_CXX_FLAGS}) -MESSAGE(STATUS "paths: " ${CMAKE_CXX_INCLUDE_PATH}) +PROJECT(singa) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -SET(CMAKE_CXX_INCLUDE_PATH "${CMAKE_CXX_INCLUDE_PATH} /home/zhongle/singav1/singav1/include/") MESSAGE(STATUS "flags: " ${CMAKE_CXX_FLAGS}) MESSAGE(STATUS "paths: " ${CMAKE_CXX_INCLUDE_PATH}) -ADD_SUBDIRECTORY(test/singa/ bin) -ADD_SUBDIRECTORY(include/singa/utils/ include/singa/utils/) -AUX_SOURCE_DIRECTORY( . DIR_SRCS ) -include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include /home/zhongle/singav1/singav1/include/ ) +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include) + +SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) +SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) +LINK_DIRECTORIES(${LIBRARY_OUTPUT_PATH}) +ADD_SUBDIRECTORY(test) +ADD_SUBDIRECTORY(src) http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/bdbffdc8/include/singa/utils/timer.h ---------------------------------------------------------------------- diff --git a/include/singa/utils/timer.h b/include/singa/utils/timer.h index dcbabc2..a54829d 100644 --- a/include/singa/utils/timer.h +++ b/include/singa/utils/timer.h @@ -3,15 +3,36 @@ #include <chrono> -namespace singa{ +namespace singa { - class Timer{ - public: - double elapsed() const - { - return std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); - } - }; -} +/// For benchmarking the time cost of operations. +class Timer { + public: + typedef std::chrono::duration<int> Seconds; + typedef std::chrono::duration<int, std::milli> Milliseconds; + typedef std::chrono::duration<int, std::ratio<60 * 60>> Hours; + + /// Init the internal time point to the current time + Timer() { Tick(); } + /// Reset the internal time point to the current time + void Tick() { last_ = std::chrono::high_resolution_clock::now(); } + /// Return the duration since last call to Tick() or since the creation of + /// Timer. The template arg must be from Second or Millisecond or Hour. + /// The returned value is the count of the time metric. + template <typename T> + int Elapsed() const { + static_assert(std::is_same<T, Seconds>::value || + std::is_same<T, Milliseconds>::value || + std::is_same<T, Hours>::value, + "Template arg must be Seconds | Milliseconds | Hours"); + auto now = std::chrono::high_resolution_clock::now(); + return std::chrono::duration_cast<T>(now - last_).count(); + } + /// Return the string rep of current wall time + // std::string CurrentTime(); + private: + std::chrono::high_resolution_clock::time_point last_; +}; +} #endif http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/bdbffdc8/src/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..7e5adba --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,14 @@ +PROJECT(singa) +#AUX_SOURCE_DIRECTORY(core CORE_SOURCE) +#AUX_SOURCE_DIRECTORY(layer LAYER_SOURCE) +#AUX_SOURCE_DIRECTORY(model MODEL_SOURCE) +AUX_SOURCE_DIRECTORY(utils UTILS_SOURCE) + +#ADD_LIBRARY(singa_core SHARED ${CORE_SOURCE}) +#ADD_LIBRARY(singa_layer SHARED ${LAYER_SOURCE}) +#ADD_LIBRARY(singa_model SHARED ${MODEL_SOURCE}) +ADD_LIBRARY(singa_utils SHARED ${UTILS_SOURCE}) + +#TARGET_LINK_LIBRARIES(singa_core singa_utils) +#TARGET_LINK_LIBRARIES(singa_layer singa_core singa_utils) +#TARGET_LINK_LIBRARIES(singa_model singa_layer singa_core singa_utils) http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/bdbffdc8/test/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..f3e16c0 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,8 @@ +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) +ADD_LIBRARY(gtest STATIC EXCLUDE_FROM_ALL "gtest/gtest.h" "gtest/gtest-all.cc") + +AUX_SOURCE_DIRECTORY(singa singa_test_source) + +ADD_EXECUTABLE(test_singa "gtest/gtest_main.cc" ${singa_test_source}) +TARGET_LINK_LIBRARIES(test_singa gtest) +SET_TARGET_PROPERTIES(test_singa PROPERTIES LINK_FLAGS "${LINK_FLAGS} -pthread") http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/bdbffdc8/test/gtest/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt new file mode 100644 index 0000000..e69de29 http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/bdbffdc8/test/singa/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/test/singa/CMakeLists.txt b/test/singa/CMakeLists.txt deleted file mode 100644 index c6f7e9c..0000000 --- a/test/singa/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -PROJECT(timer) -ADD_EXECUTABLE(timer test_timer.cc) -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) - http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/bdbffdc8/test/singa/test_timer.cc ---------------------------------------------------------------------- diff --git a/test/singa/test_timer.cc b/test/singa/test_timer.cc index c08d08d..fa6c9af 100644 --- a/test/singa/test_timer.cc +++ b/test/singa/test_timer.cc @@ -1,16 +1,12 @@ +#include "gtest/gtest.h" #include "singa/utils/timer.h" -#include <iostream> -#include <unistd.h> -int main(){ - singa::Timer t; - double t1 = t.elapsed(); - std::cout << "t1 = " << t1 << std::endl; - sleep(1); - double t2 = t.elapsed(); - std::cout << "t2 = " << t2 << std::endl; - std::cout << "delta = " << t2 - t1 << " ms" << std::endl; - - return 0; +#include <chrono> +#include <thread> +TEST(TimerTest, TestTick) { + singa::Timer t; + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + int time = t.Elapsed<singa::Timer::Milliseconds>(); + EXPECT_GE(time, 1000); }
