我使用
differential_evolution
中的 scipy
和 workers
来并行计算。我从 pytorch
切换到 numpy
以加快代码速度。
from torch.multiprocessing import set_start_method,Pool
if __name__ == '__main__':
#device = get_device()
device = torch.device('cpu') # testing with cpu
num_workers=int(sys.argv[1])
set_start_method("spawn",force=True)
pool=Pool(num_workers)
results = differential_evolution(likelihood, seed=np.random.seed(0),workers=pool.map,
callback=print_de, bounds=bounds, maxiter=1500,
disp=True,recombination=0.1,mutation=(0.9,1),
constraints=NonlinearConstraint(positive_definite, lb=0, ub=np.inf),
popsize=25,
polish=False
)
在我自己的笔记本电脑(m1 macbook)中,使用
torch.multiprocessing
或 multiprocessing
都可以正常工作(只需设置 workers=int(sys.argv[1])
)。
当我在 1 个节点有 256 个核心的 HPC 中测试这一点时,它的速度变慢了很多。使用
torch.multiprocessing
比 multiprocessing
更快,但一次迭代仍然比我不进行并行时慢得多。
当我使用
top
时,我可以看到正在运行的正确数量的python
实例,但CPU使用率超过100%,有些甚至每个python
为1000%,这可能是问题吗?当我使用numpy
时,CPU使用率几乎是100%。
我该如何解决这个问题?
每种情况都是独特的。但这里有一些提示: