【问题标题】:SLURM Array Job BASH scripting within python subprocesspython 子进程中的 SLURM 数组作业 BASH 脚本
【发布时间】:2022-12-01 10:24:13
【问题描述】:

更新:我能够通过这一行从 SLURM_JOB_ID 获得变量赋值。 JOBID=`echo ${SLURM_JOB_ID}` 但是,我还没有将 SLURM_ARRAY_JOB_ID 分配给 JOBID。


由于需要支持现有的 HPC 工作流。我需要在 python 子进程中传递 bash 脚本。它在 openpbs 上运行良好,现在我需要将它转换为 SLURM。我主要在 Ubuntu 20.04 上托管的 SLURM 中工作,除了没有填充作业数组。下面是一个代码 sn-p ,它被大大简化为相关内容。

我的具体问题是。为什么 JOBID=${SLURM_JOB_ID} 和 JOBID=${SLURM_ARRAY_JOB_ID} 行没有得到它们的分配?我试过使用 heredoc 和各种 bashisms 但没有成功。

代码当然可以更简洁,这是多人没有共同标准的结果。

这些是相关的

Accessing task id for array jobs

Handling bash system variables and slurm environmental variables in a wrapper script

       sbatch_arguments = "#SBATCH --array=1-{}".format(get_instance_count())

       proc = Popen('ssh ${USER}@server_hostname /apps/workflows/slurm_wrapper.sh sbatch', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
        job_string = """#!/bin/bash -x
        #SBATCH --job-name=%(name)s
        #SBATCH -t %(walltime)s
        #SBATCH --cpus-per-task %(processors)s
        #SBATCH --mem=%(memory)s
        %(sbatch_args)s

        # Assign JOBID
        if [ %(num_jobs)s -eq 1 ]; then
            JOBID=${SLURM_JOB_ID}
        else
            JOBID=${SLURM_ARRAY_JOB_ID}
        fi

        exit ${returnCode}

        """ % ({"walltime": walltime
                ,"processors": total_cores
                ,"binary": self.binary_name
                ,"name": ''.join(x for x in self.binary_name if x.isalnum())
                ,"memory": memory
                ,"num_jobs": self.get_instance_count()
                ,"sbatch_args": sbatch_arguments
                })

        # Send job_string to sbatch
        stdout, stderr = proc.communicate(input=job_string)

【问题讨论】:

  • 再调试一下我已经意识到 SBATCH --array 指令不能被识别。它作为参数传递,我可以看到它已正确传递。可能有一些我不知道的执行顺序
  • 也许需要一些进一步的信息。我可以用这种方法很好地运行非数组作业。 SLURM_ARRAY_* 变量显然被 BASH 评估得太早了。在 SBATCH 定义它们之前。我一直在尝试用 heredoc 和一些 bashisms 来改变它。我仍然没有成功。我希望我不必为了一种全新的方法而放弃它。

标签: python ubuntu subprocess slurm hpc


【解决方案1】:

跟进此事。我通过将 SBATCH 指令作为参数传递给 sbatch 命令来解决它

    sbatch_args = """--job-name=%(name)s --time=%(walltime)s --partition=defq --cpus-per-task=%(processors)s --mem=%(memory)s""" % (
                    {"walltime": walltime
                    ,"processors": cores
                    ,"name": ''.join(x for x in self.binary_name if x.isalnum())
                    ,"memory": memory
                    })

    # Open a pipe to the sbatch command. {tee /home/ahs/schuec1/_stderr_slurmqueue | sbatch; }
    # The SLURM variables SLURM_ARRAY_* do not exist until after sbatch is called.
    # Popen.communicate has BASH interpret all variables at the same time the script is sent.
    # Because of that, the job array needs to be declared prior to the rest of the BASH script.

    # It seems further that all SBATCH directives are not being evaultated when passed via a string with .communicate
    # due to this, all SBATCH directives will be passed as arguments to the slurm_wrapper.sh as the first command to the Popen pipe.

    proc = Popen('ssh ${USER}@ch3lahpcgw1.corp.cat.com /apps/workflows/slurm_wrapper.sh sbatch %s' % sbatch_args,
    shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE,
    close_fds=True,
    executable='/bin/bash')

【讨论】:

    猜你喜欢
    • 2018-01-06
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    相关资源
    最近更新 更多