One weakness of scripted languages is that the computer's CPU has to interpret the instructions before it can execute them. This takes time, so if you want your algorithm to run faster you should get the interpretation out of the way ahead of time and make a binary executable version of your program in machine code that your CPU can execute directly. This is called compiling, and all applications that involve massive, time-consuming calculations are written in compilable languages. The most common of these are C and C++, but the oldest and in many respects the simplest is FORTRAN, which is still the most widely used for certain types of computational physics. So this week we will introduce ourselves to FORTRAN.
On hyper, the FORTRAN compiler is gfortran, which stands for the GNU implementation of Fortran 95. (There is a lot of idiosyncratic history here - big surprise . . . . )
As usual, create your /home2/phys210/$USER/a07/
directory
and put all your finished results in it.
~phys210/fib.f
contains the FORTRAN source code
to generate a sequence of Fibonacci numbers,
starting from one specified by the user
and continuing for an additional number of Fibonacci numbers
also specified by the user.2
gfortran fib.f -o fib
which creates an executable image in the file fib (if you left out the "-o fib" part, it would put the executable image in the default file a.out).
call linfit (npts, x, y, w, p0, p1)
and the second (linfit_sub.f) containing the subroutine "source" code, starting with the lines
subroutine linfit (npts, x, y, w, p0, p1)
integer npts
real x(*), y(*), w(*), p0, p1
. . . and ending with . . .
return
end
Generating the executable image is only slightly more complicated now that it has to be assembled from two components. See if you can guess how to do this. (Try the most obvious guess first!)
To learn about static libraries, just say "man ar" - because the program that manages static libraries is called ar (for "archiver"). All you really need to know are these three steps:
gfortran -c linfit_sub.f
ar rcs libmy.a linfit_sub.o
gfortran linfit_main.f libmy.a -o linfit4
Do it. Save all your results in the usual place, along with all your source code, object modules and executable files.
In Linux this script is called a Makefile, because
the program that interprets the commands in the Makefile
is called make and, as usual, it is invoked by the command "make".
See (e.g.)
http://en.wikipedia.org/wiki/Make_(software)
or Google "Makefile" for details.
The syntax of a Makefile is extremely arcane.
Some consider it to be the ultimate in "Geek lingo".
Nevertheless it is possible to adapt a working Makefile
to make your own project, by just changing the names
of the programs, libraries and sources.
If you copy the files fib_main.f
,
fib_sub.f
and Makefile
from ~phys210/
to your own
/home2/phys210/$USER/a07/
directory, cd to that directory
and enter the terse command
make
you should get a new version of the fib executable and a new file called libmy.a containing the fib_sub.o object module. Then, without necessarily understanding exactly what the Makefile is doing,5 edit and adapt your copy of the Makefile to add linfit_sub.o to the libmy.a static library and, from it and linfit_main.f, compile and build your linfit executable.
Once this works, you can make any modifications you like to either linfit_main.f or linfit_sub.f and generate a new, up-to-date, executable linfit just by entering "make".