30

this is how we use MPI_Init function

int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);
…
}

why does MPI_Init use pointers to argc and argv instead of values of argv?

Rohit Banga
  • 18,458
  • 31
  • 113
  • 191
  • 2
    They are passed by reference to allow an MPI implementation to provide them in environments where the command-line arguments are not provided to main. http://www.mpi-forum.org/docs/mpi-11-html/node151.html can someone shed more light on this with an example? – Rohit Banga Apr 15 '10 at 08:07

5 Answers5

24

According to the answer stated here:

Passing arguments via command line with MPI

Most MPI implementations will remove all the mpirun-related arguments in this function so that, after calling it, you can address command line arguments as though it were a normal (non-mpirun) command execution.

i.e. after

mpirun -np 10 myapp myparam1 myparam2

argc = 7(?) because of the mpirun parameters (it also seems to add some) and the indices of myparam1 and myparam2 are unknown

but after

MPI_Init(&argc, &argv)

argc = 3 and myparam1 is at argv[1] and myparam2 is at argv[2]

Apparently this is outside the standard, but I've tested it on linux mpich and it certainly seems to be the case. Without this behaviour it would be very difficult (impossible?) to distinguish application parameters from mpirun parameters.

Community
  • 1
  • 1
NickTee
  • 673
  • 6
  • 13
  • 2
    At least `mpirun` from OpenMPI 3.1.1 strips its parameters even before calling the subprograms, so you'll get `argc = 3` even before calling `MPI_Init`. – Jakub Klinkovský Sep 16 '18 at 16:07
  • I am curious with what distribution you observed this behavior. It makes sense, but I couldn't reproduce it with mpich or openmpi. – alfC Jul 01 '21 at 02:57
4

my guess to potentially allow to remove mpi arguments from commandline. passing argument count by pointer allows to modify its value from the point of main.

Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • why would they do that? what if i examine the arguments before calling MPI_Init – Rohit Banga Apr 16 '10 at 06:18
  • 2
    To parse MPI specific arguments. I don't have example for MPI, but GTK GUI toolkit has almost identical `gtk_init` function, that filters out some common X commandline options, such as `--display` and `--screen`. – el.pescado - нет войне Jul 07 '10 at 20:37
  • @RohitBanga You can inspect the contents of `argc` and `argv` *before* calling `MPI_Init`. Besides you can pass dummy arguments to `MPI_Init` if you want to keep the originals. :) (I agree it is not very elegant but there is a workaround.) – alfC Apr 07 '18 at 03:35
3

According to OpenMPI man pages: MPI_Init(3) man page

Open MPI accepts the C/C++ argc and argv arguments to main, but neither modifies, interprets, nor distributes them.

Wey Dong
  • 59
  • 3
  • MPICH doesn't make the same claim, but my little experiment with MPICH 3.3.2 indicates that all extraneous parameter in argv are already cleaned up before initializing mpi, so it seem that it doesn't need to do anything with &argc and &argv either. – alfC Jul 01 '21 at 02:31
1

I'm not an expert but I believe the simple answer is that each node that you're working with is working with its own copy of the code. Passing these arguments allows each of the nodes to have access to argc and argv even though they were not passed them through the command line interface. The original or master node that calls MPI_Init is passed these arguments. MPI_Init allows the other nodes to access them as well.

-5

It is less overhead to just pass two pointers.

Chad Brewbaker
  • 2,523
  • 2
  • 19
  • 26
  • actually i meant invoke the function as MPI_Init(argc, argv); – Rohit Banga Apr 27 '10 at 16:17
  • Well, many times in practice the MPI code is the whole executable so it would make sense to pass all the command line arguments. Remember though, argc and argv are just local variable names. Inside your code before you call MPI_Init() you can pass it whatever you want. – Chad Brewbaker Jun 14 '10 at 07:36