With following code I am splitting 4 processes in column groups, then broadcasting in same column from diagonal (0,3). Process 0 broadcasts to 2. And 3 should broadcast to 1. But it is not working as expected. Can some one see whats wrong ?
0 1
2 3
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <mpi.h>
#include <mpi.h>
using namespace std;
int main(int argc, char **argv){
MPI_Comm col_comm,row_comm;
int myrank, size, even, value=0;
int localRank=0;
MPI_Init (&argc, &argv);
MPI_Comm_rank (MPI_COMM_WORLD, &myrank);
MPI_Comm_size (MPI_COMM_WORLD, &size);
MPI_Comm_split(MPI_COMM_WORLD, myrank%2, myrank, &col_comm);
MPI_Comm_rank (col_comm, &localRank);
if(myrank%3==0){
value = myrank*5+1;
MPI_Bcast(&value, 1, MPI_INT, localRank, col_comm);
}
printf("Rank=%d | LocalRank=%d | Got broadcast value of %d\n", myrank, localRank, value);
MPI_Finalize();
return 0;
}
The Output
ubuntu@root:~/matrixmult$ mpirun comtest -np 4
Rank=0 | LocalRank=0 | Got broadcast value of 1
Rank=1 | LocalRank=0 | Got broadcast value of 0
Rank=2 | LocalRank=1 | Got broadcast value of 0
Rank=3 | LocalRank=1 | Got broadcast value of 16