Addressing Memory in MPI_Gather C

隐身守侯 提交于 2019-12-13 08:10:40

问题


I am trying to pass on data to MPI_Gather. I allocate memory as follows:

float *phie, *phitemp;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

phitemp=(float *) malloc(20*sizeof(float));
if (rank==1) phie=(float *) malloc(itermax*20*size*sizeof(float));

and then get all processes to send data to rank 1 using MPI_Gather() as below:

for (iter=0;iter<itermax;iter++) {    
   MPI_Gather((float *) phitemp, 20, MPI_FLOAT, (float *) (phie+iter*20*size*sizeof(float)), 20, MPI_FLOAT, 1, MPI_COMM_WORLD);
   iter=0;

}

I get error messages suggesting I am not allocating memory appropriately.


回答1:


Pointer arithmetic is done on the size of word the pointer is pointing to. Since phie is a float *, the sizeof(float) in phie+iter*20*size*sizeof(float) is redundant and you are going outside of the valid memory.

Remove the sizeof(float), or better change it to a clearer array indexing: &(phie[iter * 20 * size])

You should also remove all the pointer casts, they are all redundant and can hide issues. You should only cast when you know that you need it.

Regarding your additional question: The recvbuf parameter of MPI_Gather is only significant at the root process. Therefore only the iter of the root process matters. Nevertheless since all your processes go through the same loop of running MPI_Gather, and they cannot overtake each other, they will always have the same iter during matching gathers.



来源:https://stackoverflow.com/questions/41985758/addressing-memory-in-mpi-gather-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!