批处理作业sbatch

简介

批处理作业是指用户编写作业脚本,指定资源需求约束,提交后台执行作业。 提交批处理作业的命令为sbatch,用户提交命令即返回命令行窗口, 但此时作业在进入调度状态, 在资源满足要求时,分配完计算节点之后, 系统将在所分配的第一个计算节点(而不是登录节点)上加载执行用户的作业脚本。

批处理作业的脚本为一个文本文件, 脚本第一行以 #! 字符开头,并指定脚本文件的解释程序, 如 sh,bash。 由于计算节点为精简环境,只提供 sh 和 bash 的默认支持。

使用示例

例如用户的脚本名为myjob.sh,内容如下:

#!/bin/bash

srun -n 4 -N 4 -p hw-32C768G --comment group_name hostname

使用该脚本用户提交批处理作业,需要明确申请的资源为 arm 分 区的 4 个节点。

备注

需给该文本文件设置myjob.sh可执行权限,利用命令: chmod +x myjob.sh

用户sbatch批处理命令如下:

$sbatch -N 4 -p hw-32C768G ./myjob.sh
Submitted batch job 1813520

计算开始后,工作目录中会生成以slurm开头的.out 文件为输出文件。

 $cat slurm-1813520.out
cpu1
cpu4
cpu2
cpu3

一个简单的脚本示例如下:

 1#! /bin/bash
 2### This is a sample script file for slurm
 3
 4#SBATCH --job-name=JOBNAME
 5### set the job name
 6
 7#SBATCH --partition=PARTITION_NAME
 8### set the partition name : can be hw-32C768G/insp-4V100/insp-8V100
 9
10#SBATCH --nodes=1
11### set the number of nodes
12
13#SBATCH --ntasks-per-node=1
14### setting ntasks-per-node=1 is equivalent to setting
15
16#SBATCH --output=%u-%x-%j.out
17### set the output file name
18
19#SBATCH --error=%u-%x-%j.err
20### set the error file name
21
22#SBATCH --time=2:00:00
23### maximum time allowed for job to run, if it exceeds this time, the job will be terminated
24
25#SBATCH --comment project_name
26### point out which project to charge. If this parameter is not specified, it will be charged from the personal account
27
28mpirun hostname ### execute the program

注意

上述中###为注释行。

第一行表示这是一个bash脚本,第4-17行以#SBATCH开头的命令表示这些是需要slurm系统处理的参数。

如下图所示,通过sbatch+作业脚本名提交作业,系统会返回作业编号,通过squeue命令可以看到作业运行状态,等作业执行完成后,默认会把程序的输出放到slurm-作业编号.out的文件中,可通过该文件查看程序的输出。

image6

常见提交作业参数参考

参数

说明

--job­name=<name>

设定作业名称

--nodes=<n>-N

设定作业需要的节点数。如果没有指定,默认分配足够的节点来满足--ntasks=<n>--cpus-per-task=<ncpus>参数的要求。

--ntasks-per-node=<ntasks>

设定每个节点上的任务数。要和 --nodes=<n> 同时配合使用。

--ntasks=<n>-n

设定最多启动的任务数。

--cpus-per-task=<ncpus>

设定每个任务所需要的CPU核数。如果没有指定,默认为每个任务分配一个CPU核。一般运行OpenMP等多线程程序时需要,普通MPI程序不需要。

--gres=gpu:n

设定需要使用的GPU卡数量

--comment projectName

设定需要扣费的项目账户,将 projectName 替换为项目名称。如果项目名称错误,作业会提交失败。

更多选项,用户可以通过 sbatch --help 命令来查看。