我需要在我的矩阵乘法程序中使用 MPI_Gather 函数,但最近几天遇到了问题。
因此,我单独使用 gather 函数编写了一个简单的 MPI 程序,并一直试图让它运行……为此,我引用了“Peter Pacheco 的并行编程简介”一书。
程序直接退出,完全没有结果,也没有报错……到现在也没能找出错误。
/******************NOTE**********************
The program simply uses the MPI_Gather() function.
The program is exiting directly.
I have written it for only TWO processes.
(-np 2)
******************************************/
#include<stdio.h>
#include"mpi.h"
int main()
{
int i,j,proc,rank;
double d[4];
double local_a[2];
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &proc);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank==0)
{
local_a[0]=1.0;
local_a[1]=2.0;
}
else
{
local_a[0]=3.0;
local_a[1]=4.0;
}
int local=2;
if(rank==0)
{
MPI_Gather(local_a,local,MPI_DOUBLE,d,local,MPI_DOUBLE,0,MPI_COMM_WORLD);
//MPI_Gather(&local_a,local,MPI_DOUBLE,&d,local,MPI_DOUBLE,0,MPI_COMM_WORLD);
//also tried the above line just to be certain.
printf("\n");
for(j=0;j<4;j++)
printf("\t%f",d[j]);
}
else
{
MPI_Gather(local_a,local,MPI_DOUBLE,d,local,MPI_DOUBLE,0,MPI_COMM_WORLD);
//MPI_Gather(&local_a,local,MPI_DOUBLE,&d,local,MPI_DOUBLE,0,MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
谁能帮帮我。
谢谢。
阿纳加·马杜苏达南
最佳答案
你的程序对我来说工作正常,给出输出:
1.000000 2.000000 3.000000 4.000000
您能否分享有关如何运行和编译可执行文件的更多信息,以便我尝试重现错误并编辑答案?
仅供引用,您可以在下面找到该程序的一个稍微修改的版本,它清楚地表明只能在根目录下分配接收缓冲区:
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
int main() {
int rank = -1;
int commsize = -1;
double sendbuffer[2];
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &commsize);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
sendbuffer[0]=2.0*rank;
sendbuffer[1]=2.0*rank + 1;
int count=2;
if(rank==0) {
// Recvbuffer is significant only at root
double * recvbuffer = malloc(2*commsize*sizeof(double));
// Gather values at root
MPI_Gather(sendbuffer,count,MPI_DOUBLE,recvbuffer,count,MPI_DOUBLE,0,MPI_COMM_WORLD);
// Print to screen
printf("\n");
for(int jj=0; jj<2*commsize ;jj++)
printf("%f\n",recvbuffer[jj]);
// Free recvbuffer
free(recvbuffer);
} else {
MPI_Gather(sendbuffer,count,MPI_DOUBLE,NULL,0,MPI_DOUBLE,0,MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
关于c - MPI_Gather 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702224/