I write some code like this:
void main(int argc, char **argv ) {
char message[20];
int i, rank, size, type=99;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0) {
strcpy_s(message, "Hello, world");
for (i=1; i<size; i++)
MPI_Send(message, 13, MPI_CHAR, i, type, MPI_COMM_WORLD);
} else {
MPI_Recv(message, 20, MPI_CHAR, 0, type, MPI_COMM_WORLD, &status);
}
printf( "Message from process =%d : %.13s\n", rank, message);
MPI_Finalize();
}
My output is random ordered. For example:
Message from process = 4 : Hello, world
Message from process = 2 : Hello, world
Message from process = 0 : Hello, world
Message from process = 1 : Hello, world
Message from process = 3 : Hello, world
But i want to do like this:
Message from process = 0 : Hello, world
Message from process = 1 : Hello, world
Message from process = 2 : Hello, world
Message from process = 3 : Hello, world
Message from process = 4 : Hello, world
I try some code, but still process random order.
I change my code. Now, every process wait its message from predecessor. But still processes outputs random ordered. I write MPI_Wait but it is not working.
if(rank == 0) {
strcpy_s(message, "Hello, world");
printf( "Message from process =%d : %.13s\n", rank, message);
MPI_Isend(message, 13, MPI_CHAR, rank + 1, type, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
} else {
MPI_Recv(message, 20, MPI_CHAR, (rank - 1) % size, type, MPI_COMM_WORLD, &status);
printf( "Message from process =%d : %.13s\n", rank, message);
if(rank < (size - 1)){
MPI_Isend(message, 13, MPI_CHAR, (rank + 1) % size, type, MPI_COMM_WORLD, &request);
MPI_Wait(&request, &status);
}
}
I hope some people help me. Thanks.