-fPIC and -mcmodel=medium not supported together

For the last a few days I was developing a project on my local machine with PGI 16.1 installed, and my compiling process was as follows:

mpifort -Mpreprocess -DTIME -fast -c emodule.cuf
mpifort -Mpreprocess -DTIME -fast -c earthr1.cuf
mpifort -Mpreprocess -DTIME -fast -mcmodel=medium earthr1.o emodule.o -o earthr1

Everything was just fine until I finally need to test my program on a cluster with PGI 15.9 installed. When I tried to compile the source on the cluster with the same command, the compiler gave me an error:

pgf90-Error-Switches -fPIC and -mcmodel=medium are not supported together

I’m not sure what is the wrong, I mean I didn’t put no -fPIC option in my compile command so why would the compiler give me such an error message?

The thing is I have to add this mcmodel=medium option in my compiling command because the total memory size exceeds 2GB, if I remove that option, I will get the following errors:

/home/XXX/earthr1.cuf:84:(.text+0xd8): relocation truncated to fit: R_X86_64_PC32 against symbol _zoner_0_' defined in COMMON section in emodule.o /home/XXX/./earthr1.cuf:87:(.text+0xe9): relocation truncated to fit: R_X86_64_PC32 against symbol zoner_0’ defined in COMMON section in emodule.o

Please tell me what I am supposed to do to solve this problem.

After searching a while, I finally realized that mpif90 is nothing but a script. So the pgi compiler has added the -fPIC to mpif90 script by default, the solution is to get a copy of the mpif90, and then delete all -fPIC options in the script.

A better solution is to get rid of the -mcmodel=medium requirement by
dynamically declaring the large arrays.

parameter (n4G=4294967296) ! 4 giga
double precision, dimension(1),allocatable::a(:),b(:),c(:)
allocate(a(n4G/4),b(n4G/4), c(n4G/4)) ! 3 8GB arrays
! then assign values to the arrays
!

and you can drop -mcmodel=medium. Your executable gets smaller,
and you can link in code not compiled -fPIC (unlike with -mcmodel=medium).
Since the optimization technology works better w/o -mcmodel=medium, the
code may run faster as well, when compiled -fast.

I just installed 16.5 workstation, and added
$PGI/linux86-64/16.5/bin and $PGI/linux86-64/2016/mpi/openmpi/bin

to my $PATH.

I then found I had problems with mpif90 until I added

$PGI/linux86-64/16.5/lib to $LD_LIBRARY_PATH .

Then
mpif90 -mcmodel=medium foo.f

compiled with incident.

Check your environment, and add -dryrun or -v to your
compilation and see if you everything is coming from the correct
directories.