diff --git a/final_assignment/cuda/cuda_test b/final_assignment/cuda/cuda_test index c891cd5..04d7a7f 100755 Binary files a/final_assignment/cuda/cuda_test and b/final_assignment/cuda/cuda_test differ diff --git a/final_assignment/java/CountThrees.class b/final_assignment/java/CountThrees.class new file mode 100644 index 0000000..e2e8cff Binary files /dev/null and b/final_assignment/java/CountThrees.class differ diff --git a/final_assignment/performance_metrics.csv b/final_assignment/performance_metrics.csv new file mode 100644 index 0000000..e69de29 diff --git a/final_assignment/run_tests.py b/final_assignment/run_tests.py index f19234b..bbe9d8c 100644 --- a/final_assignment/run_tests.py +++ b/final_assignment/run_tests.py @@ -1,14 +1,102 @@ -"""This script will test all the implementations of count_threes -and record the running times of each implementation. Each implementation -will be tested on sizes of 10000, 100000, 1000000, 10000000. It will be run -5 times each and the average will be taken. The results will be saved to a csv. -The implementations are C programs and a Java Program contained in subfolders. -Each one has a makefile to compile it and can be run from the command line as -follows: - - $ make - $ ./count_threes +"""This script tests all implementations of count_threes and records running +times. Each implementation will be tested on sizes of 10000, 100000, +1000000, 10000000, running 5 times each for an average. Results are saved to +csv. Implementations are C programs and a Java Program in subfolders, each +with a makefile. + +Usage: + $ make + $ ./count_threes """ -import time +import subprocess import csv +import re +import os + +def parse_performance_metrics(line): + match = re.match(r'"(.+)": ([\d.]+)', line) + if match: + metric, value = match.groups() + return metric, float(value) + return None + +def run_program(command, folder='.'): + env = os.environ.copy() + command = command.split() + if 'OMP_NUM_THREADS' in command[0]: + omp_threads, num_threads = command[0].split('=') + env[omp_threads] = num_threads + command = command[1:] + + # Change directory to program's folder + initial_dir = os.getcwd() + os.chdir(f'{initial_dir}/{folder.lstrip("./")}') + + # Execute the command + result = subprocess.run( + command, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + env=env + ) + + execution_times = [] + for line in result.stdout.split('\n'): + parsed_metric = parse_performance_metrics(line) + if parsed_metric and parsed_metric[0] == 'executionTime': + execution_times.append(parsed_metric[1]) + + # Return to initial directory + os.chdir(initial_dir) + return execution_times + +def build_commands(): + array_sizes = [10000, 100000, 1000000, 10000000] + thread_counts = [4, 8, 16, 32] + commands = [] + + # Build commands for each program + for size in array_sizes: + for count in thread_counts: + commands.append( + (f'OMP_NUM_THREADS={count} ./count_threes {size}', + size, count, 'OpenMP', './c_openmp') + ) + commands.append( + (f'java CountThrees {size} {count}', size, count, + 'Java', './java') + ) + commands.append( + (f'/usr/lib64/mpi/gcc/openmpi4/bin/mpirun --hostfile ./remotes.txt -np {count} ./count_threes {size}', + size, count, 'MPI', './mpi') + ) + commands.append( + (f'./padded {size} {count}', size, count, + 'Padded', './padded') + ) + + # CUDA program + commands.append( + (f'./cuda_test {size}', size, 'N/A', 'CUDA', './cuda') + ) + + return commands + +def export_to_csv(results, filename): + with open(filename, 'w', newline='') as file: + writer = csv.writer(file) + writer.writerow(['Program', 'Array Size', 'Thread Count', 'Execution Time']) + writer.writerows(results) + +# Execute and record results +commands = build_commands() +results = [] + +for cmd, size, count, program, folder in commands: + execution_times = run_program(cmd, folder=folder) + for time in execution_times: + results.append([program, size, count, time]) + +export_to_csv(results, 'performance_metrics.csv')