2013-10-31 11 views
12

運行numpy多處理時,我遇到了Python意外退出的問題。我已經分離出的問題,所以,我現在可以確認的是,多運行下面陳述的代碼時,可以完美運行:發生使用numpy進行多處理會使Python在OSX上意外退出

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

請幫助別人。我接受所有建議。謝謝。注意:我嘗試過兩種不同的機器,發生同樣的問題。

+0

我使用WinPython跑Windows7的位/ 64位的代碼。這兩種情況都執行並退出,沒有錯誤。 – alandarev

+0

對不起。有趣的是,它可以在Windows上運行。任何能向我解釋爲什麼發生這種情況的Apple用戶? – Maal

+0

我可能誤導了你,當我對使用「意外」術語的蘋果發表憤怒評論時,無處不在。我高度懷疑你的問題是操作系統特定的。你可以嘗試運行你使用numpy安裝對付乾淨Python的腳本來查看問題是否存在? – alandarev

回答

5

我想出了一個解決問題的方法。在初始化之前將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() 
6

這是一個已知的問題,多和numpy的MacOS X上,還有一點點重複的:

segfault using numpy's lapack_lite with multiprocessing on osx, not linux

http://mail.scipy.org/pipermail/numpy-discussion/2012-August/063589.html

答案似乎是使用其他不同的BLAS比連接Numpy時蘋果加速框架...不幸:(

+2

正如@ogrisel在第一個鏈接評論中指出的那樣,在新版本的多處理中也有一個'forkserver'模式可以解決問題。 – Dougal