从多核分布式处理到多核分布式处理

2024-05-29 10:15:03 发布

您现在位置:Python中文网/ 问答频道 /正文

背景

在python中,我最近学习了如何使用模块名serial_procedure.py,通过使用产生模块multicore_procedure.pymultiprocessing包来利用笔记本电脑的4个内核来加速串行过程。你知道吗

问题

现在,我要使用一个有3个节点的计算机集群,每个节点有4个核心。如何通过将multicore_procedure.py在集群的3*4核上并行化来加速它?你知道吗

更具体的要求

考虑到我在编程和软件工程方面的知识非常有限,我希望能用python编写一些明确的示例脚本,用于slurm和condor资源管理软件系统。目标是自动检测可用的3*4=12核,并编写统一的代码。你知道吗

更多信息

下面提供了示例模块serial_procdure.pymulticore_procedure.py。注意,在每一次迭代ell中,都会进行密集的计算,从而得到标记为total的值。在标签total下,将随机段落写入给定的文件名。并行进程不应将段落混合在一起,但所有进程都应写入同一个文件。你知道吗

我尝试使用mpi4py包来实现mpi,但是我似乎没有正确地实现它,因为结果的性能类似于serial_程序.py. 你知道吗

串行程序

import numpy as np
import csv
import lorem
import time

# data
high=2**10;
nsamples=2**18;
labels=np.random.randint(0,high,nsamples)

# Serial version
serial_start_time=time.time()
filename='serial.txt'
total=0
with open(filename,'w') as file:
    for ell in labels:
        # supposedly intensive computation:
        for k in range(ell):
            total=total+k; 
        w = csv.writer(file) ;
        w.writerow([total])
        w.writerow(['****'])
        w.writerow(lorem.paragraph())
        total=0;
serial_time=time.time()-serial_start_time;
print('Serial write takes '+ str(serial_time)+' seconds')
# Serial write takes 43.09408092498779 second

多核程序

import numpy as np
import csv
import lorem
import time
from multiprocessing import Pool, Value

# data
high=2**10;
nsamples=2**18;
labels=np.random.randint(0,high,nsamples)
filename='four_cores_parallel.txt'

# supposedly intensive computation
def worker(ell):
    total=0
    for k in range(ell):
        total=total+k;
    return(total)

multicore_start_time=time.time()
pool=Pool(4);

with open(filename,'w') as file:
    for total in pool.map(worker, labels):
        w = csv.writer(file) ;
        w.writerow([total])
        w.writerow(['****'])
        w.writerow(lorem.paragraph())
multicore_time=time.time()-multicore_start_time;
print('Multicore write takes '+ str(multicore_time)+' seconds')
# Multicore write takes 20.171985149383545 seconds

Tags: csvpyimporttimeasnpserialtotal

热门问题