Passing arguments via command line with MPI
Asked Answered
B

2

20

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?

Brunella answered 7/2, 2012 at 23:42 Comment(5)
Have you tried simply printing the contents of argv? What's the result?Laveen
I did. When I printed out the contents in the array, it did recognize the two arguments 10, 10 but not at positions 4 and 5.Brunella
It seems to be reading them as argv[1] and argv[2]. I solved my problem yes, but I wanted to know why is this happening. I am not exactly understanding MPI_Init.Brunella
When I printed out the other two arguments argv[4] and argv[5], arguments are : OMPI_MCA_orte_precondition_transports=70175593b8f2c60b-4d521364633b88b3 and this: OMPI_MCA_rds=proxyBrunella
Here's one way to think about it: in the command you provided, the two 10 values are not at positions 4 and 5, they are at 1 and 2. When you run mpiexec, it parses its arguments and sees that you wish to run program 10 10 on all the machines in the cluster. mpiexec can also append extra arguments (such as these OpenMPI values you are seeing), but the call to MPI_Init will modify argc and argv to make this invisible (see Jonathan Dursi's answer).Pooh
R
22

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.

Refrigerate answered 8/2, 2012 at 1:49 Comment(0)
B
1
#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.

Brunet answered 27/3, 2019 at 14:44 Comment(2)
this was already answered in the comment section 7 years ago.Force
still useful to elevate to an answer, indeed this is recommended.Sassenach

© 2022 - 2024 — McMap. All rights reserved.