Rookie HPC

Topology

C | FORTRAN-legacy | FORTRAN-2008

MPI_Comm_spawn

Definition

MPI_Comm_spawn is the means by which MPI processes can create siblings. The spawning processes and spawned processes reside in two different communicators, nonetheless they can communicate together via the intercommunicator returned. MPI_Comm_spawn is a collective operation; all processes in the communicator must call it.

Copy

Feedback

int MPI_Comm_spawn(const char* command,
                   char** arguments,
                   int max_process_number,
                   MPI_Info info,
                   int root,
                   MPI_Comm intercommunicator,
                   int* error_codes);

Parameters

command
The command to execute to spawn the new processes.
arguments
The array containing the strings to pass as arguments to the command. The list must be ended with a NULL element. An empty list is represented by MPI_ARGV_NULL.
max_process_number
The maximum number of MPI processes to spawn in total; not per spawning process. By default, the MPI application will try to spawn the number of processes given and fail if it cannot. However, that default behaviour can be overriden by the info parameter; in which case, on failure the MPI application will try to spawn fewer processes if possible, hence the notion of a “maximum number”.
info
A set of key-value pairs telling the runtime system where and how to start the processes. If no information is to be passed, MPI_INFO_NULL can be used.
root
Rank of the process from which read the argument list. The argument lists of other processes are ignored.
intercommunicator
The communicator containing that of spawning processes and that of spawned processes.
error_codes
The array in which write the error code (if any) for each process to spawn.

Returned value

The error code returned from the communicator spawning.

MPI_SUCCESS
The routine successfully completed.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to spawn MPI processes.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    MPI_Comm parent;
    MPI_Comm_get_parent(&parent);
    if(parent == MPI_COMM_NULL)
    {
        // We have no parent communicator so we have been spawned directly by the user
        MPI_Comm child;
        int spawn_error;
        printf("We are processes spawned directly by you, we now spawn a new instance of an MPI application.\n");
        MPI_Comm_spawn(argv[0], MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &child, &spawn_error);
    }
    else
    {
        // We have been spawned by another MPI process
        printf("I have been spawned by MPI processes.\n");
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}