draw.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import os.path as osp
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. import configs as cf
  6. def draw():
  7. results = {
  8. 'XS': (cf.OUT_CSV, 'XS'),
  9. }
  10. configs = list(results.keys())
  11. color_types = 10
  12. cmap = plt.get_cmap('tab10')
  13. color_index = np.arange(0, 1, 1.0 / color_types)
  14. colors = [cmap(c) for c in color_index] * 3
  15. hatches = [None] * color_types + ['//'] * color_types + ['|'] * color_types
  16. n_conf = len(configs)
  17. # Draw stacked bar chart for each simulator
  18. width = 0.8 / n_conf
  19. # set figure size:
  20. fig, ax = plt.subplots()
  21. fig.set_size_inches(8.0, 5.0)
  22. x = None
  23. have_set_label = False
  24. dfs = [pd.read_csv(result[0], index_col=0)
  25. for _, result in results.items()]
  26. common_bmk = list(set.intersection(*[set(df.index) for df in dfs]))
  27. dfs = [df.loc[common_bmk] for df in dfs]
  28. rename = True
  29. fine_grain_rename = False
  30. renamed_dfs = []
  31. for df in dfs:
  32. to_drops = []
  33. sorted_cols = []
  34. def rename_with_map(df, rename_map):
  35. for k in rename_map:
  36. if rename_map[k] is not None:
  37. if rename_map[k].startswith('Merge'):
  38. merged = rename_map[k][5:]
  39. if merged not in df.columns:
  40. df[merged] = df[k]
  41. sorted_cols.append(merged)
  42. else:
  43. df[merged] += df[k]
  44. else:
  45. df[rename_map[k]] = df[k]
  46. sorted_cols.append(rename_map[k])
  47. to_drops.append(k)
  48. else:
  49. sorted_cols.append(k)
  50. df.drop(columns=to_drops, inplace=True)
  51. # Merge df columns according to the rename map if value starting with 'Merge'
  52. if rename:
  53. if fine_grain_rename:
  54. rename_with_map(df, cf.xs_fine_grain_rename_map)
  55. else:
  56. rename_with_map(df, cf.xs_coarse_rename_map)
  57. icount = 20 * 10 ** 6
  58. if 'BadSpecInst' in df.columns:
  59. df['BadSpecInst'] += df['Base'] - icount
  60. else:
  61. df['BadSpecInst'] = df['Base'] - icount
  62. df['Base'] = icount
  63. df = df.astype(float)
  64. renamed_dfs.append(df)
  65. common_col = list(set.intersection(
  66. *[set(df.columns) for df in renamed_dfs]))
  67. unique_cols = set()
  68. for df in renamed_dfs:
  69. unique_col = set(df.columns) - set(common_col)
  70. for col in unique_col:
  71. unique_cols.add(col)
  72. for df in renamed_dfs:
  73. for col in unique_cols:
  74. if col not in df.columns:
  75. df[col] = 0.0
  76. df.sort_index(axis=1, inplace=True)
  77. put_to_front = ['Base', 'BadSpec']
  78. tmp_df = renamed_dfs[0].sort_values(by='cpi', ascending=False)
  79. bmk_sort = tmp_df.index.tolist()
  80. for df in renamed_dfs:
  81. df = df.loc[bmk_sort]
  82. df = df[put_to_front +
  83. [col for col in df.columns if col not in put_to_front]]
  84. df = df.drop(columns=['cpi'])
  85. for to_drop in ['ipc', 'cpi', 'Cycles', 'Insts', 'coverage']:
  86. if to_drop in df.columns:
  87. df = df.drop(columns=[to_drop])
  88. # draw stacked bar chart
  89. bottom = np.zeros(len(df))
  90. highest = 0.0
  91. if x is None:
  92. x = np.arange(len(df), dtype=float)
  93. for component, color, hatch in zip(df.columns, colors[:len(df.columns)], hatches[:len(df.columns)]):
  94. if have_set_label:
  95. label = None
  96. else:
  97. label = component
  98. ax.bar(x, df[component], bottom=bottom,
  99. width=width, color=color, label=label, edgecolor='black', hatch=hatch)
  100. highest = max((bottom + df[component]).max(), highest)
  101. bottom += df[component]
  102. x += width
  103. have_set_label = True
  104. # replace x tick labels with df.index with rotation
  105. ax.set_xticks(x - width * len(results) / n_conf - 0.25)
  106. ax.set_xticklabels(bmk_sort, rotation=90)
  107. ax.tick_params(left=False, bottom=False)
  108. ax.set_ylabel('Slots')
  109. ax.set_xlabel('SPECCPU 2006 Benchmarks')
  110. handles, labels = plt.gca().get_legend_handles_labels()
  111. ax.legend(reversed(handles), reversed(labels), fancybox=True,
  112. framealpha=0.3,
  113. loc='best',
  114. ncol=3,
  115. )
  116. if n_conf == 2:
  117. ax.set_title(f'{configs[0]} <-- VS. --> {configs[1]}')
  118. fig.savefig(osp.join('results', 'result.png'),
  119. bbox_inches='tight', pad_inches=0.05, dpi=200)