【发布时间】:2016-08-11 16:00:17
【问题描述】:
我有一个迭代算法,需要 openmp 和 MPI 来加速。这是我的代码
#pragma omp parallel
while (allmax > E) /* The precision requirement */
{
lmax = 0.0;
for(i = 0; i < m; i ++)
{
if(rank * m + i < size)
{
sum = 0.0;
for(j = 0; j < size; j ++)
{
if (j != (rank * m + i)) sum = sum + a(i, j) * v(j);
}
/* computes the new elements */
v1(i) = (b(i) - sum) / a(i, rank * m + i);
#pragma omp critical
{
if (fabs(v1(i) - v(i)) > lmax)
lmax = fabs(v1(i) - v(rank * m + i));
}
}
}
/*Find the max element in the vector*/
MPI_Allreduce(&lmax, &allmax, 1, MPI_FLOAT, MPI_MAX, MPI_COMM_WORLD);
/*Gather all the elements of the vector from all nodes*/
MPI_Allgather(x1.data(), m, MPI_FLOAT, x.data(), m, MPI_FLOAT, MPI_COMM_WORLD);
#pragma omp critical
{
loop ++;
}
}
但是当它没有加速时,甚至无法得到正确的答案,我的代码有什么问题? openmp不支持while循环吗?谢谢!
【问题讨论】:
-
显然,您要求每个线程执行代码并更新共享变量,从而产生竞争条件。对于并行性,无论是 OpenMP 还是 MPI,您都必须安排每个线程对自己的数据集进行操作,通过同时解决多个独立问题来获得性能。
-
@Alexander_Yau 您需要考虑线程将执行 MPI_Allreduce 和 MPI_Allgather。此外,最好使用原子操作来更新循环。 #pragma omp 原子更新循环++;
-
@Angelos,
MPI_Allreduce & MPI_Allgather处于竞争状态,对吧?