我有一个迭代算法需要 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循环吗?谢谢!
最佳答案
关于您的问题,#pragma omp parallel
构造只是生成 OpenMP 线程并并行执行它之后的 block 。是的,它支持执行 while
循环作为这个简约示例。
#include <stdio.h>
#include <omp.h>
void main (void)
{
int i = 0;
#pragma omp parallel
while (i < 10)
{
printf ("Hello. I am thread %d and i is %d\n", omp_get_thread_num(), i);
#pragma omp atomic
i++;
}
}
但是,正如 Tim18 和您自己提到的,您的代码中有几个注意事项。每个线程都需要访问自己的数据,这里的 MPI 调用是竞争条件,因为它们由所有线程执行。
您的代码中的这个更改怎么样?
while (allmax > E) /* The precision requirement */
{
lmax = 0.0;
#pragma omp parallel for shared (m,size,rank,v,v1,b,a,lmax) private(sum,j)
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);
loop ++;
}
主要的 while
循环是串行执行的,但是一旦循环开始,OpenMP 就会在遇到 #pragma omp parallel for
时在多个线程上生成工作。使用#pragma omp parallel for
(而不是#pragma omp parallel
)会自动将循环的工作分配给工作线程。此外,您需要在并行区域中指定变量类型(共享、私有(private))。我根据你的代码猜到了这里。
在 while
循环结束时,MPI 调用仅由主线程调用。
关于c++ - 如何在 openmp + mpi 的 While 循环中启动多进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38901181/