Parallel and distributed computing
Doing work in order
- Most simple programs are sequential: they do one step, then the next, then the next.
- The computer finishes step 1 before it starts step 2.
- This is easy to understand, but it can be slow for big jobs.
การทำงานตามลำดับ
- โปรแกรมส่วนใหญ่ที่เป็นพื้นฐานเป็น sequential (แบบลำดับ): ทำขั้นตอนหนึ่ง แล้วตามด้วยขั้นตอนถัดไป
- คอมพิวเตอร์จะเสร็จสิ้นขั้นตอนที่ 1 ก่อนจึงจะเริ่มขั้นตอนที่ 2
- สิ่งนี้เข้าใจง่าย แต่อาจ slow (ช้า) สำหรับงานขนาดใหญ่
Sequential (one worker):
task A -> task B -> task C -> task D
|-------|--------|--------|--------|
total time = A + B + C + D
Parallel computing
- Parallel computing splits work so several parts run at the same time.
- A modern computer has several processors (also called cores) that can each do work.
- If four workers each take one task, four tasks can finish in about the time of one.
การคำนวณแบบขนาน
- Parallel computing (การคำนวณแบบขนาน) แบ่งงานออกเพื่อให้หลายส่วนทำงาน at the same time (พร้อมกัน)
- คอมพิวเตอร์สมัยใหม่มี processors (โปรเซสเซอร์) หลายตัว (หรือที่เรียกว่า cores) ที่สามารถทำงานได้อย่างละตัว
- หากคนงานสี่คนรับงานละหนึ่งอย่าง งานสี่งานจะเสร็จภายในเวลาเท่ากับการทำงานหนึ่งงาน
Parallel (four workers at once):
worker 1: task A
worker 2: task B
worker 3: task C
worker 4: task D
|--------|
total time ≈ the longest single task
Speedup
- Speedup asks: how many times faster is the parallel version?
- speedup = (sequential time) / (parallel time).
- Example: a job takes 8 seconds in order, but 2 seconds split up. Speedup = 8 / 2 = 4 times.
Speedup (ความเร็วเพิ่มขึ้น)
- Speedup ถามว่า: เวอร์ชันแบบขนานเร็วขึ้นกี่เท่า?
- speedup = (เวลาแบบลำดับ) / (เวลาแบบขนาน)
- ตัวอย่าง: งานหนึ่งใช้ 8 วินาทีเมื่อทำเป็นลำดับ แต่ใช้ 2 วินาทีเมื่อแบ่งแยก ความเร็วเพิ่มขึ้น = 8 / 2 = 4 เท่า.
Not always N times faster
- More processors does not always mean N times faster.
- Some parts of a job cannot be split — they must happen in order.
- Also, splitting work and joining results back together takes some extra time.
ไม่จำเป็นต้องเร็วขึ้น N เท่าเสมอไป
- การเพิ่มจำนวนโปรเซสเซอร์ not (ไม่) ได้หมายความว่าเร็วขึ้น N เท่าเสมอไป
- บางส่วนของงาน cannot (ไม่สามารถ) แบ่งแยกได้—they must happen in order (ต้องทำตามลำดับ)
- นอกจากนี้ การแบ่งงานและรวมผลลัพธ์กลับมาใช้เวลารวม额外时间 (เวลาเพิ่มพูน)
Job = setup (must be in order) + main work (can split)
setup main work (split over 4)
|-----| |--------------------------------|
|--------| <- this part gets 4x
The setup part stays the same length.
Distributed computing
- Distributed computing uses many separate computers that cooperate over a network.
- They may sit in different rooms, cities, or countries.
- Examples: the web (many servers), big data jobs split over thousands of machines, and large science projects.
การคำนวณแบบกระจาย
- Distributed computing (การคำนวณแบบกระจาย) ใช้ many separate computers (คอมพิวเตอร์แยกอิสระจำนวนมาก) ที่ร่วมมือกันผ่านเครือข่าย
- คอมพิวเตอร์เหล่านี้อาจอยู่ในห้อง的不同, เมือง, หรือประเทศที่ต่างกัน
- ตัวอย่าง: The web (เวิลด์ไวยาฯ) (เซิร์ฟเวอร์จำนวนมาก), งาน big data (บิ๊กดาต้า) ที่แบ่งย่อยบนเครื่องนับพัน, และโครงการวิทยาศาสตร์ขนาดใหญ่
Distributed (computers cooperate over a network):
[computer 1] [computer 2] [computer 3]
\ | /
\ | /
shared network / job
Each computer does part of the work.
Trade-offs
- Good: parallel and distributed systems can be much faster, and can handle huge jobs.
- Harder: the code is more complex; parts must be coordinated; results must be combined.
- Limits: speedup is capped by the parts that cannot be split, and by network delays between computers.
Trade-offs (ข้อดีข้อเสีย)
- Good (ข้อดี): ระบบแบบขนานและแบบกระจายสามารถเร็วขึ้นมาก และสามารถจัดการงานมหาศาลได้
- Harder (ยากขึ้น): โค้ดซับซ้อนกว่า; ส่วนต่างๆ ต้องประสานงาน; ผลลัพธ์ต้องถูกรวมเข้าด้วยกัน
- Limits (ข้อจำกัด): ความเร็วเพิ่มขึ้นถูกจำกัดโดยส่วนที่ไม่สามารถแบ่งแยกได้ และความล่าช้าของเครือข่ายระหว่างคอมพิวเตอร์
Key words
- Sequential: steps run one after another, in order.
- Parallel: parts run at the same time on several processors.
- Speedup: sequential time divided by parallel time.
- Distributed: many separate computers cooperate over a network.
คำศัพท์สำคัญ
- Sequential (แบบลำดับ): ขั้นตอนทำงานทีละขั้นตอนตามลำดับ
- Parallel (แบบขนาน): ส่วนต่างๆ ทำงานพร้อมกันบนโปรเซสเซอร์หลายตัว
- Speedup (ความเร็วเพิ่มขึ้น): เวลาแบบลำดับหารด้วยเวลาแบบขนาน
- Distributed (แบบกระจาย): คอมพิวเตอร์แยกอิสระจำนวนมากร่วมมือกันผ่านเครือข่าย
Common mistakes
- Parallel speed-up only helps work that can be split into independent parts.
- Each extra processor adds less and less speed.
ข้อผิดพลาดที่พบบ่อย
- ความเร็วเพิ่มขึ้นแบบขนานจะช่วยเฉพาะงานที่สามารถแบ่งเป็นส่วนอิสระได้เท่านั้น
- โปรเซสเซอร์เพิ่มเติมแต่ละตัวจะให้ความเร็วเพิ่มขึ้นน้อยลงเรื่อยๆ
Now you try
- Put the speedup formulas to work as small functions.
- Model a job that is part fixed setup and part splittable work. Press Check answer.
ลองดูเลย
- นำสูตรความเร็วไปใช้งานเป็นฟังก์ชันเล็กๆ
- จำลองงานที่มีทั้งส่วนการตั้งค่าคงที่และส่วนงานที่แบ่งแยกได้ กด Check answer (ตรวจสอบคำตอบ)
A job has a setup part that must run in order, and a splittable part that workers can share at the same time. Write parallel_time(setup, splittable, workers) that returns the total time: the setup, plus the splittable part divided among the workers. Example: parallel_time(2, 8, 4) → 4.0 (2 + 8/4). · Job มีส่วน setup ที่ต้อง run แบบลำดับ, และส่วน splittable ที่ workers สามารถ share พร้อมกันได้ เขียน parallel_time(setup, splittable, workers) ที่ return เวลาทั้งหมด: setup, บวก ส่วนที่แบ่งได้หารด้วย workers ตัวอย่าง: parallel_time(2, 8, 4) → 4.0 (2 + 8/4)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write speedup(sequential, parallel) that returns how many times faster the parallel version is: the sequential time divided by the parallel time. Example: speedup(8, 2) → 4.0. · เขียน speedup(sequential, parallel) ที่คืนค่าว่าเวอร์ชัน parallel เร็วกว่าเท่าไร: เวลา sequential หารด้วยเวลา parallel ตัวอย่าง: speedup(8, 2) → 4.0
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Even with unlimited workers, the setup part still cannot be split. Write max_speedup(setup, splittable) for the best possible speedup: the whole job time divided by the setup time (endless workers shrink the splittable part to almost nothing, leaving only the setup). Example: max_speedup(2, 6) → 4.0 ((2+6)/2). · แม้จะมี workers ไม่มีจำกัด ส่วน setup ก็ยังไม่สามารถแบ่งได้ เขียน max_speedup(setup, splittable) สำหรับ speedup ที่ดีที่สุด: เวลาทั้ง job หารด้วยเวลา setup (workers无穷无尽的ลดส่วนที่แบ่งได้เหลือเกือบศูนย์ เหลือแต่ setup) ตัวอย่าง: max_speedup(2, 6) → 4.0 ((2+6)/2)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่