Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29274 Discussions

Intel Fortran Include Files (*.fi): Arrays lose their values from subroutine to subroutine

egpuckett
Beginner
1,634 Views
I am running Intel Visual Fortran Compiler 11.0.074 which (I assume) is the latest version of Intel Visual Fortran Compiler 11.0. (BTW How do I check?)

I have an include file named "test_include.fi" that contains just one array double precision a(16) I call a subroutine named "sub_1(dummy)" from the main program "Test_Include.f". The subroutine "sub_1" includes the statement

include "test_include.fi"

and sets the array "a" as follows:

do j = 1, 16
a(j) = j
end do T

hen I return to the main program ""Test_Include.f" and call "sub_2(dummy)" which also includes the statement

include "test_include.fi"

However when I print the values of these variables they are now all 0.

This is completely unexpected to me. I've been programming in Fortran for about 30 years, and I expect the values in the include files to remain unchanged, just as if they were in a common block. That was the whole point of FORTRAN include files -- so one didn't have to change 1 line in 50 different routines when changing the common block.

I ran the same code on a 64 bit AMD machine running Linux with the g77 compiler and the values that that I printed out in sub_2 were the ones that were originally set in sub_1. On the same machine I then compiled and ran the code with Intel Visual Fortran Compiler 9.0

fuzzy$ ifort --version
ifort (IFORT) 9.0 20051201
Copyright (C) 1985-2005 Intel Corporation. All rights reserved.

and the values of a(j) are all 0.0d0 in sub_2.

I'm totally confused. Isn't there a ANSI type standard for how a FORTRAN include file should behave with respect to the values of the variables that are declared in that include file?

What's even more confusing is that if I declare "a" to just be a single variable rather than an array double precision a it retains its value from sub_1 to sub_2, but NOT if I add the array a(16) to the include file. Very puzzling. I am attaching all of the relevant files.

Thanks!
--
Elbridge Gerry Puckett email: egpuckett@ucdavis.edu
Professor
Dept. of Mathematics
U. C. Davis Davis, CA 95616
0 Kudos
6 Replies
TimP
Honored Contributor III
1,634 Views
Do you mean that you want arrays declared in an include file, without a common block, to behave like module arrays?
By the way, I'm a graduate of Elbridge Gerry school, which was 3 blocks from where he lived at the time he made history, for good and bad, thus I'm a butt of one of Steve L's favorite cracks.
0 Kudos
egpuckett
Beginner
1,634 Views
Quoting - tim18
Do you mean that you want arrays declared in an include file, without a common block, to behave like module arrays?
By the way, I'm a graduate of Elbridge Gerry school, which was 3 blocks from where he lived at the time he made history, for good and bad, thus I'm a butt of one of Steve L's favorite cracks.

I do not know what a module is, especially when considered as a FORTRAN construct. I read the Intel VF manual explanation of modules, but it seemed that they were a C or C++ construct that was then implemented in FORTRAN. Am I mistaken about this?

All of the other FORTRAN compilers that I've worked with (that I can remember -- I've forgotten FORTRAN on DEC machines running VMS and FORTRAN on CRAYs running the original Cray OS -- had variables in include files keep their value. That included Cray Fortran (at least under "UNICOS", Cray's version of Unix), SUN FORTRAN, f77, g77, etc.

Perhaps, Intel is trying to make FORTRAN include files behave like include files in more modern languages like C++?

Would you plesae enlighten me? Thanks!

Where is Elbridge Gerry school? In Cambridge or Boston? I have never heard of it, even though I grew up in a suburb of Boston and at one point when to high school in Watertown passing by the "Elbridge Gerry" house in Cambridge (near Harvard Square) each day. I'm supposed to be a descendent, but my genealogical reasearch into the matter leads me to doubt this.

- G

P.S. I'm guessing that you work for Intel if you are a butt of one of Steve L's favorite cracks. Do you mind telling me what it is?
0 Kudos
Steven_L_Intel1
Employee
1,634 Views
Actually, I'd like Tim to explain the joke too, because I don't get it. I also don't recall making Tim the butt of any jokes. I suppose I could always start.

Anyway, the problem is that you are expecting non-Fortran behavior. In particular, the Fortran language says that local variables in subroutines do NOT retain values between calls unless they have the SAVE attribute. Many older compilers, though, either by accident of design or as an extension, preserved variables across calls. Intel Fortran does not do that unless the /Qsave option is used.

My recommendation is to add SAVE statements for those variables you want preserved. For example, in test_include.fi, add the line:

save a

Add SAVE statements for any other variables you want saved.

And, by the way, that you used INCLUDE files had no effect on the problem The same would happen if the declarations were typed directly into the subroutine source.

For further reading, see an article I wrote ten years ago, Better SAVE Than Sorry.
0 Kudos
TimP
Honored Contributor III
1,634 Views
I don't remember exactly how it went, but I've heard Steve tell the one about light dawning on Marblehead more than once. If it was meant to include me, it was undoubtedly deserved. As I lived there more years than anywhere else, including my time at Elbridge Gerry school, I'm famiiar with some of Elbridge's claims to fame. No doubt he lived closer to Boston when he was governor.
1. signed Declaration of Independence
2. refused to sign Constitution, but stood for election and served 2 terms in US House.
3. switched parties (more than once?)
4. elected governor of Mass. on 5th attempt.
5. lost popular support over invention of gerrymander, but became Vice President of USA, died in office
No doubt he captured as much media coverage as was possible in those positions at the time.

We have another thread here where someone appears to be depending on SAVE behavior, not accepting that it isn't compatible with threaded parallelism. I wasn't certain that was what was requested here. Up to 20 years ago, INCLUDE used to be used frequently to put COMMON blocks in subroutines, so as to share data among subroutines.
0 Kudos
Steven_L_Intel1
Employee
1,634 Views
Oh my - the "Light dawns on Marblehead" is a New England joke (I'm from New Hampshire) and is supposed to be self-referential. In other words, when I say it, I'm the marblehead. Tim, I had no idea you ever lived out here and never intended the joke to refer to you.
0 Kudos
bmchenry
New Contributor II
1,634 Views

In any include file you wish to have data between routines, you will have to 'include' in the 'include' file either a common block or a Module.
A module is basically similar to a common block.
I'd get a Fortran book and read up on Modules. They are simple to use and can be used to pass data between routines.
And of course, use SAVE!!
0 Kudos
Reply