stable_point_multi_ineff.py 10.4 KB
import os, sys, argparse
import numpy
import matplotlib.pyplot as plt
import json
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.patches import Rectangle
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from sets import Set

from common import *

def plot_2d_stable_point_mulineff(args):
	dir_path = args.input_dir
	output_dir_path = args.output_dir

	benchmarks, labels = get_benchmarks(args)
	budgets = get_mul_ineffs()
	colors=['g','b','r','y']
	linestyles=['-','--',':','-']
        
	#plot constants
	cpuflist = get_cpu_freq_plot_list(args)
	memflist = get_mem_freq_plot_list(args)
	samplelabel = "Instructions (x 10M)"
	cpuflabel = "CPU frequency(MHz)"
	memflabel = "Memory frequency (MHz)"
	offset = 0
	rotation = 340

	# finding points with 3% threshold
	thresh = 3
	noise_thresh = 0.5
	for bmark in benchmarks:
		fig = plt.figure(figsize=(6, 2.5))
		canvas = FigureCanvas(fig)
		fig.set_canvas(canvas)
		ax = fig.add_subplot('111')
		ax_cpuf = plt.subplot(2, 1, 1)
		ax_memf = plt.subplot(2, 1, 2)

		for budget in budgets:
			bmarkDirPath = os.path.join(os.path.join(dir_path, "per_sample_data"), bmark)
			frontiers_file = os.path.join(bmarkDirPath, "per_sample_frontiers.json")
			frontiers_data = json.loads(open(frontiers_file).read())

			data_to_plot=[]
			for data in frontiers_data["data"]:
				filtered_data=[]
				#filtering all those points which have inefficiency with error% of the budget
				for point in data:
					if point["inefficiency"] <= budget:
						filtered_data.append(point)
					elif (point["inefficiency"] - budget) * 100 / budget <= thresh:
						filtered_data.append(point)

				#finding the point with highest performance among filtered data
				optimal_point = filtered_data[0]
				for point in filtered_data:
					if point["speedup"] > optimal_point["speedup"]:
						optimal_point = point

				#filtering the simulation noise by considering all points within noise_thresh% of optimal point
				local_rect_points=[]
				for point in filtered_data:
					if (optimal_point["speedup"] - point["speedup"]) < 0:
						print "something is wrong!"

					if (optimal_point["speedup"] - point["speedup"]) * 100 / optimal_point["speedup"] <= noise_thresh:
						local_rect_points.append(point)

				# among filtered points, choosing points with highest frequencies (first CPU and then by memory)
				final_point = optimal_point
				min_dist = 100000000 # some random large number
				for point in local_rect_points:
					if point["cpu_freq"] > final_point["cpu_freq"]:
						final_point = point
					elif point["cpu_freq"] == final_point["cpu_freq"]:
						if point["mem_freq"] > final_point["mem_freq"]:
							final_point = point

				prev_optimal = final_point
				data_to_plot.append(final_point)

			cpufpoints = [cpuf for cpuf in [data_to_plot[sample]["cpu_freq"] for sample in range(len(data_to_plot))]]
			memfpoints = [memf for memf in [data_to_plot[sample]["mem_freq"] for sample in range(len(data_to_plot))]]
			samplepoints = [sample for sample in range(len(data_to_plot))]

			if budget < 2.0:
				ax_cpuf.plot(samplepoints, cpufpoints, color=colors[budgets.index(budget)], label=budget)
				ax_memf.plot(samplepoints, memfpoints, color=colors[budgets.index(budget)], label=budget)
			else:
				ax_cpuf.plot(samplepoints, cpufpoints, color=colors[budgets.index(budget)], label="unconstrained")
				ax_memf.plot(samplepoints, memfpoints, color=colors[budgets.index(budget)], label="unconstrained")

			ax_cpuf.legend(fontsize=legend_size)
			ax_memf.legend(fontsize=legend_size)

		plt.subplots_adjust(hspace=0.05)

		ax_memf.set_xlabel(samplelabel)
		ax_cpuf.set_ylabel(cpuflabel)
		ax_memf.set_ylabel(memflabel)

		ax_cpuf.set_xlim([0, max(samplepoints) + 5])
		ax_cpuf.set_xticklabels([])
		ax_cpuf.set_ylim([0, max(cpuflist)+100])

		ax_memf.set_xlim([0, max(samplepoints) + 5])
		ax_memf.set_ylim([0, max(memflist)+100])

#		cpuf_ticks = [i + offset for i in cpuflist]
#		ax_cpuf.set_yticks(cpuf_ticks)
#		ax_cpuf.set_yticklabels(cpuflist)
		cpuf_ticks = [i + offset for i in range(100,1001, 100)]
		ax_cpuf.set_yticks(cpuf_ticks)
		ax_cpuf.set_yticklabels([i for i in range(100, 1001, 100)])

		instr_ticks = xrange(0, max(samplepoints), 10)
		ax_cpuf.set_xticks(instr_ticks)
		ax_cpuf.set_xticklabels([])
		ax_memf.set_xticks(instr_ticks)
		ax_memf.set_xticklabels(instr_ticks)

		ax_cpuf.grid('on')
		ax_memf.grid('on')

		ax_cpuf.set_title(bmark)

		outputf = os.path.join(os.path.join(output_dir_path, "2d_best_point_variation_mulineff"), "%s_2d_stable_point_mulineff" % (bmark))
		save_plot(ax, outputf)
		canvas.close()

def plot_2d_stable_point_mulineff_cpi_mpki(args):
	dir_path = args.input_dir
	output_dir_path = args.output_dir

	benchmarks, labels = get_benchmarks(args)
	benchmarks = ['gobmk']
	budgets = get_mul_ineffs()
	colors =     [ 'r' , 'b'   , 'g'  , 'y' ]
	linestyles = [ '-' , '--'  , '-' , '-' ]
	linewidths = [ 1   , 1     , 1    , 1   ]
	linedashes = [ []  , [5,1] , [2,0.5,1,0.5] , []   ] # [ on_len, off_len....]
        
	legend_size = 8  

	#plot constants
	cpuflist = get_cpu_freq_plot_list(args)
	memflist = get_mem_freq_plot_list(args)
	samplelabel = "Instructions (x 10M)"
	cpuflabel = "CPU"
	memflabel = "Memory"
	offset = 0
	rotation = 340

	# finding points with 3% threshold
	thresh = 3
	noise_thresh = 0.5
	for bmark in benchmarks:
		fig = plt.figure(figsize=(4, 3))
		canvas = FigureCanvas(fig)
		fig.set_canvas(canvas)
		ax = fig.add_subplot('111')
		fig.text(-0.009, 0.3, "Freq. (MHz)", va = 'center', rotation='vertical')
		ax_cpuf = plt.subplot(4, 1, 3)
		ax_memf = plt.subplot(4, 1, 4)
		ax_cpi = plt.subplot(4, 1, 1)
		ax_mpki = plt.subplot(4, 1, 2)

		for budget in budgets:
			bmarkDirPath = os.path.join(os.path.join(dir_path, "per_sample_data"), bmark)
			frontiers_file = os.path.join(bmarkDirPath, "per_sample_frontiers.json")
			frontiers_data = json.loads(open(frontiers_file).read())

			data_to_plot=[]
			for data in frontiers_data["data"]:
				filtered_data=[]
				#filtering all those points which have inefficiency with error% of the budget
				for point in data:
					if point["inefficiency"] <= budget:
						filtered_data.append(point)
					elif (point["inefficiency"] - budget) * 100 / budget <= thresh:
						filtered_data.append(point)

				#finding the point with highest performance among filtered data
				optimal_point = filtered_data[0]
				for point in filtered_data:
					if point["speedup"] > optimal_point["speedup"]:
						optimal_point = point

				#filtering the simulation noise by considering all points within noise_thresh% of optimal point
				local_rect_points=[]
				for point in filtered_data:
					if (optimal_point["speedup"] - point["speedup"]) < 0:
						print "something is wrong!"

					if (optimal_point["speedup"] - point["speedup"]) * 100 / optimal_point["speedup"] <= noise_thresh:
						local_rect_points.append(point)

				# among filtered points, choosing points with highest frequencies (first CPU and then by memory)
				final_point = optimal_point
				min_dist = 100000000 # some random large number
				for point in local_rect_points:
					if point["cpu_freq"] > final_point["cpu_freq"]:
						final_point = point
					elif point["cpu_freq"] == final_point["cpu_freq"]:
						if point["mem_freq"] > final_point["mem_freq"]:
							final_point = point

				prev_optimal = final_point
				data_to_plot.append(final_point)

			cpufpoints = [cpuf for cpuf in [data_to_plot[sample]["cpu_freq"] for sample in range(len(data_to_plot))]]
			memfpoints = [memf for memf in [data_to_plot[sample]["mem_freq"] for sample in range(len(data_to_plot))]]
			samplepoints = [sample for sample in range(len(data_to_plot))]

			label = budget if budget < 2  else "\infty"
			ax_cpuf.plot(samplepoints, cpufpoints, color=colors[budgets.index(budget)],
			linestyle=linestyles[budgets.index(budget)], label=label,
			linewidth=linewidths[budgets.index(budget)], dashes= linedashes[budgets.index(budget)])

			ax_memf.plot(samplepoints, memfpoints, color=colors[budgets.index(budget)],
			linestyle=linestyles[budgets.index(budget)], label=label,
			linewidth=linewidths[budgets.index(budget)], dashes= linedashes[budgets.index(budget)])

		samplepoints = [sample for sample in range(len(frontiers_data["data"]))]
		cpi = get_cpi(bmark, dir_path)
		ax_cpi.plot(samplepoints, cpi, color = 'k')

		mpki = get_mpki(bmark, dir_path)
		ax_mpki.plot(samplepoints, mpki, color = 'k')

		handles, labels  = ax_cpuf.get_legend_handles_labels()
		ax_cpi.legend(handles, labels, fontsize=legend_size, ncol = 4, bbox_to_anchor=(0.15, 1.02, 1., .102), handlelength=2.0, handletextpad=0.1, loc=3, borderaxespad=0.,)
#		ax_memf.legend(fontsize=legend_size)

		plt.subplots_adjust(hspace=0.05)

		ax_memf.set_xlabel(samplelabel)
		ax_cpuf.set_ylabel(cpuflabel)
		ax_memf.set_ylabel(memflabel)
		ax_cpi.set_ylabel("CPI")
		ax_mpki.set_ylabel("MPKI")

		ax_cpuf.set_xlim([0, max(samplepoints) + 5])
		ax_cpuf.set_xticklabels([])
		ax_cpuf.set_ylim([0, max(cpuflist)+100])

		ax_memf.set_xlim([0, max(samplepoints) + 5])
		ax_memf.set_ylim([0, max(memflist)+100])

		ax_cpi.set_xlim([0, max(samplepoints) + 5])
		ax_cpi.set_xticklabels([])
		ax_cpi.set_ylim([0, max(cpi) +1 ])

		ax_mpki.set_xlim([0, max(samplepoints) + 5])
		ax_mpki.set_xticklabels([])
		ax_mpki.set_ylim([0, max(mpki) +1])

#		cpuf_ticks = [i + offset for i in cpuflist]
#		ax_cpuf.set_yticks(cpuf_ticks)
#		ax_cpuf.set_yticklabels(cpuflist)
		cpuf_ticks = [i + offset for i in range(100,1001, 200)]
		ax_cpuf.set_yticks(cpuf_ticks)
		ax_cpuf.set_yticklabels([i for i in range(100, 1001, 200)])
		
		memf_ticks = [i + offset for i in range(100,801, 200)]
		ax_memf.set_yticks(memf_ticks)
		ax_memf.set_yticklabels([i for i in range(100, 801, 200)])

		instr_ticks = xrange(0, max(samplepoints)+5, 10)
		ax_cpuf.set_xticks(instr_ticks)
		ax_cpuf.set_xticklabels([])
		ax_memf.set_xticks(instr_ticks)
		ax_memf.set_xticklabels(instr_ticks)
		ax_cpi.set_xticks(instr_ticks)
		ax_cpi.set_xticklabels([])
		ax_mpki.set_xticks(instr_ticks)
		ax_mpki.set_xticklabels([])

		ax_cpuf.grid('on')
		ax_memf.grid('on')
		ax_cpi.grid('on')
		ax_mpki.grid('on')

		ax_cpuf.set_title(bmark)
		ax_cpuf.set_axisbelow(True)
		ax_memf.set_axisbelow(True)

		fig.subplots_adjust(hspace=0)
		outputf = os.path.join(os.path.join(output_dir_path, "2d_best_point_variation_mulineff"), "%s_2d_stable_point_mulineff_cpi_mpki" % (bmark))
		save_plot(ax, outputf)
		canvas.close()

def main(argv):
	args = parse(argv)

	#plot_2d_stable_point_mulineff(args)
	plot_2d_stable_point_mulineff_cpi_mpki(args)


if __name__ == "__main__":
	main(sys.argv)