Say, i have 8 processes. When i do the following, the MPU_COMM_WORLD communicator will be splitted into two communicators. The processes with even ids will belong to one communicator and the processes with odd ids will belong to another communicator.
color=myid % 2;
MPI_Comm_split(MPI_COMM_WORLD,color,myid,&NEW_COMM);
MPI_Comm_rank( NEW_COMM, &new_id);
My question is where is the handle for these two communicators. After the split the ids of processors which before were 0 1 2 3 4 5 6 7 will become 0 2 4 6 | 1 3 5 7.
Now, my question is: suppose I want to send and receive in a particular communicator, say the one hosting the even ids, then when i send a message from 0 to 2 using the wrong communicator the message could end up in the second communicator, is this correct? Thank you in advance for clarification!
if(new_id < 2){
MPI_Send(&my_num, 1, MPI_INT, 2 + new_id, 0, NEW_COMM);
MPI_Recv(&my_received, 1, MPI_INT, 2 + new_id, 0, NEW_COMM, MPI_STATUS_IGNORE);
}
else
{
MPI_Recv(&my_received, 1, MPI_INT, new_id - 2, 0, NEW_COMM, MPI_STATUS_IGNORE);
MPI_Send(&my_num, 1, MPI_INT, new_id - 2 , 0, NEW_COMM);
}
Full code
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
int main(argc,argv)
int argc;
char *argv[];
{
int myid, numprocs;
int color,Zero_one,new_id,new_nodes;
MPI_Comm NEW_COMM;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
int my_num, my_received;
int old_id;
switch(myid){
case 0:
my_num = 0;
old_id = 0;
break;
case 1:
my_num = 1;
old_id = 1;
break;
case 2:
my_num = 2;
old_id = 2;
break;
case 3:
my_num = 3;
old_id = 3;
break;
case 4:
my_num = 4;
old_id = 4;
break;
case 5:
my_num = 5;
old_id = 5;
break;
case 6:
my_num = 6;
old_id = 6;
break;
case 7:
my_num = 7;
old_id = 7;
break;
}
color=myid % 2;
MPI_Comm_split(MPI_COMM_WORLD,color,myid,&NEW_COMM);
MPI_Comm_rank( NEW_COMM, &new_id);
MPI_Comm_rank( NEW_COMM, &new_nodes);
// 0 1 2 3 4 5 6 7 //After splits we have these nums for 8 processors
// 2 3 0 1 6 7 4 5 //After the below exchange we should have this...each two elements in each communicator will exchange to next two elements in that same communicator
if(new_id < 2){
MPI_Send(&my_num, 1, MPI_INT, 2 + new_id, 0, NEW_COMM);
MPI_Recv(&my_received, 1, MPI_INT, 2 + new_id, 0, NEW_COMM, MPI_STATUS_IGNORE);
}
else
{
MPI_Recv(&my_received, 1, MPI_INT, new_id - 2, 0, NEW_COMM, MPI_STATUS_IGNORE);
MPI_Send(&my_num, 1, MPI_INT, new_id - 2 , 0, NEW_COMM);
}
printf("old_id= %d received num= %d\n", old_id, my_received);
MPI_Finalize();
}