[英]Multiprocessing with numpy makes Python quit unexpectedly on OSX
當遇到numpy運行多處理時,我遇到了一個問題,即Python意外退出。 我已經解決了這個問題,所以我現在可以確認在運行下面描述的代碼時多處理工作是完美的:
import numpy as np
from multiprocessing import Pool, Process
import time
import cPickle as p
def test(args):
x,i = args
if i == 2:
time.sleep(4)
arr = np.dot(x.T,x)
print i
if __name__ == '__main__':
x = np.random.random(size=((2000,500)))
evaluations = [(x,i) for i in range(5)]
p = Pool()
p.map_async(test,evaluations)
p.close()
p.join()
當我嘗試評估下面的代碼時會出現問題。 這使Python意外退出:
import numpy as np
from multiprocessing import Pool, Process
import time
import cPickle as p
def test(args):
x,i = args
if i == 2:
time.sleep(4)
arr = np.dot(x.T,x)
print i
if __name__ == '__main__':
x = np.random.random(size=((2000,500)))
test((x,4)) # Added code
evaluations = [(x,i) for i in range(5)]
p = Pool()
p.map_async(test,evaluations)
p.close()
p.join()
請幫助別人。 我對所有建議持開放態度。 謝謝。 注意:我嘗試過兩台不同的機器,出現同樣的問題。
這是MacOS X上多處理和numpy的已知問題,並且有點重復:
在osx上使用numpy的lapack_lite和多處理的段錯誤,而不是linux
http://mail.scipy.org/pipermail/numpy-discussion/2012-August/063589.html
答案似乎是在連接Numpy時使用除Apple加速框架之外的其他BLAS ...不幸的是:(
我找到了問題的解決方法。 在初始化多處理實例之前,將Numpy與BLAS一起使用時會出現問題。 我的解決方法是簡單地將Numpy代碼(運行BLAS)放入一個進程,然后運行多處理實例。 這不是一個好的編碼風格,但它的工作原理。 見下面的例子:
以下將失敗 - Python將退出:
import numpy as np
from multiprocessing import Pool, Process
def test(x):
arr = np.dot(x.T,x) # On large matrices, this calc will use BLAS.
if __name__ == '__main__':
x = np.random.random(size=((2000,500))) # Random matrix
test(x)
evaluations = [x for _ in range(5)]
p = Pool()
p.map_async(test,evaluations) # This is where Python will quit, because of the prior use of BLAS.
p.close()
p.join()
以下將成功:
import numpy as np
from multiprocessing import Pool, Process
def test(x):
arr = np.dot(x.T,x) # On large matrices, this calc will use BLAS.
if __name__ == '__main__':
x = np.random.random(size=((2000,500))) # Random matrix
p = Process(target = test,args = (x,))
p.start()
p.join()
evaluations = [x for _ in range(5)]
p = Pool()
p.map_async(test,evaluations)
p.close()
p.join()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.