I would like to create a program that would work with both MPI and without at run time and I have reached a point where I think that is not possible.
For example, an MPI program might look like this:
#include <mpi.h>
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
...
MPI_Finalize ();
return 0;
}
But in order to allow users who don't have MPI installed to compile this, I would have to wrap it around #if
's:
// Somewhere here, pull in a .h file which would set HAVE_MPI to 0 or 1
#if HAVE_MPI
#include <mpi.h>
#endif
int main(int argc, char* argv[])
{
#if HAVE_MPI
MPI_Init(&argc, &argv);
#endif
...
#if HAVE_MPI
MPI_Finalize ();
#endif
return 0;
}
Until now, I think I'm correct? If this program compiled to a.out
with HAVE_MPI
set to 1
, it would be run as: mpirun -np 4 ./a.out
or mpirun -np 1 ./a.out
. It could never be run as ./a.out
, because even if it isn't run within mpirun
, it would call the MPI_*
functions.
I guess to achieve what I would like -- an executable that could be run with and without mpirun
is not possible. And the only option is to create two separate executables -- one with HAVE_MPI
set to 0 and another set to 1. Is this correct or am I missing something about how C++ programs using MPI are implemented?
As a side-note, I don't think this is true with shared memory parallelization (i.e., with OpenMP). The same executable should work with a single or multiple threads.
Is my understanding correct?