Skip to main content Skip to navigation

Fortran Compiler options - Basics

Putting it into Practice:

Code up the statements below - it is a trivial example of using a module in a program (called area) where the module myconstants defines some constants.
! declare your module
module myconstants
implicit none
real, parameter :: pi=3.1415927, ee=2.7182818
end module myconstants
! start the main body of code
program area
! include your module with the *use* statement
use myconstants
implicit none
real :: radius, result

print*,"Please enter the radius of a circle"
read*,radius

result = pi * radius**2
print*,"the area of the circle is ",result," units"

end program area
Compile and run this code. Try writing some variations on this code.
Back to top

2. Subroutine/function definition

In f90, a program procedure can be either a subroutine or a function. A subroutine is a named section of code that performs a given task, and can be invoked from within a program unit. A function is similar, but is used to generate a single answer, and is invoked by referring to the function name. The function may take any type (eg integer, real, etc). It is often the case that there is no need to write a procedure yourself, since f90 has many useful intrinsic functions, plus there are many numerical libraries (eg, NAG, BLAS, LAPACK, ATLAS etc) that are available. These libraries will be optimised for running on your particular platform and should be very fast - probably much faster than anything you could write yourself.

A program procedure may be declared and specified within the main body of code, or may be external to the main body of code. A procedure must have arguments (variables or constants) passed to it. Furthermore, a procedure can contain local variables/constants. These local objects are created when the procedure is invoked and destroyed when the program exits from this procedure - these local objects do not retain values between invocations (calls). The procedure declaration must contain placeholders for these passed arguments - called dummy arguments. This is known as argument association.

Example:

In the main body of a code, suppose we make a call to a function called work, passing it a real variable called a, eg
  print*,work(a)
The corresponding definition (declaration) of this function in this code is:
 real function work(x) 
So in this example, the dummy variable is x. When the program enters this function, it will replace the value of x with the current value of a
Back to top
  1. The basic compile command on solar machine is f77. Please try this first
  • f77 program.f
  • The default execution file name is a.out. To execute the program, just type a.out and press the return key.
  • If you want double precision for your calculation, use -r8 option.
  • f77 -r8 program.f
  • For a more efficient run, use -O4 option (O is capital o, not number zero).
  • f77 -r8 -O4 program.f
  • You can compare the effect of this option with the first one.
  • The execution file name can be changed with -o option (small o, not number zero).
  • f77 -r8 -O4 program.f -o test.out
  • To save all the terminal output into a file, use a redirection command >
  • a.out > test.log
  • To run a background job, use & command. Note that the background job will terminate when you log-out the computer.
  • f77 -r8 -O4 program.f &
  • a.out &
  • a.out > test.log &
  • To keep the background job running after log-out, use nohup command (no hang-up command).
  • nohup a.out &
  • nohup a.out > test.log &
  • Compiler command changes depending on the type o machines. Please check this prior to using the machine.
  • For Tsubame, the compile command is pgf77 (Tsubame) or cf77.
  • To compile, pgf77 ch*.f fourier.f -r8 -O3 -fastsse -tp k8-64 -mcmodel=medium -o a.out
  • To execute, n1ge -smp 16 -N ST-RE10 -q A re10.out -sgeout test.log

In Summary

  • f77 jet.f [single precision]
  • f77 -r8 jet.f [double precision]
  • f77 -r8 jet.f -o jet.out