并行编程和并行编程的区别是什么?我问了谷歌,但没有找到任何帮助我理解这种区别的东西。你能给我举个例子吗?
现在我找到了这个解释:http://www.linux-mag.com/id/7411 -但是“并发性是程序的属性”vs“并行执行是机器的属性”对我来说还不够-我仍然不能说什么是什么。
并行编程和并行编程的区别是什么?我问了谷歌,但没有找到任何帮助我理解这种区别的东西。你能给我举个例子吗?
现在我找到了这个解释:http://www.linux-mag.com/id/7411 -但是“并发性是程序的属性”vs“并行执行是机器的属性”对我来说还不够-我仍然不能说什么是什么。
当前回答
传统的任务调度可以是串行、并行或并发的。
Serial: tasks must be executed one after the other in a known tricked order or it will not work. Easy enough. Parallel: tasks must be executed at the same time or it will not work. Any failure of any of the tasks - functionally or in time - will result in total system failure. All tasks must have a common reliable sense of time. Try to avoid this or we will have tears by tea time. Concurrent: we do not care. We are not careless, though: we have analysed it and it doesn't matter; we can therefore execute any task using any available facility at any time. Happy days.
通常,在已知事件发生时,可用的调度会发生变化,我们称之为状态变化。
人们通常认为这是关于软件的,但实际上这是一种早于计算机的系统设计概念;软件系统的吸收速度有点慢,甚至很少有软件语言试图解决这个问题。如果你感兴趣,你可以试着查一下transputer language occam。
简而言之,系统设计解决以下问题:
动词——你在做什么(操作或算法) 名词——对(数据或接口)进行操作的对象 启动时,进度、状态改变 是串行、并行还是并发 地点——一旦你知道事情发生的时间,你就能说出事情可能发生的地点,而不是之前。 为什么,这是正确的方法吗?还有别的办法吗,更重要的是,更好的办法?如果你不做会怎么样?
祝你好运。
其他回答
我会试着用我自己的方式来解释它,它可能不是计算机术语,但它能给你一个大概的概念。
让我们以家务为例:洗碗,倒垃圾,修剪草坪等,我们有三个人(线程)A, B, C来做这些事情
并发: 三个人独立地开始不同的任务,例如,
A --> cleaning dishes
B --> taking out trash
C --> mowing the lawn
在这里,任务的顺序是不确定的,响应取决于工作量
并行: 在这里,如果我们想要提高吞吐量,我们可以分配多个人来完成一项任务,例如,清洁盘子,我们分配两个人,A擦洗盘子,B洗盘子,这可能会提高吞吐量。
洗碗:
A --> soaping the dishes
B --> washing the dishes
等等
希望这能给你一些启发!现在转到其他答案中解释的技术术语;)
Concurrent programming regards operations that appear to overlap and is primarily concerned with the complexity that arises due to non-deterministic control flow. The quantitative costs associated with concurrent programs are typically both throughput and latency. Concurrent programs are often IO bound but not always, e.g. concurrent garbage collectors are entirely on-CPU. The pedagogical example of a concurrent program is a web crawler. This program initiates requests for web pages and accepts the responses concurrently as the results of the downloads become available, accumulating a set of pages that have already been visited. Control flow is non-deterministic because the responses are not necessarily received in the same order each time the program is run. This characteristic can make it very hard to debug concurrent programs. Some applications are fundamentally concurrent, e.g. web servers must handle client connections concurrently. Erlang, F# asynchronous workflows and Scala's Akka library are perhaps the most promising approaches to highly concurrent programming.
Multicore programming is a special case of parallel programming. Parallel programming concerns operations that are overlapped for the specific goal of improving throughput. The difficulties of concurrent programming are evaded by making control flow deterministic. Typically, programs spawn sets of child tasks that run in parallel and the parent task only continues once every subtask has finished. This makes parallel programs much easier to debug than concurrent programs. The hard part of parallel programming is performance optimization with respect to issues such as granularity and communication. The latter is still an issue in the context of multicores because there is a considerable cost associated with transferring data from one cache to another. Dense matrix-matrix multiply is a pedagogical example of parallel programming and it can be solved efficiently by using Straasen's divide-and-conquer algorithm and attacking the sub-problems in parallel. Cilk is perhaps the most promising approach for high-performance parallel programming on multicores and it has been adopted in both Intel's Threaded Building Blocks and Microsoft's Task Parallel Library (in .NET 4).
并发:在单核机器上,多任务以cpu时间片共享的方式运行。 并行:在多核机器上,多个任务同时在每个核上运行。
我在一些博客上找到了这个内容。认为它是有用的和相关的。
并发性和并行性不是一回事。两个任务T1和T2是并发的,如果这两个任务的执行顺序不是预先确定的,
T1可以在T2之前执行和完成, T2可以在T1之前执行和完成, T1和T2可以在同一个时间实例中同时执行(并行性), T1和T2可以交替执行, ... 如果操作系统安排两个并发线程在一个单核非smt非cmp处理器上运行,您可能会得到并发性而不是并行性。并行在多核、多处理器或分布式系统上是可能的。
并发性通常被认为是程序的一种属性,是一个比并行性更普遍的概念。
来源:https://blogs.oracle.com/yuanlin/entry/concurrency_vs_parallelism_concurrent_programming
只是分享一个有助于突出区别的例子:
并行编程:假设您想实现归并排序算法。每次将问题划分为两个子问题时,可以有两个线程来解决它们。然而,为了进行合并步骤,您必须等待这两个线程完成,因为合并需要两个子解决方案。这种“强制等待”使其成为并行程序。
并发程序:假设你想压缩n个文本文件,并为每个文件生成一个压缩文件。您可以有2个(最多n个)线程,每个线程处理压缩文件的一个子集。当每个线程完成时,它就完成了,它不需要等待或做任何其他事情。因此,由于不同的任务以“任意顺序”交错的方式执行,所以程序是并发的,而不是并行的。
正如其他人提到的,每个并行程序都是并发的(事实上必须是),而不是相反。