How do we write parallel programs?
重要概念
- 数据并行(data-parallelism)
Partiton various tasks carried out solving the problem the problem among the cores
每个核都做一样的任务 - 任务并行(task-parallism)
- partition the data used in solving the problem among the cores
形象化解释:
教授和助教一共3人,一共改试卷300份试卷,一份试卷有15题。
数据并行:
任务并行
Cores needs to coordinate(协同) their work
- communication(通信)
one or more cores send their current partial sums to another core.
核与核之间要交流 - load balancing(负载均衡)
share the work evenly among the cores so that one is not heavily loaded
每一个处理器在工作时负载大致相同。 - Synchronization(同步)
because each core works at its own pace,make sure don’t get too far ahead of the rest.
* 例子1:求累加的n项和
假设把数据分为n组分别累加,有一个接收数据累加和的变量x。正常情况下需要等变量的初始化在往其传数据。
如果变量还没初始化但其他的部分和已经送我来,会导致数据丢失等等。(没有同步的情况)
解决方案:
写一个函数给他们一个同步(同步函数:提出要求),让其他的核等,等x初始化后,在传数据(同步)
并发,并行,分布式
-
并发(concurrent)
Task can be in progress at any instant
一个程序的多个任务在同一时间段内可以同时执行
Parallel and distributed programs are concurrent
Multitasking operating system is also concurrent:enven in case of one core -
并行(parallel):
Multiple tasks cooperate closely to solve a problem
好多个任务协同工作去解决一个问题
Run multiple tasks simultaneously on cores:
多个核一起工作close to each other Share the same memory or connected by a very high-speed network — **computing cluster**
-
分布式(distributed)
Cooperate with other programs to solve a problem
多个程序协同工作解决一个问题
More “loosely coupled” (松耦合)May be executed by multiple computers that are separated by distances Tasks are often executed by programs that were created independently
例子区别并行与分布式
- Parallel:
two concurrent addition programs mentioned above - Distributed:
Web search program
不同浏览器可以做相同的事,前浏览器端,后服务器端。协同工作