plot.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import matplotlib.mlab as mlab
  4. import matplotlib.pyplot as plt
  5. import matplotlib.patches as mpatches
  6. import seaborn
  7. import os
  8. seaborn.set()
  9. # Helper function to attach labels to the bars
  10. def autolabel(subplot, bars):
  11. for bar in bars:
  12. height = round(bar.get_height(), 1)
  13. subplot.text(bar.get_x() + bar.get_width()/2., 1.05*height, '%s' % height, ha='center', va='bottom')
  14. # Helper function to strip away the "opencl/" prefix and other stuff from all the names
  15. def stripNames (category, names):
  16. stripped_names = []
  17. for elem in names:
  18. elem = elem.replace("opencl/", "")
  19. elem = elem.replace("/ocl", "")
  20. elem = elem.replace("/OpenCL", "")
  21. stripped_names.append(elem)
  22. return stripped_names
  23. # List containing all the platform names
  24. platforms = ['cpu', 'gpu-primary', 'gpu-secondary']
  25. # List containing the categories of the benchamrks
  26. categories = ['short', 'long']
  27. averageTotal = {}
  28. stderrTotal = {}
  29. # Import data from file
  30. for platform in platforms:
  31. averageTotal[platform] = np.genfromtxt('results/' + platform + '/average.csv', dtype=None, delimiter=',', names=['name', 'time', 'power'])
  32. stderrTotal[platform] = np.genfromtxt('results/' + platform + '/stderr.csv', dtype=None, delimiter=',', names=['name', 'time', 'power'])
  33. # Create the folder where to store the charts, if it doesn't already exist
  34. if not os.path.exists('charts'):
  35. os.makedirs('charts')
  36. # Dict that contains the indexes(entry in the data files) of the benchmarks taking into account their category
  37. indexes = {'short': [], 'long': []}
  38. # We cycle on the benchmarks results for the cpu (since usually they are the ones with the longest run time) and divide in two groups the benchmarks, putting the relative indexes in two lists
  39. for i in range(0,17):
  40. record = averageTotal['cpu'].take(i)
  41. if record['time'] < 30:
  42. indexes['short'].append(i)
  43. else:
  44. indexes['long'].append(i)
  45. # We instantiate a new dict to contain the average and the std err of the measurements on the various platforms
  46. average = {'cpu': {}, 'gpu-primary': {}, 'gpu-secondary': {}}
  47. stderr = {'cpu': {}, 'gpu-primary': {}, 'gpu-secondary': {}}
  48. for platform in platforms:
  49. for category in categories:
  50. average[platform][category] = averageTotal[platform].take(indexes[category])
  51. stderr[platform][category] = stderrTotal[platform].take(indexes[category])
  52. # Generate an array as placeholder for the x axis (we need to pass from a list to an array to take advantage of range)
  53. x = {}
  54. x['short'] = np.arange(len(indexes['short']))
  55. x['long'] = np.arange(len(indexes['long']))
  56. # Strip from the names of the benchmarks unwanted parts, taking advantage of the helper function defined before
  57. x_names_stripped = {'short': [], 'long': []}
  58. for category in categories:
  59. x_names = averageTotal['cpu'].take(indexes[category])['name'].tolist()
  60. x_names_stripped[category] = stripNames(category, x_names)
  61. # Helper function that create the plots of the execution time, takes as parameter the category of the benchmark we want to plot
  62. def plotTimes (category):
  63. # Initialization of the figures
  64. fig, ax = plt.subplots(figsize=(20, 10))
  65. # Create the bar plot for the time values
  66. time_cpu_bars = ax.bar(x[category]-0.3, average['cpu'][category]['time'], width=0.2, color='b', align='edge', yerr=stderr['cpu'][category]['time'])
  67. time_gpu_primary_bars = ax.bar(x[category], average['gpu-primary'][category]['time'], width=0.2, color='r', align='center', yerr=stderr['gpu-primary'][category]['time'])
  68. time_gpu_secondary_bars = ax.bar(x[category]+0.3, average['gpu-secondary'][category]['time'], width=-0.2, color='g', align='edge', yerr=stderr['gpu-secondary'][category]['time'])
  69. # Change the labels of the x axis to contain the names of the benchmarks
  70. ax.set_xticks(x[category])
  71. ax.set_xticklabels(x_names_stripped[category])
  72. ax.set_title('Execution time of the various benchmarks expressed in seconds')
  73. ax.set_xlabel('Benchmark')
  74. ax.set_ylabel('seconds')
  75. # Add some patches as legend of the colors used for the various benchmarks
  76. red_patch = mpatches.Patch(color='blue', label='Execution time for cpu')
  77. blue_patch = mpatches.Patch(color='red', label='Execution time for gpu(4 core)')
  78. green_patch = mpatches.Patch(color='green', label='Execution time for gpu(2 core)')
  79. ax.legend(handles=[red_patch, blue_patch, green_patch])
  80. # Invoke the helper function to attach labels for each bar
  81. autolabel(ax, time_cpu_bars)
  82. autolabel(ax, time_gpu_primary_bars)
  83. autolabel(ax, time_gpu_secondary_bars)
  84. # Save the obtained plot on file
  85. plt.savefig('charts/times-' + category + '.pdf')
  86. # Helper function that create the plots of the execution time, takes as parameter the category of the benchmark we want to plot
  87. def plotPower (category):
  88. # Create a new figure
  89. fig, bx = plt.subplots(figsize=(20, 10))
  90. # Create the bar plot for the power values
  91. power_cpu_bars = bx.bar(x[category]-0.3, average['cpu'][category]['power'], width=0.2, color='b', align='edge', yerr=stderr['cpu'][category]['power'])
  92. power_gpu_primary_bars = bx.bar(x[category], average['gpu-primary'][category]['power'], width=0.2, color='r', align='center', yerr=stderr['gpu-primary'][category]['power'])
  93. power_gpu_secondary_bars = bx.bar(x[category]+0.3, average['gpu-secondary'][category]['power'], width=-0.2, color='g', align='edge', yerr=stderr['gpu-secondary'][category]['power'])
  94. # Change the labels of the x axis to contain the names of the benchmarks
  95. bx.set_xticks(x[category])
  96. bx.set_xticklabels(x_names_stripped[category])
  97. bx.set_title('Power consumption of the various benchmarks expressed in mWatt/hour')
  98. bx.set_xlabel('Benchmark')
  99. bx.set_ylabel('mWatt/hour')
  100. # Add some patches as legend of the colors used for the various benchmarks
  101. red_patch = mpatches.Patch(color='blue', label='Power consumption for cpu')
  102. blue_patch = mpatches.Patch(color='red', label='Power consumption for gpu(4 core)')
  103. green_patch = mpatches.Patch(color='green', label='Execution time for gpu(2 core)')
  104. bx.legend(handles=[red_patch, blue_patch, green_patch])
  105. # Invoke the helper function to attach labels one for each bar
  106. autolabel(bx, power_cpu_bars)
  107. autolabel(bx, power_gpu_primary_bars)
  108. autolabel(bx, power_gpu_secondary_bars)
  109. # Save the obtained plot on file
  110. plt.savefig('charts/power-' + category + '.pdf')
  111. # Invoke the helper functions to plot the data
  112. for category in categories:
  113. plotTimes(category)
  114. plotPower(category)