double*** esfld;
void allocate()
{ //... malloced all three dimensions of the esfld 3D array }
void initialize()
{ //... Initialize every element in all three dimensions of esfld to zero}
MPI_Datatype make3dtype()
{
int array_of_sizes[]={nz,ny,nx};
int array_of_subsizes[]={nz,ny,nx};
int array_of_starts[]={0,0,0};
MPI_Datatype arr3d;
int ndims=3;
MPI_Type_create_subarray(ndims,array_of_sizes,array_of_subsizes,array_of_starts,MPI_ORDER_C,MPI_DOUBLE,&arr3d);
MPI_Type_commit(&arr3d);
return arr3d;
}
void sendData()
{
MPI_Datatype arr3d=make3dtype();
MPI_Type_commit(&arr3d);
MPI_Send(esfld,1,arr3d,1,1,MPI_COMM_WORLD);
}
void receiveData()
{
MPI_Status status;
MPI_Datatype arr3d=make3dtype();
MPI_Type_commit(&arr3d);
MPI_Recv(esfld,1,arr3d,0,1,MPI_COMM_WORLD,&status);
}
int main()
{
MPI_Init(&argc,&argv);
allocate();
initialize();
int size,rank;
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
for(int i=0;i<5;i++){
if(rank==0){
sendData();
}
else if(rank==1){
receiveData();
}
}
MPI_Finalize();
return 0;
}
由于此代码而获得段错误。段错误不在分配或初始化函数中,我已经尝试过了。它是导致这种情况的 arr3d 类型的发送/接收。我究竟做错了什么 ?