MPI_Bcast一个动态二维数组 [英] MPI_Bcast a dynamic 2d array
问题描述
我试图用通过动态二维数组BCAST所有的行列。
我有以下的code。
I am trying to pass a dynamic 2d array with bcast to all ranks. I have the following code.
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char **argv)
{
float **array;
int rank,size,i,j;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank==0)
{
array = (float **)malloc(10*sizeof(float));
for(i=0;i<10;i++)
array[i] = (float *)malloc(10*sizeof(float));
for(i=0;i<10;i++)
for(j=0;j<10;j++)
array[i][j]=i+j;
}
MPI_Bcast(array,10*10,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Finalize();
}
出于某种原因,我无法理解我得到分段错误。
任何人都知道这是什么问题?
For some reason i cant understand i get segmentation fault. Anyone that knows what is the problem?
推荐答案
的阵列
应该是100,而不是10,因为你每次分配每行10彩车。 JackN的回答有code做到这一点。
The array
should be 100 rather than 10, since you assign 10 floats per each row. JackN's answer has the code to do this.
不过,比排名0以外的任何过程中,指针数组将空
。
你需要初始化所有进程的数组,然后填写根的数组。
However, on any process other than rank 0, the pointer to array will be null
.
You need to initialise array on all processes, then fill the array on the root.
您只需移动的malloc code出如果(排名== 0)
块,它应该工作你如何期待。
You can just move the malloc code out of the if (rank ==0)
block and it should work how you expect.
这篇关于MPI_Bcast一个动态二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!