TPL数据并行问题 [英] TPL Data Parallelism Issue

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

问题描述

我有一个情况来处理组并行数据,到底我想知道有多少人在总已成功处理。我带着通过在 HTTP样本以下以下伪code:// MSDN .microsoft.com / EN-US /库/ dd460703.aspx 和<一href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/" rel="nofollow">http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/

I have a situation to process the set of data in parallel, in the end I want to know how many of them in total have been processed successfully. I come with following dummy code by following the sample at http://msdn.microsoft.com/en-us/library/dd460703.aspx and http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/

    public void DoWork2()
    {
        int sum = 0;
        Parallel.For<int>(0, 10,
            () => 0,
            (i, lockState, localState) =>
            {
                DummyEntity entity = DoWork3(i);
                if (entity != null)
                {
                    Console.WriteLine("Processed {0}, sum need to be increased by 1.", i);
                    return 1;
                }
                else
                {
                    Console.WriteLine("Processed {0}, sum need to be increased by 0.", i);
                    return 0;
                }
            },
            localState =>
            {
                lock (syncRoot)
                {
                    Console.WriteLine("Increase sum {0} by {1}", sum, localState);
                    sum += localState;
                }
            }
            );
        Console.WriteLine("Total items {0}", sum);
    }

    private DummyEntity DoWork3(int i)
    {
        if (i % 2 == 0)
        {
            return new DummyEntity();
        }
        else
        {
            return null;
        }
    }

但结果每次运行时间的变化。我觉得有一些东西错了code。但是想不出为什么。

However the result changes every time I run. I think there is some thing wrong with the code. But could not figure out why.

推荐答案

您的问题是你的选择的过载。你存储的本地状态信息,以尽量减少使用全局状态的,但你不使用本地状态。

Your problem is your choice in overloads. You've stored local state information to minimize the use of global state, yet you're not using the local state.

如果你从你给他们使用小计(你所名为 localState )的循环体的例子说明:

If you note from the example you gave they use the subtotal (what you've called localState) in the body of the loop:

subtotal += nums[j];
return subtotal;

与此相比,您的code(稍作更简洁):

Compare this to your code (made a bit more concise):

if (entity != null)
{
    return 1;
}
else
{
    return 0;
}

没有提及 localState 中是存在的,所以你有效地扔掉一些答案。如果你改变它,而不是阅读:

No mention of localState is there, so you've effectively thrown away some of the answers. If you change it instead to read:

if (entity != null)
{
    return localState + 1;
}
else
{
    return localState;
}

您会发现在命令行下面的回答(这个给定的问题):

You'll find the following answer on the command line (for this given problem):

Total items 5

中的本地状态的这种用法,以减少访问共享状态。

This usage of local state is in order to reduce access to shared state.

下面是使用0..50作为范围的一个片段:

Here is a snippet from using 0..50 as the range:

Processed 22, sum need to be increased by 1.
Processed 23, sum need to be increased by 0.
Increase sum 0 by 1
Processed 8, sum need to be increased by 1.
Processed 9, sum need to be increased by 0.
Processed 10, sum need to be increased by 1.
Processed 11, sum need to be increased by 0.
Increase sum 1 by 2
Increase sum 3 by 8
Increase sum 11 by 10
Processed 16, sum need to be increased by 1.
Processed 17, sum need to be increased by 0.
Processed 18, sum need to be increased by 1.
Increase sum 21 by 4
Total items 25

这篇关于TPL数据并行问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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