MaxDegreeOfParallelism 有什么作用? [英] What does MaxDegreeOfParallelism do?

查看:23
本文介绍了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.

现在我无法控制运行代码的地方可用的处理器内核类型,但是否可以使用 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.

因此,即使您因为等待 IO 或锁而没有使用 CPU,也不会并行运行额外的任务,只有您指定的最大值.

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.

为了解决这个问题,我写了这段测试代码.那里有一个人工锁来刺激 TPL 使用更多线程.当您的代码等待 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天全站免登陆