perf.py 5.6 KB

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