简体   繁体   English

将参数传递到Slurm脚本

[英]Passing a parameter into a slurm script

I am using slurm scripts to run arrays for Matlab computing on a cluster. 我正在使用Slurm脚本在群集上运行用于Matlab计算的数组。 Each script uses an array to loop over a matlab parameter. 每个脚本使用一个数组来循环一个matlab参数。

1) Is it possible to create a shell script to loop over another variable? 1)是否可以创建一个Shell脚本来循环另一个变量?
2) Can I pass variables to a slurm script? 2)我可以将变量传递到Slurm脚本吗?

For example, my slurm files currently look like 例如,我的Slurm文件当前看起来像

#!/bin/bash
#SBATCH --array=1-128
...
matlab -nodesktop r "frame=[${SLURM_ARRAY_TASK_ID}]; filename=['Person24']; myfunction(frame, filename);";

I frequently need to run this array to process a number of different files. 我经常需要运行此数组来处理许多不同的文件。 This means I will submit the job (sbatch exampleScript.slurm), edit the file, update 'Person24' to 'Person25', and then resubmit the job. 这意味着我将提交作业(批处理exampleScript.slurm),编辑文件,将“ Person24”更新为“ Person25”,然后重新提交作业。 This is pretty inefficient when I have a large number of files to process. 当我要处理大量文件时,这效率很低。

Could I make a shell script that would pass a variable to the slurm script? 我可以制作一个将变量传递给Slurm脚本的Shell脚本吗? For example, something like this: 例如,如下所示:

Shell Script (myshell.sh) Shell脚本(myshell.sh)

#!/bin/bash
for ((FNUM=24; FNUM<=30; FNUM+=1));
do
     sbatch myscript.slurm  >> SOMEHOW PASS ${FNUM} HERE (?)
done 

Slurm script (myscript.slurm) Slurm脚本(myscript.slurm)

#!/bin/bash
#SBATCH --array=1-128
...
matlab -nodesktop -nodisplay r "frame=[${SLURM_ARRAY_TASK_ID}]; filename=[${FNUM}]; myfunction(frame, filename);";

where I could efficiently submit all of the jobs using something like sbatch myshell.sh 在这里我可以使用sbatch myshell.sh之类的东西有效地提交所有工作

Thank you! 谢谢!

In order to avoid possible name collisions with shell and anvironment variables, it is a good habit to always use lowercase or mixed case variables in your Bash scripts. 为了避免与shell和环境变量发生名称冲突,在Bash脚本中始终使用小写或大小写混合的变量是一个好习惯。

You were almost there. 你快到了 You just need to pass the variable as an argument to the second script and then pick it up there based on the positional parameters. 您只需要将变量作为参数传递给第二个脚本,然后根据位置参数在第二个脚本中进行选择即可。 In this case, it looks like you're only passing one argument, so $1 is OK to use. 在这种情况下,看起来您只传递了一个参数,因此$1可以使用。 In other cases, with multiple parameters of a fixed number you could also use $2 , $3 , etc. With a variable number of arguments "$@" would be more appropriate. 在其他情况下,如果使用固定数量的多个参数,则还可以使用$2$3等。使用可变数量的参数“ $ @”会更合适。

Shell Script (myshell.sh) Shell脚本(myshell.sh)

#!/bin/bash
for ((fnum=24; fnum<=30; fnum+=1))
do
     sbatch myscript.slurm "$fnum"
done 

Slurm script (myscript.slurm) Slurm脚本(myscript.slurm)

#!/bin/bash
#SBATCH --array=1-128

fnum=$1

...
matlab -nodesktop -nodisplay r "frame=[${slurm_array_task_ID}]; filename=[${fnum}]; myfunction(frame, filename);";

For handling various timeout conditions this might work: 为了处理各种超时条件,这可能会起作用:

A=$(sbatch --parsable a.slurm)

case $? in
    9|64|130|131|137|140)
        echo "some sort of timeout occurred"
        B=$(sbatch --parsable --dependency=afternotok:$A a.slurm)
        ;;
    *)
        echo "some other exit condition occurred"
        ;;
esac

You will just need to decide what conditions you want to handle and how you want to handle them. 您只需要确定要处理的条件以及如何处理它们。 I have listed all the ones that seem to involve timeouts. 我列出了所有似乎涉及超时的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM