Blame view

3rdparty/boost_1_81_0/libs/compute/perf/perf.py 5.6 KB
e6ccf0ce   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  #!/usr/bin/python
  
  # Copyright (c) 2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  # Distributed under the Boost Software License, Version 1.0
  # See accompanying file LICENSE_1_0.txt or copy at
  # http://www.boost.org/LICENSE_1_0.txt
  #
  # See http://boostorg.github.com/compute for more information.
  
  # driver script for boost.compute benchmarking. will run a
  # benchmark for a given function (e.g. accumulate, sort).
  
  import os
  import sys
  import subprocess
  
  try:
      import pylab
  except:
      print('pylab not found, no ploting...')
      pass
  
  def run_perf_process(name, size, backend = ""):
      if not backend:
          proc = "perf_%s" % name
      else:
          proc = "perf_%s_%s" % (backend, name)
  
      filename = "./perf/" + proc
  
      if not os.path.isfile(filename):
          print("Error: failed to find ", filename, " for running")
          return 0
      try:
          output = subprocess.check_output([filename, str(int(size))])
      except:
          return 0
  
      t = 0
      for line in output.decode('utf8').split("\n"):
          if line.startswith("time:"):
              t = float(line.split(":")[1].split()[0])
  
      return t
  
  class Report:
      def __init__(self, name):
          self.name = name
          self.samples = {}
  
      def add_sample(self, name, size, time):
          if not name in self.samples:
              self.samples[name] = []
  
          self.samples[name].append((size, time))
  
      def display(self):
          for name in self.samples.keys():
              print('=== %s with %s ===' % (self.name, name))
              print('size,time (ms)')
  
              for sample in self.samples[name]:
                  print('%d,%f' % sample)
  
      def plot_time(self, name):
          if not name in self.samples:
              return
  
          x = []
          y = []
  
          any_valid_samples = False
  
          for sample in self.samples[name]:
              if sample[1] == 0:
                  continue
  
              x.append(sample[0])
              y.append(sample[1])
              any_valid_samples = True
  
          if not any_valid_samples:
              return
  
          pylab.loglog(x, y, marker='o', label=name)
          pylab.xlabel("Size")
          pylab.ylabel("Time (ms)")
          pylab.title(self.name)
  
      def plot_rate(self, name):
          if not name in self.samples:
              return
  
          x = []
          y = []
  
          any_valid_samples = False
  
          for sample in self.samples[name]:
              if sample[1] == 0:
                  continue
  
              x.append(sample[0])
              y.append(float(sample[0]) / (float(sample[1]) * 1e-3))
              any_valid_samples = True
  
          if not any_valid_samples:
              return
  
          pylab.loglog(x, y, marker='o', label=name)
          pylab.xlabel("Size")
          pylab.ylabel("Rate (values/s)")
          pylab.title(self.name)
  
  def run_benchmark(name, sizes, vs=[]):
      report = Report(name)
  
      for size in sizes:
          time = run_perf_process(name, size)
  
          report.add_sample("compute", size, time)
  
      competitors = {
          "thrust" : [
              "accumulate",
              "count",
              "exclusive_scan",
              "find",
              "inner_product",
              "merge",
              "partial_sum",
              "partition",
              "reduce_by_key",
              "reverse",
              "reverse_copy",
              "rotate",
              "saxpy",
              "sort",
              "unique"
          ],
          "bolt" : [
              "accumulate",
              "count",
              "exclusive_scan",
              "fill",
              "inner_product",
              "max_element",
              "merge",
              "partial_sum",
              "reduce_by_key",
              "saxpy",
              "sort"
          ],
          "tbb": [
              "accumulate",
              "merge",
              "sort"
          ],
          "stl": [
              "accumulate",
              "count",
              "find",
              "find_end",
              "includes",
              "inner_product",
              "is_permutation",
              "max_element",
              "merge",
              "next_permutation",
              "nth_element",
              "partial_sum",
              "partition",
              "partition_point",
              "prev_permutation",
              "reverse",
              "reverse_copy",
              "rotate",
              "rotate_copy",
              "saxpy",
              "search",
              "search_n",
              "set_difference",
              "set_intersection",
              "set_symmetric_difference",
              "set_union",
              "sort",
              "stable_partition",
              "unique",
              "unique_copy"
          ]
      }
  
      for other in vs:
          if not other in competitors:
              continue
          if not name in competitors[other]:
              continue
  
          for size in sizes:
              time = run_perf_process(name, size, other)
              report.add_sample(other, size, time)
  
      return report
  
  if __name__ == '__main__':
      test = "sort"
      if len(sys.argv) >= 2:
          test = sys.argv[1]
      print('running %s perf test' % test)
  
      sizes = [ pow(2, x) for x in range(1, 26) ]
  
      sizes = sorted(sizes)
  
      competitors = ["bolt", "tbb", "thrust", "stl"]
  
      report = run_benchmark(test, sizes, competitors)
  
      plot = None
      if "--plot-time" in sys.argv:
          plot = "time"
      elif "--plot-rate" in sys.argv:
          plot = "rate"
  
      if plot == "time":
          report.plot_time("compute")
          for competitor in competitors:
              report.plot_time(competitor)
      elif plot == "rate":
          report.plot_rate("compute")
          for competitor in competitors:
              report.plot_rate(competitor)
  
      if plot:
          pylab.legend(loc='upper left')
          pylab.show()
      else:
          report.display()