Fortran: Differences Between Generated Code Compiled Using Two Different Compilers

I need to work on a fortran program that used to be compiled using Microsoft Compaq Visual Fortran 6.6. I would rather work with gfortran, but I met a lot of problems. The main problem is that the generated binaries have different types of behavior. My program takes an input file and then generates an output file. But sometimes, when using a binary file compiled by gfortran, it crashes to the end or gives different numerical results. This is a program written by researchers that uses many floating point numbers.

So my question is: what are the differences between the two compilers that might lead to this problem?

edit: My program calculates the values โ€‹โ€‹of some parameters and there are many iterations. In the beginning, everything is going well. After several iterations, some NaN values โ€‹โ€‹appear (only when compiling gfortran).

edit: Think all of you for your answers. Therefore, I used the Intel compiler, which helped me by providing useful error messages. The origin of my problems is that some variables are not initialized properly. It seems that when compiling with visual fortran compaq, these variables automatically take 0 as a value, while with gfortran (and intel) it takes random values โ€‹โ€‹that explain some numerical differences that add up to the next iterations. So now the solution is a better understanding of the program to fix these missing initializations.

+4
source share
4 answers

There may be several reasons for this behavior. I would do the following:

  • Turn off any optimization

  • Enable all debugging options. If you have access to, for example, Intel, use ifort -CB -CU -debug -traceback . If you need to stick with gfortran, use valgrind , its result is somewhat less clear to humans, but often better than nothing.

  • Make sure there are no implicit typed variables, use implicit none in all modules and code blocks.

  • Use consistent float types. I personally always use real*8 as the only type of float in my codes. If you use external libraries, you may need to change the call signatures for some routines (for example, BLAS has different routine names for single and double precision variables).

If you are lucky, just some variable will not be correctly initialized, and you will catch it with one of these methods. Otherwise, as MSB is suggesting, a deeper understanding of what the program really does is necessary. And, yes, you may just need to check the algorithm manually, starting from the point where you say "some NaNs values."

+2
source

Different compilers can generate different instructions for the same source code. If the numerical calculation is on the line of work, one set of instructions may work and the other not. Most compilers have options for using more conservative floating point arithmetic and optimization for speed โ€” I suggest checking the compiler options that you use for the available options. More fundamentally, this problem - especially that compilers agree on several iterations, but then diverge - may be a sign that the numerical approach of the program is borderline. A simplified solution is to increase the accuracy of calculations, for example, from one to double. It is also possible to configure parameters such as step size or a similar parameter. It would be better to gain a deeper understanding of the algorithm and, possibly, make more fundamental changes.

+3
source

I donโ€™t know about the failure, but some differences in the results of the numerical code on the Intel machine may be due to one compiler using 80-doubling, and the remaining 64-bit doubles, even if not for variables, but maybe for temporary values. In addition, floating point computation is order sensitive; elementary operations are performed. Different compilers can generate a different sequence of operations.

+2
source

Differences in implementations of different types, differences in various custom extensions of suppliers, can be very important.

Here are just a few of the language features that differ (look at gfortran and intel). Programs written for the standard fortran work the same on each compiler, but many people do not know what the standard language functions are, and what language extensions, and therefore use them ... when compiling with various compiler problems, they arise.

If you put the code somewhere, I would take a quick look at it; otherwise, itโ€™s hard to say for sure.

+1
source

Source: https://habr.com/ru/post/1339270/


All Articles