Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments via command line with MPI

Tags:

c++

mpi

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?

like image 317
freshmaster Avatar asked Feb 07 '12 23:02

freshmaster


People also ask

How do you pass command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

How do I pass a command line argument in Command Prompt?

option. You can test command line arguments by running an executable from the "Command Prompt" in XP, Vista or later, or from the "DOS prompt" in older versions of Windows. You can also use command line arguments in program shortcuts, or when running an application by using Start -> Run.

Which command is used to run MPI program?

Syntax for the mpirun Command % mpirun [options] [program-name] : [options2] [program-name2] ... This command starts x number of copies of the program program1, and then starts y copies of the program program2.


1 Answers

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.

like image 129
Jonathan Dursi Avatar answered Oct 07 '22 17:10

Jonathan Dursi