Search Unity

Burst compiler: deterministic floats

Discussion in 'Data Oriented Technology Stack' started by PaulNK, Mar 21, 2018.

  1. PaulNK

    PaulNK

    Joined:
    Apr 8, 2015
    Posts:
    6
    I was just reading the ECS docs and noticed that the Burst compiler can create deterministic code. Would this also apply to engine code e.g Vector3, path finding, physics? I’d like to try making an RTS game and it sounds like networking with deterministic lock step simulation is currently being worked on.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,671
    The principles and vision document is about our future direction. Burst does not support deterministic cross platform code execution at this point.
     
  3. AurelWu

    AurelWu

    Joined:
    Oct 11, 2013
    Posts:
    26
    I love the ambition to have deterministic floating point operations on all platforms. However as this seems to be quite some time in the Future, I think it would be great if Unity.Math could include some fixedpoint types in the mean time. I know that there are lots of fixedpoint libraries for c# out there but the beauty of having it integrated would be that it could have 100% consistent naming and methods with float,float2,float3,float4 and once the support for deterministic floats is there it would be super easily refactorable.from fixed to float.
     
  4. meanmonkey

    meanmonkey

    Joined:
    Nov 13, 2014
    Posts:
    110
    @PaulNK When they announce that their floats are deterministic, this should automatically include structs like Vector2, Vector3, etc, as these are just using floats.

    @AurelWu I totally agree with you. I would love to see a standard fixed point math library for c#. I read into this topic once for quite a while, and I came to the conclusion that implementing a fixed point math library is the safest way in terms of determinism. 1+1 will always be 2. period. It's not just 100% safe, it's very fast too, including the conversion from fixed to float.

    I am curious how unity wants to achive floating point determinism. It's a heavilly discussed topic, and there are lots of problems to be solved / factors to be terminated. Cpu architecture, operating system, compiler settings. I'm not a pro on this topic but I think they will do something like the IEEE 754 strict mode for the compiler (which will bring a significant performance drawback for all float calculations) but even with this, there is a chance that eventual thirdparty libraries / assemblies which do not implement that setting can blow the whole thing up.

    As I understand, a 100% guarantee is not possible, depending on your usecase. For example If you stick to Windows 10 on 64bit only, it's very unlikley that you ever run into problems with float determinism, even with different cpu architectures, but still no guarantee. There are some games out there which rely on floating point determinsim but you have to be very careful if you do so.

    Regarding lockstep simulation, I would run that whole thing in a separate c# assembly (and thread too) anyway, because you accidentally could bypass the lockstep command wrapper and access / manipulate values directly which will lead to async sim states. You won't be able to benefit from unity libraries like the new job system, at least not in the simuation (still in the game itself) but that shouldn't matter at all. The simulation runs in a slower tickrate and if you are going the datadriven design all the iterations will be very fast due to hardware prefetching. I also like the clean separation of 3D engine and simulation, you will never mix up things and it stays very maintainable.
     
  5. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    It is a bit early for me to share details because we are still studying all the options and figure out what the best approach is. But yes, cross-platform floating point determinism is a huge beast to fight.
    That said, we have full control over the whole pipeline, from the input code to the binary that gets generated. On top of that, we have a restricted context to apply determinism to, which helps to reduce the options.

    More in general, what we are aiming for is a solution to make Burst-compiled HPC# code determinist cross CPU. If a game uses code that is not Burst-compatible or uses native plugins, then we won't be able to guarantee determinism. Within that restricted context, we have a few solutions that allow for the best performance possible while retaining bit-wise equality in the results of the calculations.

    It is important to note that, in general, you don't need every single floating point calculation to be deterministic. But we will make sure that all the future system that we will ship, will be Burst-compatible and thus deterministic by default. And provide tooling on top of Unity to validate determinism and help debug issues.

    It will take a bit until we reach that point, but that's definitely our goal and all our internal experiments are extremely positive.

    We will share more in-depth information moving forward, as soon as the approach will be finalized.
     
  6. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    410
    Greetings, I am researching about Determinism in Unity, and came across this topic.

    We are creating an RTS based on what is available to us back then.
    Starting from Fixed Point based float, vector2/3, maths, and physics.

    We need a clear yes/no to decide on our next steps.

    1. Will burst compiler provide cross platform determinism?
    2. Will we be able to enjoy the benefits of ECS, Jobs, and Burst with our own custom fixed point library?
    3. When is ECS planned to ready for production? Will it be deterministic when ECS is out of preview?
    4. Is the new DOTS Unity Physics deterministic?

    Thank you!
     
    Last edited: Apr 26, 2019
    meanmonkey likes this.
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,671
    1. Yes the burst compiler will support cross platform floating point determinism end 2019 / early 2020.

    2. You can use fixed point math with ECS / Jobs / Burst. Since all our own code like physics / transforms / AI / Animation will use floating point math it obviously requires you building a lot of the more foundational code yourself if you use fixed point math.

    3. The Unity.Entities package should be out of preview end of this year. But of course all kinds of packages that depend on it might still be in preview, like dots animation, dots physics, dots AI, dots cinemachine etc.

    We have customers who build games in production on top of preview, it means you need to have a certain tolerance level to us changing API's while you build your game.

    4. It deterministic but not yet cross platform deterministic since burst does not support cross platform float determinism yet.
     
    kvfreedom, meanmonkey, PaulNK and 4 others like this.
  8. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    410
    Thank you for the clear answer, it definitely helped us decide how to proceed our next steps.

    DOTS seems to be everything we are looking for, given all the benefits of safe multi threading and Burst compiler level optimization, it is unfortunate that we already have invested a lot in the foundation code built for determinism from scratch. It became pointless to have a deterministic library when everything built with Burst will already be deterministic.

    However, with these news, I can't help but look forward to the future of Unity with passion, I wish you all a good future ahead!

    Thank you.
     
    meanmonkey and FROS7 like this.
  9. meanmonkey

    meanmonkey

    Joined:
    Nov 13, 2014
    Posts:
    110
    I guess it depends on what you are simulating. If you really need deterministic / accurate / complex 3D physics, I think you should dare the switch to floats.

    Despite mass unit / lockstep sims, you will need cross-platform determinism rather for stuff like replay-features in racing games, for example. As for mass unit / lockstep sims, depending on your maximum unit count to be simulated, I would do custom 2D based / faked physics anyway (collision detection, steering, pathfinding based on 2D maps, etc), as doing this with 3D methods will cost you much more performance. But if you really need accurate 3D physics in your mass unit / lockstep sim for whatever reason, then floats using unity is the solution.

    I also would wait for the final solution of unitys float determinism, I guess there will be a performance drawback (even if it's very fast with all the nice unity performance features) when running the burst method in deterministic mode (at least I guess there will be compiler options), so again, depending on your needs, your fixed point math solution (using burst, too, of course) may be even faster. Also it's not clear yet what operations exactly will be deterministc.

    Remember, only your simulation needs to be deterministic, not the render stuff, at least if you are not creating some kind of scientific simulation where the visual stuff matters too.
     
    Last edited: May 4, 2019
    Gekigengar likes this.