bash脚本中的mpirun命令与MPI代码之间的连接

bash脚本中的mpirun命令与MPI代码之间的连接,mpi,slurm,Mpi,Slurm,我想知道如何在bash脚本(slurm)中设置一个变量,并在C语言的MPI程序中使用该变量,反之亦然。 例如: 在测试mpi.c中定义int i。。。。。。 然后,在bash脚本中,按如下方式使用它: if (i=o) mpirun --map-by ppr:1:socket ./test-mpi if (i=1) mpirun --map-by ppr:1:node ./test-mpi i = $(./test-mpi --dump-i) if [ "$i" == "1" ];

我想知道如何在bash脚本(slurm)中设置一个变量,并在C语言的MPI程序中使用该变量,反之亦然。 例如: 在测试mpi.c中定义int i。。。。。。 然后,在bash脚本中,按如下方式使用它:

if (i=o)
  mpirun --map-by ppr:1:socket ./test-mpi
if (i=1)
  mpirun --map-by ppr:1:node ./test-mpi  
i = $(./test-mpi --dump-i)
if [ "$i" == "1" ]; then
    mpirun --map-by ppr:1:socket ./test-mpi
else
    mpirun --map-by ppr:1:node ./test-mpi  
fi
实际上,我想用ppr:1:socket完成代码的一部分,用ppr:1:node完成另一部分

另外,在MPI程序中是否有将进程映射到套接字的方法,而不是在bash脚本中

如有任何建议,将不胜感激

编辑:

如果我这样使用argc、argv可以吗:

for (count =0; count < argc; count++){
 if (argv[i] == "state1"){
  for (....)
      do something

for (count =0; count < argc; count++){
 if (argv[i] == "state2"){
  for (....)
      do something
for(count=0;count
您可以向应用程序添加命令行标志,如:

int main(int argc, char* argv[]) {
    // argc is the number of arguments, including the executable.
    // so 2 means one argument
    // check if that argument equals "--dump-i"
    if (argc == 2 && 0 == strcmp(argv[1], "--dump-i")) {
        printf("%d", i);
        return 0;
    }
    MPI_Init(NULl, NULL);
    ...
}
在bash脚本中,类似这样的内容:

if (i=o)
  mpirun --map-by ppr:1:socket ./test-mpi
if (i=1)
  mpirun --map-by ppr:1:node ./test-mpi  
i = $(./test-mpi --dump-i)
if [ "$i" == "1" ]; then
    mpirun --map-by ppr:1:socket ./test-mpi
else
    mpirun --map-by ppr:1:node ./test-mpi  
fi
(未经测试)

但我怀疑,如果你提供更多关于你真正想做什么的细节,会有更好的解决方案

编辑 要在slurm中并行运行多个作业步骤,请执行以下操作:

# Make sure to restrict the resources, such that 
# the entire job has enough to run both steps simultaneously.
# The ampersand launches the jobs in the background such that
# they are not executed sequentially
srun -n ... --map-by ppr:1:socket ./test-mpi --first-loop &
srun -n ... --map-by ppr:1:node ./test-mpi --second-loop &
我希望您能够实现与上述示例类似的命令行切换。您也可以只构建二进制文件

编辑2
处理编辑时,您无法将C字符串与
==
进行比较,您需要使用
strcmp
strncmp
。在更复杂的命令行参数解析之前,请看一看。

这没有任何意义。该变量仅在执行
mpirun
之后定义,然后每个进程定义一次,可能使用不同的值。您可以向您的C程序添加一个特殊的命令行标志,强制执行特定的输出,然后在bash脚本中执行该标志,并使用该输出进行决策。这就是您想要的吗?您可以给我一个“向您的C程序添加特殊命令行标志”的示例吗?谢谢您的回答。我的mpi代码中有两个for循环,我想使用mpirun执行其中一个for循环——按ppr:1:socket映射。/test mpi(使用每个socket映射1个进程执行for循环),使用mpirun执行另一个for循环——按ppr:1:node映射。/test mpi(每个节点映射1个进程)@Sarah创建一个命令行参数来选择循环并从脚本中传递参数?@Sarah是的,如果有足够的资源,在slurm中,您可以通过在bash中添加
&
来同时运行多个作业步骤/
srun
。但是,您的作业步骤将具有单独的MPI范围,这两个作业步骤的列组将不存在能够相互通信。是的,这就是我所需要的。并且可以在脚本中使用多个mpirun命令来同时运行它们,对吗?您能告诉我第一个if条件的作用吗?(if(argc==2…)@Sarah我编辑了我的答案,以解决您的其他问题。