我使用MPI写我的第一个程序,我有困难时期试图发送正确使用MPI_Scatter数据到其它进程,修改它们和接收使用MPI_Gather值。 代码如下:
int** matrix;
int m = 2, n = 2;
int status;
// could have been int matrix[2][2];
matrix = malloc(m*sizeof(int*));
for(i = 0; i < m; i++) {
matrix[i] = malloc(n*sizeof(int));
}
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 2;
matrix[1][1] = 3;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
printf("My name is %d out of %d\n", rank, size);
MPI_Scatter(&matrix, 1, MPI_INT, &status, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("My name is %d and my status is %d\n", rank, status);
status += 1;
MPI_Gather(&status,1,MPI_INT,&matrix,1,MPI_INT,0,MPI_COMM_WORLD);
结果如下:
My name is 0 out of 4
My name is 1 out of 4
My name is 1 and my status is 0
My name is 2 out of 4
My name is 2 and my status is 120
My name is 0 and my status is 17773264
My name is 3 out of 4
My name is 3 and my status is 0
MPI_Gather后,当我尝试打印矩阵的内容,我得到段错误...
这是不是我所期望......我怀疑,这个问题是我有一个二维数组,并要送单值。 这是正确的吗? 如何纠正呢?