MPI_COMM_SPLIT does not give a new reduced size
I use MPI to parallellize my Fortran code.
MPI_Comm_reduce() is used to sum up the results from the different ranks. In some cases only a minor part of the available ranks are necessary for the computations and I therefore want to construct a communicator containing only the size1 necessary ranks,e.g. ranks 0,1,2,...,size1-1
IF(rank.LE.size1-1)THEN; color=1; ELSE; color=2; ENDIF key=0 CALL MPI_COMM_SPLIT(MPI_COMM_WORLD,1,key,REDCOMM,ierr) CALL MPI_COMM_SIZE(REDCOMM,sizered,ierr)
size1=6 the last call returns
sizered=8 instead of 6!
Where is my mistake?
At first, you should try to build a Minimal, Complete, and Verifiable example that evidences the issue.
MPI_Comm_split() man page
int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm *newcomm)
- comm communicator (handle)
- color control of subset assignment (nonnegative integer). Processes with the same color are in the same new communicator
- key control of rank assignment (integer)
so this is how you should invoke
color instead of hard coded
1, otherwise all MPI tasks end up in the same communicator. since you do not need to re-order ranks within the new communicator, you can also use
rank as the key.
Since you only care of the first ranks, you can use
MPI_UNDEFINED as a color for the highest rank. in this case, the highest rank will be in
Note you cannot invoke
The code would become
作者: Gilles Gouaillardet 发布者: 15.09.2017 05:42
IF(rank.LT.size1)THEN; color=0; ELSE; color=MPI_UNDEFINED; ENDIF key=rank CALL MPI_COMM_SPLIT(MPI_COMM_WORLD,color,key,REDCOMM,ierr) IF(rank.LT.size1) CALL MPI_COMM_SIZE(REDCOMM,sizered,ierr)