Skip to main content
 首页 » 编程设计

求和的 Python 多重处理

2025年05月04日8unruledboy

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

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)