MPI_Scatter永远完成

时间:2013-10-08 10:28:54

标签: parallel-processing cluster-computing mpi openmpi

我正在使用MPI进行并行LU分解,其中我从原始矩阵发送一堆连续的行,并在稍后计算后检索它们。 Scatter and Gather工作正常,但我搞砸了。我知道这是一个愚蠢的错误,但我不知道弄清楚我搞砸了什么。这是代码,只是分散,收集和验证它是否有效。

#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;
}

0 个答案:

没有答案