1#!/usr/bin/env python3 2import os 3import subprocess 4import sys 5import math 6import argparse 7import matplotlib.pyplot as plot 8import statistics as stat 9 10tmp_file="/tmp/tmp.txt" 11 12def analyze(Y): 13 print( 14 " -> [min, max, avg] = [%f, %f, %f]" % 15 (min(Y), max(Y), stat.mean(Y)) 16 ) 17 18########################################################### 19# regression: run 20########################################################### 21def run(target, method, thread, round): 22 23 exe = target + '/' + target 24 print(exe, '-m', method, '-t', thread, '-r', round) 25 26 with open(tmp_file, "w") as ofs: 27 subprocess.call( 28 [exe, '-m', method, '-t', str(thread), '-r', str(round)], 29 stdout=ofs 30 ) 31 32 X = [] 33 Y = [] 34 35 with open(tmp_file, "r") as ifs: 36 ifs.readline() 37 ifs.readline() 38 for line in ifs: 39 token = line.split() 40 assert len(token) == 2, "output line must have exactly two numbers" 41 X.append(int(token[0])) 42 Y.append(float(token[1])) 43 44 #analyze(Y) 45 46 return X, Y 47 48########################################################### 49# main function 50########################################################### 51def main(): 52 53 # example usage 54 # -b wavefront graph_traversal -t 1 2 3 -m tbb omp tf 55 56 parser = argparse.ArgumentParser(description='regression') 57 58 parser.add_argument( 59 '-b', '--benchmarks', 60 nargs='+', 61 help='list of benchmark names', 62 choices=['wavefront', 63 'graph_traversal', 64 'binary_tree', 65 'linear_chain', 66 'matrix_multiplication', 67 'mnist'], 68 required=True 69 ) 70 71 parser.add_argument( 72 '-m','--methods', 73 nargs='+', 74 help='list of tasking methods', 75 default=['tf', 'tbb', 'omp'], 76 choices=['tf', 'tbb', 'omp'] 77 ) 78 79 parser.add_argument( 80 '-t', '--threads', 81 type=int, 82 nargs='+', 83 help='list of the number of threads', 84 required=True 85 ) 86 87 parser.add_argument( 88 '-r', '--num_rounds', 89 type=int, 90 help='number of rounds to average', 91 default=1 92 ) 93 94 parser.add_argument( 95 '-p', '--plot', 96 type=bool, 97 help='show the plot or not', 98 default=False 99 ) 100 101 parser.add_argument( 102 '-o', '--output', 103 type=str, 104 help='file name to save the plot result', 105 default="result.png" 106 ) 107 108 # parse the arguments 109 args = parser.parse_args() 110 111 print('benchmarks: ', args.benchmarks) 112 print('threads:', args.threads) 113 print('methods:', args.methods) 114 print('num_rounds:', args.num_rounds) 115 print('plot:', args.plot) 116 117 rows = len(args.benchmarks) 118 cols = len(args.threads) 119 120 figc = plot.rcParams["figure.figsize"][0] 121 figr = plot.rcParams["figure.figsize"][1] 122 123 plot.rcParams["figure.figsize"] = [figc*cols*0.5, figr*rows*0.5] 124 125 fig, axes = plot.subplots(rows, cols) 126 plot_index = 1 127 128 for benchmark in args.benchmarks: 129 for thread in args.threads: 130 ax = plot.subplot(rows, cols, plot_index) 131 for method in args.methods: 132 ax = plot.title(benchmark + ' (' + str(thread) + ' threads)') 133 X, Y = run( 134 benchmark, method, thread, args.num_rounds 135 ) 136 #ax.text( 137 # .5, .9, 138 # benchmark + ' (' + str(thread) + ' threads)', 139 # horizontalalignment='center', 140 # transform=ax.transAxes 141 #) 142 if method == 'tf': 143 marker = '' 144 color = 'b' 145 elif method == 'omp': 146 marker = '+' 147 color = 'g' 148 else: 149 marker = '.' 150 color = 'r' 151 152 plot.plot(X, Y, label=method, marker=marker, color=color) 153 plot.legend() 154 print(X) 155 print(Y) 156 plot_index = plot_index + 1 157 158 plot.tight_layout() 159 plot.savefig(args.output) 160 161 if args.plot: 162 plot.show() 163 164 plot.close(fig) 165 166# run the main entry 167if __name__ == "__main__": 168 main() 169 170 171 172