Passing arguments via command line with MPI

freshmaster picture freshmaster · Feb 8, 2012 · Viewed 17.3k times · Source

I am using MPI calls to run a procedure on multiple processes using c++. The first few lines in my Main function look like:

int main(int argc, char *argv[]){
int comm_sz;
int my_rank;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

x = atoi(argv[4]);
y = atoi(argv[5]);

Now when I execute and run my program using

mpiexec -n 1 program 10 10

I want x and y to be assigned the values 10 and 10, as they are the 4 and 5th arguments passed. But this isn't happening and it assigns these variables to 0 and 0 accordingly. and my program does not run as desired.

I have my serial code running when I change these numbers. Its just that I am new to MPI.

Can you suggest where am I going wrong?

Answer

Jonathan Dursi picture Jonathan Dursi · Feb 8, 2012

In most MPI implementations on Linux/Windows/Mac OSX, when you call MPI_Init(&argc, &argv), the argument list is modified just as if you had run the serial problem as program 10 10; it eats the argument list up to the executable, which can potentially contain any number of options to the mpirun command itself.

The standard doesn't specify this; the standard leaves a lot of things about launching processes and the initialization process somewhat vague, as MPI has to work on systems that behave very differently than POSIX-type systems. But I've never seen an MPI implementation in a POSIX-type environment that doesn't do this.

(Updated to add:) g.inozemtsev 's comment on the question is an excellent, concise explanation as to why this happens.