u3dreal / molecular-plus

particle solver for Blender 3.0+ / 4.0+
GNU General Public License v3.0
268 stars 20 forks source link

Are you open to performance focussed pull requests? #64

Open MohamadZeina opened 2 months ago

MohamadZeina commented 2 months ago

Hey! I use this a lot on my YouTube channel Moby Motion, and would love to improve the performance for my own learning, and to help me in producing videos. Just wondering:

a) would you be interested in merging if I can manage to improve performance using these ideas, or are they not worth the complexity? b) do you have any feedback on the route I'm taking? ie if you've tried something similar, or thought about it more deeply, and you know it doesn't work for some reason. Do you simply have any better ideas? c) how do you feel about trading off performance for some accuracy?

What I’ve tried / future ideas:

  1. I tried compiling with more advanced vectorised instruction sets, like AVX2, but the compilers aren’t vectorizing any loops that I could see. I’ll try refactoring some things to better leverage this
  2. You can make huge improvements to neighbourhood search if you’re prepared to give up some accuracy, and it seems the slowest part in all my tests.
    • You could just run the neighbourhood search less frequently, say every 2 subframes, for a 2x speed up, based on an assumption that the particle’s immediate neighbourhood of particles doesn’t change too often subframe to subframe (as far as I can tell, when it comes to the real collision detection, it uses the actual location of those local particles). To get this to work, I had to remove some later code that resets the neighbourhoods after doing collision detection. Simulation is faster, visually looks similar, maybe a bit “spongier” (balls are a touch slower to react) but I'd be willing to take that trade-off personally
    • Alternatively you can try something fancier: each particle can keep a list of neighbours, and “nearly neighbours” from a larger field. Neighbours and nearly neighbours could be updated every subframe, but the vast majority of particles, which are much further away, could be checked infrequently.
  3. It also seems to spend a lot of time passing data back and forth to Blender. Correct me if I’m wrong, but I think molecular sim does the particle-particle interactions, it passes the frame to Blender to do particle-mesh collisions and forcefields, back and forth. What do you think of moving more of the work to molecular plus? Say you have subframes of 15 - molecular plus could do all 15 subframes, and only hand back to blender every frame. I tried this, it’s faster, but gravity has to be increased because I guess Blender was handling the gravity. Behaviour is still a bit different - particles seem to disperse a bit more, would appreciate any feedback if you know why that is, or what other important stuff Blender is doing that I'm unaware of
u3dreal commented 1 month ago

Feel free to contribute ! .. Just ONE thing ... try to keep it simple and do small pull requests .. otherwise it is a horror to check for me.

  1. So far there is no AVX code so i would assume there is no benefit now.
  2. Open for ideas here
  3. Not sure here but subframes also have to be processed by Blender ... the optimal thing would be to get the particles as pointer and pass this one to molecular

Feel free to pm me on discord.