如何从 for 循环中的作业数组中获取作业 ID?

How to get job ID from job array within for loop?

我是运行一个贪婪的特征选择算法,我正在尝试使用作业数组来探索并行化。

这个想法是我们有三个依赖于上一步的步骤:

因为您需要所有模型 (>10) 在开始第 3 步之前完成训练,所以普通的旧作业链不是最优的。 所以我正在尝试使用作业数组,它完全符合我的要求:只有当我的所有模型都安装好后,我才会转到第 3 步。

但是,我在设置依赖项时遇到问题。 我被告知整个作业数组的依赖项需要是作业 ID(这是一个数字)而不是作业名称(例如 runSetup$n_subject$i)。

那么:如何从整个作业数组中获取作业 ID? 或者更好:如何最好地为整个作业数组设置依赖关系?

这个 answer 非常有趣,但没有告诉我当我的作业数组包含 10 个或更多作业时如何最好地设置依赖关系。

#!/bin/bash

# Subject to consider
n_subject= # takes in input arguments from command line.
cohort=
priors_and_init=
nparam=16

for ((i = 1; i <= $nparam; i++)); do
    # Run setup
    if [[ $i -eq 1 ]]; then
      bsub -J "runSetup$n_subject$i" matlab  -singleCompThread -nodisplay -r "setup_greedy_forward($n_subject,$cohort, $priors_and_init, $i)"
    else
      last_iter=$((i-1))
      bsub -J "runSetup$n_subject$i" -w "done(saveBest$n_subject$last_iter)" matlab  -singleCompThread -nodisplay -r "setup_greedy_forward($n_subject,$cohort, $priors_and_init, $i)"
    fi

    # Fit models
    max_sim=$((nparam-i+1))
    bsub -W 08:00 -J "fitDCMs$n_subject[1-$max_sim]" -w "done(runSetup$n_subject$i)" -R "rusage[mem=16000]" matlab  -singleCompThread -nodisplay -r "fit_dcm_greedy_forward($n_subject,$cohort, $priors_and_init, $LSB_JOBINDEX)"

    # Extracting the job ID from the fitDCMs jobs
    # Then: For all trained DCMs, get the best model and save it
    JOBID=$(get_jobid bsub -W 08:00 -J "fitDCMs$n_subject[1-$max_sim]" -w "done(runSetup$n_subject$i)" -R "rusage[mem=16000]" matlab  -singleCompThread -nodisplay -r "fit_dcm_greedy_forward($n_subject,$cohort, $priors_and_init, $LSB_JOBINDEX)" 2> /dev/null)
    if [ -n "$jobid" ]; then
        bsub -J "saveBest$n_subject$i" -w "numdone($JOBID,*)" matlab -singleCompThread -nodisplay -r "save_best_model($n_subject,$cohort, $priors_and_init, $i)"
    fi
done


我得到的输出:

MATLAB job.
Job <94564566> is submitted to queue <normal.24h>.
MATLAB job.
Job <94564567> is submitted to queue <normal.24h>.
MATLAB job.
saveBest121: No matching job found. Job not submitted.
MATLAB job.
runSetup122: No matching job found. Job not submitted.
[…]

I was told that the dependency for a whole job array needs to be the job ID (which is a number) and not the job name

应该可以。例如:

bsub -J "iterate[1-10]" ...
bsub -J "finalize" -w "done(iterate)" ...

作业 finalize 直到 iterate 的所有元素都完成后才会开始。

经过一番搜索,我找到了获取工作ID的方法。

JOBID=$(bsub command1 | awk '/is submitted/{print substr(, 2, length()-2);}')
if [ -n "$JOBID" ]; then
    bsub -w "numdone($JOBID,*)" command2
fi

第一行提交作业并提取其作业 ID。

已找到此答案 here