user1715985
user1715985

Reputation: 11

MPI_Gather does not seem to work

I needed to use the MPI_Gather function in my Matrix Multiplication program but am facing trouble with it for the last couple of days.

Hence I wrote a simple MPI program using the gather function alone and have been trying to get it to run... For this I referred to the book 'Introduction to Parallel Programming by Peter Pacheco'.

The program directly exits, giving me absolutely no results or errors... I have not been able to figure out the mistake till now.

   /******************NOTE**********************

      The program simply uses the MPI_Gather() function. 
      The program is exiting directly.
      I have written it for only TWO processes.
      (-np 2)

   ******************************************/

    #include<stdio.h>
    #include"mpi.h"

    int main()
    {
     int i,j,proc,rank;
     double d[4];
     double local_a[2];


     MPI_Init(NULL,NULL);
     MPI_Comm_size(MPI_COMM_WORLD, &proc);
     MPI_Comm_rank(MPI_COMM_WORLD, &rank);

     if(rank==0)
     { 

       local_a[0]=1.0;
       local_a[1]=2.0;
     }

     else
     {
       local_a[0]=3.0;
       local_a[1]=4.0;
     }

     int local=2;

     if(rank==0)
     {   

       MPI_Gather(local_a,local,MPI_DOUBLE,d,local,MPI_DOUBLE,0,MPI_COMM_WORLD);
     //MPI_Gather(&local_a,local,MPI_DOUBLE,&d,local,MPI_DOUBLE,0,MPI_COMM_WORLD);
     //also tried the above line just to be certain.


       printf("\n");
       for(j=0;j<4;j++)
         printf("\t%f",d[j]);
     }
     else
     {
       MPI_Gather(local_a,local,MPI_DOUBLE,d,local,MPI_DOUBLE,0,MPI_COMM_WORLD);
     //MPI_Gather(&local_a,local,MPI_DOUBLE,&d,local,MPI_DOUBLE,0,MPI_COMM_WORLD); 
     }

     MPI_Finalize();

     return 0;

    }

Can anyone please help me out.

Thank You.

Anagha Madhusudanan

Upvotes: 1

Views: 2139

Answers (1)

Massimiliano
Massimiliano

Reputation: 8032

Your program works fine for me, giving as output:

    1.000000    2.000000    3.000000    4.000000

Can you share more information on how you run and compile the executable, so that I will try to reproduce the error and in case edit the answer?

Just for your information, you can find below a slightly modified version of the program that makes evident that the receive buffer can be allocated only at root:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main() {

  int rank     = -1;
  int commsize = -1;
  double sendbuffer[2];

  MPI_Init(NULL,NULL);
  MPI_Comm_size(MPI_COMM_WORLD, &commsize);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  sendbuffer[0]=2.0*rank;
  sendbuffer[1]=2.0*rank + 1;

  int count=2;

  if(rank==0) {   

    // Recvbuffer is significant only at root
    double * recvbuffer = malloc(2*commsize*sizeof(double));
    // Gather values at root
    MPI_Gather(sendbuffer,count,MPI_DOUBLE,recvbuffer,count,MPI_DOUBLE,0,MPI_COMM_WORLD);
    // Print to screen
    printf("\n");
    for(int jj=0; jj<2*commsize ;jj++)
      printf("%f\n",recvbuffer[jj]);
    // Free recvbuffer
    free(recvbuffer);

  } else {
    MPI_Gather(sendbuffer,count,MPI_DOUBLE,NULL,0,MPI_DOUBLE,0,MPI_COMM_WORLD);
  }

  MPI_Finalize();

  return 0;

}

Upvotes: 2

Related Questions