2012-08-28 69 views
0

我是IPython並行軟件包的新手,但真的想讓它順利運行。我所擁有的是一個4D numpy數組,我想通過切片,行,列來處理第四維(時間)。處理是一個最小化例程,需要一點時間,這就是爲什麼我想並行化它。ipython map_async輸入和輸出數據

from IPython.parallel import Client 
from numpy import * 
from matplotlib.pylab import * 

c = Client() 

v = c.load_balanced_view() 
v.block=False 

def process(src, freq, d): 
     # Get slice, row, col 
     sl,r,c = src 

     # Get data 
     mm = d[:,sl,c,r] 

     # Call fitting routine 
     <fiting routine that requires freq, mm and outputs multiple parameters> 

     return <output parameters??> 


## Create the mask of what we are going to process 
mask = zeros(d[0].shape) 
mask[sl][ nonzero(d[0,sl] > 10*median(d[0])) ] = 1 

# find all non-zero points in the mask 
points = array(nonzero(mask == 1)).transpose() 

# Call async 
asyncresult = v.map_async(process, points, freq=freq, d=d) 

My功能 「過程」 需要兩個參數:1)頻率是numpy的陣列(100,1)和2)d,它是(100,50,110,110)左右。我想從配件中檢索幾個參數。

我看到的所有使用map_async的例子都有簡單的lambda函數等,輸出似乎很簡單。

我想要的是將「過程」應用於掩模不爲零的d中的每個點,並使輸出參數的映射位於同一空間中。 [補充:我得到的「process()只需要3個參數(給出1)]

(這可能需要步驟2,因爲我正在向每個進程傳遞巨大的numpy數組」d「找出數據傳遞我希望能夠找出這樣做的更有效的方式。)

感謝您的幫助。

回答

2

我身邊路過問題得到的數據做

def mapper(x): 
    return apply(x[0], x[1:]) 

然後用第一個元素所在的元組列表調用map_async我的函數和其餘元素是我函數的參數。

asyncResult = pool.map_async(mapper, [(func, arg1, arg2) for arg1, arg2 in myArgs]) 

我第一次嘗試一個lambda但顯然不能被醃所以這是一個沒有去。

+0

嗨,丹尼爾。當我用函數def f(x)使用你的解決方案時:return x我得到錯誤'DummyMod object no attribute'f''。如果我使用cos(x),它工作正常。你能告訴我的函數f有什麼問題嗎?謝謝。 – user17375