Win Than Aung wrote:
thanks for your reply jeffThere is only one receive, so it receives only one message. When you specify the element count for the receive, you're only specifying the size of the buffer into which the message will be received. Only after the message has been received can you inquire how big the message actually was. Here is an example: % cat a.c #include <stdio.h> #include <mpi.h> int main(int argc, char **argv) { int np, me, peer, value; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&np); MPI_Comm_rank(MPI_COMM_WORLD,&me); value = me * me + 1; if ( me == 0 ) { for ( peer = 0; peer < np; peer++ ) { if ( peer != 0 ) MPI_Recv(&value,1,MPI_INT,peer,343,MPI_COMM_WORLD,MPI_STATUS_IGNORE); printf("peer %d had value %d\n", peer, value); } } else MPI_Send(&value,1,MPI_INT,0,343,MPI_COMM_WORLD); MPI_Finalize(); return 0; } % mpirun -np 3 a.out peer 0 had value 1 peer 1 had value 2 peer 2 had value 5 % Alternatively, #include <stdio.h> #include <mpi.h> #define MAXNP 1024 int main(int argc, char **argv) { int np, me, peer, value, values[MAXNP]; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&np); if ( np > MAXNP ) MPI_Abort(MPI_COMM_WORLD,-1); MPI_Comm_rank(MPI_COMM_WORLD,&me); value = me * me + 1; MPI_Gather(&value, 1, MPI_INT, values, 1, MPI_INT, 0, MPI_COMM_WORLD); if ( me == 0 ) for ( peer = 0; peer < np; peer++ ) printf("peer %d had value %d\n", peer, values[peer]); MPI_Finalize(); return 0; } % mpirun -np 3 a.out peer 0 had value 1 peer 1 had value 2 peer 2 had value 5 % Which is better? Up to you. The collective routines (like MPI_Gather) do offer MPI implementors (like people developing Open MPI) the opportunity to perform special optimizations (e.g., gather using a binary tree instead of having the root process perform so many receives). |
- [OMPI users] sending message to the source(0) from other pro... Win Than Aung
- Re: [OMPI users] sending message to the source(0) from ... Eugene Loh
- Re: [OMPI users] sending message to the source(0) f... Win Than Aung
- Re: [OMPI users] sending message to the source(... Jeff Squyres
- Re: [OMPI users] sending message to the source(0) f... Win Than Aung
- Re: [OMPI users] sending message to the source(... Jeff Squyres
- Re: [OMPI users] sending message to the sou... Win Than Aung
- Re: [OMPI users] sending message to th... Eugene Loh
- Re: [OMPI users] sending message t... Win Than Aung
- Re: [OMPI users] sending message t... Win Than Aung
- Re: [OMPI users] sending messa... Win Than Aung
- Re: [OMPI users] sending messa... Jeff Squyres