You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.h 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * \file dnn/test/common/timer.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #pragma once
  12. #include "test/common/utils.h"
  13. #include <chrono>
  14. namespace megdnn {
  15. namespace test {
  16. class Timer {
  17. private:
  18. using clock = std::chrono::high_resolution_clock;
  19. using time_point = clock::time_point;
  20. public:
  21. Timer() { reset(); }
  22. void reset() {
  23. m_started = false;
  24. m_stopped = false;
  25. }
  26. void start() {
  27. megdnn_assert(!m_started);
  28. megdnn_assert(!m_stopped);
  29. m_started = true;
  30. m_start_point = clock::now();
  31. }
  32. void stop() {
  33. megdnn_assert(m_started);
  34. megdnn_assert(!m_stopped);
  35. m_stopped = true;
  36. m_stop_point = clock::now();
  37. }
  38. size_t get_time_in_us() const {
  39. return std::chrono::duration_cast<std::chrono::microseconds>(
  40. m_stop_point - m_start_point)
  41. .count();
  42. }
  43. private:
  44. bool m_started, m_stopped;
  45. time_point m_start_point, m_stop_point;
  46. };
  47. class Timer2 {
  48. std::chrono::high_resolution_clock::time_point m_start;
  49. public:
  50. Timer2() { reset(); }
  51. void reset() { m_start = std::chrono::high_resolution_clock::now(); }
  52. double get_secs() const {
  53. auto now = std::chrono::high_resolution_clock::now();
  54. return std::chrono::duration_cast<std::chrono::nanoseconds>(now -
  55. m_start)
  56. .count() *
  57. 1e-9;
  58. }
  59. double get_msecs() const { return get_secs() * 1e3; }
  60. double get_secs_reset() {
  61. auto ret = get_secs();
  62. reset();
  63. return ret;
  64. }
  65. double get_msecs_reset() { return get_secs_reset() * 1e3; }
  66. };
  67. } // namespace test
  68. } // namespace megdnn
  69. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台