Commit 76bc8c242788057c69146b97594af082a71920b2

Authored by Rizwana Begum
2 parents 2d01fdba 6cc1e680

Merge branch 'fix_issues' of ssh://blue.cse.buffalo.edu/papers/iiswc15-frontiers into fix_issues

Showing 1 changed file with 162 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', label='No Tuning')
  101 + ax.plot(x_axis, t_ineff, label='W. Tuning')
  102 + ax.legend()
  103 +
  104 +
  105 + ax = axes_array[1]
  106 + ax.set_ylabel('Performance (ms)')
  107 +
  108 + for nt_points, t_points in itertools.izip(nt_points_list, t_points_list):
  109 + nt_perf = [x['performance']/1e6 for x in nt_points]
  110 + t_perf = [x['performance']/1e6 for x in t_points]
  111 +
  112 + ax.plot(x_axis, nt_perf, 'k')
  113 + ax.plot(x_axis, t_perf)
  114 +
  115 +
  116 + ax = axes_array[2]
  117 + cpi = get_cpi(bmark, args.input_dir)
  118 +
  119 + ax.set_xlabel('Instructions (x10 Million)')
  120 + ax.set_ylabel('CPI')
  121 +
  122 + ax.plot(x_axis, cpi, 'g')
  123 +
  124 + plt.tight_layout()
  125 + fig.subplots_adjust(hspace=0.03)
  126 +
  127 + for ax in axes_array:
  128 + ax.grid(True)
  129 + ax.set_axisbelow(True)
  130 +
  131 + out_fname = os.path.join(os.path.join(os.path.join(args.output_dir, 'tuning_comparison'), bmark), '%.1f.jpg' % (inefficiency))
  132 + if not os.path.exists(os.path.dirname(out_fname)):
  133 + os.makedirs(os.path.dirname(out_fname))
  134 + plt.savefig(out_fname)
  135 +
  136 +def main(argv):
  137 + args = parse(argv)
  138 +
  139 + bmarks, labels = get_benchmarks(args)
  140 +
  141 + for bmark in bmarks:
  142 + print 'BENCHMARK: %s' % (bmark)
  143 + nt_points_list = []
  144 + t_points_list = []
  145 + for ineff in args.inefficiency:
  146 + string = '\tinefficiency: %0.2f\n' % (ineff)
  147 +
  148 + nt_point, data, nt_performance, nt_energy = no_tuning(args, bmark, ineff, 3)
  149 + string += '\t\tNO TUNING: %0.2fms %0.2fmJ\n' % (nt_performance / 1e6, nt_energy / 1e6)
  150 +
  151 + t_points, t_performance, t_energy = with_tuning(args, bmark, ineff, 3)
  152 + string += '\t\tW. TUNING: %0.2fms %0.2fmJ\n' % (t_performance / 1e6, t_energy / 1e6)
  153 +
  154 + nt_points_list.append(data)
  155 + t_points_list.append(t_points)
  156 + print string
  157 + plot(args, bmark, ineff, [data], [t_points])
  158 + # Uncomment this line if you want an aggregate plot per benchmark (Not very clean)
  159 + #plot(args, bmark, nt_points_list, t_points_list)
  160 +
  161 +if __name__ == '__main__':
  162 + main(sys.argv)