MPI_COMM_SPLIT does not give a new reduced size

fortran mpi

126 观看

1回复

1 作者的声誉

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)

However, with size=8 and size1=6 the last call returns sizered=8 instead of 6! Where is my mistake?

作者: Sanders 的来源 发布者: 2017 年 9 月 15 日

回应 1


1

5423 作者的声誉

At first, you should try to build a Minimal, Complete, and Verifiable example that evidences the issue.

From MPI_Comm_split() man page

Synopsis

int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm *newcomm)

Input Parameters

  • 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 MPI_Comm_split()

MPI_COMM_SPLIT(MPI_COMM_WORLD,color,rank,REDCOMM,ierr)

use 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.

EDIT

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 MPI_COMM_NULL.

Note you cannot invoke MPI_Comm_size() on MPI_COMM_NULL.

The code would become

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)
作者: Gilles Gouaillardet 发布者: 2017 年 9 月 15 日
32x32