[ODE] Re: Different simulation results with different versions
of Visual C++?
Jon Watte (ODE)
hplus-ode at mindcontrol.org
Mon Aug 15 09:38:17 MST 2005
You want to round in binary, not in decimal. The powf() function is both
slow and (sometimes) inexact. Convert to some form of fixed point and
back; something like:
float round( float number ) {
if( number > 32767 ) return 32767;
if( number < -32767 ) return -32767;
int i = (int)(number*65536);
return (float)i/65536.f;
}
You may need to adjust the precision of your fixed-point values based on
what kind of quantities you're dealing with, if values > 30000 are
commonly allowed in your system.
(btw: passing float by value is usually faster than by reference)
Also make sure that the rounding policy is set the same on the PowerPC
and the x86 (round-towards-zero, round-to-even, etc). You need to do
this by setting the CPU control bits, typically.
Also, make sure you synchronize the random number generators between the
two instances, and that you use the same internal precision for both
CPUs. If the PowerPC has native float calculations (separate from double
precision) then you need to set the x86 FPU mode to 23-bit mantissa to
match.
Evenso, it's not clear that you'll be able to always get exact matches.
Even if you use the same compiler (say, gcc) on both machines, the CPUs
just are different in lots of little ways. If you're doing game
recording and playback, you'll probably do well to store keyframes every
so often.
Cheers,
/ h+
Keith Johnston wrote:
> Since I am supporting multiple platforms, it looks
> like option 2 is my only choice:
>
> 2) Quantize/round/truncate all physical quantities to
> a known resolution after each simulation step.
>
> I am trying this now - however, I am still seeing
> differences in the mac and PC simulations. I am
> quantizing all the physics properties to 2 decimal
> places after each time step: position, velocity,
> angular velocity, and orientation.
>
> Couldn't optimizations in the compilers still cause
> different results even with quantized values?
>
> This is the rounding function I am using - is there a
> better one?
>
> float Round(const float &number, const int num_digits)
> {
>
> float doComplete5i, doComplete5(number * powf(10.0f,
> (float) (num_digits + 1)));
>
>
> if(number < 0.0f)
> doComplete5 -= 5.0f;
> else
> doComplete5 += 5.0f;
>
> doComplete5 /= 10.0f;
> modff(doComplete5, &doComplete5i);
>
> return doComplete5i / powf(10.0f, (float)
> num_digits);
> }
>
>
>
>
> --- "Jon Watte (ODE)" <hplus-ode at mindcontrol.org>
> wrote:
>
>
>>>This makes game development in which simulations
>>
>>must repeat exactly
>>
>>>from one run to the next very difficult - as you
>>
>>can't guarantee that
>>
>>>the compiler on one platform will produce the same
>>
>>results as on another.
>>
>>>
>>>Have others found a way to work around this
>>
>>problem? I need *exactly*
>>
>>>the same results from one simulation to another.
>>
>>Two solutions:
>>
>>1) make sure all developers use exactly the same
>>compiler (this can be
>>done multi-platform by generating code for Windows
>>out of Intel's C++
>>compiler, or GCC).
>>
>>2) Quantize/round/truncate all physical quantities
>>to a known resolution
>>after each simulation step.
>>
>>
>>Also beware that various DLLs will likely change
>>your preferred rounding
>>mode and internal CPU floating point precision. If
>>that's not enough,
>>Athlon CPUs don't actually support the 80-bit
>>internal floating point
>>format that Pentium CPUs do, so you'll have to set
>>the pentium to 64-bit
>>internal mode, too. Throwing exceptions or catching
>>signals will also
>>change your CPU FP environment bits (the FPU control
>>register).
>>
>>
>>Note that if this is a multi-player game, this isn't
>>enough, because you
>>will see different objects with different latencies
>>on different
>>machines, and thus the matrices that get solved by
>>ODE are different,
>>and thus the simulation will diverge anyway.
>>
>>
>>Cheers,
>>
>> / h+
>>
>>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
>
More information about the ODE
mailing list