与dask并行比顺序编码要慢。
我有一个嵌套的for循环,我试图在本地群集上并行处理,但是找不到正确的方法。
我想并行处理内部循环。
我有2个大的numpy矩阵,我试图对其进行迭代并对矩阵的子集执行数学计算。 尺寸:
data_mat.shape = (38, 243863)
indicies_mat.shape (243863, 27)
idxX.shape = (19,)
idxY.shape = (19,)
seq_code:
start = datetime.datetime.now()
for i in range(num+1):
if i == 0:
labels = np.array(true_labels)
else:
labels = label_mat[i]
idxX = list(np.where(labels == 1))
idxY = list(np.where(labels == 2))
ansColumn = []
for j in range(indices.shape[0]):
list_of_indices = [[i] for i in indices_slice]
dataX = (data_mat[idxX, list_of_indices]).T
dataY = (data_mat[idxY, list_of_indices]).T
ansColumn.append(calc_func(dataX, dataY))
if i == 0:
ansMat = ansColumn
else:
ansMat = np.c_[ansMat, ansColumn]
end = datetime.datetime.now()
print(end - start)
并行代码:
start = datetime.datetime.now()
cluster = LocalCluster(n_workers=4, processes=False)
client = Client(cluster)
for i in range(num+1):
if i == 0:
labels = np.array(true_labels)
else:
labels = label_mat[i]
idxX = list(np.where(labels == 1))
idxY = list(np.where(labels == 2))
[big_future] = client.scatter([data_mat], broadcast=True)
[idx_b] = client.scatter([idxX], broadcast=True)
[idy_b] = client.scatter([idxY], broadcast=True)
futures = [client.submit(prep_calc_func, idx_b, idy_b, indices[j, :], big_future) for j in range(indices.shape[0])]
ansColumn = []
for fut in dask.distributed.client.as_completed(futures):
ansColumn.append(fut.result())
if i == 0:
ansMat = ansColumn
else:
ansMat = np.c_[ansMat, ansColumn]
end = datetime.datetime.now()
print(end - start)
辅助功能:
def = prep_calc_func(idxX, idxY, subset_of_indices, data_mat):
list_of_indices = [[i] for i in indices_slice]
dataX = (data_mat[idxX, subset_of_indices]).T
dataY = (data_mat[idxY, subset_of_indices]).T
ret_val = calc_func(dataX, dataY)
return ret_val
本地计算机:MacBook Pro(Retina,13英寸,2014年中) 处理器:2.6 GHz Intel Core i5
hw.physicalcpu:2 hw.logicalcpu:4
内存:8 GB 1600 MHz DDR3
当我执行seq代码时,需要花费01:52分钟(不到2分钟)完成
但是当我尝试并行代码时,它花费了超过15分钟的时间。 (无论我使用哪种方法:计算,结果和client.submit或dask延迟)
(我更喜欢使用dask分布式软件包,因为下一阶段也可能使用远程集群。)
知道我在做什么错吗?
答案 0 :(得分:0)
为什么有些东西变慢可能有很多原因。可能会有很多交流。您的任务可能太小(回想一下Dask每个任务的开销大约为1ms),或者完全是其他事情。有关了解Dask性能的更多信息,我建议以下文档: