我需要并行化这段代码.如您所见,q
数组包含在每个循环中求和的值.
/// <summary>
/// q = A * d, with A stored in CSR format.
/// </summary>
public static double[] Multiplication(int order, double[] d, List<double> A, List<int> J, int[] I)
{
double[] q = new double[order];
for (int i = 0; i < order; i++)
{
int indexFirst = I[i];
q[i] += A[indexFirst] * d[J[indexFirst]];
for (int j = indexFirst + 1; j < I[i + 1]; j++)
{
int col = J[j];
double a = A[j];
q[i] += a * d[col];
q[col] += a * d[i];
}
}
return q;
}
我能够以这种方式并行化代码(将问题分解到CPU计数的区段数中,并最终将得到的q
个数组相加),但它似乎过于复杂.有更好的主意吗?
Parallel.For(0, cpuCount, delegate(int i)
{
MultiplySection(startIndices[i], endIndices[i], d, A, J, I, qSection[i]);
});
double[] q = new double[order];
for (int j = 0; j < cpuCount; j++)
{
for (int i = startIndices[j]; i < order; i++)
{
q[i] += qSection[j][i];
}
}
private static void MultiplySection(int startIndex, int endIndex, double[] d, List<double> A, List<int> J, int[] I, double[] q)
{
for (int i = startIndex; i < endIndex; i++)
{
int indexFirst = I[i];
q[i] += A[indexFirst]*d[J[indexFirst]];
for (int j = indexFirst + 1; j < I[i + 1]; j++)
{
int col = J[j];
double a = A[j];
q[i] += a*d[col];
q[col] += a*d[i];
}
}
}