Commit bd160bde2cb92f5a0b1ec43b698e977791bd7d21

Authored by Guru Prasad
1 parent 38e0d558

Added script to plot tuning comparison

Showing 1 changed file with 161 additions and 0 deletions
figures/tuning_comparison.py 0 → 100644
  1 +import os,sys,argparse
  2 +import json
  3 +import itertools
  4 +from collections import namedtuple
  5 +from matplotlib import pyplot as plt
  6 +from common import *
  7 +
  8 +def within_threshold(value, base, threshold):
  9 + bmax = base * ((100 + threshold)/100.0)
  10 + if value <= bmax:
  11 + return True
  12 + else:
  13 + return False
  14 +
  15 +def find_optimal(data_points_list, inefficiency, ineff_threshold, cluster_threshold):
  16 + frontier = []
  17 + inefficiencies = []
  18 + energies = []
  19 +
  20 + # Find emin
  21 + emin = min(data_points_list, key=lambda x: x['energy'])['energy']
  22 + # Check
  23 + for point in data_points_list:
  24 + assert float(point['energy']) >= emin
  25 +
  26 + # Find all inefficiencies
  27 + for point in data_points_list:
  28 + inefficiencies.append(float(point['energy']) / float(emin))
  29 +
  30 + # Find the frontier
  31 + for idx, ineff in enumerate(inefficiencies):
  32 + if within_threshold(ineff, inefficiency, ineff_threshold):
  33 + frontier.append(data_points_list[idx])
  34 +
  35 + # Find the optimal point - point with best performance
  36 + optimal_point = min(frontier, key=lambda x: x['performance'])
  37 + # Check
  38 + for point in frontier:
  39 + assert point['performance'] >= optimal_point['performance']
  40 +
  41 + return optimal_point, optimal_point['performance'], optimal_point['energy']
  42 +
  43 +
  44 +
  45 +
  46 +def no_tuning(args, bmark, inefficiency, ineff_threshold=0.0, cluster_threshold=0.0):
  47 + file_path = os.path.join(os.path.join(os.path.join(args.input_dir, "aggr_data"), bmark), 'frontiers.json')
  48 + data = json.loads(open(file_path).read())
  49 +
  50 +
  51 +
  52 + nt_point, performance, energy = find_optimal(data['data'], inefficiency, ineff_threshold, cluster_threshold)
  53 +
  54 + # Get the simulation corresponding to the optimal
  55 + file_path = os.path.join(os.path.join(os.path.join(args.input_dir, "per_sample_data"), bmark), 'per_sample_frontiers.json')
  56 + data = json.loads(open(file_path).read())
  57 + no_tuning_data = []
  58 + for point in data['data']:
  59 + for sample in point:
  60 + if sample['cpu_freq'] == nt_point['cpu_freq'] and sample['mem_freq'] == nt_point['mem_freq']:
  61 + no_tuning_data.append(sample)
  62 + assert len(no_tuning_data) == len(data['data'])
  63 +
  64 + return nt_point, no_tuning_data, performance, energy
  65 +
  66 +
  67 +
  68 +
  69 +def with_tuning(args, bmark, inefficiency, ineff_threshold=0.0, cluster_threshold=0.0):
  70 + file_path = os.path.join(os.path.join(os.path.join(args.input_dir, "per_sample_data"), bmark), 'per_sample_frontiers.json')
  71 + data = json.loads(open(file_path).read())
  72 +
  73 + optimal_points = []
  74 + performance = 0
  75 + energy = 0
  76 + # For each sample
  77 + for point in data['data']:
  78 + assert len(point) == len(data['data'][0]) # 496
  79 +
  80 + optimal_point, performance, energy = find_optimal(point, inefficiency, ineff_threshold, cluster_threshold)
  81 + optimal_points.append(optimal_point)
  82 +
  83 + return optimal_points, sum(x['performance'] for x in optimal_points), sum(x['energy'] for x in optimal_points)
  84 +
  85 +
  86 +
  87 +
  88 +def plot(args, bmark, inefficiency, nt_points_list, t_points_list):
  89 + fig, axes_array = plt.subplots(3, sharex=True)
  90 + x_axis = range(len(t_points_list[0]))
  91 + x_ticklabels = [str(x) for x in x_axis]
  92 +
  93 + ax = axes_array[0]
  94 + ax.set_ylabel('Inefficiency')
  95 +
  96 + for nt_points, t_points in itertools.izip(nt_points_list, t_points_list):
  97 + nt_ineff = [x['inefficiency'] for x in nt_points]
  98 + t_ineff = [x['inefficiency'] for x in t_points]
  99 +
  100 + ax.plot(x_axis, nt_ineff, 'k')
  101 + ax.plot(x_axis, t_ineff)
  102 +
  103 +
  104 + ax = axes_array[1]
  105 + ax.set_ylabel('Performance (ms)')
  106 +
  107 + for nt_points, t_points in itertools.izip(nt_points_list, t_points_list):
  108 + nt_perf = [x['performance']/1e6 for x in nt_points]
  109 + t_perf = [x['performance']/1e6 for x in t_points]
  110 +
  111 + ax.plot(x_axis, nt_perf, 'k')
  112 + ax.plot(x_axis, t_perf)
  113 +
  114 +
  115 + ax = axes_array[2]
  116 + cpi = get_cpi(bmark, args.input_dir)
  117 +
  118 + ax.set_xlabel('Instructions (x10 Million)')
  119 + ax.set_ylabel('CPI')
  120 +
  121 + ax.plot(x_axis, cpi)
  122 +
  123 + plt.tight_layout()
  124 + fig.subplots_adjust(hspace=0.03)
  125 +
  126 + for ax in axes_array:
  127 + ax.grid(True)
  128 + ax.set_axisbelow(True)
  129 +
  130 + out_fname = os.path.join(os.path.join(os.path.join(args.output_dir, 'tuning_comparison'), bmark), '%.1f.jpg' % (inefficiency))
  131 + if not os.path.exists(os.path.dirname(out_fname)):
  132 + os.makedirs(os.path.dirname(out_fname))
  133 + plt.savefig(out_fname, dpi=300)
  134 +
  135 +def main(argv):
  136 + args = parse(argv)
  137 +
  138 + bmarks, labels = get_benchmarks(args)
  139 +
  140 + for bmark in bmarks:
  141 + print 'BENCHMARK: %s' % (bmark)
  142 + nt_points_list = []
  143 + t_points_list = []
  144 + for ineff in args.inefficiency:
  145 + string = '\tinefficiency: %0.2f\n' % (ineff)
  146 +
  147 + nt_point, data, nt_performance, nt_energy = no_tuning(args, bmark, ineff, 3)
  148 + string += '\t\tNO TUNING: %0.2fms %0.2fmJ\n' % (nt_performance / 1e6, nt_energy / 1e6)
  149 +
  150 + t_points, t_performance, t_energy = with_tuning(args, bmark, ineff, 3)
  151 + string += '\t\tW. TUNING: %0.2fms %0.2fmJ\n' % (t_performance / 1e6, t_energy / 1e6)
  152 +
  153 + nt_points_list.append(data)
  154 + t_points_list.append(t_points)
  155 + print string
  156 + plot(args, bmark, ineff, [data], [t_points])
  157 + # Uncomment this line if you want an aggregate plot per benchmark (Not very clean)
  158 + #plot(args, bmark, nt_points_list, t_points_list)
  159 +
  160 +if __name__ == '__main__':
  161 + main(sys.argv)