jeffwang66 0 Newbie Poster

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.