1 year ago
#387302
user9990604
How to update message without using if statement?
I have a number of processors, let's say, 9, that are arange like a ring together. So, the processors communicating with each other in a ring and in a non-blocking setting MPI_Isend() and MPI_Irecv()
. And the task is to recieve the rank of previous proccessor and add that to its own rank, and then pass it to its neighor. This continues until reaching to the processor '0' again. Then processor '0' prints the sum which is n(n+1)/2 ( in this case 45). I know that these non-blicking function return immediately even if the communication is not finished, and MPI_Wait()
is needed to ensure the completion of the communication. And I know that it's better to have a buffer of size 2 to store the rank and sum. But I don,t know how and when to update the message before sending it to the next rank?
I don't want to use if statemet. Lik if(rank==0)
then send to 1
and add then if(rank==1)
receive from 0
and then add 1
and then send to 2
,... Since this one is highly inefficient for larg number of processor.
int main (int argc, char *argv[])
{
int size, rank, next, prev;
int buf[2],
MPI_Request reqs[9];
MPI_Status stats[9];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
prev = rank-1;
next = rank+1;
if (rank == 0) prev = size - 1;
if (rank == (size - 1)) next = 0;
//MPI_Irecv (&buf,count,datatype,source,tag,comm,&request)
ierror = MPI_Irecv(&buf[0], 1, MPI_INT, prev, tag1, MPI_COMM_WORLD, &reqs[0]);
ierror = MPI_Irecv(&buf[1], 1, MPI_INT, next, tag2, MPI_COMM_WORLD, &reqs[1]);
//MPI_Isend (&buf,count,datatype,dest,tag,comm,&request)
ierror = MPI_Isend(&buf[0], 1, MPI_INT, prev, tag2, MPI_COMM_WORLD, &reqs[2]);
ierror = MPI_Isend(&buf[1], 1, MPI_INT, next, tag1, MPI_COMM_WORLD, &reqs[3]);
ierror = MPI_Waitall(9, reqs, stats);
mpi
hpc
0 Answers
Your Answer