我正在使用 MPI 进行并行 LU 分解,我从原始矩阵发送一堆连续的行,并在经过一些计算后检索它们。Scatter and Gather 工作正常,但我搞砸了并卡住了。我知道这是一个愚蠢的错误,但我看不出我搞砸了什么。这是代码,只要 scatter , collect 和验证它是否有效。
#include<stdio.h>
#include<stdlib.h>
#include "mpi.h"
int extractN(char *c);/*Helper to extract command line argument*/
int malloc2Ddouble(double ***array,int n, int m);/*Helper to allocate contiguous block of memory*/
int main(argc,argv)
int argc;
char *argv[];
{
int my_id;
int my_max;
int my_min;
int rows;
char hostname[32];
int len=32;
int n;
int procs;
double **a;
double **b;
double **c;
int i,j,k;
int ret;
n=extractN(argv[1]);
/*Intialize MPI Environment*/
ret=MPI_Init(&argc,&argv);
if(ret!=MPI_SUCCESS){
printf("Error in intializing MPI environment\n");
MPI_Abort(MPI_COMM_WORLD,ret);
exit(0);
}
MPI_Comm_size(MPI_COMM_WORLD,&procs);
MPI_Comm_rank(MPI_COMM_WORLD,&my_id);
MPI_Get_processor_name(hostname,&len);
rows=n/procs;
my_min=(my_id*rows);
my_max=my_min+(rows-1);
if(my_id==0){
ret=malloc2Ddouble(&a,n,n);
if(ret!=0){
printf("Error in allocating contiguous block of memory for matrix a\n");
MPI_Abort(MPI_COMM_WORLD,ret);
exit(0);
}
ret=malloc2Ddouble(&c,n,n);
if(ret!=0){
printf("Error in allocating contiguous block of memory for c\n");
MPI_Abort(MPI_COMM_WORLD,ret);
exit(0);
}
// for(i=0;i<n;i++)a[i]=(double *)malloc(sizeof(double)*n);
srand(1);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
a[i][j]=((rand()%10)+1);
}
}
}//end of master thread.
// b=(double **)malloc(sizeof(double *)*rows);// this doesn't work
// for(i=0;i<rows;i++)b[i]=(double *)malloc(sizeof(double)*n);
ret=malloc2Ddouble(&b,n,rows);
if(ret!=0){
printf("Error in allocating contiguous block of memory to array b \n");
MPI_Abort(MPI_COMM_WORLD,ret);
exit(0);
}
ret=MPI_Barrier(MPI_COMM_WORLD);
if(ret!=MPI_SUCCESS){
printf("Error in barrier point one \n");
MPI_Abort(MPI_COMM_WORLD,ret);
exit(0);
}
ret=MPI_Scatter(&(a[0][0]),(n*rows),MPI_DOUBLE, &(b[0][0]),(n*rows),MPI_DOUBLE,0,MPI_COMM_WORLD);
/*Scatter the blocks to slave threads*/
MPI_Barrier(MPI_COMM_WORLD);
MPI_Gather(&(b[0][0]),(n*rows),MPI_DOUBLE,&(c[0][0]),(n*rows),MPI_DOUBLE,0,MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
if(my_id==0){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%lf ",c[i][j]);
}
printf("\n");
}
}
MPI_Finalize();
return 0;
}
/*Helper function*/
int extractN(char *c){
int temp=0;
while((*c)!='\0'){
temp=temp*10 + ((*c)-48);
c=c++;
}
return temp;
}
int malloc2Ddouble(double ***array,int n, int m){
int i;
double *p=(double *)malloc(sizeof(double)*n*m);
if(p==NULL)return -1;
(*array)=(double **)malloc(sizeof(double *)*n);
if((*array)==NULL) return -1;
for(i=0;i<n;i++){
(*array)[i]=&(p[i*m]);
}
return 0;
}