Parallel and Distributed Computing

AP Computer Science Principles · Unit 4 — Computer Systems and Networks

Three ways to run the same work

Sequential computing performs operations one at a time, in order. The time it takes is the sum of the times of all steps.

Parallel computing breaks a program into smaller operations, some of which are performed at the same time on multiple processors. A parallel solution usually has a sequential portion (setup, steps that depend on earlier results, combining answers) plus a parallel portion. Its running time is the time of the sequential portion plus the time of the longest branch of the parallel portion, because the program cannot continue until the slowest processor finishes.

Distributed computing uses multiple separate devices, connected by a network, to run a program. It lets a problem be solved that a single computer could not handle at all, either because it would take far too long or because the data does not fit in one machine's storage. Its cost is communication: the devices must send data and partial results to each other, and that time adds to the total.

The speedup of a parallel solution is

$$\text{speedup}=\frac{\text{time of the sequential solution}}{\text{time of the parallel solution}}$$

Why more processors stop helping

Speedup is limited in three ways. First, the sequential portion never shrinks, so even infinitely many processors cannot make the program faster than that portion. Second, an indivisible task of length $L$ means the parallel portion can never take less than $L$. Third, dependencies: a task that needs another task's output cannot start until that task finishes. With dependencies, the minimum possible time is the length of the longest chain of tasks that must run one after another (the critical path), no matter how many processors are available.

For independent tasks on $k$ processors, the parallel time can be no less than $\max\left(L_{\max},\ \dfrac{\text{total}}{k}\right)$, but that lower bound is not always reachable; you must find an actual assignment of tasks to processors. A reliable method: place the longest tasks first, each on the processor that is currently least loaded, then check whether any swap lowers the finishing time of the busiest processor.

Exam questions often give a list of task times and ask for the minimum completion time or the speedup. Write the assignment explicitly, compute the finishing time of every processor, and take the largest.

Năm tác vụ 30, 50, 20, 40, 60 giây Tuần tự 30 50 20 40 60 tổng 200 s CPU 1 60 40 CPU 2 50 30 20 xong ở 100 s 0 100 200 s Thời gian song song = CPU xong MUỘN NHẤT · speedup = 200 / 100 = 2
Đọc thời gian song song ở mép phải. Hai CPU nhận $\{60,40\}$ và $\{50,30,20\}$, cả hai xong đúng lúc $100$ s — chia hoàn hảo nên speedup đạt $2$. Thêm CPU thứ ba thì tác vụ $60$ s một mình vẫn là chặn dưới, và cách xếp tốt nhất chỉ còn $70$ s.

Worked example: phần tuần tự và ba bộ xử lý

Một chương trình gồm bước khởi tạo $15$ s (tuần tự), sau đó năm tác vụ độc lập $30, 50, 20, 40, 60$ s, cuối cùng bước gộp kết quả $10$ s (tuần tự). Tính thời gian chạy tuần tự, thời gian tối thiểu trên 3 bộ xử lý và speedup.

Solution

Tuần tự: $15+\left(30+50+20+40+60\right)+10=15+200+10=225$ s.

Phần song song trên 3 CPU. Chặn dưới là $\max\left(60,\ 200/3\right)\approx 66{.}7$ s. Thử đạt dưới 70: tác vụ 60 không ghép được với tác vụ nào (nhỏ nhất là 20, ra 80), tác vụ 50 cũng vậy (50 + 20 = 70). Nếu 60 và 50 đứng riêng thì CPU thứ ba gánh $30+20+40=90$. Vậy không xếp được dưới 70. Cách xếp đạt 70: $\{60\}$, $\{50,20\}$, $\{40,30\}$.

Song song: $15+70+10=95$ s.

Speedup: $\dfrac{225}{95}\approx 2{.}37$. Chú ý: nếu chỉ tính phần song song thì $200/70\approx 2{.}86$; phần tuần tự $25$ s kéo speedup của cả chương trình xuống.

Common mistake

Bẫy số một: lấy tổng chia số bộ xử lý làm thời gian song song — chỉ đúng khi chia được đều tuyệt đối. Bẫy số hai: quên cộng phần tuần tự vào thời gian song song. Bẫy số ba: tưởng thêm bộ xử lý thì luôn nhanh hơn; khi đã chạm tác vụ dài nhất hoặc đường găng, thêm máy không đổi gì. Bẫy số bốn: lật ngược công thức speedup (song song chia tuần tự ra số nhỏ hơn 1).

Key takeaway

Tuần tự = tổng; song song = phần tuần tự + nhánh dài nhất; speedup = tuần tự ÷ song song. Speedup bị chặn bởi phần tuần tự, tác vụ dài nhất và chuỗi phụ thuộc. Phân tán dùng nhiều máy qua mạng để làm việc một máy không làm nổi, đổi lại phải trả thời gian giao tiếp.

← Redundancy and Fault Tolerance · All AP Computer Science Principles lessons · Practice AP Computer Science Principles questions (free, instant scoring) · Beneficial and Harmful Effects; the Digital Divide →