I got the solution. I just need to set the appropriate tag to send and receive.
thanks Eugene for your example, it helps me a lot.I bump into one more problemslets say I have the file content as followI have total of six files which all contain real and imaginary value."1.001212 1.0012121 //0th1.001212 1.0012121 //1st1.001212 1.0012121 //2nd1.001212 1.0012121 //3rd1.001212 1.0012121 //4th
1.001212 1.0012121 //5th1.001212 1.0012121 //6th"char send_buffer[1000];i use "mpirun -np 6 a.out" which mean i let each processor get access to one fileeach processor will add "0th and 2nd"(even values) (those values will be sent to root processor and save as file_even_add.dat" and also each processor will add "1st and 3rd"(odd values) (those values will be sent to root processor(here is 0) and saved as "file_odd_add.dat".char recv_buffer[1000];File* file_even_dat;File* file_odd_dat;if(mpi_my_id == root){filepteven = fopen("C:\\fileeven.dat");fileptodd = fopen("C:\\fileodd.dat");int peer =0;for(peer =0;peer<mpi_total_size;peer++){if(peer!=root){MPI_Recv(recv_buffer,MAX_STR_LEN,MPI_BYTE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);}fprintf(filepteven, "%s \n" ,recv_buffer);}}My question is how do i know which sentbuffer has even add values and which sentbuffer has odd add values? in which order did they get sent?thankswinthanOn Tue, Dec 23, 2008 at 3:53 PM, Eugene Loh <Eugene.Loh@sun.com> wrote:
Win Than Aung wrote:There 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.thanks for your reply jeff
so i tried following
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv) {
int np, me, sbuf = -1, rbuf = -2,mbuf=1000;
int data[2];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&np);
MPI_Comm_rank(MPI_COMM_WORLD,&me);
if ( np < 2 ) MPI_Abort(MPI_COMM_WORLD,-1);
if ( me == 1 ) MPI_Send(&sbuf,1,MPI_INT,0,344,MPI_COMM_WORLD);
if(me==2) MPI_Send( &mbuf,1,MPI_INT,0,344,MPI_COMM_WORLD);if ( me == 0 ) {
MPI_Recv(data,2,MPI_INT,MPI_ANY_SOURCE,344,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}
MPI_Finalize();
return 0;
}
it can successfuly receive the one sent from processor 1(me==1) but it failed to receive the one sent from processor 2(me==2)mpirun -np 3 hello
Here is an example:int np, me, peer, value;
% cat a.c
#include <stdio.h>
#include <mpi.h>
int main(int argc, char **argv) {value = me * me + 1;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&np);
MPI_Comm_rank(MPI_COMM_WORLD,&me);
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,#define MAXNP 1024
#include <stdio.h>
#include <mpi.h>
int np, me, peer, value, values[MAXNP];
int main(int argc, char **argv) {if ( np > MAXNP ) MPI_Abort(MPI_COMM_WORLD,-1);
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&np);value = me * me + 1;
MPI_Comm_rank(MPI_COMM_WORLD,&me);
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).
_______________________________________________
users mailing list
users@open-mpi.org
http://www.open-mpi.org/mailman/listinfo.cgi/users