<div dir="ltr">Jed,<div><br></div><div>Thanks,</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 20, 2014 at 10:39 PM, Jed Brown <span dir="ltr"><<a href="mailto:jed@jedbrown.org" target="_blank">jed@jedbrown.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Fande Kong <<a href="mailto:fd.kong@siat.ac.cn">fd.kong@siat.ac.cn</a>> writes:<br>
<br>
> /*<br>
> * commproblem.cpp<br>
> *<br>
> * Created on: Mar 20, 2014<br>
> * Author: fdkong<br>
> */<br>
><br>
> #include <petscsnes.h><br>
> #include <petscdm.h><br>
> #include <petsc-private/snesimpl.h><br>
><br>
> static char help[] = "Need helps.\n\n";<br>
><br>
> #undef __FUNCT__<br>
> #define __FUNCT__ "main"<br>
> int main(int argc,char **argv)<br>
> {<br>
> Vec vec;<br>
> MPI_Request request;<br>
> MPI_Status status;<br>
> PetscMPIInt tag =123;<br>
> MPI_Comm comm;<br>
> PetscMPIInt rank, size;<br>
> PetscInt recv =10;<br>
> PetscInt send = 0;<br>
> DM dm;<br>
> PetscErrorCode ierr;<br>
><br>
> PetscInitialize(&argc,&argv,(char *)0,help);<br>
> comm = PETSC_COMM_WORLD;<br>
> ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);<br>
> ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);<br>
> // create an object<br>
> ierr = VecCreate(comm, &vec);CHKERRQ(ierr);<br>
> // take a comm from that object<br>
> comm = ((PetscObject) vec)->comm;<br>
> // if we set comm back to PETSC_COMM_WORLD, the code should work fine<br>
> //comm = PETSC_COMM_WORLD;<br>
> // receive messages from rank 0<br>
> if( rank!=0)<br>
> {<br>
> ierr = MPI_Irecv(&recv, 1, MPIU_INT, 0,tag, comm, &request);CHKERRQ(ierr);<br>
> }<br>
<br>
You have to wait on this request. MPI_Requests are not automatically<br>
collected at some point where you can prove that the operation has<br>
finished. The Wait should return immediately, but you have to call it.<br>
Otherwise MPI holds a reference to the communicator and will not call<br>
the destructors.<br>
<br>
> if(!rank)<br>
> {<br>
> //send messages to all others<br>
> for(PetscMPIInt i =1; i<size;i++)<br>
> {<br>
> ierr = MPI_Isend(&send, 1, MPIU_INT, i, tag, comm, &request);CHKERRQ(ierr);<br>
> }<br>
> }<br>
> // rank 0 doest not need to wait, it could continue to do other things.<br>
<br>
It *does* need to wait eventually.<br>
<br>
> if(rank !=0)<br>
> {<br>
> ierr = MPI_Waitall(1,&request, &status);CHKERRQ(ierr);<br>
> }<br>
> ierr = VecDestroy(&vec);CHKERRQ(ierr);<br>
> ierr = PetscFinalize();CHKERRQ(ierr);<br>
> }<br>
</blockquote></div><br></div>