4

I have a strange behavior in a fortran subroutine that looks like:

subroutine compute(a, b, c)
    real(8), dimension(:,:), intent(in) :: a
    real(8), dimension(:), intent(in)   :: b
    real(8), dimension(:), intent(in out) :: c

    !print*, c
    ! do some computation here to update c
end subroutine compute

If I uncomment the print statement, I get the expected result. If I keep it commented the result becomes very strange with huge numbers. By the way, the print statement got there just for debugging purpose. Strangely, it "solved" the problem, but that is not a reliable solution. The subroutine is part of a large code and I have not yet been able to take the problem out of the context of the large code. The debugger did not help much. It is obvious that the problem is somewhere else, because a print statement is not supposed to change the result of a computation.

My question is: what are the possible mistakes that can lead to such a problem? Had someone had similar problem?

innoSPG
  • 4,588
  • 1
  • 29
  • 42

1 Answers1

3

This is a typical symptom of memory corruption. Try compiling with "-fcheck=all -Wall -g", and fix all warnings and errors. If that doesn't help, run via valgrind and/or address sanitizer.

janneb
  • 36,249
  • 2
  • 81
  • 97
  • It is certainly a memory corruption, but difficult to locate. I already have "-fcheck=all -Wall -g" on and I have no warning. I will check if it possible to have valgrind or address sanitizer in my working environment. – innoSPG Oct 14 '15 at 14:45
  • 1
    @innoSPG I had the same error a while back. I fixed it by compiling my code with all the debug options possible and going through it meticulously. The error came from accessing an array out of bounds. Your error might be something else See: https://en.wikipedia.org/wiki/Heisenbug – solalito Oct 14 '15 at 14:52
  • 1
    @innoSPG I posted a similar question of SOF: http://stackoverflow.com/questions/27920043/adding-removing-a-print-statement-changes-variables – solalito Oct 14 '15 at 14:53
  • @solalito thanks! Interresting paths to follow. I am in the debugging process. I have the `-fbounds-check` flag on, so chances are that it is not an out of bounds. Your previous S.O question gives me more hints. – innoSPG Oct 14 '15 at 15:15
  • 1
    @innoSPG just an FYI, but the bound check options won't catch all improper access. Particularly with procedures without explicit interfaces you can sneak bad array access through without being caught. – casey Oct 14 '15 at 16:40
  • @casey, good point, that is what I am after right now. A part of the code has a lot of nasty thing that I am exploring. Whithout being able to explain exactly where the problem was, I rewrote some part of the 1D array case of the code by adapting another code I already have and it is working great now. The 2D array case is more challenging, but I am on it. – innoSPG Oct 14 '15 at 16:50
  • @casey In general yes, though in this case the OP must be using an explicit interface since the dummy arguments are assumed shape. – janneb Oct 14 '15 at 18:34
  • 4
    @innoSPG You can also try the sanitizations `-fsanitize=address,unefined` if you have a recent version of gfortran. – Vladimir F Героям слава Oct 14 '15 at 18:46
  • @VladimirF thanks for the suggestion. Unfortunately my version of compiler (gfortran 4.8.2) does not have the option. Hopefully I manage to convince the management to upgrade. – innoSPG Oct 14 '15 at 19:26
  • 1
    @VladimirF shouldn't it be -fsanitize=address,un**d**efined ? – mivkov Apr 04 '18 at 22:51
  • Of course it should. It is just a typo which I can't correct now. – Vladimir F Героям слава Apr 05 '18 at 05:30
  • @VladimirF I see. Well, I thought it shouldn't hurt to mention it. But back to the question: I have the same thing happening, but none of the advice above has worked for me so far. Is there any other advice/help you could offer? – mivkov Apr 05 '18 at 12:34
  • @VladimirF how do I read the sanitizer results, it shows some buffer overflows in some address spaces, but how do i debug that? – Eular Sep 13 '21 at 03:16