我有一个 python 代码,它对 Shapefile 中包含的数据进行一些操作。
除其他事项外,代码执行以下操作:
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)
基本上,代码会扫描多段线并搜索最小距离以便合并到一个公共(public)图形中(使用 Networkx)。这部分工作正常,除了这真的很慢,因为我处理了超过 100 个对象(在当前版本中大约需要 20 个小时)。
这些嵌入式循环效率不高,所以我想知道使用多线程是否有帮助,如果有帮助,我该如何修改这部分代码?如果 CUDA 或 OpenCL 有帮助,我觉得没问题。
感谢任何反馈!
请您参考如下方法:
由于 Global Interpreter Lock , Python 代码无法通过多线程充分利用多核,需要使用多处理才能充分利用多核。
