Search code examples
cmpiopenmpi

Problem with 2 static process which creates 2 dynamic process with open mpi


The program must create 2 static processes that we can call m0 and m1.

Each master process (m0 and m1) must create 2 child processes dynamically with mpi_spawn. The m0 master must make a bcast to all processes with a single call to bcast. I have no idea how I could solve it. The code below would be valid if for example each master makes a bcast to their own children, but cannot make them to the children of the other master. I guess I should create an intracom but I don't know how to do it

#include "mpi.h"
#include "stdio.h"
#define PROCESOS 2

int main(int argc, char *argv[])
{

    int rank, size, n = 1234, size_intercom, size_remote;
    MPI_Comm intercom, iguales;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    MPI_Comm_get_parent(&intercom); // Para conocer si tiene padre o no

    if (intercom == MPI_COMM_NULL)
    {
        MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, PROCESOS, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercom, MPI_ERRCODES_IGNORE);

        printf("soy maestro %d\n", rank);
        MPI_Comm_size(intercom, &size_intercom);
        MPI_Comm_remote_size(MPI_COMM_WORLD, &size_remote);
        printf("procesos en intercom %d, en remoto %d \n", size_intercom, size_remote);

        MPI_Bcast(&n, 1, MPI_INT, MPI_ROOT, intercom);
    }
    else
    {

        // MPI_Intercomm_merge(intercom, 1, &iguales);
        MPI_Bcast(&n, 1, MPI_INT, 0, intercom);
        printf("soy hijo %d, recibo %d\n", rank, n);
    }

    MPI_Finalize();
    return 0;
}

VERSION 2.0: I wonder if this can be an aproach, but i can't get the expected result:

 if (intercom == MPI_COMM_NULL)
    {
        if (rank == 0)
        {
            n = 1234;
        }
        MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, PROCESOS, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercom, MPI_ERRCODES_IGNORE);
        MPI_Intercomm_merge(intercom, 0, &iguales);
        MPI_Intercomm_create(MPI_COMM_SELF, 0, iguales, 1, 1, &todos);
        MPI_Bcast(&n, 1, MPI_INT, MPI_ROOT, todos);
        printf("I'M MASTER rank: %d, i get: %d\n", rank, n);
    }
    else
    {

        MPI_Intercomm_merge(intercom, 1, &iguales);
        MPI_Intercomm_create(MPI_COMM_WORLD, 0, iguales, 0, 1, &todos);

        MPI_Bcast(&n, 1, MPI_INT, 0, todos);
        printf("I'm a child %d, i get: %d\n", rank, n);
    }

With the previous modification i get the same result that if i do the bcast through de intercom comnunicator intercom. I don't understand why:

I'M MASTER rank: 1, i get: 0
I'm a child 0, i get: 0
I'm a child 1, i get: 0
I'M MASTER rank: 0, i get: 1234
I'm a child 1, i get: 1234
I'm a child 0, i get: 1234

Solution

  • If you cannot revamp your app so the two masters spawn the four slaves together, or you cannot use a two steps broadcast, here is how you can achieve the desired outcome.

    #include "mpi.h"
    #include "stdio.h"
    #define PROCESSES 2
    
    int main(int argc, char *argv[])
    {
    
        int rank, size, n = -1, remote_leader = -1;
        int master;
        MPI_Comm intercomm, mergedcomm, universe_inter, universe;
        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        MPI_Comm_size(MPI_COMM_WORLD, &size);
    
        MPI_Comm_get_parent(&intercomm);
    
        if (MPI_COMM_NULL == intercomm)
        {
            printf("I am master %d\n", rank);
            MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, PROCESSES, MPI_INFO_NULL, 0, MPI_COMM_SELF, &intercomm, MPI_ERRCODES_IGNORE);
            MPI_Bcast(&rank, 1, MPI_INT, MPI_ROOT, intercomm);
            master = rank;
            remote_leader = master^1;
            if (0 == rank) {
                n = 1234;
            }
        }
        else
        {
            MPI_Bcast(&master, 1, MPI_INT, 0, intercomm);
            printf("I am slave %d from master %d\n", rank, master);
        }
    
        /* make sure the master rank has rank 0 in the merged communicator */
        MPI_Intercomm_merge(intercomm, (NULL==MPI_COMM_NULL)?0:1, &mergedcomm);
        MPI_Intercomm_create(mergedcomm, 0, MPI_COMM_WORLD, remote_leader, 0, &universe_inter);
        /* make sure master 0 has rank 0 in the universe */
        MPI_Intercomm_merge(universe_inter, master, &universe);
        MPI_Comm_free(&mergedcomm);
    
        MPI_Bcast(&n, 1, MPI_INT, 0, universe);
        printf ("n = %d\n", n);
    
        MPI_Comm_free(&universe);
        MPI_Comm_disconnect(&intercomm);
        MPI_Finalize();
        return 0;
    }