简体   繁体   English

PLINQ:并行扩展会自动处理单核和多核环境吗?

[英]PLINQ: Will Parallel Extensions automatically handle single and multi-core environments?

Will LINQ's parallel extensions automatically detect the number of cores and utilize them all? LINQ的并行扩展会自动检测内核数量并全部利用吗? Conversely, if the code is run on a single core machine, will it still work or do I have to detect the number of cores and tell PLINQ how many to run across? 相反,如果代码在单核计算机上运行,​​它仍然可以工作,还是我必须检测核的数量并告诉PLINQ要运行多少个核?

Sadly, I don't have access to any single core machines to test my code on so I can't even test this for myself and I haven't been able to find any useful info elsewhere... 可悲的是,我无权访问任何单核计算机来测试我的代码,因此我什至无法自己进行测试,也无法在其他地方找到任何有用的信息...

Also, while it might at first seem obvious when to use parallelism, are there any rules of thumb regarding where it should and as importantly should not be used? 此外,虽然起初在何时应该使用并行性似乎很明显,但是对于应在何处以及最重要的是应该使用它方面是否有任何经验法则?

Side Note: I don't necessarily program in a specific environment. 旁注:我不一定要在特定环境中编程。 I tend to divide my time somewhat equally (depending on project) between web, client/server apps, windows apps, windows service and console utilities depending on the task at hand. 我倾向于根据手头的任务在Web,客户端/服务器应用程序,Windows应用程序,Windows服务和控制台实用程序之间平均分配时间(取决于项目)。

Yes, it handles the core count itself, and is fine on single core, pseudo-multi-core (HT), right up to stupid numbers of cores - but you do need to code against Parallel yourself; 是的,它可以自行处理内核数量,并且在单核,伪多核(HT)上也可以使用,甚至可以达到愚蠢的内核数量,但是您确实需要针对Parallel编写代码; it doesn't simply seize control over your existing code. 它并不仅仅是抓住您现有代码的控制权。

For when to use parallelism... that is a huge topic. 对于何时使用并行机制……这是一个巨大的话题。 Actually, if you are doing web programming - I'd forget it and simply let IIS use different parallel threads for different requests (rather than letting one request hog the machine). 实际上,如果您正在执行Web编程-我会忘记它,而只是让IIS对不同的请求使用不同的并行线程(而不是让一个请求占用计算机)。

It is mainly useful for big number crunching / data gathering - either on a dedicated app-server, or at the client. 它主要用于大型运算/数据收集-在专用的应用服务器上或在客户端上。

Marc's answer is right on recommending you let the parallel nature of web requests handle much of your parallelism for you if that's the environment you're in. Marc的答案是正确的建议,如果您所处的环境如此,则建议您让Web请求的并行性质为您处理大部分并行性。

One place I've had great success using the parallel extensions was in loading data from remote sources. 我使用并行扩展取得了巨大成功的一个地方是从远程源加载数据。 I had an app where I'd need to fetch one piece of data, use some information from that to fetch 30 others, and return the results. 我有一个应用程序,需要提取一个数据,使用其中的一些信息来获取30个其他数据,然后返回结果。 I ran the first fetch normally, then defined a Future (from the parallel extensions) for each other request I needed, included the little bit of processing I needed in the anonymous method, and did a WaitAll() on the array of futures. 我正常运行了第一个提取操作,然后为我需要的每个其他请求定义了一个Future(通过并行扩展),包括了我在匿名方法中所需的一点处理,并对Future数组进行了WaitAll()。 Very simple to implement, and all the requests (or at least several of them at a time) run in parallel to fetch their data. 实现非常简单,所有请求(或一次至少几个)并行运行以获取其数据。 Especially since the latency for my calls was very high (5s wait for about 1kb of data), this technique worked great. 尤其是因为我的通话延迟非常长(5秒等待大约1kb的数据),所以这项技术非常有效。

Later on, I even modified it to return an IEnumerable, and used WaitOne() to wait for one of the futures to complete, then yielded it from my enumerator, allowing the caller to start processing the results from some of the requests before they all were complete. 后来,我甚至对其进行了修改,以返回IEnumerable,并使用WaitOne()等待某一期货完成,然后从枚举器中产生它,从而允许调用方在某些请求全部开始之前开始处理它们的结果。完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM