3

I have the following MPI/fortran code to create two groups one which contains the first 2/3 of the total number of processors and the second which includes the rest 1/3. It compiles without problem but when I print out the new rank (in the recently created group), only the second group displays the correct ranks, the processes in the first group shows negative numbers.

Do you have any comment about this issue? Thanks.

program test

    implicit none
    include "mpif.h"
    integer, allocatable :: rs_use(:),ks_use(:)
    integer numnodes,myid,mpi_err
    integer ijk,new_group,old_group,num_used,used_id
    integer proc_rs,proc_ks
    integer RSPA_COMM_WORLD    !Real Space communicator
    integer KSPA_COMM_WORLD    !Recip. Space communicator

! initialize mpi
    call MPI_INIT( mpi_err )
    call MPI_COMM_SIZE( MPI_COMM_WORLD, numnodes, mpi_err )
    call MPI_Comm_rank(MPI_COMM_WORLD, myid, mpi_err)

    proc_rs = 2*numnodes/3            !Nr. of processors for Real Space
    proc_ks = numnodes - proc_rs      !Nr. of processors for Recip.  Space

    write(6,*) 'processors rs',proc_rs,'ks',proc_ks

! get our old group from MPI_COMM _WORLD
    call MPI_COMM_GROUP(MPI_COMM_WORLD,old_group,mpi_err)

! Real Space group  that will contain 2*N/3 processors
    allocate(rs_use(0:proc_rs-1))
    do ijk=0,proc_rs-1
        rs_use(ijk)=ijk
    enddo
    call MPI_GROUP_INCL(old_group,proc_rs,rs_use,new_group,mpi_err)
! create the new communicator
    call MPI_COMM_CREATE(MPI_COMM_WORLD,new_group,RSPA_COMM_WORLD, mpi_err)
! test to see if I am part of new_group.
    call MPI_GROUP_RANK(new_group,used_id, mpi_err)

! Recip.  Space group  that will contain N/3 processors
    allocate(ks_use(proc_rs:numnodes-1))
    do ijk=proc_rs,numnodes-1
        ks_use(ijk)=ijk
    enddo
    call MPI_GROUP_INCL(old_group,proc_ks,ks_use,new_group,mpi_err)
! create the new communicator
    call MPI_COMM_CREATE(MPI_COMM_WORLD,new_group,KSPA_COMM_WORLD, mpi_err)
! test to see if I am part of new_group.
    call MPI_GROUP_RANK(new_group,used_id, mpi_err)

    if(used_id==0) write(6,*) 'group ',used_id,myid


end program test
3
  • 2
    You don't test the success of any of your calls to MPI routines. Nor the success of the allocate statements. Why should I not conclude that one of those calls fails silently ? Jul 29, 2015 at 15:03
  • Hi, I tested both allocate and MPI routines, thanks for the suggestion. However, I still have the issue, used_id for the firs group prints out negative numbers.
    – armando
    Jul 29, 2015 at 15:45
  • @innoSPG has the correct answer, but I'll offer two more general suggestions: (1) In modern fortran, it is strongly suggested you use use mpi rather than include "mpif.h", and let the compiler find entire classes of potential problems for you at compile time. (2) in this case, where you're partitioning an existing communicator into two non-overlapping subsets, using MPI_Comm_split is easier. Jul 29, 2015 at 21:09

1 Answer 1

2

The problem is that only processes that belong to the group have an id in that group. What you have to do is to set new_group only in the appropriate group of processes and check the new id after you have include each process in its new group. For example, use a temp variable tmp_group to call MPI_COMM_CREATE and assign it only to the process of the group. For the first call to MPI_COMM_CREATE, you do this:

call MPI_GROUP_RANK(tmp_group,used_id, mpi_err)
if(myid<proc_rs) new_group = tmp_group

For the second call to MPI_COMM_CREATE, you do do this:

call MPI_GROUP_RANK(tmp_group,used_id, mpi_err)
if(myid>=proc_rs) new_group = tmp_group

After all of this, you can check the new rank for all:

call MPI_GROUP_RANK(new_group,used_id, mpi_err)

If you choose to check the rank in the group right after you create the group, make sure that only processes belonging to the group call. But this is not a good idea, as you will possibly not save the new_group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.