开发者

Parallel.For, max threads and cores

On our server, we got a quad-core processor. When running a Parallel.For loop without the ParallelOptions parameter, how many threads will it utilize?

Also, when running ne开发者_StackOverflow社区sted loops, should both the outer and inner loops be parallel or just the outer in terms of performance?


You can't know how many exactly, so the number of threads is between 1 and n, where n is the number of tasks to be run in parallel (one thread could be the current thread of execution by the way, since Parallel waits for its tasks to complete). Parallel does not guarantee that operations are run concurrently however, only that they may be. Implement your own Task Scheduler and use it if you want control over how many threads are used.

If you want to know whether it is better to run outer and inner loops in parallel, there is no answer. We don't know what your loops are doing. Use the Performance Analyzer before trying to optimize loops, since the odds are that you have no idea where your performance bottlenecks are.


It depends. A nested Parallel.For within a Parallel.For will parallel-ize each parallel-ized loop. However, if you don't then the for loop will execute entirely inside the same thread. Definitely read up on the Parallel library in MSDN.

http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for.aspx

I'd say the .Net CLR does some evaluation of what you're trying to do and generate as many threads that make sense but I can't say for sure.


The TPL will automatically determine how many threads to utilize, you can assume that it will be able to utilize all of the CPU.

As for the loops, only one of them should be parallel, but which one really depends on the workload and size of each loop. If the outer one can be parallel and you don't need any synchronization that would probably be the better one to parallel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜