c - 并非所有数组元素都发送

标签 c parallel-processing mpi

我创建程序将二维数组从从站发送到主站。代码如下:

int **hasil;
hasil=(int**)malloc(baris*sizeof(int));
for(t=0;t<baris;t++){
    hasil[t]=(int*)malloc(kolom*sizeof(int));
}

//some code to generate data

MPI_Type_contiguous(kolom, MPI_INT,&rowtype);
MPI_Type_commit(&rowtype);

if(rank==master){
    int **terima;
    int t,m,x;

    terima=(int**)malloc(baris*sizeof(int));
    for(t=0;t<baris;t++){
        terima[t]=(int*)malloc(kolom*sizeof(int));        
        for(m=0;m<kolom;m++){
            terima[t][m]=-1;
        }
    }
    for(x=1;x<numOfProc;x++){       
        MPI_Recv(&(terima[0][0]),baris,rowtype,x,99,MPI_COMM_WORLD,&status);
    }
} else {
    MPI_Send(&(hasil[0][0]),baris,rowtype,master,99,MPI_COMM_WORLD);
}

我不知道为什么不发送所有数组元素。

关于'hasil':

1 123 1234 1234 55345
2 123 1234 1234 12345
3 98765 1234 1234 12345
4 123 1234 1234 12345
5 123 1234 1234 12345
6 123 1234 1234 12345
7 123 1234 1234 12345
8 123 1234 1234 12345
9 123 1234 1234 12345
10 123 1234 1234 12345
11 123 1234 1234 12345

但在 'terima' 上:

1 123 1234 1234 55345
2 123 1234 1234 12345
3 98765 1234 1234 12345
4 123 1234 1234 12345
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1

有人知道我的代码有什么问题吗?请告诉我。

谢谢大家

最佳答案

我在您的 malloc 语句中看到一个明显的错误:

int **hasil;
hasil=(int**)malloc(baris*sizeof(int));

分配行时,基本上是分配指针,因此语句应如下所示:

int **hasil;
hasil=(int**)malloc(baris*sizeof(int *));

我在分配 terima 时看到同样的错误。

不确定这是否能解决您的问题,但这确实需要更正。请试试这个并告诉我。

关于c - 并非所有数组元素都发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252019/

相关文章:

c - 为嵌套循环声明虚拟变量的更好方法是什么?

performance - Kotlin:coroutineScope 比 GlobalScope 慢

R 并行包 - 在我的玩具示例中性能非常慢

c - 这个双重声明在 C 中是什么意思?

c - 指针数组的大小

c# - 并行任务的最大数量是否有一般规则?

python-3.x - 如何在 python 中使用 mpi4py 库连接收集的数据

计算C中耗时

asynchronous - openmpi 中的即时通信与同步通信

c - 为什么宏不能使用静态函数?