Amdahl's law keeps the problem size fixed, while Gustafson's law lets it grow with the number of cores. Compare them for several core counts.
What to build
A program that prints both speedups for each core count.
Requirements
- For each core count n print
n amdahl A gustafson Gwith both speedups to 3 decimals. - Then print
amdahl limit Lwith L = 1 ÷ (1 − p) to 3 decimals, oramdahl limit infiniteif p is 1.
Input
A line with p, a line with q, then a line with q core counts.
Output
q + 1 lines.
Constraints
0 ≤ p ≤ 1; 1 ≤ q ≤ 20; core counts are from 1 to 100000.
Example
Input:
0.9
3
1 4 16
Output:
1 amdahl 1.000 gustafson 1.000
4 amdahl 3.077 gustafson 3.700
16 amdahl 6.400 gustafson 14.500
amdahl limit 10.000
Why it works
Amdahl asks how much faster the same job gets, so the serial part caps it. Gustafson asks how much more work fits in the same time, and the parallel part of the bigger job grows with the cores, so the answer keeps growing.
Examples
0.9 3 1 4 16
1 amdahl 1.000 gustafson 1.000 4 amdahl 3.077 gustafson 3.700 16 amdahl 6.400 gustafson 14.500 amdahl limit 10.000
1 2 2 8
2 amdahl 2.000 gustafson 2.000 8 amdahl 8.000 gustafson 8.000 amdahl limit infinite
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Amdahl: 1 / ((1 - p) + p / n). Gustafson: (1 - p) + p * n.
Hint 2
Print numbers with f"{x:.3f}".
Solution
Show one correct solution (try it yourself first)
p = float(input())
q = int(input())
for n in map(int, input().split()):
a = 1 / ((1 - p) + p / n)
g = (1 - p) + p * n
print(n, "amdahl", f"{a:.3f}", "gustafson", f"{g:.3f}")
print("amdahl limit infinite" if p == 1 else f"amdahl limit {1 / (1 - p):.3f}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step