最大并行度的进行AsParallel() [英] Max Degree of Parallelism for AsParallel()

查看:457
本文介绍了最大并行度的进行AsParallel()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Parallel.ForEach 我们要定义并行选项和设置最大并行度,如选择:

  Parallel.ForEach(价值观,新的ParallelOptions {MaxDegreeOfParallelism =号},值=> {
//做工作
})
 

但在做PLINQ这样的:

  Tabel.AsEnumberable()
     .AsParallel()
     。凡(//逻辑)
 

我没能找到一种方法来设置 MaxDegreeOfParallelism 。我抬起头,在网上为好,但没有发现任何东西。因为任何人都找到了解决的办法吗?任何帮助是AP preciated。

解决方案

您可以使用的 ParallelEnumerable.WithDegreeOfParallelism

  

设定并行度在查询中使用。程度   平行度的同时执行的任务的最大数目   将用于处理查询。

  VAR的结果= Tabel.AsEnumberable()
                  .AsParallel()
                  .WithDegreeOfParallelism(数)
                  。凡(/ * predicate * /);
 

编辑:

@svick提供的一个很好的ParallelOptions .MaxDegreeOfParallelism VS PLINQ的WithDegreeOfParallelism 强调了两者的区别:

  

使用下的非涵盖概念,我们称之为复制任务的并行工程。这个概念是,一个循环将开始一个任务,用于处理循环,但如果多个线程变得可用,以协助处理,额外的任务将被创建到那些线程运行。这使得最小的资源消耗。 鉴于此,这将是不准确的指出ParallelOptions使DegreeOfParallelism的规范,因为它确实最大程度:循环开始于一个度为1,并可能向上走任何最高这指定的是资源可用。

     

PLINQ是不同的。一些重要的标准查询操作中PLINQ需要查询中涉及的处理线程,包括一些依靠屏障,使螺纹锁步操作之间的通信。所述PLINQ设计需要积极参与该查询取得任何进展的线程特定号码。因此,当你指定一个DegreeOfParallelism为PLINQ,你指定的将要参与其中,而不仅仅是一个最大线程的实际数量。

While using Parallel.ForEach we have the option to define the Parallel options and set the Max Degree of Parallelism like :

Parallel.ForEach( values, new ParallelOptions {MaxDegreeOfParallelism = number}, value = > {
//Do Work
})

But while doing PLINQ like:

Tabel.AsEnumberable()
     .AsParallel()
     .Where(//Logic)

I was not able to find a way to set MaxDegreeOfParallelism. I looked up on the net as well, but didn't find anything. As anyone found a way around this? Any help is appreciated.

解决方案

You can use ParallelEnumerable.WithDegreeOfParallelism:

Sets the degree of parallelism to use in a query. Degree of parallelism is the maximum number of concurrently executing tasks that will be used to process the query.

var result = Tabel.AsEnumberable()
                  .AsParallel()
                  .WithDegreeOfParallelism(number)
                  .Where(/* predicate */);

Edit:

@svick provided an excellent on ParallelOptions.MaxDegreeOfParallelism vs PLINQ’s WithDegreeOfParallelism which emphasizes the difference between the two:

Parallel works using an under-the-covers concept we refer to as replicating tasks. The concept is that a loop will start with one task for processing the loop, but if more threads become available to assist in the processing, additional tasks will be created to run on those threads. This enables minimization of resource consumption. Given this, it would be inaccurate to state that ParallelOptions enables the specification of a DegreeOfParallelism, because it’s really a maximum degree: the loop starts with a degree of 1, and may work its way up to any maximum that’s specified as resources become available.

PLINQ is different. Some important Standard Query Operators in PLINQ require communication between the threads involved in the processing of the query, including some that rely on a Barrier to enable threads to operate in lock-step. The PLINQ design requires that a specific number of threads be actively involved for the query to make any progress. Thus when you specify a DegreeOfParallelism for PLINQ, you’re specifying the actual number of threads that will be involved, rather than just a maximum.

这篇关于最大并行度的进行AsParallel()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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