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 1.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * \file dnn/test/cuda/timer.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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 <cuda_profiler_api.h>
  13. #include <cuda_runtime_api.h>
  14. #include "test/common/utils.h"
  15. namespace megdnn {
  16. namespace test {
  17. class CUTimer {
  18. public:
  19. CUTimer(cudaStream_t& stream, cudaEvent_t& evt0, cudaEvent_t& evt1)
  20. : m_stream{stream}, m_evt0{evt0}, m_evt1{evt1} {
  21. reset();
  22. }
  23. void reset() {
  24. m_started = false;
  25. m_stopped = false;
  26. }
  27. void start() {
  28. megdnn_assert(!m_started);
  29. megdnn_assert(!m_stopped);
  30. m_started = true;
  31. cudaProfilerStart();
  32. cudaEventRecord(m_evt0, m_stream);
  33. }
  34. void stop() {
  35. megdnn_assert(m_started);
  36. megdnn_assert(!m_stopped);
  37. m_stopped = true;
  38. cudaEventRecord(m_evt1, m_stream);
  39. cudaProfilerStop();
  40. }
  41. size_t get_time_in_us() const {
  42. cudaStreamSynchronize(m_stream);
  43. float t = -1;
  44. cudaEventElapsedTime(&t, m_evt0, m_evt1);
  45. return static_cast<size_t>(t * 1e3);
  46. }
  47. private:
  48. bool m_started, m_stopped;
  49. size_t m_start_point, m_stop_point;
  50. cudaStream_t& m_stream;
  51. cudaEvent_t &m_evt0, &m_evt1;
  52. };
  53. } // namespace test
  54. } // namespace megdnn
  55. // vim: syntax=cpp.doxygen

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