C# 用PLINQ加速pi的计算
我需要使用PLINQ库通过蒙特卡罗方法计算Pi数,但当我的并行程序运行时,它计算Pi数的时间比它的非并行模拟长得多。如何修复它?并行计算类及其非并行模拟如下:C# 用PLINQ加速pi的计算,c#,parallel-processing,plinq,C#,Parallel Processing,Plinq,我需要使用PLINQ库通过蒙特卡罗方法计算Pi数,但当我的并行程序运行时,它计算Pi数的时间比它的非并行模拟长得多。如何修复它?并行计算类及其非并行模拟如下: class CalcPiPLINQ { int n; double aPi; int counter; public StringBuilder Msg; // diagnostic message public void Init(int aN)
class CalcPiPLINQ
{
int n;
double aPi;
int counter;
public StringBuilder Msg; // diagnostic message
public void Init(int aN)
{
//stopWatch.Start();
n = aN; // save total calculate-iterations amount
aPi = -1; // flag, if no any calculate-iteration has been completed
Msg = new StringBuilder("No any calculate-iteration has been completed");
}
public void Run()
{
if (n < 1)
{
Msg = new StringBuilder("Invalid N-value");
return;
}
Random rnd = new Random(); // create random-numbers generator
Stopwatch stopWatch = Stopwatch.StartNew();
var numbers = ParallelEnumerable.Range(0, n);
var result = Enumerable
.Range(0, n)
.Select(i => new { X = rnd.NextDouble(), Y = rnd.NextDouble() })
.AsParallel()
.Select(p => Check(p.X, p.Y))
.Count(b => b);
aPi = 4.0 * ((double)result / (double)n); // calculate approximate Pi-value
//stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
}
public double Done()
{
if (aPi > 0)
{
Msg = new StringBuilder("Calculations are completed successful!");
return aPi; // return calculated value
}
else
{
return 0; // no result
}
}
public bool Check(double x, double y)
{
return ((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) < 0.25;
}
public void CheckWithCounter(double x, double y)
{
if(((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) < 0.25)
{
Interlocked.Increment(ref counter);
}
}
}
class calcpilinq
{
int n;
双aPi;
整数计数器;
公共StringBuilder Msg;//诊断消息
公共void Init(int-aN)
{
//秒表。开始();
n=aN;//保存总计算迭代次数
aPi=-1;//标志,如果没有完成任何计算迭代
Msg=新的StringBuilder(“未完成任何计算迭代”);
}
公开募捐
{
if(n<1)
{
Msg=新的StringBuilder(“无效的N值”);
返回;
}
Random rnd=new Random();//创建随机数生成器
秒表秒表=Stopwatch.StartNew();
变量数=可并行枚举的范围(0,n);
var结果=可枚举
.范围(0,n)
.Select(i=>new{X=rnd.NextDouble(),Y=rnd.NextDouble()})
.天冬酰胺()
.选择(p=>检查(p.X,p.Y))
.计数(b=>b);
aPi=4.0*((双)结果/(双)n);//计算近似Pi值
//秒表;
TimeSpan ts=秒表。已用时间;
string elapsedTime=string.Format(“{0:00}:{1:00}:{2:00}.{3:00}”,
时,分,秒,
ts.毫秒/10);
Console.WriteLine(“运行时”+elapsedTime);
}
公共事务双重完成()
{
如果(aPi>0)
{
Msg=新建StringBuilder(“计算成功完成!”);
return aPi;//返回计算值
}
其他的
{
返回0;//没有结果
}
}
公共边界检查(双x,双y)
{
回报率((x-0.5)*(x-0.5)+(y-0.5)*(y-0.5))<0.25;
}
带计数器的公共作废支票(双x,双y)
{
如果((x-0.5)*(x-0.5)+(y-0.5)*(y-0.5))<0.25)
{
联锁增量(参考计数器);
}
}
}
非并行模拟:
class TCalcPi//unparallel calculating method
{
int N;
int N_0;
double aPi;
public StringBuilder Msg; // diagnostic message
double x, y;
Stopwatch stopWatch = new Stopwatch();
public void Init(int aN)
{
stopWatch.Start();
N = aN; // save total calculate-iterations amount
aPi = -1; // flag, if no any calculate-iteration has been completed
Msg = new StringBuilder("No any calculate-iteration has been completed");
}
public void Run()
{
if (N < 1)
{
Msg = new StringBuilder("Invalid N - value");
return;
}
int i;
Random rnd = new Random(); // to create randomizer
for (i = 1; i <= N; i++)
{
x = rnd.NextDouble(); // to generate coordinates
y = rnd.NextDouble(); //
if (((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) < 0.25)
{
N_0++; // coordinate in a circle! mark it by incrementing N_0
}
}
aPi = 4.0 * ((double)N_0 / (double)N); // to calculate approximate Pi - value
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
}
public double Done()
{
if (aPi > 0)
{
Msg = new StringBuilder("Calculates has been completed successful");
return aPi; // return gotten value
}
else
{
return 0; // no result
}
}
}
class TCalcPi//非并行计算方法
{
int N;
int N_0;
双aPi;
公共StringBuilder Msg;//诊断消息
双x,y;
秒表秒表=新秒表();
公共void Init(int-aN)
{
秒表。开始();
N=aN;//保存总计算迭代次数
aPi=-1;//标志,如果没有完成任何计算迭代
Msg=新的StringBuilder(“未完成任何计算迭代”);
}
公开募捐
{
if(N<1)
{
Msg=新的StringBuilder(“无效的N值”);
返回;
}
int i;
Random rnd=new Random();//创建随机化器
对于(i=1;i 0)
{
Msg=新建StringBuilder(“计算已成功完成”);
return aPi;//返回获取的值
}
其他的
{
返回0;//没有结果
}
}
}
是什么让您认为它应该更快?我认为问题不在于并行性,而在于LINQ select语句。这比循环需要更多的时间