From f03b7e7a66dbaab0ae789fed2e8caee9e6341b93 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Fri, 26 Sep 2025 12:54:57 +0300 Subject: [PATCH] . --- exercises/exerciseA/heavy_computation.py | 35 ++++++++++++------------ exercises/exerciseA/plot.py | 13 ++++++++- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/exercises/exerciseA/heavy_computation.py b/exercises/exerciseA/heavy_computation.py index 896a1e7..53fb337 100755 --- a/exercises/exerciseA/heavy_computation.py +++ b/exercises/exerciseA/heavy_computation.py @@ -5,28 +5,29 @@ from datetime import datetime import time # Timestamp that will be put in the file name -timestamp = datetime.now().strftime("%H%M%S%f") # Get the environment variable for threads -threads = os.getenv('OMP_NUM_THREADS') +for threads in range(1,12): + for _ in range(3): + timestamp = datetime.now().strftime("%H%M%S%f") -# A relatively large matrix to work on -n = 5_000 -x = np.random.random(size=(n, n)) + # A relatively large matrix to work on + n = 5_000 + x = np.random.random(size=(n, n)) -print(f"We are executed with OMP_NUM_THREADS={threads} for {n=}") + print(f"We are executed with OMP_NUM_THREADS={threads} for {n=}") -# Measure the time required for matrix multiplication -start_time = time.time() -y = x @ x # The heavy compute -elapsed_time = time.time() - start_time + # Measure the time required for matrix multiplication + start_time = time.time() + y = x @ x # The heavy compute + elapsed_time = time.time() - start_time -print(f'Time used for matrix multiplication: {elapsed_time:.2f} s') + print(f'Time used for matrix multiplication: {elapsed_time:.2f} s') -# Check if timings folder exists -if not os.path.isdir('timings/'): - os.mkdir('timings') + # Check if timings folder exists + if not os.path.isdir('timings/'): + os.mkdir('timings') -# IO: Save the timing to a unique txt file -with open(f'timings/{threads}_threads_t{timestamp}.txt', 'w') as file: - print(f'{threads},{elapsed_time:.6f}', file=file) + # IO: Save the timing to a unique txt file + with open(f'timings/{threads}_threads_t{timestamp}.txt', 'w') as file: + print(f'{threads},{elapsed_time:.6f}', file=file) diff --git a/exercises/exerciseA/plot.py b/exercises/exerciseA/plot.py index 4130224..984d456 100755 --- a/exercises/exerciseA/plot.py +++ b/exercises/exerciseA/plot.py @@ -1,6 +1,7 @@ import os import numpy as np import matplotlib.pyplot as plt +import pandas as pd # IO: This loads the timings for you threads, timings = [], [] @@ -11,13 +12,23 @@ for file in os.listdir('timings'): timings.append(float(t)) threads = np.array(threads) timings = np.array(timings) +dat = {'timings': timings, 'threads': threads} -print('This is the data I loaded: threads =', threads, ', timings =',timings) +data = pd.DataFrame(dat) +averages = data.groupby('threads').aggregate(['mean','std']) +averages.to_csv('data.csv') fig, axs = plt.subplots() # CREATE YOUR PLOT HERE # Remember to label your axis # Feel free to make it pretty +means = averages['timings']['mean'] +stds = averages['timings']['std'] +axs.plot(averages.index, means) +axs.fill_between(averages.index, means-stds, means+stds, alpha=0.3) +axs.set_xlabel('Num threads') +axs.set_ylabel('Time (s)') +plt.show() plt.savefig('threads_v_timings.png', dpi=300)