• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow 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 ==============================================================================*/
15 
16 // Simple benchmarking facility.
17 #ifndef TENSORFLOW_CORE_PLATFORM_TEST_BENCHMARK_H_
18 #define TENSORFLOW_CORE_PLATFORM_TEST_BENCHMARK_H_
19 
20 #include <utility>
21 #include <vector>
22 #include "tensorflow/core/platform/macros.h"
23 #include "tensorflow/core/platform/platform.h"
24 #include "tensorflow/core/platform/types.h"
25 
26 #if defined(PLATFORM_GOOGLE)
27 #include "tensorflow/core/platform/google/build_config/benchmark.h"
28 
29 #else
30 #define BENCHMARK(n)                                            \
31   static ::tensorflow::testing::Benchmark* TF_BENCHMARK_CONCAT( \
32       __benchmark_, n, __LINE__) TF_ATTRIBUTE_UNUSED =          \
33       (new ::tensorflow::testing::Benchmark(#n, (n)))
34 #define TF_BENCHMARK_CONCAT(a, b, c) TF_BENCHMARK_CONCAT2(a, b, c)
35 #define TF_BENCHMARK_CONCAT2(a, b, c) a##b##c
36 
37 #endif  // PLATFORM_GOOGLE
38 
39 namespace tensorflow {
40 namespace testing {
41 
42 #if defined(PLATFORM_GOOGLE)
43 
44 using ::testing::Benchmark;
45 using ::testing::DoNotOptimize;
46 
47 #else
48 
49 // The DoNotOptimize(...) function can be used to prevent a value or
50 // expression from being optimized away by the compiler. This function is
51 // intended to add little to no overhead.
52 // See: http://stackoverflow.com/questions/28287064
53 //
54 // The specific guarantees of DoNotOptimize(x) are:
55 //  1) x, and any data it transitively points to, will exist (in a register or
56 //     in memory) at the current point in the program.
57 //  2) The optimizer will assume that DoNotOptimize(x) could mutate x or
58 //     anything it transitively points to (although it actually doesn't).
59 //
60 // To see this in action:
61 //
62 //   void BM_multiply(benchmark::State& state) {
63 //     int a = 2;
64 //     int b = 4;
65 //     for (auto _ : state) {
66 //       testing::DoNotOptimize(a);
67 //       testing::DoNotOptimize(b);
68 //       int c = a * b;
69 //       testing::DoNotOptimize(c);
70 //     }
71 //   }
72 //   BENCHMARK(BM_multiply);
73 //
74 // Guarantee (2) applied to 'a' and 'b' prevents the compiler lifting the
75 // multiplication outside of the loop. Guarantee (1) applied to 'c' prevents the
76 // compiler from optimizing away 'c' as dead code.
77 template <class T>
78 void DoNotOptimize(const T& var) {
79   asm volatile("" : "+m"(const_cast<T&>(var)));
80 }
81 
82 class Benchmark {
83  public:
84   Benchmark(const char* name, void (*fn)(int));
85   Benchmark(const char* name, void (*fn)(int, int));
86   Benchmark(const char* name, void (*fn)(int, int, int));
87 
88   Benchmark* Arg(int x);
89   Benchmark* ArgPair(int x, int y);
90   Benchmark* Range(int lo, int hi);
91   Benchmark* RangePair(int lo1, int hi1, int lo2, int hi2);
92   static void Run(const char* pattern);
93 
94  private:
95   string name_;
96   int num_args_;
97   std::vector<std::pair<int, int> > args_;
98   void (*fn0_)(int) = nullptr;
99   void (*fn1_)(int, int) = nullptr;
100   void (*fn2_)(int, int, int) = nullptr;
101 
102   void Register();
103   void Run(int arg1, int arg2, int* run_count, double* run_seconds);
104 };
105 #endif
106 
107 void RunBenchmarks();
108 void SetLabel(const std::string& label);
109 void BytesProcessed(int64);
110 void ItemsProcessed(int64);
111 void StartTiming();
112 void StopTiming();
113 void UseRealTime();
114 
115 }  // namespace testing
116 }  // namespace tensorflow
117 
118 #endif  // TENSORFLOW_CORE_PLATFORM_TEST_BENCHMARK_H_
119