gpt4 book ai didi

python - 如何在python中轻松实现并行?

转载 作者:行者123 更新时间:2023-12-03 13:08:46 25 4
gpt4 key购买 nike

我是一名 C++ 专家,我喜欢并行性、HPC 应用程序,而且我非常了解 OpenMP。现在我正在学习 python 并且我了解基础知识。

作为提高我的 python 技能的个人项目,我想实现 Elkan algorithm for k-means 的并行版本.该算法的 C 和 OpenMP 并行版本是 present in VLFeat .

现在,我知道如何在 python 中轻松实现 Elkan 算法...但是并行性呢? OpenMP 的妙处在于您采用串行代码,添加#pragma omp parallel for 和 SBAM,它是并行的!

我怎样才能在 python 中实现类似的东西?还是我必须从一开始就设计并行算法,即不是先编写串行版本然后使其并行,而是显式管理线程等(这是一个巨大的痛苦)?

最佳答案

首先,Python 中的 CPU 并行性只能通过 native 代码或多进程获得。

其次,与 OpenMP 的类比是 multiprocessing模块。

它有很多功能,但是对于简单的 for 循环等,它非常容易使用。

results = map(operation, things)

会变成

import multiprocessing
pool = multiprocessing.Pool()
results = pool.map(operation, things)

关于python - 如何在python中轻松实现并行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46152722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com