这可能是一个非常愚蠢的问题,但我看不到我的错误在此代码...输出是错误的,1个打印:MPI错误 - 发生了什么?
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 ... B发生了什么事? = /这是我的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();
}
哦!对,是真的!!我不记得那个,因为我习惯用[] ...创造完美答案! 非常感谢! = d – user2183693 2013-03-18 21:20:23