[英]mpi4py with processes and threads
嗨这是一个非常具体的问题,所以我希望StackOverflow适用于所有编程语言,而不仅仅是javascript / html
我正在MPICH2(流行的消息传递接口)中编写一个多程序。 我的程序是用Python编写的,所以我使用MPI4Py Python绑定。 MPI最适合没有共享内存的情况,因此,它不适合多核编程。 要使用我的5节点集群的完整4个核心,我还在使用线程。 但是,我注意到使用线程实际上减慢了我的模拟速度。 我的程序是几万行代码,所以我不能把它全部搞定,但这里是导致问题的片段
from threading import Thread
...
threadIndeces=[[0,10],[11,20],[21,30],[31,40]] #subset for each thread
for indeces in treadIndeces:
t=Thread(target=foo,args=(indeces,))
t.start()
另外,我确保稍后加入线程。 如果我在没有线程的情况下运行它,并且只用所有的indeces调用foo
,它大约快10-15倍。 当我记录多线程版本的时间时,在调用t=Thread(target=foo,args=(indeces,))
创建线程大约需要0.05秒,连接类似地需要0.05秒但是t.start()
电话需要0.2秒。
start()
是一个昂贵的电话吗? 我应该改变我的做法吗? 我考虑过保留一个线程池,而不是每次迭代都创建新的线程,但它似乎不是t=Thread(target=foo,args=(indeces,))
导致速度减慢的原因。
此外,柜面有人想知道的复杂foo
,这里是它被调用的功能之一i
多次对indeces
每次迭代(非离散时间):
def HD_training_firing_rate(HD_cell):
"""During training, the firing rate is governed by the difference between the
current heading direction and the preferred heading direction. This is made
to resemble a Gaussian distribution
"""
global fabs
global exp
global direction
#loop over twice due to concurrent CW and CCW HD training
for c in [0,1]:
d=direction[c]
dp=HD_cell.dp #directional preferance
s_d=20.0 #standard deviation
s_i=min(fabs(dp-d),360-fabs(dp-d)) #circular deviation from preferred dir.
HD_cell.r[c]=exp(-s_i*s_i/(2*s_d*s_d)) #normal distribution
如果你需要线程,python可能不是你最好的选择,因为Global Interpreter Lock会阻止真正的并发。 另见Dave Beazly的令人不安的谈话 。
您可能最好只运行20个进程以保持4个核心和5个节点繁忙,并且只需使用MPI进行所有通信。
如果你真的致力于联合线程/消息传递方法,Python可能会想到C或C ++(或者我敢说Fortran?)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.