C 从OpenMP到MPI
我只是想知道如何将下面的openMP程序转换为MPI程序C 从OpenMP到MPI,c,mpi,openmp,C,Mpi,Openmp,我只是想知道如何将下面的openMP程序转换为MPI程序 #include <omp.h> #define CHUNKSIZE 100 #define N 1000 int main (int argc, char *argv[]) { int i, chunk; float a[N], b[N], c[N]; /* Some initializations */ for (i=0; i < N; i++) a[i] =
#include <omp.h>
#define CHUNKSIZE 100
#define N 1000
int main (int argc, char *argv[])
{
int i, chunk;
float a[N], b[N], c[N];
/* Some initializations */
for (i=0; i < N; i++)
a[i] = b[i] = i * 1.0;
chunk = CHUNKSIZE;
#pragma omp parallel shared(a,b,c,chunk) private(i)
{
#pragma omp for schedule(dynamic,chunk) nowait
for (i=0; i < N; i++)
c[i] = a[i] + b[i];
} /* end of parallel section */
return 0;
}
因此,在我看来,并行部分并不局限于MPI_Init()和MPI_Finalize()之间。您只需要将一部分数组(a、b、c)分配给每个进程。大概是这样的:
#include <mpi.h>
#define N 1000
int main(int argc, char *argv[])
{
int i, myrank, myfirstindex, mylastindex, procnum;
float a[N], b[N], c[N];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &procnum);
MPI_Comm_rank(comm, &myrank);
/* Dynamic assignment of chunks,
* depending on number of processes
*/
if (myrank == 0)
myfirstindex = 0;
else if (myrank < N % procnum)
myfirstindex = myrank * (N / procnum + 1);
else
myfirstindex = N % procnum + myrank * (N / procnum);
if (myrank == procnum - 1)
mylastindex = N - 1;
else if (myrank < N % procnum)
mylastindex = myfirstindex + N / procnum + 1;
else
mylastindex = myfirstindex + N / procnum;
// Initializations
for(i = myfirstindex; i < mylastindex; i++)
a[i] = b[i] = i * 1.0;
// Computations
for(i = myfirstindex; i < mylastindex; i++)
c[i] = a[i] + b[i];
MPI_Finalize();
}
#包括
#定义N 1000
int main(int argc,char*argv[])
{
inti,myrank,myfirstindex,mylastinex,procnum;
浮点数a[N],b[N],c[N];
MPI_Init(&argc,&argv);
MPI_Comm_大小(MPI_Comm_WORLD和procnum);
MPI_通信等级(通信和我的等级);
/*块的动态分配,
*取决于进程的数量
*/
如果(myrank==0)
myfirstindex=0;
else if(myrank
您只需要为每个进程分配一部分数组(a、b、c)。大概是这样的:
#include <mpi.h>
#define N 1000
int main(int argc, char *argv[])
{
int i, myrank, myfirstindex, mylastindex, procnum;
float a[N], b[N], c[N];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &procnum);
MPI_Comm_rank(comm, &myrank);
/* Dynamic assignment of chunks,
* depending on number of processes
*/
if (myrank == 0)
myfirstindex = 0;
else if (myrank < N % procnum)
myfirstindex = myrank * (N / procnum + 1);
else
myfirstindex = N % procnum + myrank * (N / procnum);
if (myrank == procnum - 1)
mylastindex = N - 1;
else if (myrank < N % procnum)
mylastindex = myfirstindex + N / procnum + 1;
else
mylastindex = myfirstindex + N / procnum;
// Initializations
for(i = myfirstindex; i < mylastindex; i++)
a[i] = b[i] = i * 1.0;
// Computations
for(i = myfirstindex; i < mylastindex; i++)
c[i] = a[i] + b[i];
MPI_Finalize();
}
#包括
#定义N 1000
int main(int argc,char*argv[])
{
inti,myrank,myfirstindex,mylastinex,procnum;
浮点数a[N],b[N],c[N];
MPI_Init(&argc,&argv);
MPI_Comm_大小(MPI_Comm_WORLD和procnum);
MPI_通信等级(通信和我的等级);
/*块的动态分配,
*取决于进程的数量
*/
如果(myrank==0)
myfirstindex=0;
else if(myrank
您可以尝试使用专有的英特尔群集OpenMP。它将在群集上运行OpenMP程序。
是的,它使用“软件分布式共享内存”在分布式内存集群上模拟共享内存计算机
< P>英特尔C++编译器(9.1 +)中使用简单。但它只能在64位处理器上工作。您可以尝试使用专有的英特尔群集OpenMP。它将在群集上运行OpenMP程序。 是的,它使用“软件分布式共享内存”在分布式内存集群上模拟共享内存计算机
< P>英特尔C++编译器(9.1 +)中使用简单。但它只能在64位处理器上工作。要回答您的更新: 使用MPI时,每个处理器运行相同的程序。为了限制平行部件,您需要使用如下语句:
if(秩==0){…串行工作…}
这将确保只有一个处理器在该块内工作
您可以在您发布的示例程序中看到这是如何工作的,在f()
中有if(myid==0)
语句。该语句块将仅由进程0执行,所有其他进程直接转到else
并接收它们的消息,然后再发送回
关于MPI_Init
和MPI_Finalize
-MPI_Init
初始化MPI环境。调用此方法后,可以使用其他MPI方法,如Send
和Recv
。一旦使用完MPI方法,MPI\u Finalize
将释放资源等,但程序将继续运行。例如,您可以在执行一些需要很长时间的I/O之前调用MPI\u Finalize
。这些方法不会限定代码的并行部分,而只是在可以使用其他MPI调用的地方
希望这有帮助。要回答您的更新: 使用MPI时,每个处理器运行相同的程序。为了限制平行部件,您需要使用如下语句:
if(秩==0){…串行工作…}
这将确保只有一个处理器在该块内工作
您可以在您发布的示例程序中看到这是如何工作的,在f()
中有if(myid==0)
语句。该语句块将仅由进程0执行,所有其他进程直接转到else
并接收它们的消息,然后再发送回
关于MPI_Init
和MPI_Finalize
-MPI_Init
初始化MPI环境。调用此方法后,可以使用其他MPI方法,如Send
和Recv
。一旦使用完MPI方法,MPI\u Finalize
将释放资源等,但程序将继续运行。例如,您可以在执行一些需要很长时间的I/O之前调用MPI\u Finalize
。这些方法不会限定代码的并行部分,而只是在可以使用其他MPI调用的地方
希望这有帮助。谢谢,3Electrologos!我的实际问题要复杂一点。我在信中说的。请看一看。提前谢谢。谢谢你,3Electrologos!我只是在我的问题中添加了一些更新,以表明并行部分以MPI_Init开始并以MPI_Finilize结束似乎不是真的。谢谢,3electrologos!我的实际问题要复杂一点。我在信中说的。请看一看。提前谢谢。谢谢你,3Electrologos!我只是在我的问题中添加了一些更新,以表明并行部分似乎不是以MPI_Init和en开头的
#include <mpi.h>
#define N 1000
int main(int argc, char *argv[])
{
int i, myrank, myfirstindex, mylastindex, procnum;
float a[N], b[N], c[N];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &procnum);
MPI_Comm_rank(comm, &myrank);
/* Dynamic assignment of chunks,
* depending on number of processes
*/
if (myrank == 0)
myfirstindex = 0;
else if (myrank < N % procnum)
myfirstindex = myrank * (N / procnum + 1);
else
myfirstindex = N % procnum + myrank * (N / procnum);
if (myrank == procnum - 1)
mylastindex = N - 1;
else if (myrank < N % procnum)
mylastindex = myfirstindex + N / procnum + 1;
else
mylastindex = myfirstindex + N / procnum;
// Initializations
for(i = myfirstindex; i < mylastindex; i++)
a[i] = b[i] = i * 1.0;
// Computations
for(i = myfirstindex; i < mylastindex; i++)
c[i] = a[i] + b[i];
MPI_Finalize();
}