简体   繁体   English

如何解释parallel.for(c#)的这种奇怪行为

[英]How can this strange behaviour of parallel.for (c#) be explained

I have problem with my library for neural networks. 我的神经网络库存在问题。 It uses multithreading to fasten computations. 它使用多线程来加速计算。 But after about 30-60 sec of runtime my program does not utilize 100% of my i7 3610QM 4cores 8threads anymore. 但是在运行约30-60秒之后,我的程序不再使用100%的i7 3610QM 4cores 8threads了。

Basically my processing looks like (c# with pseudocode) 基本上我的处理看起来像(c#with pseudocode)

for each training example t in training set
    for each layer l in neural network
        Parallel.For(0, N, (int i)=>{l.processForward(l.regions[i])})
    for each layer l in neural network (but with reversed order)
        Parallel.For(0, N, (int i)=>{l.backPropageteError(l.regions[i])})

Where regions is layer's list of precalculated regions of neuron to process. 区域是要处理的神经元的预先计算区域的图层列表。 Every region is the same size of 1/N of current layer so Tasks are same size to minimize chance that other threads need to wait for longest task to finish. 每个区域的大小与当前层的1 / N相同,因此任务大小相同,以最大限度地减少其他线程需要等待最长任务完成的机会。

Like i said, this processing scheme is consuming 100% of my processor only for a short time and then drops to about 80-85%. 就像我说的那样,这种处理方案仅在短时间内消耗100%的处理器然后降低到大约80-85%。 In my case i set N to Environment.ProcessorsCount (= 8); 在我的例子中,我将N设置为Environment.ProcessorsCount(= 8);

I can share whole code/repository if anyone is willing to help. 如果有人愿意提供帮助,我可以分享整个代码/存储库。

I tried to investigate and I created new console project and put there almost Hello World of Parallel.For() and i simply can't tell what is going on. 我试图调查,我创建了新的控制台项目,并把几乎Hello World的Parallel.For()放在那里,我根本无法分辨出发生了什么。 This might be other issue of Parallel.For() but i also want you to address this problem. 这可能是Parallel.For()的另一个问题,但我也希望你解决这个问题。 Here is the code: 这是代码:

class Program
{
    static void Main(string[] args)
    {
        const int n = 1;

        while (true)
        {
            //int counter = 0; for (int ii = 0; ii < 1000; ++ii) counter++;

            Parallel.For(0, n, (int i) => { int counter = 0; for (int ii = 0; ii < 1000; ++ii) counter++; });
        }

    }
}

In this code, I constantly (while loop) create one task (n=1) that has some work to do (increase counter one thousand times). 在这段代码中,我经常(while循环)创建一个任务(n = 1),有一些工作要做(增加计数器一千次)。 As i know, Parallel.For blocks execution / waits for all parallel calls to finish. 据我所知,Parallel.For块执行/等待所有并行调用完成。 If that is true it should be doing the same work as commented section (provided n=1). 如果这是真的,它应该与评论部分做同样的工作(提供n = 1)。 But on my computer, this program uses 100% of CPU, like there is work for more than one thread! 但是在我的电脑上,这个程序使用100%的CPU,就像有多个线程的工作一样! How is that possible? 怎么可能? When i switch to commented version, program uses less than 20% of CPU and this is what I expected. 当我切换到注释版本时,程序使用不到20%的CPU,这是我的预期。 Please help me understand this behaviour. 请帮我理解这个行为。

As @TaW said, their is a cost of going parallel. 正如@TaW所说,他们是并行的代价。 That's why f() and Parallel.For(0, n, _ => f()) are not equivalent. 这就是f()Parallel.For(0, n, _ => f())不等价的原因。 Parallel version incurs thread scheduling and context switching. 并行版本引起线程调度和上下文切换。 In your case the execution time of f() is comparable to thread scheduling overhead. 在您的情况下, f()的执行时间与线程调度开销相当。 That why you do get performance degrade with parallel version. 这就是为什么使用并行版本会降低性能的原因。 Parallel.For do wait until operation completes, but is completes so fast that several threads run on the CPU in a very short period of time (remember that each time you invoke Parallel.For it may choose different thread to run f() on it) on different CPU cores. Parallel.For等到操作完成,但是很快就完成了几个线程在很短的时间内在CPU上运行(请记住每次调用Parallel.For它可以选择不同的线程来运行f()就可以了)在不同的CPU核心上。

As for the first part of the question, i guess the problem lies in the index range passed to Parallel.For . 至于问题的第一部分,我猜问题在于传递给Parallel.For的索引范围。 Instead of [0, number of CPU cores), it should be equal to the index range of data. 它应该等于数据的索引范围,而不是[0,CPU核心数量]。

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

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