Fortran - recompile program from 32bit to 64bit machine

烂漫一生 提交于 2019-12-24 16:41:08

问题


I've got really old program which I want to run on my 64bit computer. There are a lot of depreciated statements. During debugging I found that a lot of variables become NaN or Infinity... Hence, I changed variables from 4-bytes to 8-bytes long (i.e. REAL to REAL*8) but now caluclations and results are considerably different on both computers. Could somebody explain me if it really does matter if I use longer types and why on a 32-bit computer everything was OK but on a 64-bit one I get Infinity and NaN values?

P.S. I use gfortran compiler with -fbackslash -ffixed-line-length-0 -std=legacy -g -O0 -fno-inline options

Regards, kozooh


回答1:


Your problem is probably due to differences between the compilers and not between the bit-levels of the machines. For example, some FORTRAN 77 compilers implicitly apply save to all procedure (subroutine and function) local variables. This is not required by the standard and this behavior should not be relied upon. It frequently causes problems when a legacy program is compiled with a modern compiler that demands that save be used if local variables should have their values retained across invocations of a procedure. I don't know if g77 has this "feature". You can turn on this behavior in gfortran with the compiler option -fno-automatic.

EDIT: consider:

  subroutine MySub
  logical FirstCall
  save FirstCall
  data FirstCall / .TRUE. /
  integer I

  if ( FirstCall ) then
     I = 0
     FirstCall = .FALSE.
  end if

  I = I + 1

  write (6, *) "Call", I

  end

  program main

  integer j

  do j=1, 4
     call MySub ()
  end do

  end program main

Compiled with g77 (no compiler options), the output is:

 Call 1
 Call 2
 Call 3
 Call 4

The local variable I is retaining its value across invocations of MySub. So it appears that g77 is saving local variables even when not requested with save. At least at the default optimization level.

Compiled with gfortran with options fbackslash -ffixed-line-length-0 -std=legacy -g -O0 -fno-inline the output is the same. Now change to -O3 and the output is:

 Call           1
 Call           2
 Call           3
 Call         129

Sometimes I retains its value, sometimes not.

Change one line of the program to: save FirstCall, I and the value is always retained:

 Call 1
 Call 2
 Call 3
 Call 4

Try -fno-automatic ...



来源:https://stackoverflow.com/questions/17880649/fortran-recompile-program-from-32bit-to-64bit-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!