• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MINDSPORE_PI_JIT_PERF_H
17 #define MINDSPORE_PI_JIT_PERF_H
18 
19 #include <memory>
20 #include <map>
21 #include <chrono>
22 #include <functional>
23 #include <limits>
24 
25 namespace mindspore {
26 namespace pijit {
27 
28 /// \brief performance statistics
29 class PerfStatistics {
30  public:
31   virtual double GetAverageDuration() = 0;
32   virtual double GetMaxDuration() = 0;
33   virtual double GetMinDuration() = 0;
34   virtual int GetTotalCount() = 0;
35 };
36 using PerfStatisticsPtr = std::shared_ptr<PerfStatistics>;
37 
38 /// \brief performance for optimized code
39 class OptPerf {
40  public:
41   enum PerfKind {
42     kPerfPyNative = 0,
43     kPerfGraph,
44     kPerfCount,
45   };
46   OptPerf();
47   virtual ~OptPerf() = default;
48   void AddDuration(double duration);
49   PerfStatisticsPtr GetStatistics();
50 
51  protected:
52   PerfStatisticsPtr stat_;
53 };
54 using OptPerfPtr = std::shared_ptr<OptPerf>;
55 
56 template <typename Ret, typename... Args>
CallFunction(OptPerfPtr perf,std::function<Ret (Args...)> func,Args...args)57 Ret CallFunction(OptPerfPtr perf, std::function<Ret(Args...)> func, Args... args) {
58   constexpr auto kMicroExecUnit = 1000000;
59   auto start_time = std::chrono::steady_clock::now();
60   Ret res = func(args...);
61   std::chrono::duration<double, std::ratio<1, kMicroExecUnit>> duration = std::chrono::steady_clock::now() - start_time;
62   perf->AddDuration(duration.count());
63   return res;
64 }
65 }  // namespace pijit
66 }  // namespace mindspore
67 
68 #endif  // MINDSPORE_PI_JIT_PERF_H
69