Troubleshooting Monte Carlo Simulation Compiler Issues

  • #1
alastor
4
0
Hello,

my colleague has a problem. He has a Monte Carlo simulation written in C. But his program gives different outputs in case he compile it using g++ or icc. He has nothing changed in his source. He is using standard random number generator drand() with initialization srand((unsigned) time (NULL)).

Didn't you have some similar problems with icc compiller?
Do you think, that the differencies in outputs could be caused by used compiller?

Thank you.
 
Last edited:
Technology news on Phys.org
  • #2
Shouldn't he be getting different outputs every time he runs the program, regardless of how it was compiled?
 
  • #3
No, for the same compiler he obtain every time similar outputs. Results are different only when compilers are different.
 
  • #4
what kind of different outputs (eg unexpected errors)? and what kind of similar outputs(eg. identical results)...what type of optimization,what type of #-bit PC.

Are his randomizations working properly on initial condition?
 
  • #5
Probably each compiler handles seed generation differently. With each compiler, does it produce the same first number each time? Or maybe some compilers generate code that uses the PC clock in the seed generation?
 
  • #6
The two compilers probably use different math libraries. The required behavior of rand() is very poorly specified in the C/C++ standards. Bottom line: Don't use srand/rand. Use of these functions is verboten in many places. Good random number generators include those in the Gnu scientific library and some in Numerical Recipes.
 
  • #7
alastor said:
No, for the same compiler he obtain every time similar outputs. Results are different only when compilers are different.

This might still happen if he is not "seeding" the simulation.
 
  • #8
In addition to different (and in all likelihood, poor) implementations of rand/srand, there is also the possibility that different compilers may use different floating point instructions or order instructions differently. This matters because floating point operations are in general non-associative.

On a complex chip like a Pentium-4, Xeon, Athlon, Opteron, etc. there are multiple floating point units as well. These different units (x87, SSE) have different internal representations of floating point numbers. Prior to gcc-4.0, gcc would target x87 by default. Don't know what icc targets, but I would guess they've been going after SSE for a longer time. Running on one unit would likely have different rounding characteristics versus the other.

I would like to emphasize what D H said: do not use rand/srand unless you really know what you're doing.
 

Similar threads

Replies
4
Views
3K
Replies
1
Views
2K
Replies
4
Views
1K
Replies
1
Views
4K
Replies
3
Views
1K
Replies
2
Views
450
Replies
2
Views
2K
Back
Top