我正在尝试使用 MPI_Scatter,发送矩阵行(动态分配),但它只发送一行,其他行是垃圾。当我使用静态内存分配时 - 一切都很好。
MPI_Init(&argc, &argv);
int **matrix, *matrix_stor, *row,rank, P;
MPI_Comm_size(MPI_COMM_WORLD, &P);
row = new int [P];
for(int i = 0; i < P; i++)
{
row[i] = 0;
}
matrix = new int *[P];
for(int i = 0; i < P; i ++)
matrix[i] = new int [P];
//int matrix[4][4], row[4], rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0){
for(int i = 0; i < P; i++){
for(int j = 0; j < P; j++){
matrix[i][j] = rand()%20;
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
cout << endl;
MPI_Scatter(&matrix[0][0], P, MPI_INT,&row[0], P, MPI_INT,0,MPI_COMM_WORLD);
for(int i = 0; i < P; i++)
cout << row[i] << " ";
cout << endl;
free(matrix);
free(row);
MPI_Finalize();
return 0;
结果是:
源矩阵:
1 7 14 0
9 4 18 18
2 4 5 5
1 7 1 11
接收行:
1 7 14 0
3626672 3626800 0 0
16 1 119 -33686019
-33686019 -572662307 524296 78676