OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
profiler.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_UTILITIES_PROFILER_HPP
2 #define OPENPOSE_UTILITIES_PROFILER_HPP
3 
4 #include <chrono>
5 #include <string>
8 
9 namespace op
10 {
11  // The following functions provides basic functions to measure time. Usage example:
12  // const auto timerInit = getTimerInit();
13  // // [Some code in here]
14  // const auto timeSeconds = getTimeSeconds(timerInit);
15  // const printTime(timeSeconds, "Function X took ", " seconds.");
16  OP_API std::chrono::time_point<std::chrono::high_resolution_clock> getTimerInit();
17 
18  OP_API double getTimeSeconds(const std::chrono::time_point<std::chrono::high_resolution_clock>& timerInit);
19 
21  const std::chrono::time_point<std::chrono::high_resolution_clock>& timerInit, const std::string& firstMessage,
22  const std::string& secondMessage, const Priority priority);
23 
24  // The following functions will run REPS times and average the final time in seconds. Usage example:
25  // const auto REPS = 1000;
26  // double time = 0.;
27  // OP_PROFILE_INIT(REPS);
28  // // [Some code in here]
29  // OP_PROFILE_END(time, 1e3, REPS); // Time in msec. 1 = sec, 1e3 = msec, 1e6 = usec, 1e9 = nsec, etc.
30  // opLog("Function X took " + std::to_string(time) + " milliseconds.");
31  #define OP_PROFILE_INIT(REPS) \
32  { \
33  const auto timerInit = getTimerInit(); \
34  for (auto rep = 0 ; rep < (REPS) ; ++rep) \
35  {
36  #define OP_PROFILE_END(finalTime, factor, REPS) \
37  } \
38  (finalTime) = (factor)/(float)(REPS)*getTimeSeconds(timerInit); \
39  }
40 
41  // The following functions will run REPS times, wait for the kernels to finish, and then average the final time
42  // in seconds. Usage example:
43  // const auto REPS = 1000;
44  // double time = 0.;
45  // OP_CUDA_PROFILE_INIT(REPS);
46  // // [Some code with CUDA calls in here]
47  // OP_CUDA_PROFILE_END(time, 1e3, REPS); // Time in msec. 1 = sec, 1e3 = msec, 1e6 = usec, 1e9 = nsec, etc.
48  // opLog("Function X took " + std::to_string(time) + " milliseconds.");
49  // Analogous to OP_PROFILE_INIT, but also waits for CUDA kernels to finish their asynchronous operations
50  // It requires: #include <cuda_runtime.h>
51  #define OP_CUDA_PROFILE_INIT(REPS) \
52  { \
53  cudaDeviceSynchronize(); \
54  const auto timerInit = getTimerInit(); \
55  for (auto rep = 0 ; rep < (REPS) ; ++rep) \
56  {
57  // Analogous to OP_PROFILE_END, but also waits for CUDA kernels to finish their asynchronous operations
58  // It requires: #include <cuda_runtime.h>
59  #define OP_CUDA_PROFILE_END(finalTime, factor, REPS) \
60  } \
61  cudaDeviceSynchronize(); \
62  (finalTime) = (factor)/(float)(REPS)*getTimeSeconds(timerInit); \
63  cudaCheck(__LINE__, __FUNCTION__, __FILE__); \
64  }
65 
66  // Enable PROFILER_ENABLED on Makefile.config or CMake in order to use this function. Otherwise nothing will be outputted.
67  // How to use - example:
68  // For GPU - It can only be applied in the main.cpp file:
69  // Profiler::profileGpuMemory(__LINE__, __FUNCTION__, __FILE__);
70  // For time:
71  // // ... inside continuous loop ...
72  // const auto profilerKey = Profiler::timerInit(__LINE__, __FUNCTION__, __FILE__);
73  // // functions to do...
74  // Profiler::timerEnd(profilerKey);
75  // Profiler::printAveragedTimeMsOnIterationX(profilerKey, __LINE__, __FUNCTION__, __FILE__, NUMBER_ITERATIONS);
77  {
78  public:
79  static unsigned long long DEFAULT_X;
80 
81  // Non-thread safe, it must be performed at the beginning of the code before any parallelization occurs
82  static void setDefaultX(const unsigned long long defaultX);
83 
84  static const std::string timerInit(const int line, const std::string& function, const std::string& file);
85 
86  static void timerEnd(const std::string& key);
87 
89  const std::string& key, const int line, const std::string& function, const std::string& file,
90  const unsigned long long x = DEFAULT_X);
91 
93  const std::string& key, const int line, const std::string& function, const std::string& file,
94  const unsigned long long x = DEFAULT_X);
95 
96  static void profileGpuMemory(const int line, const std::string& function, const std::string& file);
97  };
98 }
99 
100 #endif // OPENPOSE_UTILITIES_PROFILER_HPP
static void printAveragedTimeMsEveryXIterations(const std::string &key, const int line, const std::string &function, const std::string &file, const unsigned long long x=DEFAULT_X)
static unsigned long long DEFAULT_X
Definition: profiler.hpp:79
static void setDefaultX(const unsigned long long defaultX)
static void printAveragedTimeMsOnIterationX(const std::string &key, const int line, const std::string &function, const std::string &file, const unsigned long long x=DEFAULT_X)
static void profileGpuMemory(const int line, const std::string &function, const std::string &file)
static const std::string timerInit(const int line, const std::string &function, const std::string &file)
static void timerEnd(const std::string &key)
#define OP_API
Definition: macros.hpp:18
OP_API double getTimeSeconds(const std::chrono::time_point< std::chrono::high_resolution_clock > &timerInit)
OP_API void printTime(const std::chrono::time_point< std::chrono::high_resolution_clock > &timerInit, const std::string &firstMessage, const std::string &secondMessage, const Priority priority)
Priority
Definition: enumClasses.hpp:22
OP_API std::chrono::time_point< std::chrono::high_resolution_clock > getTimerInit()