Computer Laguages A to Z: Fortran
by Gary L. Ratliff Sr. (eronstuc)
Fortran was the first high level language created. It was established in 1954 by a team at IBM led by John Backus. This gentleman later worked on ALGOL and created another language called FP. He received the Turing Award for his major contributions to computing throughout his life.
Fortran was widely accepted and is currently the language of choice for scientific computing and engineering. Each company tried to improve the features offered so that soon many differing versions of Fortran were offered. This made it very difficult to write code which was portable. Therefore, the language was standardized and the first standard for the language came to be known as Fortran66.
Fortran was also the first high level language I learned. The version I learned was FORTRAN IV in 1970 at what was then called Delta State College. Now at this time computers filled entire rooms. The program was keyed into cardboard cards which could hold 80 characters. Each statement in the program occupied one card. The program would be assembled into a deck and then on top of this would be added a set of cards which constituted the JCL (Job Control Language) for the computer.
The earliest version of FORTRAN had very rigid requirements. ONLY CAPITOL LETTERS COULD BE USED TO KEY IN THE PROGRAM. The non-alphabetic keys which could be used in writing a program were limited to just 13 characters. The Hollerith cards used to key in the program were divided into specific fields: Column 1 was for a comment and was indicated by entering an * in this column. Columns 1-5 were reserved for labels. Column 6 was reserved for a continuation marker the one we were encouraged to use was &. Columns 7-72 were for the text of the program statements; finally columns 73-80 were for the card sequence number.
As times past and more versions of Fortran became the current standard these strict rules were retained mostly so that the firms who had much invested in the software could still use their old programs. As computer techniques improved these old features were regarded as archaic. In fact when receiving his Turing Award John Backus delivered and address which complained about the inefficiencies of the "Von Neumann bottleneck." This is that there is essentially one word flowing from the CPU to the memory (store) or from the store to the CPU and any one time.
How to obtain Fortran for your computer
As usual either apt-get or the synaptic package manager may be used to install a FORTRAN compiler on your computer. In fact there is more than one version of FORTRAN available. The gnu compiler collection has compilers for both Fortran77 and Fortran95. So in order to use synaptic you will first become root.
su <enter root password> synaptic
Once you have gotten the package manager perform a search for fortran. And we will install both the Fortran 77 and the Fortran 95 compilers. The synaptic program will inform you that it is going to install these six packages: gcc3.3, gcc3.3-cpp, gcc3.3-g77, libf2c0, gcc-gfortran, and libgfortranl. (If you followed the instructions in the language article about C/C++ you will already have a version of the GNU Compiler Collection on your computer. Unfortunately the Fortran 77 in the repository uses an older version of the collection for its f77 compiler.
Having obtained the necessary compilers the next goal would be to learn to use the FORTRAN computer language. For this purpose we will perform a search for Fortran 77 Tutorials and also another search for Fortran 95 tutorials. Now just as C is a proper subset of C++, Fortran 77 is a proper subset of Fortran 95. Now the gcc identifies the language the source code is in by the file suffix: program.c is a C program while program.cpp is a C++ program; furthermore, program.f would be a Fortran 77 program while program.f95 would be one for the Fortran 95 language.
In the treatment of Elisp we installed the Emacs text editor. This will also allow you to compile and run programs from within the editor. If you use a xterm to open emacs with the command:
$ emacs program.f
You will see that the menu now contains a Fortran item. Once you have keyed in your program you would select the Tools menu and from there select compile. Then you would back over the make -k command this will provide and enter the command to compile the source file.
$ f77 -o program program.f
Here the program will either report that the program compiled or it will show you in which line there was discovered a syntax or other type of error. If the program contains no request for user input you would also be able to run this program from within Emacs by using the Tools menu and selecting shell. In this example the shell command to use would be:
$ ./program
If you forget that your program contains any input requests they you will receive an error message. However, the program was compiled correctly it will just need to be launched from a terminal program such as xterm using the same command. In a like fashion your Fortran 95 programs may be created in emacs by issuing from a terminal the command:
$ emacs program.f95
However, it might be better to use the extension f90 as I noted the .f extension will produce a menu for Fortran and the .f90 extension will produce an item for f90. Now when these are displayed the text of the source code will be highlighted. Now this highlighting will make it easier to discover errors in the program. For some reason the version of emacs does not have a tab when the exttension f95 is used. Nor does it highlight the source code.
Odd features and restrictions of the Fortran compilers
Fortran 77 is a fixed form language and Fortran95 is a free form language. Therefore, if you have the first line "program testz" beginning in column 3 instead of column 7. An attempt to compile using the f77 compiler would fail while the command: gfortran -o testz testz.f would compile. The Fortran 77 compiler does relax some of the rigid restrictions required by strict adhearance to the Fortran 77 standard. One can use lower case letters, one can use a do while loop instead of the only recognized do loop of FORTRAN 77.
The Fortran 77 compiler may be started by either f77 or g77 and the Fortran 95 compiler may be started with either f95 or gfortran. This mystery is solved by looking in /usr/bin. Here you will find that f77 and g77 are symbolic links to /etc/alternatives/f77 and /etc/alternatives/g77. Once there it is discovered that both point to: /usr/bin/g77-3.3.6. In a similar manner f95 and gfortran are links to /etc/alternatives/f95 and gfortran which ultimately point to /usr/bin/gfortran-4.1.1.
Next, both these compilers first translate the fortran source into C and then compile the C language translation. This may be verified by compiling a source program using the -g flag to enable debugging. Then when trying to list the code in the gdb program the line will read init c. The clue to this is that the fortran 77 support library is named libf2c0 and f2c is the name of the program which trans- lates fortran source code into C. At this time the repositories do not offer the f2c program, Howeverm I had it installed in another of my Linux partitions and show an example.
The technique of cutting a section of code and then pasting this into the emacs editor may be used in perusing the tutorials. All that is required is to write a driver for the routine and the program will compile. This will be illustrated in thepictures. Now some of the tutorial are more useful than others. The Fortran 77 Tutorial from Stanford University is a particularly good one. Also if you open many of the other selected tutorials you will learn that this tutorial seems to serve as a model for many of the others.
The Fortran 95 tutorials assume that one is already familiar with Fortran 77. So if you are interested in learning this language it would be good to concentrate on that language before delving into Fortran 95. The tutorial on Fortran 90 for the Fortran 77 programmer gives the features which are altered.
The Fortran 77 programs are very much like those same programs I keyed into Hollerith cards back in the 70's Yes several courses in working on my Masters required computer programming. Just a little joke on me one night the schools computers were down. So a graduate student who worked at Baxter Labs offered to let the class come over and run their class assignments on their terminal which was connected to the mainframe in Cincinnati. Now after a while I learned that my assignment had been stopped for using excessive computer time. I received printout which contained page after page of neatly formatted zeros!! On debugging the program the massive printout was found to have been caused by a misplaced comma!!
The Fortran compilers have been installed by the package manager.
Just the tip of the iceberg is shown by the first page of tutorials offered for Fortran 77. The one from Stanford University is very complete. Don't miss the tips about obtaining commercially written Fortran subroutines if your business needs some advanced engineering calculations. The tutorial in the obiquity.com has one section with a script to perform a spped test on how fast your computer really does math. Also the repository offers some of these advanced Fortran packages. Look closely at just what a search on Fortran offers.
Here we see that a program may be compiled and also executed from within the Emacs editor. Just as an experiment move some of the text to the forbidden zone and watch the highlight change. However, the program may be executed from within Emacs only if it requires no input from the user.
Here the ability to copy code from a tutorial and then paste it into the Emacs editor is shown. This is showing a nested if construction. This is from the Fortran 77 Tutorial from Stanford University. Their copyright states that it may be used for academic purpose. All that need be done is to write a driver for the routine.
Here the driver consists of the required program line. Then the variable section defines the values of x and y. Then the nested if routine has been inserted into the program and finally the required stop and end lines are added. Also as a means of keeping one from placing program statements into the areas reserved for labels and continuation lines some comments have been placed at the top of the program. As the GNU COMPILER COLLECTION version of the Fortran 77 compiler allows the use of c in column 1 as a comment the rest are just the numbered positions. The second comment marks the decades.
I mentioned that both the compilers translate the Fortran code into C. The repository does not currently have the application f2c. So in the partition for Dreamlinux I noted that this has f2c as well as ratfor and other gcc compilers listed. I created a very simple test program and then ran the f2c program on it. Then the resulting test.f and test.c were combined into one file using: cat test.f test.c > example.f2c.
Now back in PCLinux I entered the Dreamlinux partition and took a snapshot of it. Essentially this shows how the compilers actually compile Fortran programs.