一笔Python的多 [英] Python multiprocessing of a sum
本文介绍了一笔Python的多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白我的code运行和其他人,在了解多的作品我有问题。这里是我的code到目前为止
I dont get my code to run, and as others, i have problems in understand how multiprocessing works. here is my code so far
if __name__ == "__main__":
start = time.clock()
bins = np.linspace(0,5 * 2 ** 15, 2 ** 15, endpoint=False) # 1e3
t_full = np.linspace(0, 0.2, 2 * bins.shape[0], endpoint=False)
po = Pool()
res = po.map_async(timeseries, ((m, n, params, bins, 1, t_full, i, i + 1) for i in xrange(2 ** 15)))
signal = sum(res.get())
在这里的时间序列是由
where timeseries is given by
def timeseries_para(m, n, params, bins, seed, t, sum_min, sum_max):
np.random.seed(seed)
PSD_data = PSD(m, n, params, bins)
dataReal = np.empty_like(PSD_data)
for i in range(bins.shape[0]):
dataReal[i] = np.random.normal(PSD_data[i], 0.1 * PSD_data[i])
plt.loglog(bins, dataReal, 'red')
dataCOS = np.sqrt(dataReal)
signal = np.zeros(t.shape[0])
## Calculating timeseries
#for i in range(bins.shape[0]):
for i in range(sum_min, sum_max):
#start = time.clock()
signal += dataCOS[i] * np.cos(2 * np.pi * t * bins[i] + random.uniform(0, 2 * np.pi))
#print time.clock() - start
return signal
我的总和从0上升到2 ** 16,所以加速这件事是必不可少的。我的问题是,我第一次不知道怎么称呼我的功能正确的,我怎么能总结我的所有答复了。
My sum goes from 0 up to 2**16, so speeding this up is essential. My problem is, that i first don't know how call my function correct and how i can sum all my replies up.
感谢您的任何意见!
推荐答案
此解决方案的工作,我用href=\"http://stackoverflow.com/a/19364277/832621\">这里提出的解决方案矢量为了避免Python的环
This solution works and I am using the vectorized solution proposed here in order to avoid the Python loops:
from multiprocessing import Pool
import numpy as np
def calc(t_full, w, dataCOS):
thetas = np.multiply.outer((2*np.pi*t_full), w)
thetas += 2*np.pi*np.random.random(thetas.shape)
signal = np.cos(thetas)
signal *= dataCOS
signal = signal.sum(-1)
return signal
def parallel_calc(w, dataCOS, t_full, processes, num):
'''Parallel calculation
processes : integer
Number of processes, usually one processor for each process
num : integer
Number of sub-divisions for `w` and `dataCOS`
Must be an exact divisor of `len(w)` and `len(dataCOS)`
'''
pool = Pool(processes=processes)
#
results = []
wd = np.vstack((w, dataCOS))
for wd_s in np.split(wd.T, num):
w_s = wd_s.T[0]
d_s = wd_s.T[1]
results.append(pool.apply_async(calc, (t_full, w_s, d_s)))
#
pool.close()
pool.join()
return sum((r.get() for r in results))
if __name__ == '__main__':
w = np.random.random(1000)
dataCOS = np.random.random(1000)
t_full = np.arange(2**16)
#
parallel_calc(w, dataCOS, t_full, 4, 10)
这篇关于一笔Python的多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文