【问题标题】:How do I use Slurm to run Python parallel program using concurrent.futures?如何使用 Slurm 使用 concurrent.futures 运行 Python 并行程序?
【发布时间】:2021-07-31 05:35:35
【问题描述】:

我想使用 hpc 来运行我的代码,所以我使用 concurrent.futures 模块运行了 test.py。我有 3 个节点,每个节点有 24 个核心,每个核心有两个线程。我想运行一次 72 核(144 线程),但我的代码运行了 144 次。

test.py:

import concurrent.futures
import time


def do_something(seconds=1):
    print(f'Sleeping {seconds} second(s)')
    time.sleep(seconds)
    return f"Done Sleeping... {seconds}"


start = time.perf_counter()

secs = [2]*48
with concurrent.futures.ProcessPoolExecutor() as executor:
    reaults = executor.map(do_something, secs)
    for reault in reaults:
        print(reault)

end = time.perf_counter()

print(f'Finished in {round (end - start,2)} second(s)')

提交给 hpc 的jobs.sh

sbatch -N 3 -n 72 python3 test.py

我使用命令sbatch -N 3 ./job.sh 运行它。

结果是 48 行显示“Sleeping 2 seconds”,重复 144 次。

我还尝试了另一个job.sh

#!/bin/bash
#SBATCH --job-name=mpi4py-test
#SBATCH --nodes=3
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=72

python3 test.py

我使用sbatch -N 3 ./job.sh 运行它 但是,我得到了输出:

sbatch: Warning: can't run 1 processes on 3 nodes, setting nnodes to 1
sbatch: error: Batch job submission failed: Requested node configuration is not available

如何同时使用 72 个内核来运行我的一项任务?

【问题讨论】:

    标签: python pool slurm concurrent.futures


    【解决方案1】:
    1. 您的 python 脚本实际上告诉 python 运行函数 48 次,每项运行一次,以秒为单位。你为什么希望它显示一次消息?
    2. Slurm 在每个节点上运行此脚本至少一次
    3. ProcessPoolExecutor() 支持 max_workers 参数,更好地适应您的问题
    4. --cpus-per-task=72 可能对 python 没用,因为它是 per-process 限制,但是由于 GIL,python 进程不能使用 >1 个内核,而且我不确定 slurm 是如何计算的子进程。

    因此,如果您想在一个作业中使用所有 3 个节点、所有 72 个 CPU,则将资源管理完全委托给 slurm 可能更容易。尝试分解你的任务,在你的 python 脚本中做一个小动作

    import sys, time
    
    def do_something(seconds=1):
        print(f'Sleeping {seconds} second(s)')
        time.sleep(seconds)
        return f"Done Sleeping... {seconds}"
    
    jobs = sys.argv[1:]
    
    for job in jobs:
        do_something(int(job))
    

    然后在job.sh 中完成其余的工作

    【讨论】:

    • 嗨,对不起。我没有准确表达我的意思。我希望我的代码使用 72 个内核打印 48 行“Sleeping ...”,因此每行与一个或多个内核相关,总时间不超过 2 秒。但现在是每个核心自己打印 48 行,并重复 72 次。总时间为 2*48 = 96 秒 >> 2 秒。所以我想用full of core只运行一个任务,不重复72次
    猜你喜欢
    • 2018-08-22
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2020-10-24
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多