12

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)?

Jay
  • 9,585
  • 6
  • 49
  • 72
  • It can be done, although I don't know how from the top of my head. I know a couple of programs I've run into that do this. There's no real magic in there, it just does a bunch of stuff behind the scenes that you can also do yourself. – Kenneth Hoste Jun 24 '12 at 17:41
  • Do I understand correctly - you want to skip `mpirun` or you want to somehow automatically call `mpirun`? – Hristo Iliev Jun 24 '12 at 18:24
  • 1
    @Hristo Iliev: It would be nice if I had a single static binary. – Jay Jun 24 '12 at 20:25
  • 1
    Well, you cannot get without `mpirun`. It is not just a launcher, but also part of the MPI run-time that acts as central coordinator for each MPI job, especially in the case of Open MPI (`mpirun` is actually symlink to OMPI's `orterun`). But you can make your code to check for a special option, e.g. `-launchmpi` and make it execute `mpirun -np argv[0] ` if it is set. – Hristo Iliev Jun 24 '12 at 21:20
  • 2
    Seel also [this](http://stackoverflow.com/questions/15578009/difference-between-running-a-program-with-and-without-mpirun) SO question. – Richard Apr 01 '15 at 21:57

3 Answers3

12

If I get it correctly, you want a self-launching MPI executable. As I have written in my comment, you can go with a special option that makes your code execute mpirun if supplied, e.g. -launchmpi. With Open MPI it is even easier since it exports special environment variables to launched MPI processes, e.g. OMPI_COMM_WORLD_RANK. If this variable exists in the environment, then you know that the program was launched from mpirun and not directly. You can combine both methods in a single check this:

int main (int argc, char **argv)
{
    int perform_launch = 0;
    // Scan argv[] for special option like "-launchmpi"
    // and set perform_launch if found 

    if (perform_launch || getenv("OMPI_COMM_WORLD_RANK") == NULL)
    {
        // #args = argc + 3 ("mpirun -np 4" added) + NULL
        // #args should be reduced by one if "-launchmpi" is present
        char **args = (char **)calloc(
           argc + (perform_launch ? 3 : 4),
           sizeof(char *));
        args[0] = "mpirun";
        args[1] = "-np";
        args[2] = "4";
        // Copy the entire argv to the rest of args but skip "-launchmpi"

        execvp("mpirun", args);

        return EXIT_SUCCESS;
    }

    // Proceed as regular MPI code
    MPI_Init(&argc, &argv);
    ...
    // Magic happens here
    ...
    MPI_Finalize();

    return EXIT_SUCCESS;
}

If you'd like to control the number of processes in the MPI job, you can supply it as an additional arugment, e.g. -launchmpi 12, or in an environment variable and use its value instead of "4" in the above code.

Note that MPI executables cannot be generally launched without mpirun. The latter is an integral part of the MPI run-time and it does much more that just launching multiple copies of the MPI executable. Also you are always linking explicitly to the MPI library when compiling with any of the MPI compiler wrappers (try mpicc -showme). Although you can link MPI libraries statically (not recommended, see here), you will still need mpirun in order to be able to run MPI jobs - AFAIK there is no way to embed mpirun functionality in your program, at least not in Open MPI.

Rufflewind
  • 8,545
  • 2
  • 35
  • 55
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • `MPI executables cannot be generally launched without mpirun`. This is very new to me since if np=1 I generally skip mpirun. Recently we encounter a workstation that executable failed to run without mpirun. We verify that only one mpi (openmpi) is installed on the system. Which could be the reason for that? – kstn Jun 26 '23 at 13:37
  • @kstn Open MPI has a modular architecture that dynamically loads components stored as shared object files. If the executable is linked against a given Open MPI build, it will look for those object files at the place where the build installed them. If they are not there, e.g. if you transferred the executable to a different host that has OMPI installed elsewhere, the executable will fail to run. Launching it via `mpirun` works because the latter sets environment variables telling the library where to find the modules. This is the most likely reason. – Hristo Iliev Jun 27 '23 at 14:15
2

You can do this with a bash script:

# If you change this script has executable (chmod +x script_name)
# and if you have the current path in the PATH variable (add export PATH=.:$PATH in your .bashrc)
#Then, you can run this has: script_name program_args

mpirun -np 4 your_executable_name "$@"
RSFalcon7
  • 2,241
  • 6
  • 34
  • 55
0

I am new to MPI, but MPI comm can spawn child tasks, you could use that to spawn task from your top level app.

https://www.open-mpi.org/doc/v4.1/man3/MPI_Comm_spawn.3.php

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 23 '23 at 12:49