LAM/MPI logo

LAM/MPI General User's Mailing List Archives

  |   Home   |   Download   |   Documentation   |   FAQ   |   all just in this list

From: Jonathan Eckstein (jeckstei_at_[hidden])
Date: 2004-10-11 17:32:31


Hi --

Could somebody explain why the following program hangs? I am running
LAM 7.0.3 on a dual-processor xeon box with RedHat 9. If I run it
simulating parallelism with more than 2 processes, it hangs. I know
there are more efficient ways to implement what the program is doing,
but it distills the nature of something that is messing up a much more
complex application. I have been coding in MPI since 1996, although
mostly in a more asynchronous mode with Isends and Irecvs. It seems to
me this program should work. I tried putting a barrier between the
"gather" and "scatter" phases, but that did *not* help! Perhaps my
brain is just locked -- anything to clear the fog would be appreciated!

    -- (Professor) Jonathan Eckstein


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

int main(int argc, char** argv)
{
  int r,s,t,u,v;
  MPI_Status status;

  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD,&r);
  MPI_Comm_size(MPI_COMM_WORLD,&s);
  t = 0;
  if (r==0)
    {
      for(u=1; u<s; u++)
        {
          MPI_Recv(&v,1,MPI_INT,MPI_ANY_SOURCE,1,MPI_COMM_WORLD,&status);
          printf("Got message from %d\n",status.MPI_SOURCE);
          t += v;
        }
      printf("Total=%d\n",t);
      for(u=1; u<s; u++)
        {
          printf("Root sending to %d\n",u);
          MPI_Send(&t,1,MPI_INT,1,2,MPI_COMM_WORLD);
        }
    }
  else
    {
      printf("Processor %d sending...\n",r);
      MPI_Send(&r,1,MPI_INT,0,1,MPI_COMM_WORLD);
      MPI_Recv(&t,1,MPI_INT,0,2,MPI_COMM_WORLD,&status);
      printf("Processor %d got %d\n",r,t);
    }

  MPI_Finalize();
  return 0;
}