7

How can my MPI program detect, if it was launched as a standalone application or via mpirun?

fhucho
  • 34,062
  • 40
  • 136
  • 186
  • What problem are you trying to solve that requires understanding how the rank was launched? – Stan Graves Oct 01 '12 at 21:46
  • 1
    @semiuseless We have an assignment to solve some NP problem, first as a standalone single core program and then on an MPI cluster. And I thought it would be neat and simple if I had just one binary. – fhucho Oct 02 '12 at 09:51
  • `mpirun -np 1`? why not just do this via a script... – pyCthon Oct 02 '12 at 18:19
  • @pyCthon it should be runnable without MPI installed... – fhucho Oct 02 '12 at 19:10
  • @fhucho yeah and in your script thats running the code you can make it echo or print to console how it was run.... – pyCthon Oct 02 '12 at 19:12
  • It's run directly without script: either "./program" or "mpirun -np 10 program" – fhucho Oct 02 '12 at 20:39
  • Most MPI implementations default to shared libraries. So, if you build a single binary, you will still need to isolate all the MPI calls into a separate library that can be dlopen'd only in cases where the MPI environment is installed. This same technique can be used to support multiple different MPI implementations...but is generally not worth the complexity for "toy" applications. – Stan Graves Oct 03 '12 at 20:21
  • Its really a shame that there is no good way to do this. It would be really nice to compile a non-distributed and distributed version of the program into one binary and have it figure out what to do based on if its being run from `mpirun` or not. – Jon Deaton Oct 16 '17 at 06:26

2 Answers2

2

Considering the answer and comments by semiuseless and Hristo Iliev, there is no general and portable way to do this. As a workaround, you can check for environment variables that are set by mpirun. See e.g.:
http://www.open-mpi.org/faq/?category=running#mpi-environmental-variables

alfC
  • 14,261
  • 4
  • 67
  • 118
Douglas B. Staple
  • 10,510
  • 8
  • 31
  • 58
2

There is no MPI standard way to tell the difference between an MPI application that is launched directly, or as a single rank with mpirun. See "Singleton MPI_Init" for more on this kind of MPI job.

The environment variable checking answer from Douglas is a reasonable hack...but is not portable to any other MPI implementation.

alfC
  • 14,261
  • 4
  • 67
  • 118
Stan Graves
  • 6,795
  • 2
  • 18
  • 14
  • Yes, I would also describe the environment variables answer as a hack. I'm surprised there's no better solution. – Douglas B. Staple Oct 02 '12 at 02:08
  • 2
    @DouglasB.Staple, the MPI standard explicitly states that it does not specify how MPI programs are started (§8.7 and §8.8). Using launchers like `mpirun` to perform the startup is considered an implementation detail, although §8.8 _suggests_ (but does not mandate) that a launcher by the name of `mpiexec` is provided and _advises_ the implementors on how to name its command line options. – Hristo Iliev Oct 02 '12 at 15:51
  • @HristoIliev Thanks -- I updated my answer to point out that it's a workaround, considering your comment and semiuseless's answer. +1 and +1. – Douglas B. Staple Oct 02 '12 at 17:41