Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Icc compiler

  1. May 10, 2007 #1

    my colleague has a problem. He has a Monte Carlo simulation writen 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: May 10, 2007
  2. jcsd
  3. May 10, 2007 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Shouldn't he be getting different outputs every time he runs the program, regardless of how it was compiled?
  4. May 10, 2007 #3
    No, for the same compiler he obtain every time similar outputs. Results are different only when compilers are different.
  5. May 10, 2007 #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?
  6. May 10, 2007 #5


    User Avatar

    Staff: Mentor

    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?
  7. May 10, 2007 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
  8. May 12, 2007 #7
    This might still happen if he is not "seeding" the simulation.
  9. May 12, 2007 #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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook