mpirun没有传递命令行参数

时间:2014-10-13 16:25:49

标签: c ubuntu-12.04 mpi

mpirun(和mpiexec)似乎没有将命令行参数传递给我的c代码。

直接从命令行运行我的可执行“gecko”会产生预期的结果:

$ ./gecko  -np 2

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

但是通过mpirun运行同样的东西是不同的:

$ mpirun -np 2 ./gecko

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

这意味着MPI_init(argc,argv)没有任何参数可供使用。 我使用MPICH 2在Ubuntu 12.04上。

任何人都可以看到没有发生这种情况的原因吗?

感谢。

---------------------------编辑------------------- --------------

网上有很多例子说初始化MPI的方法是通过命令行参数,例如:

#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;
}

并且执行mpi代码的方法是使用:

mpirun -np 2 ./code_name

所以,如果mpirun没有将参数np和2传递给c代码,那么c代码如何知道应该运行多少个处理器呢?

2 个答案:

答案 0 :(得分:5)

-np参数不是由您的可执行文件解释,而是由mpirun解释。如果要将额外的参数传递给可执行文件,则必须像往常一样在可执行文件名后面列出它们。

如果您需要MPI任务的数量,您应该使用适当的MPI功能为您提供该号码。

编辑:

MPI 可以通过提供给MPI_Init的参数或通过任何其他让实现者高兴的方式传递启动信息。根据您使用的MPI实现,您可能会看到MPI向您的程序传递了更多参数。 MPI_Init然后撕掉它需要的所有东西,只留下那些它不理解的东西。

您不应该依赖于在您的应用程序中传递约定的任何此类参数。 MPI意味着可移植,您应该只使用MPI函数来访问运行时参数。

答案 1 :(得分:1)

自1997年左右以来,MPI_Init()如果两个论点都是NULL,也会有效。这意味着几乎所有MPI实现都使用不同的机制来传递配置信息,即环境变量和文件。例如,Open MPI传递以下变量:

$ 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

它与其他MPI实现类似。

无论如何,如果您希望将参数传递给MPI可执行文件,请将它们放在可执行文件名后面:

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