我有一個python代碼,它對Shapefile中包含的數據執行一些操作。在python中嵌入循環的多線程
除其他東西,代碼執行此:
xxx=0
for i in channels:
ptsi=mat(shapes[i].points)
xx = ptsi[:,0]
yy = ptsi[:,1]
nanx=argwhere(isnan(xx))
nany=argwhere(isnan(yy))
if (nanx == nany and len(nanx) != 0):
xx[nanx] = []
yy[nany] = []
elif (len(nanx) != 0 or len(nany) != 0):
xx = []
yy = []
if (len(xx) > 0):
yyy = 0
dd = str(i*100/N_channels) + '%\r\r'
# os.write(1,dd)
dist = zeros(len(xx)-1)
dist = dist_min
for j in channels:
pts=mat(shapes[j].points)
xx2 = pts[:,0]
yy2 = pts[:,1]
nanx=argwhere(isnan(xx2))
nany=argwhere(isnan(yy2))
if (nanx == nany and len(nanx) != 0):
xx2[nanx] = []
yy2[nany] = []
elif (len(nanx) != 0 or len(nany) != 0):
xx2 = []
yy2 = []
if (len(xx2) > 0):
if (i != j):
ds = gaa(xx,yy,xx2[0],yy2[0])
de = gaa(xx,yy,xx2[-1],yy2[-1])
nande = ~isnan(de)
nands = ~isnan(ds)
fe = np.argwhere(de<=dist)
fs = np.argwhere(ds<=dist)
nozeroe = array(where(de != 0))
nozeroe = nozeroe[0]
nozeros = array(where(ds != 0))
nozeros = nozeros[0]
if(fs.size >0):
iis=array(np.argwhere(array(ds==min(ds))))
iis = iis.flatten()
iis = iis[0]
p1 = xxx + iis
p2 = yyy
G.add_edge(p2,p1,weight=w_con)
elif (fe.size > 0):
iie=array(np.argwhere(array(de==min(de))))
iie = iie.flatten()
iie = iie[0]
p1 = xxx + iie
p2 = yyy + len(pts) -1
G.add_edge(p2,p1,weight=w_con)
yyy = yyy + len(pts)
xxx = xxx + len(ptsi)
基本上,在折線的代碼掃描和爲了在(使用Networkx)的公共圖表合併然後搜索的最小距離。這部分工作正常,但這是非常緩慢的,因爲我處理的對象數量超過了100個(它在當前版本中需要大約20個小時)。
這些嵌入的循環是效率不高,所以我想知道,如果使用多線程可能是有益的,如果是這樣,我怎麼能修改此相當部分的代碼?如果可以提供幫助,我可以使用CUDA或OpenCL。
感謝您的任何反饋!
好吧,我明白GIL的限制,但我仍然可以使用當前代碼中的多處理? – user1819779
不是現在寫的,沒有。內存不在進程之間共享,因此輸入空間需要在進程之間進行分區,然後需要合併每個進程生成的獨立圖形。如果您有任何方法可以使此問題成爲n log n問題,而不是在減少運行時方面更有效的n平方問題。 –
好吧,謝謝! – user1819779