Note the use of MPI_Cancel to remove the MPI_Irecv requests that are begun before the synchronization point (the MPI_Allreduce) but never used because the loop is exited.

This approach allows the underlying system to use eager protocol independent of the size of the message.