There is a predefined attribute MPI_APPNUM of MPI_COMM_WORLD. In Fortran, the attribute is an integer value. In C, the attribute is a pointer to an integer value. If a process was spawned with MPI_COMM_SPAWN_MULTIPLE, MPI_APPNUM is the command number that generated the current process. Numbering starts from zero. If a process was spawned with MPI_COMM_SPAWN, it will have MPI_APPNUM equal to zero.
 
Additionally, if the process was not started by a spawn call,   
but by an implementation-specific startup mechanism that can  
handle multiple process specifications,  MPI_APPNUM should  
be set to the number of the corresponding process specification.  
In particular, if it is started with  
 
mpiexec spec0 [: spec1 : spec2 : ...]MPI_APPNUM should be set to the number of the corresponding specification.
If an application was not spawned with MPI_COMM_SPAWN or MPI_COMM_SPAWN_MULTIPLE, and MPI_APPNUM doesn't make sense in the context of the implementation-specific startup mechanism, MPI_APPNUM is not set.
MPI implementations may optionally provide a mechanism to override the value of MPI_APPNUM through the info argument. MPI reserves the following key for all SPAWN calls.
 
When a single application is started, it is able to figure  
out how many processes there are by looking at the  
size of  MPI_COMM_WORLD.  An application  
consisting of multiple SPMD sub-applications has no   
way to find out how many sub-applications there are  
and to which sub-application the process belongs. While there  
are ways to figure it out in special cases, there  
is no general mechanism.  MPI_APPNUM provides  
such a general mechanism.   
 ( End of rationale.)