Troubleshooting Monte Carlo Simulation Compiler Issues

In summary: Different compilers may use different math libraries, which can lead to different results with rand(). Additionally, on modern CPUs there are multiple floating point units, so even if rand() behaves the same on each CPU, the results may be different due to the way the math is executed on different units.No, for the same compiler he obtain every time similar outputs. Results are different only when compilers are different.
  • #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.
 

Related to Troubleshooting Monte Carlo Simulation Compiler Issues

1. What is a Monte Carlo simulation?

A Monte Carlo simulation is a computational technique that uses random sampling to model and analyze complex systems or processes. It is often used in scientific research and decision-making to estimate the range of possible outcomes and their probabilities.

2. Why do I need to troubleshoot Monte Carlo simulation compiler issues?

Monte Carlo simulations involve running numerous iterations of a model, which requires a significant amount of computing power. Compiler issues can arise when the code is not properly written or when there are conflicts between different programming languages or libraries. Troubleshooting these issues is necessary to ensure accurate and efficient simulations.

3. How can I identify and resolve compiler issues in Monte Carlo simulations?

One way to identify compiler issues is to carefully review the code and check for any errors or warnings. It may also be helpful to run the code in a debugger or use logging tools to track where the issue is occurring. Resolving the issue may involve debugging the code, updating libraries, or optimizing the code for better performance.

4. Is there any specific software or programming language that is best for Monte Carlo simulations?

There is no one "best" software or programming language for Monte Carlo simulations, as it depends on the specific needs and goals of the project. However, some commonly used languages and software for Monte Carlo simulations include MATLAB, R, Python, and Excel.

5. Are there any common mistakes to avoid when troubleshooting Monte Carlo simulation compiler issues?

One common mistake is not thoroughly testing the code before running large simulations. It is important to start with smaller, simpler models to catch any errors or issues before scaling up. It is also important to properly document the code and make sure all necessary libraries and dependencies are included.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
985
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
Replies
67
Views
5K
  • Programming and Computer Science
Replies
1
Views
662
  • General Math
Replies
2
Views
1K
Back
Top