我对这段代码有点困惑。首先,我阅读了许多建议不要在 MPI 中发送指针的帖子。但我在这里发送一个,它正在工作。我对这段代码的另一个问题是它最多可以从其他进程写入 4 个字符来处理 0 的分配内存,就像在代码中一样。有人可以解释一下吗?
#include<mpi.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int MyRank, NumProcs, index, ret;
MPI_Status status;
MPI_File cFile;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&MyRank);
MPI_Comm_size(MPI_COMM_WORLD,&NumProcs);
//Allocated memory for a double pointer.
char **var;
MPI_Alloc_mem(sizeof(char *)*NumProcs, MPI_INFO_NULL, &var);
for(index = 0; index < NumProcs; index++)
MPI_Alloc_mem(sizeof(char)*50, MPI_INFO_NULL, &var[index]);
if(MyRank == 0)
{
//Copied data to the 0th position of the double pointer
strcpy(var[MyRank],"KKKKKK");
//Process 0 sent the array to other process to overwrite the data
for(index = 1; index < NumProcs; index++)
MPI_Send(var[index],1,MPI_INT,index,1,MPI_COMM_WORLD );
for(index = 1; index < NumProcs; index++)
MPI_Recv(var[index],1,MPI_INT,index,1,MPI_COMM_WORLD,&status);
}
else
{
//Overwrite of data is done.
MPI_Recv(var[MyRank],1,MPI_INT,0,1,MPI_COMM_WORLD,&status);
strcpy(var[MyRank],"MMMMMM");
MPI_Send(var[MyRank],1,MPI_INT,0,1,MPI_COMM_WORLD );
}
if(MyRank == 0)
{
//After writing data, printed in process 0;
printf("My Rank is -%d\n", MyRank);
for(index = 0; index < NumProcs; index++)
printf(" %s\n",var[index]);
}
MPI_Finalize();
return 0;
}