Fortran: Syntax error in CHARACTER declaration - fortran

I am trying to run a program, which works fine on my laptop, on a remote supercomputer. But the program is not compiling there. Trying to trace the problem, I reduced the program to the basic minimum, and still it is giving me a compilation error. Anyone has any ideas what might be going wrong here?
[k00603#fe01p08 python_utilities]$cat test.f90
program test
character(:), allocatable :: out
end program test
[k00603#fe01p08 python_utilities]$gfortran test.f90
In file test.f90:3
character(:), allocatable :: out
1
Error: Syntax error in CHARACTER declaration at (1)
I guess the gfortran is running fine as when I do the following, it works:
[k00603#fe01p08 python_utilities]$cat test.f90
program test
print *, "Hello World!"
end program test
[k00603#fe01p08 python_utilities]$gfortran test.f90
[k00603#fe01p08 python_utilities]$./a.out
Hello World!
The compiler on the supercomputer is:
[k00603#fe01p08 256]$gfortran --version
GNU Fortran (GCC) 4.1.2 20080704 (Red Hat 4.1.2-51)
Copyright (C) 2007 Free Software Foundation, Inc.

CHARACTER(:)... is a Fortran 2003 feature known as deferred length character. It was only recently added to gfortran and support in some areas (deferred length character components) is still incomplete.
Your supercomputer is probably running a older version of the compiler which lacks support for this feature.

character(:), allocatable :: out is declaring an allocatable scaler. This is a new feature of Fortran 2003. The compiler on the super computer likely doesn't support this newer feature.
You imply that you are using gfortran. http://gcc.gnu.org/wiki/GFortran lists allocatable scalers as added in gfortran version 4.5 (see 4.5 / Fortran 2003). The current release version is 4.7.

Related

Fortran program using character variable with allocatable length always shows a warning when compiling

I have the following code that uses a character variable with allocatable length.
PROGRAM testprog
IMPLICIT NONE
CHARACTER(LEN=5) :: param
CHARACTER(LEN=:), ALLOCATABLE :: val
param = '12455'
val = param
WRITE(*,*) val
END PROGRAM testprog
I compile it using gfortran versions 7.5 or 8.4 with all warnings activated (option -Wall) and I get the following warning:
test.f90:6:0:
val = param
Warning: ‘.val’ may be used uninitialized in this function [-Wmaybe-uninitialized]
The program works. However, I do not understand why this warning message appears.
This is a compiler bug. It is well-known, but not fixed in GCC yet. You can see the report at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91442
You can either ignore it, or disable the "may be used uninitialized" warnings with -Wno-maybe-uninitialized or compile with optimizations (-O1 and more).

ieee_arithmetic intrinsic module in gfortran

I need to use gfortran to compile a library that is dependent on ieee_arithmetic. However, it is found that gfortran can not identify this module.
For example with the code a.f90
program test
use,intrinsic :: ieee_arithmetic
real :: x
read *, x
if (ieee_is_nan(x)) then
print *, "Nan"
else
print *, "Not NaN"
end if
end program test
I have the following message when compiling
$ gfortran a.f90
a.f90:2.19:
use,intrinsic :: ieee_arithmetic
1
Fatal Error: Can't find an intrinsic module named 'ieee_arithmetic' at (1)
How can I let gfortran know where the ieee_arithmetic intrinsic module is?
ifort is found to be able to use the ieee_arithmetic module. But I wish to make gfortran work for this case.
The IEEE modules are supported as of GFortran version 5. See https://gcc.gnu.org/gcc-5/changes.html If you are using an older version, you should see the error message you have shown in your post.

fortran syntax eorr while using DATA statement to initialize an array

I encountered the following Fortran code which cannot be compiled using gfortran:
CHARACTER(LEN=20) :: filename(max_xoms,2)
DATA(filename = RESHAPE(SOURCE=(/'XobsXOM0.txt','XobsXOM1.txt','XobsXOM2.txt','XobsXOM3.txt','XobsXOM4.txt', &
'XobsXOM5.txt','XobsXOM6.txt','XobsXOM7.txt','XobsXOM8.txt','XobsXOM9.txt', &
'XobsXOS0.txt','XobsXOS1.txt','XobsXOS2.txt','XobsXOS3.txt','XobsXOS4.txt', &
'XobsXOS5.txt','XobsXOS6.txt','XobsXOS7.txt','XobsXOS8.txt','XobsXOS9.txt'/), &
SHAPE=(/max_xoms,2/)))
The makefile that comes with the code uses ifort. I changed the compiler to gfortran and got an error message while compiling the above:
gfortran -c -fbacktrace -ffree-line-length-none -Wall hype_indata.f90
hype_indata.f90:48.16:
DATA(filename = RESHAPE(SOURCE=(/'XobsXOM0.txt','XobsXOM1.txt','XobsXOM2.txt'
1
Error: Syntax error in DATA statement at (1)
I've tried removing = at 1, but that doesn't fix the statement.
Can anyone please explain how should I fix this statement?
Thanks
BTW, gfortran --version returns:
GNU Fortran (tdm64-2) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
if max_oms is a parameter (i.e. a constant, and it probably is one) you can do:
CHARACTER(LEN=20) :: filename(max_xoms,2) = RESHAPE(SOURCE=(/'XobsXOM0.txt','XobsXOM1.txt','XobsXOM2.txt','XobsXOM3.txt','XobsXOM4.txt', &
'XobsXOM5.txt','XobsXOM6.txt','XobsXOM7.txt','XobsXOM8.txt','XobsXOM9.txt', &
'XobsXOS0.txt','XobsXOS1.txt','XobsXOS2.txt','XobsXOS3.txt','XobsXOS4.txt', &
'XobsXOS5.txt','XobsXOS6.txt','XobsXOS7.txt','XobsXOS8.txt','XobsXOS9.txt'/), &
SHAPE=(/max_xoms,2/))
otherwise move
filename = RESHAPE(SOURCE=(/'XobsXOM0.txt','XobsXOM1.txt','XobsXOM2.txt','XobsXOM3.txt','XobsXOM4.txt', &
'XobsXOM5.txt','XobsXOM6.txt','XobsXOM7.txt','XobsXOM8.txt','XobsXOM9.txt', &
'XobsXOS0.txt','XobsXOS1.txt','XobsXOS2.txt','XobsXOS3.txt','XobsXOS4.txt', &
'XobsXOS5.txt','XobsXOS6.txt','XobsXOS7.txt','XobsXOS8.txt','XobsXOS9.txt'/), &
SHAPE=(/max_xoms,2/))
to the position of a first executable statement.
Generally, avoid DATA in Fortran 90 and later.

Why does 'use mpi' fail with mpif90

In order to compile MPI code in gfortran I have to use the syntax
include mpif.h
in my code instead of
use mpi
Several websites indicate that this syntax is for Fortran 77 however, I am using gfortran gcc version 4.7.2 (Debian 4.7.2-5) and mpfi90 for MPICH2 version 1.4.1p1.
The command line
mpif90 test1.f90 -o test1.exe
produces the following error
test1.f90:4.8: use mpi 1 Fatal Error: Parse error when checking module version for file 'mpi.mod' opened at (1)
test1.f90 (from Coursera course on HPC)
program test1
use mpi !(fails to compile)
implicit none
include 'mpif.h' !(this works)
integer :: ierr, numprocs, proc_num
call mpi_init(ierr)
call mpi_comm_size(MPI_COMM_WORLD, numprocs, ierr)
call mpi_comm_rank(MPI_COMM_WORLD, proc_num, ierr)
print *, 'Hello from Process number', proc_num, &
' of ', numprocs, ' processes'
call mpi_finalize(ierr)
end program test1
Another option I often encounter is when the Fortran compiler used to build the MPI library is not compatible with your current Fortran compiler. Then the problem is the incompatibility of the .mod files. Gfortran is more susceptible to this, than say Intel Fortran, because it changes the module format more often.
Depending on how MPICH2 was compiled, perhaps the F90 interface wasn't built. That tends to happen depressingly often when using packages built by C-heads.

In Fortran, is the stop intrinsic expected to print a 'STOP' in standard output?

I often use the stop intrinsic in Fortran to stop the execution for various reasons (mainly after fails in tests).
program test1
stop
end program
does not do anything but stop the program execution.
program test2
stop 'hello'
end program
prints in the standard output :
STOP hello
I never noticed this behavior before. Is this "STOP" expected before "hello" in the output? or is it a compiler bug or ...?
My compiler is gfortran --version:
GNU Fortran (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
Copyright (C) 2012 Free Software Foundation, Inc.
It is not a bug. The Fortran standard specifies that it is up to the Fortran processor how the stop code is "made available". It recommends the use of formatted output to the error unit, but the format (in a general sense) can be whatever the compiler authors thought best.