gfortran 4.4.1 optimization bug for ran_state.F

Discussion on computers, ROMS installation and compiling

Moderators: arango, robertson

Post Reply
Message
Author
abarth
Posts: 5
Joined: Mon Jul 27, 2009 3:50 pm
Location: University of Liege

gfortran 4.4.1 optimization bug for ran_state.F

#1 Unread post by abarth »

I stumbled upon an optimization problem with gfortran and I wanted to share my solution in case anybody would have the same issue.

I compiled ROMS 3.3 with gfortran 4.4.1 and tried to compute the error covariance normalization
factors using the randomization method. It fails with the following error message:

Computing initial conditions 2D normalization factors at RHO-points
RAN_INIT: arith assump 3 fails

ran_state.F relies on overflow of integer arithmetics, which is assumed not to happen with the -O3 optimization flag (and even -O2). Luckily, you can easily disable optimization based on this assumption with -fno-strict-overflow. Below is the change I made to Linux-gfortran.mk. Maybe it would be useful to include it in the svn version?

Cheers,
Alex


Code: Select all

Index: Linux-gfortran.mk
===================================================================
--- Linux-gfortran.mk   (revision 433)
+++ Linux-gfortran.mk   (working copy)
@@ -106,6 +106,11 @@
 
 $(SCRATCH_DIR)/def_var.o: FFLAGS += -fno-bounds-check
 
+# Prevent gfortran from assuming that no overflow occurs in ran_state
+
+$(SCRATCH_DIR)/ran_state.o: FFLAGS += -fno-strict-overflow
+
+
 #
 # Set free form format in source files to allow long string for
 # local directory and compilation flags inside the code.

User avatar
arango
Site Admin
Posts: 1350
Joined: Wed Feb 26, 2003 4:41 pm
Location: DMCS, Rutgers University
Contact:

Re: gfortran 4.4.1 optimization bug for ran_state.F

#2 Unread post by arango »

Yeah, good idea, Thank you. Random number generators in parallel are very tricky. These routines requires very special integer manipulations that go beyond the F90 integer model.

User avatar
m.hadfield
Posts: 521
Joined: Tue Jul 01, 2003 4:12 am
Location: NIWA

Re: gfortran 4.4.1 optimization bug for ran_state.F

#3 Unread post by m.hadfield »

Note that the fix to this problem implemented in ticket 399 breaks for Gfortran 4.1.2, which does not support the -fno-strict-overflow option. A quick search of the WWW uncovers this document...

http://vmlinux.org/cgi-bin/dwww/usr/sha ... /NEWS.html

...which, as I read it, says the strict-overflow optimisation (the one that's giving the problem) was introduced in GCC 4.2, so for older compilers the -fno-strict-overflow option is neither necessary nor allowed.

The best solution is probably for me to upgrade my compiler.

User avatar
m.hadfield
Posts: 521
Joined: Tue Jul 01, 2003 4:12 am
Location: NIWA

Re: gfortran 4.4.1 optimization bug for ran_state.F

#4 Unread post by m.hadfield »

Here's some work-around code for the make file

Code: Select all

#
# Allow integer overflow in ran_state.F.  This is not allowed
# during -O3 optimization. This option should be applied only for
# Gfortran versions >= 4.2.
#

GFV := $(shell ${FC} --version | head -1 | cut -d " " -f 5 | cut -d "." -f 1-2)

ifeq "$(findstring ${GFV},4.0 4.1)" ""
$(SCRATCH_DIR)/ran_state.o: FFLAGS += -fno-strict-overflow
endif
Additional strings could be added to the findstring function as necessary.

I'm not saying it's pretty and I'm not saying it should go in the ROMS source, but it's there for anyone who needs it.

User avatar
m.hadfield
Posts: 521
Joined: Tue Jul 01, 2003 4:12 am
Location: NIWA

Re: gfortran 4.4.1 optimization bug for ran_state.F

#5 Unread post by m.hadfield »

I've cleaned up my makefile change slightly and put it in these files (login required):

https://www.myroms.org/svn/omlab/branch ... fortran.mk
https://www.myroms.org/svn/omlab/branch ... fortran.mk
https://www.myroms.org/svn/omlab/branch ... fortran.mk

Again, I'm not suggesting it goes in the master copy, but it might be useful for someone else.

hugobastos
Posts: 15
Joined: Tue May 06, 2008 8:46 pm
Location: FURG

Re: gfortran 4.4.1 optimization bug for ran_state.F

#6 Unread post by hugobastos »

hello,

Recently, i need to install roms in another server , and found this problem. If helps anyone:


there is an error here for the Red hat GNU Fortran (the solution is base on output of --version.)

In fedora and centos distros the output with "head -1" looks like:

GNU Fortran (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)

And in debian:

GNU Fortran (Debian 4.3.2-1.1) 4.3.2


So, the findstring function in the linux-gfortran.mk generates an empty string ( because of the N local in the cut command) , which causes the flag "-fno-strict-overflow" to be used in versions <4.2.

a fast solution in redhat,fedora and centos : add ifeq ( yeah...a lazy fix,but works!):


Start Line:120

ifeq "${FC_TEST}" ""
ifeq (,"$(findstring $(shell ${FC} --version | head -1 | cut -d " " -f 4 | \
cut -d "." -f 1-2),4.0 4.1)" "")
$(SCRATCH_DIR)/ran_state.o: FFLAGS += -fno-strict-overflow
endif
endif

Post Reply