0

I'm working on a Fortran program and running into a strange bug with some Heisenbug-type characteristics, and looking for some insight into what might be going on. The code is too large to post in full but I hopefully I can the general idea.

What's basically going on is I have a subroutine that reads a list of numerical parameters from a text file,

  call read_parameters(filename, parameter_array)

and then this list of parameters is sent into another subroutine that runs a program using those parameter values.

  call run_program(parameter_array)

These calls are part of a loop that calls run_program with slightly different parameters each time through the loop---the intention is to find better parameter sets.

I've found that on the first pass through this loop, run_program gives bizarre results, which seems to indicate that something is going wrong with the first call to read_parameters. But all the subsequent passes behave normally and I haven't been able to understand what's going wrong with that first pass despite a lot of investigating, including for example printing the values of the parameters themselves within the actual run_program code.

While testing, I realized that if I put another call to read_parameters right above the call to run_program, then the first pass of the program runs normally, but here's the thing: this new call to read_parameters is just a dummy call, with an output array parameter_array2 that doesn't even get used! As in,

  call read_parameters(parameter_array)
  call read_parameters(parameter_array2)
  call run_program(parameter_array)

If the second line is present, the program runs just fine, even though parameter_array2 isn't used anywhere, while if it's absent the program gives erroneous results for the first pass through the loop.

Does anyone have any ideas about what might be going on?

Thanks.

stepheba
  • 25
  • 2
  • 5
    Uninitialized variables is my guess, but not enough information really to go on. Have you compiled and run with all run time checks turned on (`-fcheck=all` for gfortran)? Do things like `-finit-real=snan` and related (again for gfortran) make any difference? – Ian Bush Aug 23 '21 at 18:36
  • 2
    See [this other question](https://stackoverflow.com/q/3676322/3157076) about gfortran checks. Other compilers will have similar. – francescalus Aug 23 '21 at 18:46
  • 3
    Some subroutine might be called with wrong arguments, some array might have overflown. Impossible to answer without real [mcve]. – Vladimir F Героям слава Aug 23 '21 at 19:03
  • I second @IanBush on uninitialized variables. Beside the options he mentioned, if you are using gfortran then add the `-Wall` option when you compile the code. You may want to use the `-Wextra` option, but this sometimes causes warnings that are false positives. – steve Aug 23 '21 at 21:20
  • Another possibility is that `read_parameters` might (explicitly or implicitly) contain variables with the `save` attribute, or might otherwise modify global variables. It's hard to be more accurate without seeing `read_parameters` however. – veryreverie Aug 24 '21 at 08:40

0 Answers0