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?

freshmaster
  • 333
  • 2
  • 5
  • 11
  • Have you tried simply printing the contents of `argv`? What's the result? – suszterpatt Feb 07 '12 at 23:52
  • 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. – freshmaster Feb 07 '12 at 23:57
  • 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. – freshmaster Feb 07 '12 at 23:59
  • 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=proxy – freshmaster Feb 08 '12 at 00:05
  • 15
    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). – Greg Inozemtsev Feb 08 '12 at 03:59

2 Answers2

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.

Jonathan Dursi
  • 50,107
  • 9
  • 127
  • 158
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.