33import os
44import sys
55from pathlib import Path
6+ import pyopencl as cl
7+ import matplotlib .pyplot as plt
8+ import pandas as pd
69
710# Define path to the benchmark executable
811SCRIPT_DIR = Path (__file__ ).parent .parent
1114SAVE_DIR = SCRIPT_DIR / "automation" / "benchmark_results.csv"
1215
1316
17+ # Automatically detect all GPU devices
18+ def get_gpu_devices ():
19+ platforms = cl .get_platforms ()
20+ devices = [dev for p in platforms for dev in p .get_devices (device_type = cl .device_type .GPU )]
21+
22+ unique_devices = []
23+ seen_names = set ()
24+
25+ for dev in devices :
26+ if dev .name not in seen_names :
27+ unique_devices .append (dev )
28+ seen_names .add (dev .name )
29+
30+ for i , dev in enumerate (unique_devices ):
31+ print (f"{ i } : { dev .name } " )
32+ print (f" Vendor: { dev .vendor } " )
33+ print (f" Max Compute Units: { dev .max_compute_units } " )
34+ print (f" Max Clock Frequency: { dev .max_clock_frequency } MHz" )
35+ print (f" Global Memory Size: { dev .global_mem_size // (1024 ** 2 )} MB" )
36+ print (f" Local Memory Size: { dev .local_mem_size // 1024 } KB" )
37+
38+ return unique_devices
39+
1440
1541def run_benchmark (device , N ):
1642 if not BENCHMARK_EXECUTABLE .exists ():
@@ -33,26 +59,45 @@ def run_benchmark(device, N):
3359 print (f"Exception occurred while running benchmark for device { device } with N={ N } : { e } " )
3460 return None
3561
62+ def plot_results ():
63+ df = pd .read_csv (SAVE_DIR )
64+
65+ for device_index in df ["Device" ].unique ():
66+ subset = df [df ["Device" ] == device_index ]
67+ plt .plot (subset ["N" ], subset ["Time (ms)" ], label = f"Device { device_index } " )
68+
69+ plt .xlabel ("N (size)" )
70+ plt .ylabel ("Time (s)" )
71+ plt .xscale ("log" , base = 2 )
72+ plt .yscale ("log" )
73+ plt .title ("GPU Benchmark Results" )
74+ plt .legend ()
75+ plt .grid (True )
76+ plt .savefig (SCRIPT_DIR / "automation" / "benchmark_results.png" )
77+ plt .show ()
78+
3679def main ():
37- devices = [ 0 , 1 ] # List of device IDs to benchmark
38- N_values = [1 << 22 , 1 << 24 , 1 << 26 ] # Different sizes for the benchmark
80+ devices = get_gpu_devices () # List of device IDs to benchmark
81+ N_values = [1 << 22 , 1 << 24 , 1 << 26 , 1 << 28 ] # Different sizes for the benchmark
3982
4083 results = []
4184 for N in N_values :
42- for device in devices :
43- print (f"Running benchmark for device { device } with N={ N } ..." )
44- output = run_benchmark (device , N )
85+ for i , dev in enumerate ( devices ) :
86+ print (f"Running benchmark for device { i } with N={ N } ..." )
87+ output = run_benchmark (i , N )
4588 if output :
46- results .append ((device , N , output ))
89+ results .append ((i , N , output ))
4790 print (f"Result: { output } " )
4891
4992 # Save results to CSV
5093 with open (SAVE_DIR , "w" , newline = '' ) as csvfile :
5194 csvwriter = csv .writer (csvfile )
52- csvwriter .writerow (["Device" , "N" , "Output " ])
95+ csvwriter .writerow (["Device" , "N" , "Time (ms) " ])
5396 csvwriter .writerows (results )
5497
5598 print ("Benchmarking completed. Results saved to " , SAVE_DIR )
5699
100+ plot_results ()
101+
57102if __name__ == "__main__" :
58103 main ()
0 commit comments