Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在共享内存中使用numpy阵列同步访问时速度较慢_Python_Arrays_Performance_Numpy_Multiprocessing - Fatal编程技术网

Python 在共享内存中使用numpy阵列同步访问时速度较慢

Python 在共享内存中使用numpy阵列同步访问时速度较慢,python,arrays,performance,numpy,multiprocessing,Python,Arrays,Performance,Numpy,Multiprocessing,我编写了一个程序,它接收大的ish数据集作为输入(~150mb文本文件),对它们进行一些计算,然后在直方图中报告结果。必须执行的计算数量与数据集中两个点的组合数量成正比,对于100万个点的数据集来说,这是非常大的(~50亿) 我希望通过使用Python的多处理模块将部分直方图数据的计算分发到各个进程,同时在共享内存中保留最终直方图的数组,以便每个进程都可以添加到其中,从而减少一些计算时间 我已经根据中描述的过程创建了一个具有多处理的工作版本,但是我发现它实际上比我以前编写的非并行版本稍微慢一些。

我编写了一个程序,它接收大的ish数据集作为输入(~150mb文本文件),对它们进行一些计算,然后在直方图中报告结果。必须执行的计算数量与数据集中两个点的组合数量成正比,对于100万个点的数据集来说,这是非常大的(~50亿)

我希望通过使用Python的
多处理
模块将部分直方图数据的计算分发到各个进程,同时在共享内存中保留最终直方图的数组,以便每个进程都可以添加到其中,从而减少一些计算时间

我已经根据中描述的过程创建了一个具有
多处理
的工作版本,但是我发现它实际上比我以前编写的非并行版本稍微慢一些。我尝试取消对共享阵列的同步访问,发现这大大加快了速度,但会导致部分数据丢失

下面是代码的概要:

import numpy as np
from multiprocessing import Pool, Array

BINS = 200
DMAX = 3.5
DMIN = 0

def init(histo):
    global histo_shared
    histo_shared = histo

def to_np_array(mp_array):
    return np.frombuffer(mp_array.get_obj())

# synchronize access to shared array
def calc_sync(i):
    with histo_shared.get_lock():
        calc_histo(i)

def calc_histo(i):
    # create new array 'd_new' by doing some math on DATA using argument i
    histo = to_np_array(histo_shared)
    histo += np.histogram(d_new, bins=BINS,
        range=(DMIN, DMAX))[0].astype(np.int32)

def main():
    # read in data and calculate no. of iterations
    global DATA
    DATA = np.loadtxt("data.txt")
    it = len(DATA) // 2

    # create shared array 
    histo_shared = Array('l',  BINS)

    # write to shared array from different processes
    p = Pool(initializer=init, initargs=(histo_shared,))
        for i in range(1, it + 1):
            p.apply_async(calc_sync, [i])
    p.close()
    p.join()

    histo_final = to_np_array(histo_shared)
    np.savetxt("histo.txt", histo_final)

if __name__ == '__main__':
    main()
我在这里缺少的东西对我的表现有严重影响吗?我有没有办法绕过这个问题来加快速度


非常感谢您的任何见解或建议

您实际上是在锁定可能获得的任何并行性,因为在整个处理过程中,您的数据都会被锁定

当这个方法

def calc_sync(i):
    with histo_shared.get_lock():
        calc_histo(i)
在执行时,您在处理直方图时锁定了整个共享数据集。还要注意

def calc_histo(i):
    # create new array 'd_new' by doing some math on DATA using argument i
    histo = to_np_array(histo_shared)
    histo += np.histogram(d_new, bins=BINS,
        range=(DMIN, DMAX))[0].astype(np.int32)
没有对我做任何事情,所以看起来你在重复处理相同的数据。你有什么新鲜事?我在你的清单上没有看到

理想情况下,您应该做的是获取大型数据集,将其分为若干块,分别进行处理,然后合并结果。只锁定共享数据,不锁定处理步骤。这可能看起来像这样:

def calc_histo(slice):
    # process the slice asyncronously
    return np.histogram(slice, bins=BINS,
        range=(DMIN, DMAX))[0].astype(np.int32)

def calc_sync(start,stop):

    histo = None

    # grab a chunk of data, you likely don't need to lock this
    histo = raw_data[start:stop]

    # acutal calculation is async
    result = calc_histo(histo)

    with histo_shared.get_lock():
         histo_shared += result
对于成对数据:

def calc_sync(part1,part2):

    histo = None
    output = [] # or numpy array
    # acutal calculation is async
    for i in range(part1):
        for j in range(part2):
              # do whatever computation you need and add it to output

    result = calc_histo(output)

    with histo_shared.get_lock():
         histo_shared += result
现在呢

 p = Pool(initializer=init, initargs=(histo_shared,))
 for i in range(1, it + 1,slice_size):
     for j in range(1, it + 1,slice_size):
         p.apply_async(calc_sync, [histo_shared[j:j+slice_size], histo_shared[i:i+slice_size])

换句话说,我们对数据进行两两切割,生成相关数据,然后将它们放入柱状图中。唯一需要的真正同步是在组合直方图中的数据时

不幸的是,我需要整个数组来计算直方图数据,因为它需要比较数组中的每个元素。i用于确定如何比较它们;对不起,如果我不清楚的话!无论如何,关于锁定处理步骤的问题,您是对的。我将您的解决方案改编为我的代码,现在它的工作速度快得多。非常感谢。我不太清楚你想如何将它并行化。如果要对数组中的元素进行两两比较,则可以对数组进行两个切片,请参见我的编辑