写点什么

毅硕 HPC | 配置 SLURM 作业队列优先级

作者:INSVAST
  • 2025-11-18
    广东
  • 本文字数:10089 字

    阅读完需:约 33 分钟

毅硕HPC | 配置SLURM作业队列优先级

Slurm (Simple Linux Utility for Resource Management, http://slurm.schedmd.com/ )是一个开源的、具有容错性、高度可扩展的集群管理和作业调度系统,适用于大型和小型 Linux 集群。Slurm 不需要对内核进行修改,它的运行方式相对独立以避免节点相互干扰,提高运行效率。


作为集群工作负载管理器, Slurm 具有三个关键功能。首先,它分配独占和或非独占节点提供给用户在一段时间内访问资源(计算节点),以便他们可以执行业务计算。其次,它提供了一个用于启动、执行和监视分配节点上的工作(通常是并行作业)的框架。最后它通过管理待处理作业的队列来仲裁资源争用。所有需运行的作业,无论是用于程序调试还是业务计算,都可以通过交互式并行 srun 、批处理式 sbatch 或分配式 salloc 等命令提交,提交后可以利用相关命令查询作业状态等。


为了更有效地管理和分配资源,优化作业调度,提升系统利用率,并满足多样化的作业需求,队列成为任务调度中不可或缺的配置项。合理的队列设置能够确保高优先级的任务优先获得所需资源,从而最大化资源利用效率。


本文介绍在 Slurm 系统环境下,当出现作业提交或作业状态变化时,如何通过恰当的队列配置策略来实现尽可能多的任务调度处理,以达到最佳性能。


一、Slurm 队列类型

Slurm 任务按优先级排序执行,若分区存在不可调度任务,则后续任务暂停。高优先级任务可抢占低优先级任务资源,被抢占任务可以取消、重置或挂起。如果您启用回填调度(默认),按 bf_interval 周期计算低优任务能否在不延迟高优任务前提下运行,需占用整机资源并可能触发整机抢占。调度配置通过 slurm.conf 指定 SchedulerType (默认 sched/backfill 插件)及详细参数 SchedulerParameters ,具体参数配置,请参见官方文档(https://slurm.schedmd.com/sched_config.html)。


在调度过程中,所有任务都会被整合到一个列表中,并通过不同的优先级算法来确定执行顺序。Slurm 支持以下两种队列类型:

  • 先进先出(FIFO)队列,任务的排序依据是它们提交的时间顺序。

  • 多因素(MultiFactors)队列,是一种更高级的任务排队机制,默认处于启用状态,它能够根据多个因素综合计算作业的优先级。

1. 先进先出(FIFO)队列

默认情况下,Slurm 采用先进先出 FIFO 为基础分配作业优先级。关于优先级调度的配置文件存放 slurm.conf 中,可以通过修改参数 PriorityType 来配置优先级。

1.找到并编辑slurm.conf文件sudo nano /etc/slurm/slurm.conf
2.启用抢占模式,并指定基于先进先出优先级的抢占策略PriorityType=priority/basic
复制代码

2. 多因素(MultiFactors)队列

Slurm 多因素调度通过加权计算以下因子确定任务优先级:作业执行时长、资源差异(已分配 vs 已消耗)、作业规模、用户参数、数据分区、TRES(资源等价值)类型及服务质量(QOS)。权重分配与具体计算逻辑,请参见多因素优先级配置说明(https://slurm.schedmd.com/priority_multifactor.html)。

Job_priority =        site_factor +        (PriorityWeightAge) * (age_factor) +        (PriorityWeightAssoc) * (assoc_factor) +        (PriorityWeightFairshare) * (fair-share_factor) +        (PriorityWeightJobSize) * (job_size_factor) +        (PriorityWeightPartition) * (priority_job_factor) +        (PriorityWeightQOS) * (QOS_factor) +        SUM(TRES_weight_cpu * TRES_factor_cpu,            TRES_weight_<type> * TRES_factor_<type>,            ...)        - nice_factor
复制代码

Slurm 作业优先级通过以下加权因子计算:

  • 基础值:site_factor(自定义分值)。

  • 作业等待时间权重:作业等待时间越长,权重越高(PriorityWeightAge × age_factor)。

  • 关联权重:用户组/账户的资源使用公平性(PriorityWeightAssoc × assoc_factor)。

  • 公平共享权重:按资源使用比例调整分值(PriorityWeightFairshare × fair-share_factor)。

  • 作业大小权重:小/大作业优先(PriorityWeightJobSize × job_size_factor)。

  • 分区权重:分区优先级(PriorityWeightPartition × priority_job_factor)。

  • QoS 权重:服务质量等级(PriorityWeightQOS × QOS_factor)。

  • 资源权重:资源类型(CPU/GPU 等)权重加权。

  • Nice 降级:- nice_factor(数值越大,优先级越低)。

您可以通过动态调整权重参数,实现公平、高效的任务调度。


典型应用示例:

  • 快速完成小作业:设置 PriorityWeightJobSize=-1,大作业的优先级降低,小作业更快被调度。

  • 保障关键用户/组:通过 PriorityWeightAssocFair-share_factor 确保重要团队的作业优先运行。

  • 资源饥饿保护:配置 PriorityWeightFairshare=2000,低资源使用量的用户作业优先级显著提升。


二、设置多因素作业优先级

1. 设置分区优先级

  • 在配置文件中打开抢占功能开关,抢占类型设置为 preempt/partition_prio

# 1.找到并编辑slurm.conf文件sudo nano /etc/slurm.conf
# 2.启用抢占模式,并指定基于分区优先级的抢占策略PreemptType=preempt/partition_prio # 3.当作业被抢占时的行为,定义了当一个作业被抢占时会发生什么。# cancel表示取消该作业;suspend则会暂停它直到资源再次可用。。PreemptMode=suspend # 或者 "cancel"
复制代码
  • 相关参数

  • 添加一个高优先级分区

在 slurm.conf 中新增一条分区记录如:

# sudo nano /etc/slurm/slurm.conf...NodeName=rhel-efserver NodeAddr=192.168.1.25  CPUs=8 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=1024 Procs=1 State=UNKNOWNNodeName=rhel-compute NodeAddr=192.168.1.24  CPUs=16 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=10240 Procs=1 State=UNKNOWNNodeName=rhel-dcv NodeAddr=192.168.1.26  CPUs=8 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=8192 Procs=1 State=UNKNOWNNodeName=rhel-openscow NodeAddr=192.168.1.28  CPUs=16 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=4096 Procs=1 State=UNKNOWNNodeName=rhel-login NodeAddr=192.168.1.27  CPUs=8 CoresPerSocket=2 ThreadsPerCore=1 RealMemory=2048 Procs=1 State=UNKNOWN
PartitionName=compute Nodes=rhel-compute Default=YES MaxTime=INFINITE State=UP OverSubscribe=FORCE:1PartitionName=dcv Nodes=rhel-dcv Default=NO MaxTime=INFINITE State=UPPartitionName=debug Nodes=rhel-dcv,rhel-efserver,rhel-compute,rhel-openscow,rhel-login Default=NO MaxTime=INFINITE State=UP
# high_prio_debug分区优先级Priority=10000,其他默认为1PartitionName=high_prio_debug Nodes=rhel-dcv,rhel-efserver,rhel-compute,rhel-openscow,rhel-login Priority=10000 Default=NO MaxTime=INFINITE State=UP
复制代码

查看分区信息:

[root@rhel-openscow ~]# scontrol show partitions
PartitionName=compute AllowGroups=ALL AllowAccounts=ALL AllowQos=ALL AllocNodes=ALL Default=YES QoS=N/A DefaultTime=NONE DisableRootJobs=NO ExclusiveUser=NO ExclusiveTopo=NO GraceTime=0 Hidden=NO MaxNodes=UNLIMITED MaxTime=UNLIMITED MinNodes=0 LLN=NO MaxCPUsPerNode=UNLIMITED MaxCPUsPerSocket=UNLIMITED Nodes=rhel-compute PriorityJobFactor=1 PriorityTier=1 RootOnly=NO ReqResv=NO OverSubscribe=FORCE:1 OverTimeLimit=NONE PreemptMode=GANG,SUSPEND State=UP TotalCPUs=16 TotalNodes=1 SelectTypeParameters=NONE JobDefaults=(null) DefMemPerNode=UNLIMITED MaxMemPerNode=UNLIMITED TRES=cpu=16,mem=10G,node=1,billing=16
PartitionName=dcv AllowGroups=ALL AllowAccounts=ALL AllowQos=ALL AllocNodes=ALL Default=NO QoS=N/A DefaultTime=NONE DisableRootJobs=NO ExclusiveUser=NO ExclusiveTopo=NO GraceTime=0 Hidden=NO MaxNodes=UNLIMITED MaxTime=UNLIMITED MinNodes=0 LLN=NO MaxCPUsPerNode=UNLIMITED MaxCPUsPerSocket=UNLIMITED Nodes=rhel-dcv PriorityJobFactor=1 PriorityTier=1 RootOnly=NO ReqResv=NO OverSubscribe=NO OverTimeLimit=NONE PreemptMode=GANG,SUSPEND State=UP TotalCPUs=8 TotalNodes=1 SelectTypeParameters=NONE JobDefaults=(null) DefMemPerNode=UNLIMITED MaxMemPerNode=UNLIMITED TRES=cpu=8,mem=8G,node=1,billing=8
PartitionName=debug AllowGroups=ALL AllowAccounts=ALL AllowQos=ALL AllocNodes=ALL Default=NO QoS=N/A DefaultTime=NONE DisableRootJobs=NO ExclusiveUser=NO ExclusiveTopo=NO GraceTime=0 Hidden=NO MaxNodes=UNLIMITED MaxTime=UNLIMITED MinNodes=0 LLN=NO MaxCPUsPerNode=UNLIMITED MaxCPUsPerSocket=UNLIMITED Nodes=rhel-compute,rhel-dcv,rhel-efserver,rhel-login,rhel-openscow PriorityJobFactor=1 PriorityTier=1 RootOnly=NO ReqResv=NO OverSubscribe=NO OverTimeLimit=NONE PreemptMode=GANG,SUSPEND State=UP TotalCPUs=56 TotalNodes=5 SelectTypeParameters=NONE JobDefaults=(null) DefMemPerNode=UNLIMITED MaxMemPerNode=UNLIMITED TRES=cpu=56,mem=25G,node=5,billing=56
PartitionName=high_prio_debug AllowGroups=ALL AllowAccounts=ALL AllowQos=ALL AllocNodes=ALL Default=NO QoS=N/A DefaultTime=NONE DisableRootJobs=NO ExclusiveUser=NO ExclusiveTopo=NO GraceTime=0 Hidden=NO MaxNodes=UNLIMITED MaxTime=UNLIMITED MinNodes=0 LLN=NO MaxCPUsPerNode=UNLIMITED MaxCPUsPerSocket=UNLIMITED Nodes=rhel-compute,rhel-dcv,rhel-efserver,rhel-login,rhel-openscow PriorityJobFactor=10000 PriorityTier=10000 RootOnly=NO ReqResv=NO OverSubscribe=NO OverTimeLimit=NONE PreemptMode=GANG,SUSPEND State=UP TotalCPUs=56 TotalNodes=5 SelectTypeParameters=NONE JobDefaults=(null) DefMemPerNode=UNLIMITED MaxMemPerNode=UNLIMITED TRES=cpu=56,mem=25G,node=5,billing=56
复制代码

连续提交多个任务:srun sleep 1d &

[root@rhel-openscow ~]# squeue             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)                61   compute    sleep     root PD       0:00      1 (Priority)                60   compute    sleep     root PD       0:00      1 (Resources)                58   compute    sleep     root  R       0:17      1 rhel-compute                59   compute    sleep     root  R       0:17      1 rhel-compute                56   compute    sleep     root  R       0:18      1 rhel-compute                57   compute    sleep     root  R       0:18      1 rhel-compute                54   compute    sleep     root  R       0:19      1 rhel-compute                55   compute    sleep     root  R       0:19      1 rhel-compute                52   compute    sleep     root  R       0:20      1 rhel-compute                53   compute    sleep     root  R       0:20      1 rhel-compute                50   compute    sleep     root  R       0:21      1 rhel-compute                51   compute    sleep     root  R       0:21      1 rhel-compute                48   compute    sleep     root  R       0:22      1 rhel-compute                49   compute    sleep     root  R       0:22      1 rhel-compute                47   compute    sleep     root  R       0:23      1 rhel-compute                45   compute    sleep     root  R       0:24      1 rhel-compute                46   compute    sleep     root  R       0:24      1 rhel-compute                44   compute    sleep     root  R       0:26      1 rhel-compute
复制代码

在高优先级分区 high_prio_debug 提交任务:

# 任务44的ST(状态)从R变为了S,任务62的状态变为了R,说明任务44被挂起,任务62抢占资源运行[root@rhel-openscow ~]# srun --partition=high_prio_debug -w rhel-compute sleep 1d &[19] 62843[root@rhel-openscow ~]#[root@rhel-openscow ~]# squeue             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)                60   compute    sleep     root PD       0:00      1 (Resources)                61   compute    sleep     root PD       0:00      1 (Priority)                58   compute    sleep     root  R       0:52      1 rhel-compute                59   compute    sleep     root  R       0:52      1 rhel-compute                56   compute    sleep     root  R       0:53      1 rhel-compute                57   compute    sleep     root  R       0:53      1 rhel-compute                54   compute    sleep     root  R       0:54      1 rhel-compute                55   compute    sleep     root  R       0:54      1 rhel-compute                52   compute    sleep     root  R       0:55      1 rhel-compute                53   compute    sleep     root  R       0:55      1 rhel-compute                50   compute    sleep     root  R       0:56      1 rhel-compute                51   compute    sleep     root  R       0:56      1 rhel-compute                48   compute    sleep     root  R       0:57      1 rhel-compute                49   compute    sleep     root  R       0:57      1 rhel-compute                47   compute    sleep     root  R       0:58      1 rhel-compute                45   compute    sleep     root  R       0:59      1 rhel-compute                46   compute    sleep     root  R       0:59      1 rhel-compute                44   compute    sleep     root  S       0:56      1 rhel-compute                62 high_prio    sleep     root  R       0:05      1 rhel-compute
复制代码

更新任务为高优先级任务:

# 任务60立即运行,任务45的ST(状态)从R变为了S,说明任务45被挂起,任务60抢占资源运行[root@rhel-openscow ~]# scontrol update jobid=60 partition=high_prio_debug nodelist=rhel-compute[root@rhel-openscow ~]# srun: job 60 has been allocated resources
[root@rhel-openscow ~]# squeue JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) 61 compute sleep root PD 0:00 1 (Resources) 58 compute sleep root R 5:21 1 rhel-compute 59 compute sleep root R 5:21 1 rhel-compute 56 compute sleep root R 5:22 1 rhel-compute 57 compute sleep root R 5:22 1 rhel-compute 54 compute sleep root R 5:23 1 rhel-compute 55 compute sleep root R 5:23 1 rhel-compute 52 compute sleep root R 5:24 1 rhel-compute 53 compute sleep root R 5:24 1 rhel-compute 50 compute sleep root R 5:25 1 rhel-compute 51 compute sleep root R 5:25 1 rhel-compute 48 compute sleep root R 5:26 1 rhel-compute 49 compute sleep root R 5:26 1 rhel-compute 47 compute sleep root R 5:27 1 rhel-compute 46 compute sleep root R 5:28 1 rhel-compute 45 compute sleep root S 5:20 1 rhel-compute 44 compute sleep root S 0:56 1 rhel-compute 60 high_prio sleep root R 0:08 1 rhel-compute 62 high_prio sleep root R 4:34 1 rhel-compute
复制代码

2. 设置 QOS 服务质量优先级

Slurm 需配置高/低优先级 QOS(默认已存在优先级 0 的 normal),并通过 sacctmgr 创建高优 QOS 启用抢占。需在 slurm.conf 开启抢占功能(如 PreemptMode=priority)。


但需注意:若 PreemptType=SUSPEND,GANG ,高优任务抢占后,低优任务会以分时模式与高优任务共存(非完全中断)。配置 QOS 需要使用 sacctmgr 工具,以下是创建一个高优 QOS 的常用命令。

sacctmgr add qos high preempt=normal preemptmode=gang,suspend priority=10
复制代码


  • preempt=normal:指定 high QoS 可抢占 normal QoS 的任务。

  • preemptmode=gang,suspend

  • Gang 模式:抢占任务需完全获取资源后才开始执行。

  • Suspend 模式:被抢占任务暂停而非终止,释放资源供抢占者使用,待抢占任务结束时恢复执行。

  • priority=10high QoS 任务默认优先级基分为 10(数值越高优先级越高)。 在 slurm.conf 中打开抢占相关开关涉及到以下参数,同时在配置 Partition 时,需要在配置的最后增加 OverSubscribe=FORCE:1

查看当前 QOS:

[root@rhel-openscow ~]# sacctmgr show qos format=name      Name----------    normal       low
复制代码

创建高优先级 QOS:

[root@rhel-openscow ~]# sacctmgr add qos high preempt=normal preemptmode=gang,suspend priority=10 Adding QOS(s)  high Settings  Description    = high  Preempt                  = normal  PreemptMode              = GANG,SUSPEND  Priority                 = 10Would you like to commit changes? (You have 30 seconds to decide)(N/y): y[root@rhel-openscow ~]#[root@rhel-openscow ~]# sacctmgr show qos format=name      Name----------    normal       low      high[root@rhel-openscow ~]# sacctmgr show qos format=name,priority,preempt      Name   Priority    Preempt---------- ---------- ----------    normal          0       low          0      high         10     normal
复制代码

新建测试脚本:

# sudo nano test.sh#!/bin/bashsrun sleep 10m
复制代码

连续提交多个任务:

# sbatch test.sh# ...[root@rhel-openscow ~]# squeue             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)                79   compute  test.sh     root PD       0:00      1 (Resources)                77   compute  test.sh     root  R       0:07      1 rhel-compute                78   compute  test.sh     root  R       0:07      1 rhel-compute                73   compute  test.sh     root  R       0:10      1 rhel-compute                74   compute  test.sh     root  R       0:10      1 rhel-compute                75   compute  test.sh     root  R       0:10      1 rhel-compute                76   compute  test.sh     root  R       0:10      1 rhel-compute                71   compute  test.sh     root  R       0:13      1 rhel-compute                72   compute  test.sh     root  R       0:13      1 rhel-compute                67   compute  test.sh     root  R       0:14      1 rhel-compute                68   compute  test.sh     root  R       0:14      1 rhel-compute                69   compute  test.sh     root  R       0:14      1 rhel-compute                70   compute  test.sh     root  R       0:14      1 rhel-compute                64   compute  test.sh     root  R       0:17      1 rhel-compute                65   compute  test.sh     root  R       0:17      1 rhel-compute                66   compute  test.sh     root  R       0:17      1 rhel-compute                63   compute  test.sh     root  R       0:20      1 rhel-compute
复制代码

向高优先级 QOS 提交任务:

# 高优先级QOS任务开始执行,通过分时的方式与其他任务共享资源[root@rhel-openscow ~]# sbatch -w rhel-compute --qos=high test.shSubmitted batch job 80[root@rhel-openscow ~]# squeue             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)                79   compute  test.sh     root PD       0:00      1 (Resources)                77   compute  test.sh     root  R       0:44      1 rhel-compute                78   compute  test.sh     root  R       0:44      1 rhel-compute                73   compute  test.sh     root  R       0:47      1 rhel-compute                74   compute  test.sh     root  R       0:47      1 rhel-compute                75   compute  test.sh     root  R       0:47      1 rhel-compute                76   compute  test.sh     root  R       0:47      1 rhel-compute                71   compute  test.sh     root  R       0:50      1 rhel-compute                72   compute  test.sh     root  R       0:50      1 rhel-compute                67   compute  test.sh     root  R       0:51      1 rhel-compute                68   compute  test.sh     root  R       0:51      1 rhel-compute                69   compute  test.sh     root  R       0:51      1 rhel-compute                70   compute  test.sh     root  R       0:51      1 rhel-compute                64   compute  test.sh     root  R       0:54      1 rhel-compute                65   compute  test.sh     root  R       0:54      1 rhel-compute                66   compute  test.sh     root  R       0:54      1 rhel-compute                63   compute  test.sh     root  R       0:57      1 rhel-compute                80   compute  test.sh     root  S       0:00      1 rhel-compute
复制代码

3. 设置作业大小优先级

作业大小优先级是由 PriorityWeightJobSizePriorityWeightAge=1000 共同决定。


作业大小因素

非紧急任务需高效利用集群资源(不超期限)。当任务执行时间未知时,回填调度失效,此时优先调度小任务减少队头阻塞,同时依据排队时间提升大任务优先级防饿死;临近截止的大任务可抢占小任务资源(挂起小任务直至其完成)。

为提高非紧急任务集群利用率(不超过截止时间),您可以采取以下策略进行设置:

  • 优先调度小任务减少队头阻塞。

  • 按排队时长提升大任务优先级防饿死。

  • 临近截止的大任务可抢占小任务资源(小任务挂起至其完成)。任务执行时间未知时,回填调度失效,需以上机制保障资源高效利用。

通过实施上述措施,可以在保证关键任务按时完成的同时最大化利用集群资源,同时也兼顾了不同类型任务之间的平衡。

在 slurm.conf 中需要进行如下的配置(这里只展示特殊配置,slurm.conf 中的其他配置不受影响):

PriorityFavorSmall=YESPriorityWeightAge=1000PriorityWeightJobSize=1000PriorityMaxAge=1-0
复制代码


作业等待时间因素

  • 当设置完成作业大小优先级后,提交后等待时间为第二因素。Slurm 通过任务请求资源与集群总资源的占比计算任务大小得分;若启用 PriorityFavorSmall=YES ,得分公式为:得分 = (1 - 资源占比) × PriorityWeightJobSize。例如,当集群可用 4 核 CPU 时:

  • 请求 1 核任务得分: (1 - 1/4) × 权重 = 0.75×权重 → 示例得分为 0.375(若权重为 0.5)

  • 请求 4 核任务得 0 分(完全占用资源)。

  • AgeFactor 优先级计算:

  • 超过 PriorityMaxAge 的任务:直接得 PriorityWeightAge 全分。

  • 其他任务按提交时间占比得分,例如设置 PriorityWeightAge=1000 时,每分钟增加约 0.69 分,累计至 24 小时后得满分 1000。

  • 回填调度建议,若可预估任务执行时间,建议启用默认的回填调度(或手动配置 SchedulerType=sched/backfill ),使其通过时间表调度小任务填充至大任务空闲时段。结合系统默认的大任务优先机制及临近截止时间抢占功能,可平衡资源利用率与公平性。

用户头像

INSVAST

关注

还未添加个人签名 2023-07-04 加入

还未添加个人简介

评论

发布
暂无评论
毅硕HPC | 配置SLURM作业队列优先级_高性能_INSVAST_InfoQ写作社区