gpt4 book ai didi

c# - 执行 Parallel.For 从 Array 计算数据的正确方法

转载 作者:行者123 更新时间:2023-11-30 20:02:28 25 4
gpt4 key购买 nike

想要:对 x 和 x*x 求和。其中 x = 行 [i]。因为不止一个线程想要读取/写入“sumAll”和“sumAllQ”,所以我需要锁定它的访问权限。问题是锁类关闭在这里序列化了东西。我需要将此操作拆分为 #"Environment.ProcessorCount"循环,每个循环对数组的一部分求和,最后对它们的结果求和。但是我怎样才能以编程方式实现呢?

示例代码:

//line is a float[]
Parallel.For(0, line.Length,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
i =>
{
x = (double)line[i];
lock (sumLocker)
{
sumAll += x;
sumAllQ += x * x;
}
});

编辑 1:Matthew Watson 回答基准测试结果

在家里。 CPU Core 2 四核 Q9550 @ 2.83 GHz:

Result via Linq:      SumAll=49999950000, SumAllQ=3,33332833333439E+15
Result via loop: SumAll=49999950000, SumAllQ=3,33332833333439E+15
Result via partition: SumAll=49999950000, SumAllQ=3,333328333335E+15
Via Linq took: 00:00:02.6983044
Via Loop took: 00:00:00.4811901
Via Partition took: 00:00:00.1595113

在工作。中央处理器 i7 930 2.8 GHz:

Result via Linq:      SumAll=49999950000, SumAllQ=3,33332833333439E+15
Result via loop: SumAll=49999950000, SumAllQ=3,33332833333439E+15
Result via partition: SumAll=49999950000, SumAllQ=3,333328333335E+15
Via Linq took: 00:00:01.5728736
Via Loop took: 00:00:00.3436929
Via Partition took: 00:00:00.0934209

最佳答案

vcjones 想知道您是否真的会看到任何加速。好吧,答案是:这可能取决于您拥有多少个内核。在我的家用 PC(四核)上,PLinq 比普通循环慢。

我想出了一个替代方法,它使用 Partitioner将数字列表分成几个部分,这样您就可以将每个部分分别相加。还有一些 information about using a Partitioner here .

使用 Partitioner 方法似乎更快一些,至少在我的家用 PC 上是这样。

这是我的测试程序。请注意,您必须在任何调试器之外运行此发布版本,以获得正确的时间。

这段代码中的重要方法是ViaPartition():

Result ViaPartition(double[] numbers)
{
var result = new Result();

var rangePartitioner = Partitioner.Create(0, numbers.Length);

Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
var subtotal = new Result();

for (int i = range.Item1; i < range.Item2; i++)
{
double n = numbers[i];
subtotal.SumAll += n;
subtotal.SumAllQ += n*n;
}

lock (result)
{
result.SumAll += subtotal.SumAll;
result.SumAllQ += subtotal.SumAllQ;
}
});

return result;
}

我运行完整测试程序时的结果(显示在这些结果下方)是:

Result via Linq:      SumAll=49999950000, SumAllQ=3.33332833333439E+15
Result via loop: SumAll=49999950000, SumAllQ=3.33332833333439E+15
Result via partition: SumAll=49999950000, SumAllQ=3.333328333335E+15
Via Linq took: 00:00:01.1994524
Via Loop took: 00:00:00.2357107
Via Partition took: 00:00:00.0756707

(请注意由于舍入误差造成的细微差异。)

如果能看到其他系统的结果会很有趣。

完整的测试程序如下:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Demo
{
public class Result
{
public double SumAll;
public double SumAllQ;

public override string ToString()
{
return string.Format("SumAll={0}, SumAllQ={1}", SumAll, SumAllQ);
}
}

class Program
{
void run()
{
var numbers = Enumerable.Range(0, 1000000).Select(n => n/10.0).ToArray();

// Prove that the calculation is correct.
Console.WriteLine("Result via Linq: " + ViaLinq(numbers));
Console.WriteLine("Result via loop: " + ViaLoop(numbers));
Console.WriteLine("Result via partition: " + ViaPartition(numbers));

int count = 100;

TimeViaLinq(numbers, count);
TimeViaLoop(numbers, count);
TimeViaPartition(numbers, count);
}

void TimeViaLinq(double[] numbers, int count)
{
var sw = Stopwatch.StartNew();

for (int i = 0; i < count; ++i)
ViaLinq(numbers);

Console.WriteLine("Via Linq took: " + sw.Elapsed);
}

void TimeViaLoop(double[] numbers, int count)
{
var sw = Stopwatch.StartNew();

for (int i = 0; i < count; ++i)
ViaLoop(numbers);

Console.WriteLine("Via Loop took: " + sw.Elapsed);
}

void TimeViaPartition(double[] numbers, int count)
{
var sw = Stopwatch.StartNew();

for (int i = 0; i < count; ++i)
ViaPartition(numbers);

Console.WriteLine("Via Partition took: " + sw.Elapsed);
}

Result ViaLinq(double[] numbers)
{
return numbers.AsParallel().Aggregate(new Result(), (input, value) => new Result
{
SumAll = input.SumAll+value,
SumAllQ = input.SumAllQ+value*value
});
}

Result ViaLoop(double[] numbers)
{
var result = new Result();

for (int i = 0; i < numbers.Length; ++i)
{
double n = numbers[i];
result.SumAll += n;
result.SumAllQ += n*n;
}

return result;
}

Result ViaPartition(double[] numbers)
{
var result = new Result();

var rangePartitioner = Partitioner.Create(0, numbers.Length);

Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
var subtotal = new Result();

for (int i = range.Item1; i < range.Item2; i++)
{
double n = numbers[i];
subtotal.SumAll += n;
subtotal.SumAllQ += n*n;
}

lock (result)
{
result.SumAll += subtotal.SumAll;
result.SumAllQ += subtotal.SumAllQ;
}
});

return result;
}

static void Main()
{
new Program().run();
}
}
}

关于c# - 执行 Parallel.For 从 Array 计算数据的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16821403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com