【发布时间】:2021-06-17 00:25:39
【问题描述】:
我已经实现了 BW 和 FW 算法来求解 L 和 U 三角矩阵。 我实现的算法以串行方式运行得非常快,但我不知道这是否是并行化它的最佳方法。 我认为我已经考虑了所有可能的数据竞争(在 alpha 阶段),对吗?
void solveInverse (double **U, double **L, double **P, int rw, int cw) {
double **inverseA = allocateMatrix(rw,cw);
double* x = allocateArray(rw);
double* y = allocateArray(rw);
double alpha;
//int i, j, t;
// Iterate along the column , so at each iteration we generate a column of the inverse matrix
for (int j = 0; j < rw; j++) {
// Lower triangular solve Ly=P
y[0] = P[0][j];
#pragma omp parallel for reduction(+:alpha)
for (int i = 1; i < rw; i++) {
alpha = 0;
for (int t = 0; t <= i-1; t++)
alpha += L[i][t] * y[t];
y[i] = P[i][j] - alpha;
}
// Upper triangular solve Ux=P
x[rw-1] = y[rw-1] / U[rw-1][rw-1];
#pragma omp parallel for reduction(+:alpha)
for (int i = rw-2; (i < rw) && (i >= 0); i--) {
alpha = 0;
for (int t = i+1; t < rw; t++)
alpha += U[i][t]*x[t];
x[i] = (y[i] - alpha) / U[i][i];
}
for (int i = 0; i < rw; i++)
inverseA[i][j] = x[i];
}
freeMemory(inverseA,rw);
free(x);
free(y);
}
在与用户dreamcrash 私下讨论后,我们得出了他在cmets 中提出的解决方案,为每个线程创建了一对向量x 和y,它们将在单个列上独立工作。
【问题讨论】:
标签: c multithreading performance parallel-processing openmp