我正在尝试转换一个函数以使其在 C# 中并行执行而不是顺序执行,但我不确定我做错了什么:
// sequential
static void Romberg(double a, double b, int n, double[,] R)
{
int i, j, k;
double h, sum;
h = b - a;
R[0, 0] = (h / 2) * (f(a) + f(b));
for (i = 1; i <= n; i++)
{
h = h / 2;
sum = 0;
for (k = 1; k <= (Math.Pow(2.0, i) - 1); k += 2)
{
sum += f(a + k * h);
}
R[i, 0] = R[i - 1, 0] / 2 + sum * h;
for (j = 1; j <= i; j++)
{
R[i, j] = R[i, j - 1] + (R[i, j - 1] - R[i - 1, j - 1]) / (Math.Pow(4.0, j) - 1);
}
}
}
// parallel
static void RombergCP(double a, double b, int n, double[,] R)
{
int i,j, k;
double h, sum;
h = b - a;
R[0, 0] = (h / 2) * (f(a) + f(b));
Parallel.For(0, n, options, i =>
{
h = h / 2;
sum = 0;
for (k = 1; k <= (Math.Pow(2.0, i) - 1); k += 2)
{
sum += f(a + k * h);
};
R[i, 0] = R[i - 1, 0] / 2 + sum * h;
for (j = 1; j <= i; j++)
{
R[i, j] = R[i, j - 1] + (R[i, j - 1] - R[i - 1, j - 1]) / (Math.Pow(4.0, j) - 1);
}
});
}
我得到的错误是无法声明“i”,因为它会给“i”赋予不同的含义,“i”用于“父或当前”范围。我尝试在并行函数中将其重命名为 i2,但它给出了相同的错误。提前致谢!