Search code examples
fortranx86-64gfortran

Integer overflow when calculating the amount of memory to allocate


I'm trying to process a spatial dataset which is stored as a regular X, Y and Z coordinate grid with each location having multiple fields storing the attributes. However, when allocating the array to store the data, it throws an error.

Currently working with gcc gfortran version 8.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project) on Win10.

I've tried different machines (in case I'm hitting hardware limits) and looked at various compiler options, but have no managed to affect the result.

This is a simplified example with real limits for the current dataset for processing:

      program test_array

      implicit none

      real*8, allocatable :: test(:,:,:,:)

      integer*4 x,y,z,vars

      x=382
      y=390
      z=362
      vars=15

      print *, "Total bytes: ", x*y*z*vars*8

      allocate(test(x,y,z,vars))

      print *, "Allocated"

      deallocate(test)

      print *, "Deallocated"

      end program test_array

The program compiles just fine, but upon execution returns the following error:

 Total bytes:    -2118243392
Fortran runtime error: Integer overflow when calculating the amount of memory to allocate

Error termination. Backtrace:

Could not print backtrace: libbacktrace could not find executable to open
#0  0x41ad93
#1  0x413fee
#2  0x411d50
#3  0x401807
#4  0x4019dd
#5  0x40138a

Clearly I'm exceeding the 32-bit integer limit, but as I'm on a x64 system and (as far as I can tell) the compiler is a 64-bit version, I don't understand why I'm hitting a 32-bit limit. Hence, I've investigated compiler switches to force all integers to INTEGER*8 to no avail.

Is it possible to get round this limit, and if so, how?

Problem solved!

Upon searching about on my various install DIRs I came across three other installs that all include a version of gfortran.exe. Needless to say, these were being called preferentially to the most recently installed MinGW compiler suite. Once these redundant versions were removed the test program and the production tool both compiled and executed without issue (memory allocation up to around 6.5 Gb for this particular model).

Many thanks to those who commented and helped to point me in the right direction.


Solution

  • The issue above was due to multiple instances of the gfortran.exe compiler being installed as parts of other packages, such as Strawberry Perl, and solved by calling the correct compiler directly to demonstrate the 64 bit compiler produced a working program.

    Discovering the -v switch for the compiler allows the install path to be displayed along with other environment variable and version info. From here I could track down the unwanted EXEs and remove them and, if necessary, their outdated installed packages.

    Validation of the resulting model, processed using the 64 bit compiler, confirmed that the program works as intended.