Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can I run scenes in parallel with multithreading?

Discussion in 'Editor & General Support' started by erdostamasa, Nov 2, 2021.

  1. erdostamasa

    erdostamasa

    Joined:
    Mar 28, 2020
    Posts:
    32
    I'm making an evolution simulator where I iterate over generations of creatures. It would really speed up the evolution if I could run a generation on x threads then collate the results after every thread finished running. The simulation is based on Unity physics so I would like to run x number of separate scenes on x threads.
    I've been googling for a long time but haven't found anything useful.
    Is something like this possible at all?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    You may do anything you like in threads but for the most part that code may NOT interact with the Unity API.

    Everything in "normal" scripting happens on a single thread, and that single thread is the ONLY thread that can call Unity API functions.

    Delegate queue to marshal tasks from other threads to the main thread:

    https://forum.unity.com/threads/how...-everytime-it-is-called.1148354/#post-7370375

    You can always additively load as many scenes as you like, that doesn't affect threading or anything.
     
  3. erdostamasa

    erdostamasa

    Joined:
    Mar 28, 2020
    Posts:
    32
    @Kurt-Dekker Thanks for the answer!
    So because I want to run Unity physics simulations (complete scenes) parallel, what I want is impossible, do I understand correctly?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Unity's physics systems (2D and 3D) implicitly get their input from the scene and outputs the results to the scene, in a predictable fashion and only on the main thread, as seen in this diagram here:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just thinking about this, you could consider having the main game launch separate instances of evolution specific builds, which run to completion and then pass results back to the main game before exiting. So each separate instance launched has its own separate world space and physics, since it is running as a separate application. But what I would probably really do is see if there was a way to remove Unity's physics system from the simulation, so you could just run it in your own threads. You could write an approximation of the physics system for whatever physics features your current implementation uses.
     
  6. erdostamasa

    erdostamasa

    Joined:
    Mar 28, 2020
    Posts:
    32
    Sadly the physics system is an integral part of the simulation I can't remove.
    Launching separate instances would be a great solution.
    Can you give me any pointers where could I research this topic more? I have some experience making separate processes in Python, is it something like that?
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The closest thing I've done to this is with a multiplayer game I'm working on, where I launch new server instances to simulate different zones of the overall map as players enter those zones. The different server instances talk to each other using a reliable UDP system. I do it this way because I use the Unity physics system as well.

    You can use standard C# stuff to start processes, and pass command line arguments to it so it knows what the master wants it to do.

    So I don't know if this is the best approach, but it could work for you.
     
    erdostamasa likes this.
  8. erdostamasa

    erdostamasa

    Joined:
    Mar 28, 2020
    Posts:
    32

    Thanks for the answer!
    I also thought about solving this with creating a master server that collects data, and worker clients that run simulations.
     
    Joe-Censored likes this.