我正在尝试加速使用巨大矩阵的算法.我将它并行化以对行进行操作,并将数据矩阵放在共享内存中,这样系统就不会被堵塞.但是,我没有像我希望的那样顺利地工作,它现在抛出一个关于文件的奇怪错误,我不理解,因为我甚至没有打开文件中的文件.
模拟大致正在进行的程序,1000次迭代代表算法中发生的事情.
import multiprocessing
import ctypes
import numpy as np
shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape(10, 10)
def my_func(i, shared_array):
shared_array[i,:] = i
def pool_init(_shared_array, _constans):
global shared_array, constans
shared_array = _shared_array
constans = _constans
def pool_my_func(i):
my_func(i, shared_array)
if __name__ == '__main__':
for i in np.arange(1000):
pool = multiprocessing.Pool(8, pool_init, (shared_array, 4))
pool.map(pool_my_func, range(10))
print(shared_array)
Run Code Online (Sandbox Code Playgroud)
这引发了这个错误(我在OSX上):
Traceback (most recent call last):
File "weird.py", line 24, in <module>
pool = multiprocessing.Pool(8, pool_init, (shared_array, 4))
File "//anaconda/lib/python3.4/multiprocessing/context.py", …Run Code Online (Sandbox Code Playgroud)