Thread: Stuck in a MPI program.

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Stuck in a MPI program.

    Hi,guys:

    I put 15 struct variables into a continuous memory at first by using memcpy(). And then I want to print out the element of first struct variable. The correct value is 1, but sometimes "0" is printed out. I do not know why. Can someone who is familar with C and MPI tell me the reason of that?


    Thanks,

    Code:
    #include <stdio.h>
    #include <mpi.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct message message;
    struct message
    {
      message*prev;
        message*next;
        int recv_pe;
    };
    
    //typedef struct message message;
    
    int main (int argc, char **argv)
    {
      int rank, size;
     
      int iter=10000;
      int number=15;
        
        int ierr,errclass;
      ierr=0; 
        MPI_Request request;
        MPI_Status status; 
        int send_buffer[5],recv_buffer[5];
      MPI_Init (&argc, &argv);    /* starts MPI */
      MPI_Comm_rank (MPI_COMM_WORLD, &rank);    /* get current process id */
      MPI_Comm_size (MPI_COMM_WORLD, &size);    /* get number of processes */
     
    // MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_RETURN); /* return info about
    //          errors */
    //    printf( "Hello world from process %d of %d\n", rank, size );
        if(rank==0)
        {  
            int kk;
            for(kk=0;kk<iter;kk++)
                    {
            int buffsize= number*sizeof(message);
                char*buffer=(char*)malloc(buffsize*sizeof(char));
           // void*buffer=(char*)malloc((unsigned)buffsize);
                int position=0;
                message* sendevent[number];
                int i;
                for(i=0,position=0;i<number;i++,position+=sizeof(message))
                {
                    sendevent[i]=malloc(sizeof(message));
                    sendevent[i]->recv_pe=i;
            memcpy(&buffer[position],sendevent[i],sizeof(message));
          }
                ierr= MPI_Isend(buffer,buffsize, MPI_BYTE,1, 1,MPI_COMM_WORLD,&request);         
                }
    
            }
        else
            {
              int kk;
                for(kk=0;kk<iter;kk++)
                {
            int buffsize=number*sizeof(message);
                char*buffer=(char*)malloc(sizeof(char)*buffsize);
                int position=0;
                message *tmp=NULL;
                int flag=0;
                    int i;
                    MPI_Request request;
                    MPI_Status status2;
                    while(flag==0)
                        {
                            MPI_Iprobe(MPI_ANY_SOURCE,
                            MPI_ANY_TAG,
                             MPI_COMM_WORLD,
                             &flag,
                             &status);
             }
    
             ierr=MPI_Irecv(buffer,buffsize,MPI_BYTE,0,1,MPI_COMM_WORLD,&request);                   
                    
                    for(i=0,position=0;i<number;i++,position+=sizeof(message))
                    {
                     tmp=(message*)&buffer[position];
                     if(i==1&&tmp->recv_pe==0)
                     printf("error :recv data invalid\n");
                    }
                }
            }
                    
      MPI_Finalize();
      return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I notice that you've declared a status2 in your else block, but use status instead in the MPI_Iprobe call.
    EDIT: although it doesn't look like that's the problem...
    Last edited by oogabooga; 01-05-2012 at 05:49 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You ignore the return values from MPI_Init, MPI_Comm_rank and MPI_Comm_size completely. You get the return value from MPI_Isend and MPI_Irecv, but never check to see if they're valid. You may be working with a totally invalid MPI setup.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Error has me stuck
    By Tester84 in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2010, 02:04 PM
  2. Stuck on a program
    By Northstar in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 03:02 AM
  3. Help, program stuck in a while loop
    By DSman in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:28 PM
  4. Interesting program, but stuck somewhere
    By Mahesh in forum C Programming
    Replies: 5
    Last Post: 04-27-2005, 09:43 AM
  5. Stuck On Program
    By Robert Parker in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2002, 01:41 PM