In [7]:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from IPython.html.widgets import interactive
from IPython.display import display
In [8]:
def amdhals_law(B, n):
"""
Calculates Amdhals law.
:param B: float, fraction of program that is strictly serial.
:param n: integer, the number of threads.
"""
return 1. / (B + ((1 / n)*(1-B)))
In [9]:
def amdhals_law2(B, n, c=1e-6):
"""
Calculates Amdhals law.
:param B: float, fraction of program that is strictly serial.
:param n: integer, the number of threads.
"""
return 1. / (B + (c*n**2) + ((1 / n)*(1-B)))
In [10]:
def speedup_plot(speedup_fn=amdhals_law):
"""
Plot the response of amdhals law to changes in variables.
:param B: float, fraction of program that is strictly serial.
:param n: integer, the number of threads.
"""
processes = np.array([2**(x-1) for x in range(1, 12)])
strictly_serial = np.linspace(0.1, 1.0, 4)
fig, ax = plt.subplots(figsize=(10, 5))
ax.grid()
for _b in strictly_serial:
ax.plot(speedup_fn(_b, processes), linewidth=2.0, linestyle='-')
plt.legend(strictly_serial, loc='upper left', frameon=False)
ax.set_xticks(range(1,len(processes)))
ax.set_xticklabels(processes)
ax.set_ylim(0., 11.)
ax.set_xlabel('Number of processes (n)', weight='bold')
ax.set_ylabel('Theoretical speedup (x times faster)', weight='bold')
return ax
In [11]:
speedup_plot(amdhals_law);
In [12]:
speedup_plot(amdhals_law2);
In [12]:
In [12]: