Parallel algorithms
- Algorithms course
- Expert
- Lesson 34/41
- 4 min
PRAM parallel model.
Parallel algorithms
Parallel algorithms solve a problem on multiple processors simultaneously. The PRAM (Parallel Random Access Machine) is a theoretical model used to describe parallel algorithms.
In PRAM, multiple processors access a shared memory concurrently. With p processors and problem size n, the parallel time T_p is typically close to T_1 / p.
Speedup is T_1 / T_p, and efficiency is T_1 / (p · T_p).
Amdahl's law
Amdahl's law sets the upper bound on speedup based on the non-parallel portion. If a fraction p of the problem is parallel, the maximum speedup is 1 / ((1 - p) + p / n), where n is the processor count.
For example, a 95% parallel problem can be at most 20 times faster with infinite processors. This law shows the limits of parallelization.
Efficient parallel design minimizes the non-parallel portion.
Practice parallel_sum function
Write parallel_sum(arr). Return the sum via parallel reduction: at each step sum adjacent pairs. Print 36 with print(parallel_sum([1, 2, 3, 4, 5, 6, 7, 8])).
Starter code
def parallel_sum(arr):
# Parallel reduction simulationSample solution
def parallel_sum(arr):
# Parallel reduction simulation
while len(arr) > 1:
new_arr = []
for i in range(0, len(arr), 2):
if i + 1 < len(arr):
new_arr.append(arr[i] + arr[i + 1])
else:
new_arr.append(arr[i])
arr = new_arr
return arr[0]
print(parallel_sum([1, 2, 3, 4, 5, 6, 7, 8]))Expected output
36
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How is speedup defined?
speedup = T_1 / T_p- T_p · T_1
- T_1 / T_p Correct answer
- p · T_p
- T_1 + T_p
Explanation: Speedup is T_1 / T_p.
2 parallel reduction
for i in range(0, len(arr), ____):
new_arr.append(arr[i] + arr[i + 1])
for i in range(0, len(arr), 2):
new_arr.append(arr[i] + arr[i + 1])
Explanation: Stride-2 scan simulates parallel reduction.
3 Parallel concepts
| Term | Meaning |
|---|---|
PRAM |
Theoretical model |
speedup |
T_1 / T_p |
Amdahl |
Serial limit |
reduction |
Adjacent pair |