生活, 其他记录

使用Python脚本文件实现半手动的并行计算

这是之前的两篇:

其中一篇是使用sh脚本来实现这个功能,但大部分同学(包括自己)对sh脚本并不熟悉,本篇给出Python脚本实现版手动的并行计算例子。虽然在 Python 中有 multiprocessing、threading 等软件包可以实现多进程多线程并行计算,但使用起来总感觉不是特别放心,而且在原有代码的基础上需要增加的代码比较多,可能不小心会破坏原有代码产生bug,因此个人可能会倾向于半手动的并行计算

一、使用Guan软件包

Guan软件包的网址:https://py.guanjihuan.com

安装方法:

pip install --upgrade guan

需要注意的是:使用Guan软件包,最好大致看下调用函数的源码,然后再使用,可避免不必要的错误。

并行提交任务(qsub_parallel_with_guan):

import numpy as np
import guan

parameter_array = np.arange(0, 11, 2)

guan.make_sh_file(sh_filename='a', command_line='python a.py', cpu_num=1, task_name='task', cd_dir=0)

guan.copy_py_sh_file_and_qsub_task(parameter_array=parameter_array, py_filename='a', old_str_in_py='parameter=0', new_str_in_py='parameter=', sh_filename='a', qsub_task_name='task')

代码文件a.py示例:

def run(parameter_array):
    for parameter in parameter_array:
        print('hello world'+' '+str(parameter))

parameter=0
parameter_array = [parameter]
run(parameter_array)

二、自定义代码

通过运行以下Python脚本文件直接提交任务(python parallel.py),可进行修改:

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/29200
"""

import os

parameter_str_array = ['np.arange(1, 11, 1)', 'np.arange(11, 21, 1)']

index = 0
for parameter_str in parameter_str_array:
    index += 1

    # 以下处理代码文件
    old_file = 'a.py'
    new_file = 'a'+str(index)+'.py'

    # 说明:linux系统下复制用cp,windows系统下复制用copy
    os.system('cp '+old_file+' '+new_file)  # 复制python代码文件
    with open(new_file, 'r') as f:  # 读取
        content  = f.read()
    
    old_str = 'parameter_array_labeled_for_replacement = []'
    new_str = 'parameter_array_labeled_for_replacement = ' + parameter_str
    content = content.replace(old_str, new_str)

    # 如果程序需要将数据写入文件,除了需要替代参数,还需要替代文件名,方法和以上相同

    with open('a'+str(index)+'.py', 'w') as f: # 写入
        f.write(content)



    # 以下处理任务上传文件
    old_file = 'a.sh'
    new_file = 'a'+str(index)+'.sh'

    os.system('cp '+old_file+' '+new_file)  # 复制任务调度系统的sh上传文件
    with open(new_file, 'r') as f:  # 读取
        content  = f.read()
    
    old_str = 'python a.py'
    new_str = 'python a'+str(index)+'.py'
    content = content.replace(old_str, new_str)

    old_str = 'task'
    new_str = 'task_'+str(index)
    content = content.replace(old_str, new_str)

    with open('a'+str(index)+'.sh', 'w') as f: # 写入
        f.write(content)



    # 提交任务
    os.system('qsub '+new_file)

代码文件a.py示例:

def run(parameter_array):
    for parameter in parameter_array:
        print('hello world'+' '+str(parameter))

parameter_array_labeled_for_replacement = []
run(parameter_array_labeled_for_replacement)

任务上传文件a.sh示例:

#!/bin/sh
#PBS -N task
#PBS -l nodes=1:ppn=1
python a.py

更多 .sh 文件的格式可以参考:Linux常用命令和超算中PBS常用命令

424 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code