[mpich-discuss] Stuck in a MPI program

Jim Dinan dinan at mcs.anl.gov
Thu Jan 5 16:38:35 CST 2012


Hello,

 From a quick glance at your code, it looks like you need to MPI_Wait() 
on the receive request before accessing the buffer.  Alternatively, it 
looks like you could use blocking MPI_Recv().  Same goes for the send 
(or see MPI_Test()).

  ~Jim.

On 1/5/12 4:28 PM, Long Li wrote:
>
> 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,
>
>
>
>
> #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;
> }
>
>
> _______________________________________________
> mpich-discuss mailing list     mpich-discuss at mcs.anl.gov
> To manage subscription options or unsubscribe:
> https://lists.mcs.anl.gov/mailman/listinfo/mpich-discuss


More information about the mpich-discuss mailing list