#include #include #include "mpi.h" #define NUM_SLAVES 4 /* create NUM_SLAVES processes */ #define SLAVE_PROG "spawn_slave" /* slave program name */ int main (int argc, char *argv[]) { MPI_Comm COMM_CHILD_PROCESSES; /* inter-communicator */ int ntasks_world, /* # of tasks in MPI_COMM_WORLD */ ntasks_local, /* COMM_CHILD_PROCESSES local */ ntasks_remote, /* COMM_CHILD_PROCESSES remote */ mytid, /* my task id */ namelen; /* length of processor name */ char processor_name[MPI_MAX_PROCESSOR_NAME]; MPI_Init (&argc, &argv); MPI_Comm_rank (MPI_COMM_WORLD, &mytid); MPI_Comm_size (MPI_COMM_WORLD, &ntasks_world); /* check that only the master process is running in MPI_COMM_WORLD. */ if (ntasks_world > 1) { if (mytid == 0) { fprintf (stderr, "\n\nError: Too many processes (only one " "process allowed).\n" "Usage:\n" " mpiexec %s\n\n", argv[0]); } MPI_Finalize (); exit (EXIT_SUCCESS); } MPI_Get_processor_name (processor_name, &namelen); printf ("\nParent process %d running on %s\n" " I create %d slave processes\n\n", mytid, processor_name, NUM_SLAVES); MPI_Comm_spawn (SLAVE_PROG, MPI_ARGV_NULL, NUM_SLAVES, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &COMM_CHILD_PROCESSES, MPI_ERRCODES_IGNORE); MPI_Comm_size (COMM_CHILD_PROCESSES, &ntasks_local); MPI_Comm_remote_size (COMM_CHILD_PROCESSES, &ntasks_remote); printf ("Parent process %d: " "tasks in MPI_COMM_WORLD: %d\n" " tasks in COMM_CHILD_PROCESSES local " "group: %d\n" " tasks in COMM_CHILD_PROCESSES remote " "group: %d\n\n", mytid, ntasks_world, ntasks_local, ntasks_remote); MPI_Comm_free (&COMM_CHILD_PROCESSES); MPI_Finalize (); return EXIT_SUCCESS; }