88import pandas as pd
99import platform
1010
11+ MODES = ['vector add' , 'vector multiply' , 'relu' , 'sigmoid' ]
12+
1113# Define path to the benchmark executable
1214SCRIPT_DIR = Path (__file__ ).parent .parent
1315SAVE_DIR = SCRIPT_DIR / "automation" / "benchmark_results.csv"
1921else :
2022 BENCHMARK_EXECUTABLE = SCRIPT_DIR / "build" / "benchmark"
2123
24+ def run_openMP_benchmark (N , mode = 0 ):
25+ if not BENCHMARK_EXECUTABLE .exists ():
26+ print (f"Benchmark executable not found at { BENCHMARK_EXECUTABLE } " )
27+ return None
28+ try :
29+ # framework_mode=1 means CPU (OpenMP)
30+ result = subprocess .run ([str (BENCHMARK_EXECUTABLE ), "0" , str (N ), "1" , str (mode )],
31+ text = True , capture_output = True )
32+ if result .returncode != 0 :
33+ print (f"Error running OpenMP benchmark with N={ N } : { result .stderr } " )
34+ return None
35+ if "Correct:" in result .stdout and "no" in result .stdout :
36+ print ("Validation failed" )
37+ return None
38+
39+ for line in result .stdout .splitlines ():
40+ if line .startswith ("OpenMP CPU time:" ):
41+ return float (line .split ()[3 ]) # extract time in ms
42+ except Exception as e :
43+ print (f"Exception occurred while running OpenMP benchmark with N={ N } : { e } " )
44+ return None
45+
2246
2347# Automatically detect all GPU devices
2448def get_gpu_devices ():
@@ -36,12 +60,12 @@ def get_gpu_devices():
3660 return devices
3761
3862
39- def run_benchmark (device , N , mode = 0 ):
63+ def run_openCL_benchmark (device , N , mode = 0 ):
4064 if not BENCHMARK_EXECUTABLE .exists ():
4165 print (f"Benchmark executable not found at { BENCHMARK_EXECUTABLE } " )
4266 return None
4367 try :
44- result = subprocess .run ([str (BENCHMARK_EXECUTABLE ), str (device ), str (N ), str (mode )], text = True , capture_output = True )
68+ result = subprocess .run ([str (BENCHMARK_EXECUTABLE ), str (device ), str (N ), "0" , str (mode )], text = True , capture_output = True )
4569
4670 if result .returncode != 0 :
4771 print (f"Error running benchmark for device { device } with N={ N } : { result .stderr } " )
@@ -58,50 +82,67 @@ def run_benchmark(device, N, mode=0):
5882 print (f"Exception occurred while running benchmark for device { device } with N={ N } : { e } " )
5983 return None
6084
61- def plot_results ():
85+ def plot_results (mode ):
6286 if not SAVE_DIR .exists ():
6387 print ("No results file. Run benchmarks first." )
6488 return
6589
6690 df = pd .read_csv (SAVE_DIR )
6791
68- for device_index in df ["Device" ].unique ():
69- subset = df [df ["Device" ] == device_index ]
70- plt .plot (subset ["N" ], subset ["Time (ms)" ], label = f"Device { device_index } " )
92+ for device_name in df ["Device" ].unique ():
93+ subset = df [df ["Device" ] == device_name ]
94+ plt .plot (subset ["N" ], subset ["Time (ms)" ], marker = 'o' , label = device_name )
7195
7296 plt .xlabel ("N (size)" )
7397 plt .ylabel ("Time (ms)" )
7498 plt .xscale ("log" , base = 2 )
7599 plt .yscale ("log" )
76- plt .title ("GPU Benchmark Results" )
100+ plt .title ("Benchmark Results for Function: " + MODES [ mode ] )
77101 plt .legend ()
78102 plt .grid (True )
79- plt .savefig (SCRIPT_DIR / "automation" / "benchmark_results.png" )
103+ path_str = "benchmark_results_" + MODES [mode ] + ".png"
104+ plt .savefig (SCRIPT_DIR / "automation" / path_str )
80105 plt .show ()
81106
82- def main (mode ):
107+
108+ def single_benchmark (mode ):
83109 devices = get_gpu_devices () # List of device IDs to benchmark
84- N_values = [1 << 10 , 1 << 15 , 1 << 22 , 1 << 24 , 1 << 28 ] # Different sizes for the benchmark
110+ N_values = [1 << 10 , 1 << 15 , 1 << 22 , 1 << 24 , 1 << 26 ] # Different sizes for the benchmark
85111
112+ # OpenCL benchmarking
86113 results = []
87114 for N in N_values :
88115 for i , dev in enumerate (devices ):
89116 print (f"Running benchmark for device { i } with N={ N } ..." )
90- output = run_benchmark (i , N , mode )
117+ output = run_openCL_benchmark (i , N , mode )
91118 if output is not None :
92- results .append ((i , N , output ))
93- print (f"Result: { output } " )
119+ results .append (("OpenCL Device " + str (i ), N , output ))
120+ print (f"Result: { output } ms" )
121+
122+ # CPU (OpenMP)
123+ for N in N_values :
124+ print (f"Running OpenMP benchmark on CPU with N={ N } ..." )
125+ t = run_openMP_benchmark (N , mode )
126+ if t is not None :
127+ results .append (("OpenMP CPU" , N , t ))
128+ print (f"Result: { t } ms" )
94129
95130 # Save results to CSV
96131 with open (SAVE_DIR , "w" , newline = '' ) as csvfile :
97132 csvwriter = csv .writer (csvfile )
98133 csvwriter .writerow (["Device" , "N" , "Time (ms)" ])
99134 csvwriter .writerows (results )
100135
101- print ("Benchmarking completed. Results saved to " , SAVE_DIR )
136+ print ("OpenCL benchmarking completed. Results saved to " , SAVE_DIR )
102137
103- plot_results ()
138+ plot_results (mode )
104139
105140if __name__ == "__main__" :
106- mode = sys .argv [1 ] if len (sys .argv ) > 1 else 0
107- main (mode )
141+ # Single benchmark
142+ # mode = int(sys.argv[1]) if len(sys.argv) > 1 else 0
143+ # single_benchmark(mode)
144+
145+ # Run benchmarks for all modes
146+ for mode in range (len (MODES )):
147+ print (f"Starting benchmarks for mode: { MODES [mode ]} " )
148+ single_benchmark (mode )
0 commit comments