Pymunk多核碰撞解决?

huangapple go评论81阅读模式
英文:

Pymunk multi-core collision resolution?

问题

我有一个非常基本的空间设置,其中有大量(10000)个受重力影响的圆在下落。似乎只有一个核心在任何时候都处于活动状态,但据我所知,通过空间分割实现的碰撞解决是非常可并行化的,而 Chipmunk 引擎确实实现了这一点。

即使我在 Linux 上使用 threaded=Truespace.use_spatial_hash() 创建空间,问题仍然存在。我真的需要并行线程提供的额外性能,我有哪些选择?我应该转而使用带有平面约束的 pybullet 来模拟2D吗?

英文:

I have a very basic space set up with a large number (10000) of circles falling under gravity. It seems that only one of my cores is active at all at any point in time, but as far as I know collision resolution is extremely parallelizable with space partitioning, which the Chipmunk engine does indeed implement.

It seems that even when I create the space with threaded=True on Linux, and space.use_spatial_hash(), the issue persists. I really need the extra performance parallel threads could provide, what are my options? Should I move to pybullet instead with planar constraints to emulate 2d?

Pymunk多核碰撞解决?

答案1

得分: 1

你需要设置要使用的线程数,例如像这样:your_space.threads = 2

在文档中写得像这样(Space.init):

如果你设置了threaded=True,step函数将在多线程模式下运行,这可能会提高速度。请注意,即使你设置了threaded=True,你仍然需要设置Space.threads=2才能真正使用多个线程。

你可以通过运行threaded_space示例来验证它是否有效,命令是 python -m pymunk.examples.threaded_space。如果我这样做,并在htop中观察CPU使用情况,我可以轻松地看到线程从1个变为2个。

我还应该补充说,不幸的是,在大多数情况下,多线程并不能显著提高性能。只有在同时发生大量碰撞时才能提供一些帮助,但即使在这种情况下,也没有太大改善(也许在ARM上会有更大的改善,但我无法尝试)。

英文:

You need to set the number of thread to use as well, for example like your_space.threads = 2

In the docs its written like this (Space.init):
> If you set threaded=True the step function will run in threaded mode which might give a speedup. Note that even when you set threaded=True you still have to set Space.threads=2 to actually use more than one thread.

You can verify that it works by running the threaded_space example python -m pymunk.examples.threaded_space. If I do that and watch CPU usage in htop I can easily see when it goes from 1 to 2 threads.

I should also add that unfortunately the multi-threading does not improve performance much in most cases. It can only help if there's a lot of collisions going on at the same time, but even then its not much (Maybe on ARM it gives bigger improvement, but that I cannot try).

huangapple
  • 本文由 发表于 2023年7月10日 18:23:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定