Siva Srinivas Kolukula
Siva Srinivas Kolukula

Reputation: 1251

use mpi_cart_create with master and slave

I want to create a topology without 0 processor in it. My idea is to make processor 0 as master and create topology as slave. After certain computation sin topology, I will send data to master. Here is my code:

      include "mpif.h"

      integer   maxn

      integer   myid,Root,numprocs,numtasks,taskid
      integer   comm2d, ierr
      integer   dims(2)
      logical   periods(2)
      data periods/2*.false./
    Root = 0
      CALL MPI_INIT( ierr )
      CALL MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr )
      CALL MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr )
      numtasks = numprocs-1
      if(myid .eq. Root) then
      print *, 'Hello I am master'
      endif
c  Get a new communicator for a decomposition of the domain.  
c  Let MPI find a "good" decomposition
      dims(1) = 0
      dims(2) = 0
      CALL MPI_DIMS_CREATE(numtasks,2,dims,ierr)
      CALL MPI_CART_CREATE(MPI_COMM_WORLD,2,dims,periods,.true.,
     *                    comm2d,ierr)
c   Get my position in this communicator
      CALL MPI_COMM_RANK( comm2d, taskid, ierr )        
c
    print *, 'task ID= ',taskid
    if (myid .eq. master) then
        print *,dims(1),dims(2)
    endif
      CALL MPI_Comm_free( comm2d, ierr )

30    CALL MPI_FINALIZE(ierr)

      STOP
      END

But, when I run above code; I am getting the following error.

Fatal error in PMPI_Comm_rank: Invalid communicator, error stack: PMPI_Comm_rank(121): MPI_Comm_rank(MPI_COMM_NULL, rank=0x7fff08bf960c) failed PMPI_Comm_rank(73).: Null communicator

How can I eliminate the error? What I am doing wrong.

Upvotes: 0

Views: 608

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74395

You start your MPI job with numprocs processes. Then you create a Cartesian topology with numtasks = numprocs-1 processes. Therefore, one of the processes ends up not being part of the Cartesian communicator and receives MPI_COMM_NULL in comm2d. Calling MPI_COMM_RANK with a null communicator is an error. The solution is to fix your code to first check the value of comm2d:

 CALL MPI_CART_CREATE(MPI_COMM_WORLD,2,dims,periods,.true.,
*                     comm2d,ierr)
 IF (comm2d.NE.MPI_COMM_NULL) THEN
   ...
 ENDIF

Upvotes: 2

Related Questions