kotozna
kotozna

Reputation: 331

mpirun not passing command line arguments

mpirun (and mpiexec) do not seem to be passing command line arguments to my c code.

Running my exeutable "gecko" directly from the command line gives expected:

$ ./gecko  -np 2

main:There are 3 arguments:
arg=./gecko
arg=-np
arg=2

But running the same through mpirun is different:

$ mpirun -np 2 ./gecko

main:There are 1 arguments:
arg=./gecko

which means MPI_init(argc,argv) doesn't have any arguments to work with. I'm on Ubuntu 12.04 with MPICH 2.

Can anyone see a reason why this is not happening?

Thanks.

--------------------------- EDIT ---------------------------------

There are many examples on the net that say the way to initialize MPI is via the command line arguments, eg.:

#include <stdio.h>
#include “mpi.h”                                
int main(int argc, char* argv[])
{
 int size, rank;
 MPI_Init(&argc, &argv);                       
 MPI_Comm_size(MPI_COMM_WORLD, &size);           
 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
 printf(“Greetings from process %i\n”, rank);
 MPI_Finalize();                                
 return 0;
}

and that the way to execute the mpi code is to use:

mpirun -np 2 ./code_name

So, if mpirun does not pass the arguments np and 2 to the c code, how does the c code ever get to know how many processors it should be run on?

Upvotes: 1

Views: 7773

Answers (2)

Jens Gustedt
Jens Gustedt

Reputation: 78993

The -np argument is not meant to be interpreted by your executable, but by mpirun. If you want to pass extra arguments to your executable, you'd have to list them after the executable name as usual.

If you need the number of MPI tasks, you should use the appropriate MPI function that provide you with that number.

Edit:

MPI may pass startup information through the arguments given to MPI_Init or by any other means that pleases the implementor. Depending on the MPI implementation that you use, you may see that MPI passes a lot more arguments to your program. MPI_Init then rips all stuff that it needs and leaves only those things that it doesn't understand.

You should not rely on any such argument passing convention in your application. MPI is meant to be portable and you should only use MPI functions to access the runtime parameters.

Upvotes: 5

Hristo Iliev
Hristo Iliev

Reputation: 74495

Ever since circa 1997 MPI_Init() also works if both its arguments are NULL. This means that practically all MPI implementations use a different mechanism to pass configuration information, namely environment variables and files. For example, Open MPI passes the following variables:

$ cat print_ompi_vars
#!/bin/bash
printenv | grep ^OMPI | sort
$ mpiexec -np 1 ./print_ompi_vars
OMPI_COMM_WORLD_LOCAL_RANK=0
OMPI_COMM_WORLD_LOCAL_SIZE=1
OMPI_COMM_WORLD_NODE_RANK=0
OMPI_COMM_WORLD_RANK=0
OMPI_COMM_WORLD_SIZE=1
OMPI_MCA_btl=^tcp
OMPI_MCA_btl_openib_ib_timeout=24
OMPI_MCA_btl_openib_warn_default_gid_prefix=0
OMPI_MCA_btl_tcp_if_include=ib0
OMPI_MCA_carto=file
OMPI_MCA_carto_file_path=/opt/MPI/openmpi/carto/carto_2K-64C.txt
OMPI_MCA_ess=env
OMPI_MCA_mpi_yield_when_idle=0
OMPI_MCA_oob_tcp_if_include=ib0
OMPI_MCA_orte_app_num=0
OMPI_MCA_orte_cpu_model=Intel(R) Xeon(R) CPU           X5675  @ 3.07GHz
OMPI_MCA_orte_cpu_type=GenuineIntel
OMPI_MCA_orte_daemonize=1
OMPI_MCA_orte_ess_jobid=4114219009
OMPI_MCA_orte_ess_node_rank=0
OMPI_MCA_orte_ess_num_procs=1
OMPI_MCA_orte_ess_vpid=0
OMPI_MCA_orte_hnp_uri=4114219008.0;tcp://1.2.3.4:48206
OMPI_MCA_orte_local_daemon_uri=4114219008.1;tcp://1.2.3.4:59277
OMPI_MCA_orte_num_nodes=1
OMPI_MCA_orte_num_restarts=0
OMPI_MCA_orte_precondition_transports=1f4c3cf87403b137-a8e3173542efb9c3
OMPI_MCA_plm=rsh
OMPI_MCA_shmem_RUNTIME_QUERY_hint=mmap
OMPI_UNIVERSE_SIZE=1

It works similarly with other MPI implementations.

Anyway, if you wish to pass arguments to your MPI executable, put them as usual after the name of the executable:

$ mpiexec -n 16 <other mpiexec args> ./gecko -np 2 <other program args>

Upvotes: 1

Related Questions