C# Plinq给出了与Linq不同的结果-我做错了什么?

C# Plinq给出了与Linq不同的结果-我做错了什么?,c#,linq,plinq,C#,Linq,Plinq,有人能告诉我正确的Plinq代码是什么吗?我将双数组中每个元素的正弦绝对值的平方根相加,但是Plinq给出了错误的结果 该程序的输出为: Linq骨料=75.8310477905274(正确) Plinq骨料=38.0263653589291(约为其应为的一半) 我一定是做错了什么,但我想不出是什么 (我使用Visual Studio 2008在一台核心2双核Windows 7 x64 PC上运行此功能。) 代码如下: using System; using System.Collections

有人能告诉我正确的Plinq代码是什么吗?我将双数组中每个元素的正弦绝对值的平方根相加,但是Plinq给出了错误的结果

该程序的输出为:

Linq骨料=75.8310477905274(正确) Plinq骨料=38.0263653589291(约为其应为的一半)

我一定是做错了什么,但我想不出是什么

(我使用Visual Studio 2008在一台核心2双核Windows 7 x64 PC上运行此功能。)

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            double[] array = new double[100];

            for (int i = 0; i < array.Length; ++i)
            {
                array[i] = i;
            }

            double sum1 = array.Aggregate((total, current) => total + Math.Sqrt(Math.Abs(Math.Sin(current))));
            Console.WriteLine("Linq aggregate = " + sum1);

            IParallelEnumerable<double> parray = array.AsParallel<double>();
            double sum2 = parray.Aggregate((total, current) => total + Math.Sqrt(Math.Abs(Math.Sin(current))));
            Console.WriteLine("Plinq aggregate = " + sum2);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统集合;
命名空间控制台应用程序1
{
班级计划
{
静态void Main()
{
double[]数组=新的double[100];
for(int i=0;itotal+Math.Sqrt(Math.Abs(Math.Sin(当前));
Console.WriteLine(“Linq-aggregate=“+sum1”);
IParallelEnumerable parray=array.AsParallel();
double-sum2=parray.Aggregate((总计,当前)=>total+Math.Sqrt(Math.Abs(Math.Sin(当前));
Console.WriteLine(“Plinq-aggregate=“+sum2”);
}
}
}

聚合在PLINQ中的工作方式略有不同

而不是期望一个值 将累加器初始化为 用户给我们一个工厂函数 生成值:

公共静态双平均(此IEnumerable源)
{
返回source.AsParallel().Aggregate(
()=>新双[2],
(acc,elem)=>{acc[0]+=elem;acc[1]+;return acc;},
(acc1,acc2)=>{acc1[0]+=acc2[0];acc1[1]+=acc2[1];返回acc1;},
acc=>acc[0]/acc[1]);
}
现在,PLINQ可以初始化 每个蓄电池的独立蓄能器 线现在每个线程都有了自己的 自带蓄能器,可折叠 函数与累加器的结合 函数可以自由地对 蓄能器。普林克保证 不会访问蓄能器 同时从多个线程执行


因此,在您的情况下,您还需要传递一个累加器函数,该函数对并行聚合的输出求和(因此您看到的结果大约是应该结果的一半)。

谢谢MSDN博客。它现在似乎工作正常。我更改代码如下:

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Test();
        }

        static void Test()
        {
            double[] array = new double[100];

            for (int i = 0; i < array.Length; ++i)
            {
                array[i] = i;
            }

            double sum1 = array.Aggregate((total, current) => total + Math.Sqrt(Math.Abs(Math.Sin(current))));
            Console.WriteLine("Linq aggregate = " + sum1);

            IParallelEnumerable<double> parray = array.AsParallel();

            double sum2 = parray.Aggregate
            (
                0.0,
                (total1, current1) => total1 + Math.Sqrt(Math.Abs(Math.Sin(current1))),
                (total2, current2) => total2 + current2,
                acc => acc
            );

            Console.WriteLine("Plinq aggregate = " + sum2);
        }
    }
}
使用系统;
使用System.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main()
{
Test();
}
静态孔隙试验()
{
double[]数组=新的double[100];
for(int i=0;itotal+Math.Sqrt(Math.Abs(Math.Sin(当前));
Console.WriteLine(“Linq-aggregate=“+sum1”);
IParallelEnumerable parray=array.AsParallel();
双集料2=平行集料
(
0.0,
(total1,current1)=>total1+Math.Sqrt(Math.Abs(Math.Sin(current1)),
(total2,current2)=>total2+current2,
acc=>acc
);
Console.WriteLine(“Plinq-aggregate=“+sum2”);
}
}
}
using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Test();
        }

        static void Test()
        {
            double[] array = new double[100];

            for (int i = 0; i < array.Length; ++i)
            {
                array[i] = i;
            }

            double sum1 = array.Aggregate((total, current) => total + Math.Sqrt(Math.Abs(Math.Sin(current))));
            Console.WriteLine("Linq aggregate = " + sum1);

            IParallelEnumerable<double> parray = array.AsParallel();

            double sum2 = parray.Aggregate
            (
                0.0,
                (total1, current1) => total1 + Math.Sqrt(Math.Abs(Math.Sin(current1))),
                (total2, current2) => total2 + current2,
                acc => acc
            );

            Console.WriteLine("Plinq aggregate = " + sum2);
        }
    }
}