Passing arguments via command line with MPI

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.


#include<stdio.h>
#include<mpi.h>
int main(int argc, char *argv[]){
int comm_sz;
int my_rank;
int x,y;

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

x = atoi(argv[1]);
y = atoi(argv[2]);

printf("%d,%d\n",x,y);
}

Try this, the MPI consider arguments after program name so use 1 and 2 as argument.

Tags:

C++

Mpi