FastDeploy  latest
Fast & Easy to Deploy!
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
benchmark.h
1 // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 #include "fastdeploy/core/config.h"
16 #include "fastdeploy/utils/utils.h"
17 #include "fastdeploy/utils/perf.h"
18 #include "fastdeploy/benchmark/option.h"
19 #include "fastdeploy/benchmark/results.h"
20 
21 #ifdef ENABLE_BENCHMARK
22  #define __RUNTIME_PROFILE_LOOP_BEGIN(option, base_loop) \
23  int __p_loop = (base_loop); \
24  const bool __p_enable_profile = option.enable_profile; \
25  const bool __p_include_h2d_d2h = option.include_h2d_d2h; \
26  const int __p_repeats = option.repeats; \
27  const int __p_warmup = option.warmup; \
28  if (__p_enable_profile && (!__p_include_h2d_d2h)) { \
29  __p_loop = (__p_repeats) + (__p_warmup); \
30  FDINFO << option << std::endl; \
31  } \
32  TimeCounter __p_tc; \
33  bool __p_tc_start = false; \
34  for (int __p_i = 0; __p_i < __p_loop; ++__p_i) { \
35  if (__p_i >= (__p_warmup) && (!__p_tc_start)) { \
36  __p_tc.Start(); \
37  __p_tc_start = true; \
38  } \
39 
40  #define __RUNTIME_PROFILE_LOOP_END(result) \
41  } \
42  if ((__p_enable_profile && (!__p_include_h2d_d2h))) { \
43  if (__p_tc_start) { \
44  __p_tc.End(); \
45  double __p_tc_duration = __p_tc.Duration(); \
46  result.time_of_runtime = \
47  __p_tc_duration / static_cast<double>(__p_repeats); \
48  } \
49  }
50 
51  #define __RUNTIME_PROFILE_LOOP_H2D_D2H_BEGIN(option, base_loop) \
52  int __p_loop_h = (base_loop); \
53  const bool __p_enable_profile_h = option.enable_profile; \
54  const bool __p_include_h2d_d2h_h = option.include_h2d_d2h; \
55  const int __p_repeats_h = option.repeats; \
56  const int __p_warmup_h = option.warmup; \
57  if (__p_enable_profile_h && __p_include_h2d_d2h_h) { \
58  __p_loop_h = (__p_repeats_h) + (__p_warmup_h); \
59  FDINFO << option << std::endl; \
60  } \
61  TimeCounter __p_tc_h; \
62  bool __p_tc_start_h = false; \
63  for (int __p_i_h = 0; __p_i_h < __p_loop_h; ++__p_i_h) { \
64  if (__p_i_h >= (__p_warmup_h) && (!__p_tc_start_h)) { \
65  __p_tc_h.Start(); \
66  __p_tc_start_h = true; \
67  } \
68 
69  #define __RUNTIME_PROFILE_LOOP_H2D_D2H_END(result) \
70  } \
71  if ((__p_enable_profile_h && __p_include_h2d_d2h_h)) { \
72  if (__p_tc_start_h) { \
73  __p_tc_h.End(); \
74  double __p_tc_duration_h = __p_tc_h.Duration(); \
75  result.time_of_runtime = \
76  __p_tc_duration_h / static_cast<double>(__p_repeats_h); \
77  } \
78  }
79 #else
80  #define __RUNTIME_PROFILE_LOOP_BEGIN(option, base_loop) \
81  for (int __p_i = 0; __p_i < (base_loop); ++__p_i) {
82  #define __RUNTIME_PROFILE_LOOP_END(result) }
83  #define __RUNTIME_PROFILE_LOOP_H2D_D2H_BEGIN(option, base_loop) \
84  for (int __p_i_h = 0; __p_i_h < (base_loop); ++__p_i_h) {
85  #define __RUNTIME_PROFILE_LOOP_H2D_D2H_END(result) }
86 #endif