My application is sequential and I want to make it parallel. Hence I want to check if I am in MPI environment or not to adapt to parallel processing.
-
Probably the easiest way to check this is to use MPI_Comm_size... The size tells you the number of cores. However, without knowing what language you are using, I can't be more specific about the syntax. – Ash Pera Feb 12 '19 at 22:29
-
But to call any MPI method we need to call MPI_Init() first. But I would like to know if at all MPI environment has been set up. – Sanjaya Mar 15 '19 at 15:57
1 Answers
Sequential application is a special case of a parallel application (when number of processes = 1), so you should not need any special adaptation. Just call MPI_Init
and, as suggested in the comments, determine the communicator size.
Some MPI implementations (e.g., Open MPI and MS MPI) allow you execute your program directly, without mpiexec
, and still the call to MPI_Init
will not fail, so your parallel application can then be used equally well as a serial one.
To actually detect whether your application is really executed using mpiexec
, you can typically inspect the environment variables. I used the following program to print the environment variables of my Windows application (linked to MS MPI v10):
#include <mpi.h>
#include <stdio.h>
#include <windows.h>
int main (void)
{
for (const char* envs = GetEnvironmentStrings();
*envs != 0;
envs += strlen(envs) + 1)
{
printf("%s\n", envs);
}
return 0;
}
When I execute the application using mpiexec -n 1
, I see that several new environment variables are set on top of those already present when not using the launcher:
PMI_APPNUM=0
PMI_DOMAIN=a96606eb-0e17-46fc-ba6e-d8ca3f462bc6
PMI_HOST=localhost
PMI_KVS=4d648866-1723-4f0d-b2dc-450eae52e410
PMI_NODE_IDS=smp_region_5540
PMI_PORT=03fd2819-719a-43a0-ad9a-d8b9fb0443fd
PMI_RANK=0
PMI_RANK_AFFINITIES=affinity_region_5540
PMI_SIZE=1
PMI_SMPD_ID=1
PMI_SMPD_KEY=0
PMI_SPAWN=0
Hence, presence of these environment variables may indicate use of MS MPI. Similarly, presence of OMPI_COMM_WORLD_SIZE
would indicate use of Open MPI. Etc.

- 1,535
- 1
- 11
- 26