这是什么MaxDegreeOfParallelism办? [英] What does MaxDegreeOfParallelism do?

查看:274
本文介绍了这是什么MaxDegreeOfParallelism办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Parallel.ForEach和我做一些数据库的更新,现在不设定MaxDegreeOfParallelism,双核处理器的机器导致SQL客户端超时,还有什么地方四核处理器的机器不知何故没有超时。

I am using Parallel.ForEach and I am doing some database updates, now without setting MaxDegreeOfParallelism , a dual core processor machine results in sql client timeouts, where else quad core processor machine somehow does not timeout.

现在我有过什么样的处理器核心都可以在我的code运行,但没有控制有一些设置我可以MaxDegreeOfParallelism改变这种状况可能会同时运行更少的操作,而不是导致超时?

Now I have no control over what kind of processor cores are available where my code runs, but is there some settings I can change with MaxDegreeOfParallelism that will probably run less operations simultaneously and not result in timeouts?

我可以增加超时,但它不是一个很好的解决方案,如果在更低的CPU,我可以同时处理更少的操作,这将少放负载的CPU。

I can increase timeouts but it isnt a good solution, if on lower CPU I can process less operations simultaneously, that will put less load on cpu.

好吧我已阅读所有其他职位和MSDN太多,但会设置MaxDegreeOfParallelism以较低的值使我的四核机器受苦?

Ok I have read all other posts and MSDN too, but will setting MaxDegreeOfParallelism to lower value make my quad core machines suffer?

例如,反正是有做这样的事情,如果CPU有两个内核,然后用20,如果CPU有四个内核,然后40?

For example, is there anyway to do something like, if CPU has two cores, then use 20, if CPU has four cores then 40?

推荐答案

答案是,它是整个并联运行的上限,不管内核的数量。

The answer is that it is the upper limit for the entire parallel operation, irrespective of the number of cores.

所以,即使你不使用的CPU,因为你在等待IO,或锁定,没有多余的任务将并行运行,只是你specifiy最大。

So even if you don't use the CPU because you are waiting on IO, or a lock, no extra tasks will run in parallel, only the maximum that you specifiy.

要了解这一点,我写了这台测试code。有一个人造锁在那里,刺激TPL使用多个线程。同样将在您的code在等待IO或数据库发生。

To find this out, I wrote this piece of test code. There is an artificial lock in there to stimulate the TPL to use more threads. The same will happen when your code is waiting for IO or database.

class Program
{
    static void Main(string[] args)
    {
        var locker = new Object();
        int count = 0;
        Parallel.For
            (0
             , 1000
             , new ParallelOptions { MaxDegreeOfParallelism = 2 }
             , (i) =>
                   {
                       Interlocked.Increment(ref count);
                       lock (locker)
                       {
                           Console.WriteLine("Number of active threads:" + count);
                           Thread.Sleep(10);
                        }
                        Interlocked.Decrement(ref count);
                    }
            );
    }
}

如果我不指定MaxDegreeOfParallelism,控制台测井显示,高达约8任务同时运行。像这样的:

If I don't specify MaxDegreeOfParallelism, the console logging shows that up to around 8 tasks are running at the same time. Like this:

Number of active threads:6
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:6
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7

有开始降低,随着时间增加,并在结束它试图在同一时间运行8。

It starts lower, increases over time and at the end it is trying to run 8 at the same time.

如果我把它限制在一些任意值(比如2),我得到

If I limit it to some arbitrary value (say 2), I get

Number of active threads:2
Number of active threads:1
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2

呵呵,这是一个四核的机器上。

Oh, and this is on a quadcore machine.

这篇关于这是什么MaxDegreeOfParallelism办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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