vlxm
vlxm

Reputation: 68

MPI_Finalize hangs in dynamically spawned process

I have a first process (test1), which dynamically spawns a second one (test2) and then waits to receive a message with tag 1 from the spawned group. The spawned process performs some computation, then send a message with tag 1 to the parent group (which contains the first process) to say it has finished, and finally call MPI_Finalize. The first process receives correctly the message and continues, but the second hangs in MPI_Finalize. I don't understand what's preventing the second process from terminating.

Test 1

#include <stdio.h>
#include "mpi.h"

int main() {
int flag_mpi, erc, err_mpi, ndr;
MPI_Comm comm;

MPI_Initialized (&flag_mpi);
if (!flag_mpi) MPI_Init (NULL,NULL);

err_mpi = MPI_Comm_spawn("test2", MPI_ARGV_NULL, 1, MPI_INFO_NULL,
        0, MPI_COMM_WORLD, &comm, &erc);

MPI_Recv(&ndr, 1, MPI_INT, 0, 1, comm, MPI_STATUS_IGNORE);
printf("  Spawn_recv ok (ndr=%d)\n", ndr);

int i = 1;
while (i == 1) {  /* just to simulate further computation */
    printf(".");
    sleep(5);
}

MPI_Initialized (&flag_mpi);
if (flag_mpi) MPI_Finalize();
return 0;
}

Test 2

#include <stdio.h>
#include "mpi.h"

int main() {
int ndr;
MPI_Comm comm;
MPI_Init (NULL,NULL);

    /* some computation not involving MPI */

MPI_Comm_get_parent(&comm);

ndr = 1;
MPI_Send(&ndr, 1, MPI_INT, 0, 1, comm);
printf("  Spawn_sent ok (ndr=%d)\n", ndr);

MPI_Finalize();
return 0;
}

Upvotes: 0

Views: 1524

Answers (1)

francis
francis

Reputation: 9817

In your while loop, i does not change. This would explain that your code never ends. It seems that your code works with mpiexec -n 1 test1

If you use mpiexec -n 2 test1, two processes are created and processus 0 spawn a thrid one. The problem is that the second processus will wait for a message...

I suggest this modification :

 int rank;
 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
 if(rank==0){
     MPI_Recv(&ndr, 1, MPI_INT, 0, 1, comm, MPI_STATUS_IGNORE);
     printf("  Spawn_recv ok (ndr=%d)\n", ndr);
 }

And something like :

 int i = 30;
 while (i != 1) {  /* just to simulate further computation */
     printf(".");
     sleep(1);
     i--;
 }

EDIT : I misunderstood the question...So here is a better answer : you can add MPI_Comm_disconnect(&comm); in both test1.c and test2.c to close the connection between processes ! That way, the spawned process will not wait for the first one to pass MPI_Finalize()...

http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Comm_disconnect.html

Bye,

Francis

Upvotes: 0

Related Questions