64-bit medium memory model inconsistency

To compile the following Fortran code successfully with pgf77 version 6.1-6 on an Opteron, I have to specify -mcmodel=medium to accomodate the 3GB integer array; otherwise, I get “relocation truncated” errors.

      program mem
      integer n, i
      parameter (n=750 000 000)
      integer x(n)
      common /global/ x
      do i = 1, n
         x(i) = 1
      end do
      print*, 'done'
      stop
      end

This is documented in the Release Notes. However, I don’t need the switch to compile and run the following C code that defines a similar 3GB global integer array.

#include <stdio.h>
#define n 750000000

struct {int x[n];} global;

int main (int argc, char *argv[])
{
  for (int i = 0; i<n; i++)
    global.x[i] = 1;
  printf ("done\n");

  return (0);
}

Is this intended behaviour, or is the documentation slightly inconsistent?

By the way, the Intel compiler displays the same characteristic, but GCC doesn’t… strange.

Ed Sumbar

Hi Ed,

In both codes, “-mcmodel=medium” is not needed since the total size of your executable’s data section is less than 1GB. While you do use large arrays, the memory for these arrays is not allocated by the loader until runtime. If you had a very large program with many, many variables and thus a large binary, then you might needed it.

What you do need is to compile your program with is “-fpic”, since at runtime your program needs to address more than 2GB. For even larger arrays, you may also need “-Mlarge_array” as well as using “integer*8” and “long” for your index varaible’s data type.

Note that “-mcmodel=medium” implies “-fpic” and “-Mlarge_arrays”.

  • Mat

Thanks Mat. That clarifies the issue somewhat.

The PGI 6.1 Workstation Release Notes on page 35 defines DS, the maximum data size, as “all data objects in .bss”. I guess that should read .data not .bss… right?

The point is that although I need to specify -fpic (or mcmodel=medium) to compile and run the Fortran code, I don’t seem to need it for the equivalent C code.

$ pgcc -c9x mem.c
$ ./a.out
done


Ed

The PGI 6.1 Workstation Release Notes on page 35 defines DS, the maximum data size, as “all data objects in .bss”. I guess that should read .data not .bss… right?

It’s really the offset from the “.text” section to anywhere up to the end of “.bss”, which includes “.data”. I think our doc writers were trying to simplfy things. Although not technically correct either since there are other sections in there as well, would “all data objects in .data and .bss” more helpful?

I don’t seem to need it for the equivalent C code.

You are correct that “-fpic” is not need here. I was using our development compiler and was getting a relocation truncation error without “-fpic”. Looks like a new part of our runtime was not being built correctly. Thanks for finding it!

  • Mat

Hi Mat,

The table on p. 35 says that “-tp k8-64 -fpic” supports a “data size” of 2GB max, while “-tp k8-64 -mcmodel=medium” supports >2GB. When I do size a.out (or objdump -h a.out), the sum of .data and .bss is greater than 2GB. Therefore, based on the table, I’d expect to have to use -mcmodel=medium. But obviously, -fpic works. So, that table is a bit confusing.

In any event, is there any additional performance penalty in using -mcmodel=medium over -fpic?

Ed

In any event, is there any additional performance penalty in using -mcmodel=medium over -fpic?

There can be due additional address calculation overhead.

  • Mat