MPI_Bcast一个动态二维数组 [英] MPI_Bcast a dynamic 2d array

查看:1125
本文介绍了MPI_Bcast一个动态二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用通过动态二维数组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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆