gpt4 book ai didi

求和的 Python 多重处理

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:01 25 4
gpt4 key购买 nike

我没有让我的代码运行,和其他人一样,我在理解多处理的工作原理方面遇到了问题。到目前为止,这是我的代码

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())

时间序列由

给出
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,因此加快速度至关重要。我的问题是,我首先不知道如何正确调用我的函数以及如何总结我的所有回复。

感谢您的任何建议!

最佳答案

此解决方案有效,我正在使用矢量化解决方案 proposed here为了避免 Python 循环:

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 多重处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com