While MPI_COMM_SPAWN is sufficient for most cases, it does not allow the spawning of multiple binaries, or of the same binary with multiple sets of arguments. The following routine spawns multiple binaries or the same binary with multiple sets of arguments, establishing communication with them and placing them in the same MPI_COMM_WORLD.
MPI_COMM_SPAWN_MULTIPLE(count, array_of_commands, array_of_argv, array_of_maxprocs, array_of_info, root, comm, intercomm, array_of_errcodes) | |
IN count | number of commands (positive integer, significant only at root) |
IN array_of_commands | programs to be executed (array of strings, significant only at root) |
IN array_of_argv | arguments for commands (array of array of strings, significant only at root) |
IN array_of_maxprocs | maximum number of processes to start for each command (array of integers, significant only at root) |
IN array_of_info | info objects telling the runtime system where and how to start processes (array of handles, significant only at root) |
IN root | rank of process in which previous arguments are examined (integer) |
IN comm | intra-communicator containing group of spawning processes (handle) |
OUT intercomm | inter-communicator between original group and the newly spawned group (handle) |
OUT array_of_errcodes | one error code per process (array of integers) |
MPI_COMM_SPAWN_MULTIPLE is identical to MPI_COMM_SPAWN except
that there are multiple executable specifications. The first argument,
count, gives the number of specifications. Each of the
next four arguments are simply arrays of the corresponding arguments
in MPI_COMM_SPAWN.
For the Fortran version of array_of_argv, the element
array_of_argv(i,j) is the
j-th
argument to command number i.
Rationale.
This may seem backwards to
Fortran programmers who are familiar with Fortran's
column-major ordering. However, it is necessary to do
it this way to allow MPI_COMM_SPAWN to sort out
arguments. Note that the leading dimension of
array_of_argv must be the same as
count.
Also note that Fortran rules for sequence association allow a different
value in the first dimension; in this case, the sequence of array elements
is interpreted by MPI_COMM_SPAWN_MULTIPLE as if the sequence
is stored in an array defined with the first dimension set to count.
This Fortran feature allows an implementor to define MPI_ARGVS_NULL
(see below) with fixed dimensions, e.g., (1,1), or only with one dimension, e.g., (1).
( End of rationale.)
Advice to users.
The argument count is interpreted by MPI only at the root,
as is array_of_argv. Since the leading dimension
of array_of_argv is count, a nonpositive
value of count at a nonroot node could theoretically
cause a runtime bounds check error, even though
array_of_argv should be ignored by the
subroutine. If this happens, you should explicitly
supply a reasonable value of count on the nonroot
nodes.
( End of advice to users.)
In any language, an application may use the constant
MPI_ARGVS_NULL (which is likely to be (char ***)0 in C)
to specify that no arguments should be passed to any
commands. The effect of setting individual elements of array_of_argv
to MPI_ARGV_NULL is not defined. To specify arguments
for some commands but not others, the commands without
arguments should have a corresponding argv whose
first element is null ((char *)0 in C and empty string in Fortran).
In Fortran at nonroot processes, the count argument must
be set to a value that is consistent with the provided
array_of_argv although the content of these arguments
has no meaning for this operation.
All of the spawned processes have the same MPI_COMM_WORLD. Their ranks in MPI_COMM_WORLD correspond directly to the order in which the commands are specified in MPI_COMM_SPAWN_MULTIPLE. Assume that m1 MPI processes are generated by the first command, m2 by the second, etc. The MPI processes corresponding to the first command have ranks 0, 1, ..., m1-1 in MPI_COMM_WORLD. The MPI processes in the second command have ranks m1, m1+1, ..., m1+m2-1 in MPI_COMM_WORLD. The MPI processes in the third have ranks m1+m2, m1+m2+1, ..., m1+m2+m3-1 in MPI_COMM_WORLD, etc.
Advice to users.
Calling MPI_COMM_SPAWN multiple times
would create many sets of children with different MPI_COMM_WORLDs
whereas MPI_COMM_SPAWN_MULTIPLE creates children with
a single MPI_COMM_WORLD, so the two methods
are not completely equivalent.
There are also two performance-related reasons why,
if you need to spawn multiple executables, you may want to use
MPI_COMM_SPAWN_MULTIPLE instead of calling MPI_COMM_SPAWN
several times. First, spawning several
things at once may be faster than spawning them sequentially. Second,
in some implementations,
communication between processes spawned at the same time may be
faster than communication between processes spawned separately.
( End of advice to users.)
The array_of_errcodes argument is
a
1-dimensional array of size
,
where ni is the
i-th
element of array_of_maxprocs.
Command number i corresponds to the ni contiguous
slots in this array from element
to
.
Error codes are treated
the same as with MPI_COMM_SPAWN.
Example
Examples of array_of_argv in C and Fortran
To run the program ``ocean'' with arguments ``-gridfile'' and
``ocean1.grd'' and the program ``atmos'' with argument ``atmos.grd''
in C:
Here is how you do it in Fortran: