- 概览
- 安装
- 教程
- 算法接口文档
- 简易高效的并行接口
- APIS
- FREQUENTLY ASKED QUESTIONS
- EVOKIT
- 其他
- parl.algorithms.paddle.policy_gradient
- parl.algorithms.paddle.dqn
- parl.algorithms.paddle.ddpg
- parl.algorithms.paddle.ddqn
- parl.algorithms.paddle.oac
- parl.algorithms.paddle.a2c
- parl.algorithms.paddle.qmix
- parl.algorithms.paddle.td3
- parl.algorithms.paddle.sac
- parl.algorithms.paddle.ppo
- parl.algorithms.paddle.maddpg
- parl.core.paddle.model
- parl.core.paddle.algorithm
- parl.remote.remote_decorator
- parl.core.paddle.agent
- parl.remote.client
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
加速案例

@parl.remote_class
,使用python的 多线程 也能够实现并行计算。 众所周知,python 的多线程并发性能并不好,很难达到传统的编程语言比如C++或者JAVA这种加速效果,主要的原因是python 有全局锁(GIL)的限制,使得其最多只能运用单核来记性运算。下面我们通过一个简单的例子来看下GIL对于python的影响。首先,我们跑下这段代码:
class A(object): def run(self): ans = 0 for i in range(100000000): ans += i a = A() for _ in range(5): a.run()这段代码需要 17.46秒 的时间来计算5次的从1累加到1亿。 接下来我们通过python的原生多线程库改造下上面的代码,让它可以多线程跑起来。
import threading class A(object): def run(self): ans = 0 for i in range(100000000): ans += i threads = [] for _ in range(5): a = A() th = threading.Thread(target=a.run) th.start() threads.append(th) for th in threads: th.join()运行这段代码之后,居然需要 41.35秒 ,比刚才的串行运算速度更慢。主要的原因是GIL限制了python只能单核运算,使用了多线程运算之后,触发了多线程竞争CPU的问题,反而延长了计算时间。 最后,我们尝试使用PARL:
import threading import parl @parl.remote_class class A(object): def run(self): ans = 0 for i in range(100000000): ans += i threads = [] parl.connect("localhost:6006") for _ in range(5): a = A() th = threading.Thread(target=a.run) th.start() threads.append(th) for th in threads: th.join()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论