0

I am working on parallel LU decomposition using MPI, where I send bunch of contiguous rows from original matrix and retrieve them later after some computation. Scatter and Gather was working fine but I messed up and stuck. I know its is some silly mistake but I coudn't see figure out what I messed. Here is the code, just scatter , gather and verification if it worked or not.

#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;
}
  • This could be one possible reason: the processor name buffer that gets passed to `MPI_Get_processor_name` should be able to hold at least `MPI_MAX_PROCESSOR_NAME` characters. You only provide one that could hold 32 characters and if the hostname is longer, it will overwrite the value of other local variables (which one depends on how the compiler lays out the stack frame). Also the `resultlen` parameter is output-only - setting it to 32 does not limit the amount of data that MPI is going to write to the string buffer. – Hristo Iliev Oct 08 '13 at 11:46
  • @HristoIliev Thanks but, thats not the error. It is in gather step. I am printing the scattered elements. It works fine. But if I uncomment the gather step the program runs forever. – user2778110 Oct 08 '13 at 22:52

0 Answers0