renaming exercises
This commit is contained in:
parent
b9988fdda9
commit
55cccc5f4f
6 changed files with 2 additions and 2 deletions
52
exercises/exerciseA/README.md
Normal file
52
exercises/exerciseA/README.md
Normal file
|
@ -0,0 +1,52 @@
|
|||
# Exercise A: multithreading with NumPy
|
||||
|
||||
Objective: investigate speed-up of numpy code with multiple threads.
|
||||
|
||||
```HINT``` Use `htop` in your terminal to track what the CPUs are doing.
|
||||
|
||||
## First
|
||||
|
||||
The script `heavy_computation.py` performs some matrix calculations with numpy.
|
||||
|
||||
You can change the number of threads that numpy uses for the calculation
|
||||
using the `OMP_NUM_THREADS` environment variable like this:
|
||||
```
|
||||
OMP_NUM_THREADS=7 python heavy_computation.py
|
||||
```
|
||||
|
||||
The script will also measure the time to run the calculation and will save
|
||||
the timing results into the `timings/` folder as a `.txt` file.
|
||||
|
||||
**TASK**: Execute the script `heavy_computation.py`, varying the numbers of threads.
|
||||
You will plot the resulting calculating times in the second part below.
|
||||
|
||||
**QUESTION**
|
||||
> What happens if `OMP_NUM_THREADS` is not set? How many threads are there? Why?
|
||||
|
||||
|
||||
## Second
|
||||
|
||||
In `plot.py`, we have given code that will load all of the timing data in `timings/`.
|
||||
|
||||
**TASK**: Add code to plot of the execution duration vs. the number of threads
|
||||
|
||||
Open a PR with your plotting code and post your plots in the conversation, don't upload binaries to the Git remote!
|
||||
|
||||
**OPTIONAL TASK**: Add code to calculate and plot the speed-up time compared
|
||||
to single-threaded execution. Include your code and plot in the PR.
|
||||
|
||||
**QUESTIONS**
|
||||
|
||||
> What does the result tell us about the optimum number of threads? Why?
|
||||
|
||||
> Does it take the same time as your colleagues to run? Why?
|
||||
|
||||
## Optional tasks
|
||||
|
||||
Investigate the runtime variability. Systematically run multiple instances with the same number of threads by modifying `heavy_computation.py`.
|
||||
|
||||
How is the runtime affected when the problem becomes bigger? Is the optimum number of threads always the same?
|
||||
|
||||
How is the runtime affected when the memory is almost full? You can fill it up by creating a separate (unused) large numpy array.
|
||||
|
||||
How about running on battery vs. having your laptop plugged in?
|
32
exercises/exerciseA/heavy_computation.py
Normal file
32
exercises/exerciseA/heavy_computation.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
import os
|
||||
import timeit
|
||||
import numpy as np
|
||||
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')
|
||||
|
||||
# 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=}")
|
||||
|
||||
# Measure the time required for matrix multiplication
|
||||
start_time = time.time()
|
||||
y = x @ x # The heavy compute
|
||||
stop_time = time.time()
|
||||
elapsed_time = stop_time - start_time
|
||||
|
||||
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')
|
||||
|
||||
# IO: Save the timing to a unique txt file
|
||||
with open(f'timings/{threads}_threads_t{timestamp}.txt', 'w') as file:
|
||||
file.write(f'{threads},{elapsed_time:.6f}')
|
23
exercises/exerciseA/plot.py
Normal file
23
exercises/exerciseA/plot.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
import os
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# IO: This loads the timings for you
|
||||
threads, timings = [], []
|
||||
for file in os.listdir('timings'):
|
||||
with open(f'timings/{file}', 'r') as f:
|
||||
n, t = f.read().strip().split(',')
|
||||
threads.append(int(n))
|
||||
timings.append(float(t))
|
||||
threads = np.array(threads)
|
||||
timings = np.array(timings)
|
||||
|
||||
print('This is the data I loaded: threads =', threads, ', timings =',timings)
|
||||
|
||||
fig, axs = plt.subplots()
|
||||
|
||||
# CREATE YOUR PLOT HERE
|
||||
# Remember to label your axis
|
||||
# Feel free to make it pretty
|
||||
|
||||
plt.savefig('threads_v_timings.png', dpi=300)
|
Loading…
Add table
Add a link
Reference in a new issue