Why does MPI_Init accept pointers to argc and argv?
Asked Answered
H

5

31

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?

Hornbook answered 15/4, 2010 at 5:41 Comment(1)
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. mpi-forum.org/docs/mpi-11-html/node151.html can someone shed more light on this with an example?Hornbook
B
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.

Bronchopneumonia answered 21/4, 2012 at 0:3 Comment(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.Fencible
I am curious with what distribution you observed this behavior. It makes sense, but I couldn't reproduce it with mpich or openmpi.Maize
C
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.

Czarist answered 15/4, 2010 at 23:41 Comment(3)
why would they do that? what if i examine the arguments before calling MPI_InitHornbook
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.Collyer
@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.)Maize
M
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.

Morey answered 4/6, 2019 at 12:27 Comment(1)
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.Maize
S
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.

Suspire answered 4/4, 2014 at 4:23 Comment(0)
E
-6

It is less overhead to just pass two pointers.

Eran answered 17/4, 2010 at 15:22 Comment(2)
actually i meant invoke the function as MPI_Init(argc, argv);Hornbook
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.Eran

© 2022 - 2024 — McMap. All rights reserved.