Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sources of non-determinism [closed]

My supposedly deterministic program produces one of a few slightly different outputs on different runs. The input, compiler and computer are unchanging. I'm not sure which output is right because it always looks reasonable.

Besides a stray call to rand(), how could this be possible?

like image 522
zoo Avatar asked Aug 31 '10 13:08

zoo


People also ask

What are the reasons for non determinism?

In classical physics nondeterminism is due to uncontrolled causes that are recognized to exist and that, if better known, would make the predictions better.

What is non-deterministic example?

One example of a non-deterministic algorithm is the execution of concurrent algorithms with race conditions, which can exhibit different outputs on different runs.

What are non-deterministic events?

Nondeterminism means that the path of execution isn't fully determined by the specification of the computation, so the same input can produce different outcomes, while deterministic execution is guaranteed to be the same, given the same input. Related terms to "nondeterministic" are "probabilistic" and "stochastic".

What is non determinism and determinism and what is the difference between them?

In a deterministic algorithm, for a given particular input, the computer will always produce the same output going through the same states but in the case of the non-deterministic algorithm, for the same input, the compiler may produce different output in different runs.


2 Answers

If your program use float / double, there may be difference in the result if there are context switch on some architecture.

On x86, the FPU use extended precision for intermediary result, but when saved in memory (which happens when there is a context switch either process or thread), such precision is lost. That could cause some small divergence of the result (we've detected such problem in our program). One way to avoid this issue is to ask the compiler not to use FPU but SSE for floating point operations.

http://www.network-theory.co.uk/docs/gccintro/gccintro_70.html

like image 191
Sylvain Defresne Avatar answered Dec 03 '22 06:12

Sylvain Defresne


If your output depends on an address allocated on the heap:

int main(int argc, char* argv[])
{
   printf("%p", malloc(42));
   return 0;
}

For each run, the malloc() may return a different virtual address - not to mention NULL in case the allocation failed.

like image 37
zr. Avatar answered Dec 03 '22 06:12

zr.