PLINQ,内核和WithDegreeOfParallelism? [英] Plinq, Cores and WithDegreeOfParallelism?

查看:951
本文介绍了PLINQ,内核和WithDegreeOfParallelism?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,PLINQ决定多少线程打开(在每一个不同的内核线程) 由核数。

AS far as I understood , Plinq decides how many thread to open ( each on a thread on different core) by cores count.

__________

  Core 1
  Core 2
  Core 3
  Core 4
___________

所以,如果我有一个PLINQ任务,找到所有的第1000素数, 砰砰将为了最大限度地提高效率,打开每个内核新主题。

So If I Have a Plinq task which finds all the first 1000 prime numbers , Plink will open a new Thread on each Core in order to maximize the efficiency.

所以在这里,每个核心将运行于4分之1000号码,找到该素数的逻辑

So here , each core will be running on 1000/4 numbers , the logic of finding the prime numbers.

不过,我读过,如 IO WithDegreeOfParallelism 被用来使CPU赢得了阻塞操作我不觉得这是一个密集的CPU运算,它允许使用更多线程比内核

However I've read that a blocking operations like IO should be used with WithDegreeOfParallelism so that the cpu won't think that this is an intensive cpu operation , and it allowed to use more threads than cores.

问:

1)是否准确?难道我理解正确的话?

1) Is it accurate ? Did I understood it correctly ?

2)如果设置了 WithDegreeOfParallelism(7)所以它肯定会使用所有4个内核,但对于其他的3? (7-4)在这里他们将运行?在其核心/ S?

2)If I set WithDegreeOfParallelism (7) so it will definitely use all the 4 cores , but what about the other 3 ? ( 7-4) where will they be running ? on which core/s ?

推荐答案

首先,.NET不选择哪些核心执行哪个线程的操作系统一样。如果在系统上运行没有其他CPU密集型应用程序,你可以期望,每个线程将在一个单独的内核中执行。但是,如果有一些其他的应用程序,操作系统可能例如决定在单个内核上运行的所有线程的,它们之间进行切换。

First, .Net doesn't choose which core executes which thread, the OS does. If there is no other CPU-intensive application running on the system, you can expect that each thread will execute on a separate core. But if there is some other application, the OS might for example decide to run all of your threads on a single core, switching between them.

和它甚至比这更复杂。线程通常不会在单核上运行,操作系统切换从核心到核心的所有时间。例如,看一下从任务管理器下面的截图显示一个单线程CPU密集型应用程序的执行。

And it's even more complicated than that. A thread usually doesn't run on a single core, the OS switches it from core to core all the time. For example, have a look at the following screenshot from Task Manager showing the execution of a single-threaded CPU-intensive application.

您会注意到单个线程上执行所有我的4个内核,并在几秒钟内就跑到利用每个核心的大约25%。

You'll notice that the single thread executed on all of my 4 cores, and utilized approximately 25 % of each core over the few seconds it ran.

净不了解您的计算机的CPU占用率,则认为这样做CPU密集型的工作线程的最佳数量是相同的内核数量。

.Net has no knowledge of the CPU usage of your computer, so it assumes that the optimal number of threads doing CPU-intensive work is the same as the number of cores.

我不知道究竟怎样PLINQ的工作,但我不希望每个核心在你的榜样产生完全四分之一千素数。如果一个线程已经产生其份额素数的和另外一个还没有完成,就不能有效地让第一个线程闲着。

I don't know how exactly does PLINQ work, but I wouldn't expect each core to produce exactly 1000/4 prime numbers in your example. If one thread already produced its share of prime numbers and another one isn't done yet, it wouldn't be efficient to let the first thread stay idle.

是的,与IO操作,线程的最佳数量不依赖于核心数量,所以你必须手动设置并行度。 (别忘了最佳线程数量可以是1。硬碟是最快,连续读取,不求来回多个文件之间)

And yes, with IO operations, the optimal number of threads doesn't depend on the number of cores, so you should set the degree of parallelism manually. (Don't forget that the optimal number of threads may be 1; harddisks are fastest with sequential reads, not seeking back and forth between many files.)

如果您设置 WithDegreeOfParallelism(7)它一定会用7的主题的(同样,不保证核心数量)。操作系统将决定如何运行这些7线程上的4个核心。如果所有这些线程是CPU密集型的,这很可能会让每个线程像一个核心的4/7≈57%。如果他们的IO绑定,它将执行$ C $下一个线程,刚刚醒来(畅通),在任何核心的才有效。

If you set WithDegreeOfParallelism(7) it will definitely use 7 threads (again, no guarantee on the number of cores). The OS will decide how to run those 7 threads on your 4 cores. If all of those threads are CPU-intensive, it will most likely give each thread something like 4/7 ≈ 57 % of a core. If they are IO-bound, it will execute the code for a thread that just woke up (unblocked) on any core that is just available.

WithDegreeOfParallelism()线程确实设定确切的数字,而不是他们的最大值,见斯蒂芬Toub的<一个href="http://blogs.msdn.com/b/pfxteam/archive/2009/05/29/9655514.aspx"><$c$c>ParallelOptions.MaxDegreeOfParallelism VS PLINQ的 WithDegreeOfParallelism 的。

And WithDegreeOfParallelism() really does set exact number of threads, not their maximum number, see Stephen Toub's ParallelOptions.MaxDegreeOfParallelism vs PLINQ’s WithDegreeOfParallelism.

这篇关于PLINQ,内核和WithDegreeOfParallelism?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆