这可能是一个非常愚蠢的问题,但我在这段代码中看不到我的错误......输出错误,排名 1 打印:
3.000000 - 3.000000 - 3.000000 - 3.000000 - 3.000000 - 3.000000 - 3.000000 -
3.000000 - 3.000000 - 3.000000 - 3.000000 - 3.000000 - 3.000000 -
3.000000 - 3.000000 - 3.000000 - 3.000000
- 3.000000 - 3.000000 - 3.000000 -
3.000000 - 0.000000 - 0.000000 - 0.000000 - 0.000000 -
我需要使用 MPI_Recv 而不是 Bcast ...但是发生了什么?=/这是我的malloc?或者我不能使用 MPI_Recv 发送整个矩阵吗?为什么整个数组不去另一个进程?
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char **argv){
int rank, size;
int lines, cols;
int i, j;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Status status;
lines = 5;
cols = 5;
if(rank == 0){
double** array = (double**) malloc(lines*sizeof(double*));
for(i=0; i<lines; i++)
array[i] = (double*) malloc(cols*sizeof(double));
for(i=0; i<lines; i++)
for(j=0; j<cols; j++)
array[i][j] = 3;
for(i=0; i<lines; i++){
for(j=0; j<cols; j++)
printf("%f - ", array[i][j]);
printf("\n");
}
MPI_Send(&array[0][0], lines*cols, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
}
else{
double** arrayNew = (double**)malloc(lines*sizeof(double*));
for (i=0; i<lines; i++)
arrayNew[i] = (double*) malloc(cols*sizeof(double));
MPI_Recv(&arrayNew[0][0], lines*cols, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
for(i=0; i<lines; i++){
for(j=0; j<cols; j++)
printf("%f - ", arrayNew[i][j]);
printf("\n");
}
}
MPI_Finalize();
}