Thoughts on Concurrency

  • ssc4k
    19th Sep 2016 Member 0 Permalink

    I've been playing around with concurrency problems in simple programs and thought this one would be a good genre to try to think about. The main problems with concurrency in this type of simulation are the following:

     

    • The simulation must be a discrete event simulation or saves will not work
    • Simulation operations must appear to be atomic or particles from different threads may "eat" each other
    • Concurrency locks such as software atomics or mutexes cannot be used as the overhead would far exceed the performance gain
    • Concurrency needs to benefit the worst case (screen full) not best case (section of screen full)

    With that said I've come up with the following model that requires only 9 "sync points" for 4 threads in the main simulation.

     

     

    (click for full size) Particles in the concurrent sections must obey a speed limit of 1/6 the board size to prevent having to worry about memory access. Things that generate instantaneous lines of particles or must move faster than that would need to be caught in the preproccessing stage at the beginning.

     

    For normal simulations I'd expect this to give a ~2x performance boost over a single threaded implementation on a 4 core or higher machine. Before I go and try to see if this is feasible I was wondering if anyone else had come up with a clever way to squeeze out more concurrency with lower overhead?

     

    Thanks

    Edited once by ssc4k. Last: 19th Sep 2016
  • jacob1
    20th Sep 2016 Developer 0 Permalink
    It would definitely be easy to thread the air simulation (more like, why haven't we done that already, jacksonmj's fork does). Multithreading rendering wouldn't be fundamentally difficult either.

    I don't think this method of dividing up the simulation area is that viable though. Particles move faster than the limit a lot. Sure we could simulate them before the frame, and also simulate all ARAY/CRAY/DRAY/etc., but that is kind of ugly. Liquids can also move I think up to 20-30 pixels in one frame, just by falling.
    Also, particles are usually together, often near the bottom. So it isn't saving that much time to simulate the empty space at the top of the screen at the same time as the bottom right.

    There are probably parts of the simulation you could multithread. A lot of update functions don't need to care about particle movement or changes, so could use an older state to simulate. I think mniip once proposed separating out the update functions into two parts, one which can just use the previous state to do calculations and therefore could be multithreaded.
    I don't know if there will ever be a simple way to multithread particle movement. Movement and update functions are separate so I think it would be best to focus on these two individually.

    Edit: Moved to development.
    Edited once by jacob1. Last: 20th Sep 2016