<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><br> Hi,guys:<br><br> 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?<br><br> Thanks,<br></div><div><br></div><div><br></div><div><br></div><br>#include <stdio.h><br>#include <mpi.h><br>#include <stdlib.h><br>#include <string.h><br>typedef struct message message;<br>struct message<br>{<br> message*prev;<br> message*next;<br> int recv_pe;<br>};<br><br>//typedef struct message message;<br><br>int main (int argc, char **argv)<br>{<br> int rank, size;<br> <br> int iter=10000;<br> int
number=15;<br> <br> int ierr,errclass;<br> ierr=0; <br> MPI_Request request;<br> MPI_Status status; <br> int send_buffer[5],recv_buffer[5];<br> MPI_Init (&argc, &argv); /* starts MPI */<br> MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */<br> MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */<br> <br>// MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_RETURN); /* return info about<br>// errors */<br>// printf( "Hello world from process %d of %d\n", rank, size );<br> if(rank==0)<br> { <br> int kk;<br> for(kk=0;kk<iter;kk++)<br>
{<br> int buffsize= number*sizeof(message);<br> char*buffer=(char*)malloc(buffsize*sizeof(char));<br> // void*buffer=(char*)malloc((unsigned)buffsize);<br> int position=0;<br> message* sendevent[number];<br> int i;<br> for(i=0,position=0;i<number;i++,position+=sizeof(message))<br> {<br> sendevent[i]=malloc(sizeof(message));<br>
sendevent[i]->recv_pe=i;<br> memcpy(&buffer[position],sendevent[i],sizeof(message));<br> }<br> ierr= MPI_Isend(buffer,buffsize, MPI_BYTE,1, 1,MPI_COMM_WORLD,&request); <br> }<br><br> }<br> else<br> {<br> int kk;<br> for(kk=0;kk<iter;kk++)<br> {<br> int buffsize=number*sizeof(message);<br> char*buffer=(char*)malloc(sizeof(char)*buffsize);<br>
int position=0;<br> message *tmp=NULL;<br> int flag=0;<br> int i;<br> MPI_Request request;<br> MPI_Status status2;<br> while(flag==0)<br> {<br> MPI_Iprobe(MPI_ANY_SOURCE,<br> MPI_ANY_TAG,<br>
MPI_COMM_WORLD,<br> &flag,<br> &status);<br> }<br><br> ierr=MPI_Irecv(buffer,buffsize,MPI_BYTE,0,1,MPI_COMM_WORLD,&request); <br> <br> for(i=0,position=0;i<number;i++,position+=sizeof(message))<br>
{<br> tmp=(message*)&buffer[position];<br> if(i==1&&tmp->recv_pe==0)<br> printf("error :recv data invalid\n");<br> }<br> }<br> }<br> <br> MPI_Finalize();<br> return 0;<br>}</div></body></html>