compiling and running fortran 90_95 programs - a basic guide
TRANSCRIPT
7/28/2019 Compiling and Running Fortran 90_95 Programs - A Basic Guide
http://slidepdf.com/reader/full/compiling-and-running-fortran-9095-programs-a-basic-guide 1/5
7/28/2019 Compiling and Running Fortran 90_95 Programs - A Basic Guide
http://slidepdf.com/reader/full/compiling-and-running-fortran-9095-programs-a-basic-guide 2/5
6/30/13 Compiling and Running Fortran 90/95 Programs - a basic guide
www.fortran.gantep.edu.tr/compiling-g95-basic-guide.html
g95 myprog.f90 -o myprog -Wuninitialized -Wimplicit-none -Wunused-vars -Wunset-vars -fbounds-check-ftrace=full -O2
If speed of executation is important then the following options will improve speed:
g95 myprog.f90 -o myprog -Wuninitialized -Wimplicit-none -Wunused-vars -Wunset-vars -O2
On central interactive Linux servers in the university, the fortran command (or simply f) provides these options by default, example:
fortran myprog.f90
is equivalent to
g95 myprog.f90 -o myprog -Wuninitialized -Wimplicit-none -Wunused-vars -Wunset-vars -fbounds-check-ftrace=full -O2
Type fortran with no arguments to find the up-to-date list of default arguments.
5. Compiling subprogram source files
It is sometimes useful to place sub-programs into separate source files especially if the sub-programs are large or shared with other
programmers. If a Fortran 90/95 project contains more than one program source file, then to compile all source files to an executable program you
can use the following command:
g95 main.f90 sub1.f90 sub2.f90 sub3.f90 -o myprog
In this example main.f90 is the main program source and sub1.f90, sub2.f90, sub3.f90 are files contain subprogram sources.
A working example:
Consider the following main program source main.f90 and three sub-program sources sub1.f90, sub2.f90, sub3.f90.
The sources are:
main.f90------------------real :: aa=2.0call sub1(a)call sub2(a)call sub3(a)end
sub1.f90 sub2.f90 sub3.f90------------------ ------------------ ------------------subroutine sub1(x) subroutine sub2(x) subroutine sub3(x)real :: x real :: x real :: xprint *, x print *, x**2 print *, x**3end end end
The four sources can be compiled together with the command:
g95 main.f90 sub1.f90 sub2.f90 sub3.f90 -o myprog
The program is run by simply typing its name:
myprog2.4.8.
Note that in this example the subroutines are external (as in Fortran 77).
In Fortran 90/95 subroutines can also be written in a module.
In this case the program sources are:
main2.f90 mod.f90------------------ --------------------------
USE mod MODULE modreal :: aa=2.0 CONTAINScall sub1(a)call sub2(a) subroutine sub1(x)call sub3(a) real :: xend print *, x
end subroutine sub1
subroutine sub2(x)real :: xprint *, x**2end subroutine sub2
7/28/2019 Compiling and Running Fortran 90_95 Programs - A Basic Guide
http://slidepdf.com/reader/full/compiling-and-running-fortran-9095-programs-a-basic-guide 3/5
7/28/2019 Compiling and Running Fortran 90_95 Programs - A Basic Guide
http://slidepdf.com/reader/full/compiling-and-running-fortran-9095-programs-a-basic-guide 4/5
6/30/13 Compiling and Running Fortran 90/95 Programs - a basic guide
www.fortran.gantep.edu.tr/compiling-g95-basic-guide.html
Similarly, integer type data can be stored in 1, 2, 4 or 8 bytes, each giving a larger range of values that can be represented by them. The default is
4 bytes (KIND=4).
KIND types available in the g95 compiler (at the time of writing quad precisison is not yet available).
+------------------+----------+------------------+--------------------------+--------------------------| Kind | Memory | Precision | Smallest value, TINY() | Largest value, HUGE() +------------------+----------+------------------+--------------------------+--------------------------| REAL (KIND=4 )* | 4 bytes | 7 s.f. (Single) | 1.1754944E-38 | 3.4028235E+38 | REAL (KIND=8 ) | 8 bytes | 15 s.f. (Double) | 2.2250738585072014E-308 | 1.7976931348623157E+308 | REAL (KIND=16) | 16 bytes | 34 s.f. (Quad) | | +------------------+----------+------------------+--------------------------+--------------------------
+------------------+----------+----------------------------+| Kind | Memory | Range |+------------------+----------+----------------------------+| INTEGER (KIND=1) | 1 byte | -128 to 127 || INTEGER (KIND=2) | 2 bytes | -32768 to 32767 || INTEGER (KIND=4)*| 4 bytes | -2147483648 to 2147483647 | * means default kind.| INTEGER (KIND=8) | 8 bytes | about +- 9.2x10^18 | s.f. means "significant figures".+------------------+----------+----------------------------+
To assign double and quad precision correctly use the _8 and _16 notations:
In single precision: A = 1.2345678E38 or 1.2345678E38_4In double precision: A = 1.234567890123456E308_8In quad precision: A = 1.2345678901234567890123456789012 345E4931_16
To specify the kind type, the KIND attribute is given in the declaration of data.
Examples declarations of real types:
REAL :: A ! default (single precision)REAL(KIND=4) :: B ! single precisionREAL(KIND=8) :: C ! double precisionREAL(KIND=16) :: D ! quad precision
In assignments, constants can be given the required precision as follows:
A=1.2345678 _4 or simply 1.2345678 (Single)A=1.234567890123456 _8 (Double)A=1.2345678901234567890123456789012345 _16 (Quad)
The E symbol can be used for exponentiation:
A = 1.234568E38B = 1.234568E38_4C = 1.23456789012346E308 _8D = 1.234567890123456789012345678901235E4931 _16
Double and quad precision greatly reduces round-off errors in many numerical methods.
Example declarations of integer types:
INTEGER :: E ! default (KIND=4)INTEGER (KIND=1) :: FINTEGER (KIND=2) :: GINTEGER (KIND=4) :: HINTEGER (KIND=8) :: I
8. Running programs under Linux
There are two modes of running programs under Linux, the first is interactively , and the second in background . Running interactively means you
execute the program and your command prompts waits for it to complete:
$ mybigjob
the command prompt will not return until the job is complete:
Alternatively, the job can be run in background ; in this case the command prompt is returned to you and the job continues to run. You may log off
the system, and the job will continue to run. This is a great convenience especially for long-running programs.
A program is placed into background by appending the & symbol at the end of the execution command; for example:
$ mybigjob &
It usual to require screen output to be redirected to a file; this is acheived using output redirection:
$ mybigjob >mybigjob.log &
In this method error messages are not included in the output file; there are two methods to record also error messages in a file:
7/28/2019 Compiling and Running Fortran 90_95 Programs - A Basic Guide
http://slidepdf.com/reader/full/compiling-and-running-fortran-9095-programs-a-basic-guide 5/5
6/30/13 Compiling and Running Fortran 90/95 Programs - a basic guide
www.fortran.gantep.edu.tr/compiling-g95-basic-guide.html
$ mybigjob 1>mybigjob.output 2>mybigjob.error &
$ mybigjob 1>mybigjob.log 2>&1 &
In the first method the two outputs go to separate files, in the second method the two outputs go to the same file.
With an interactive login, the progress of a job can be viewed with the top command. Jobs can be listed with the jobs command and terminated
with the kill command. System resources can be viewed with the df and free commands.
19/12/2005
small update 04/11/2008