I'm using gcc
and OpenMPI. Usually I run MPI programs using the mpirun
wrapper -- for example,
mpirun -np 4 myprogram
to start 4 processes.
However, I was wondering if it's possible to easily generate a binary which will do that automatically (maybe with some hardcoded options like -np 4
above).
I know I can write a C wrapper that calls my program, such as the following:
#include <stdlib.h>
#include <unistd.h>
int main() {
char *options[] = { "mpirun", "-np", "4", "myprogram" };
execvp("mpirun", options);
/* Ignoring return value to keep example simple */
return EXIT_SUCCESS;
}
but this seems a bit clumsy and I end up with two executables instead of one.
I have tried to explicitly link the MPI libraries, like
gcc -o myprogram -I/usr/lib/openmpi/include/ \
-lmpi -L/usr/lib/openmpi/lib/ myprogram.c
but the when I run resulting executable, MPI_Comm_size
sets zero as the group size (as if I had given -np 0
as argument). Can I use an environment variable or something else to pass the group size? Or, is there another way to build a single-executable MPI program (using Linux and gcc
)?