compiling and running fortran 90_95 programs - a basic guide

5
6/30/13 Compiling and Running Fortran 90/95 Programs - a basi c guide www.fortran.gantep.edu.tr/compiling-g95-basic-guide.html 1/5 Compiling and Running Fortran Programs - a basic guide http://www.fortran.gan tep.edu.tr/compiling-g95 -basic-guide.html CONTENTS 1. Introduction 2. File name convention 3. Compiling a program 4. Compiler Options 5. Compiling subprogram source files 6. Creating and linking object and library files 7. KIND types for REALs and INTEGERs 8. Running programs under Lin ux 1. Introduction This guide is for Fortran programmers using central Linux servers such as gul3, gul4 and gul5, where theg95 compiler is installed. This guide covers at a basic level topics relating to the compilation of program sources; it is not a guide to the Fortran programming language (I have however included a section on KIND types is given at the end of this guide. 2. File name Convention File names ending in .f90 and .f95 are assumed to be free source form - suitable for Fortran 90/95 compilation. File names ending in .f and .for are assumed to be assumed fixed form source - compatible with old Fortran 77 compilation. 3. Compiling a program The role of g95 Fortran compiler is to compile your Fortran source code into an executable program. Consider that you have a Fortran 95 source file called myprog.f90. The simplest command for creation of an executable is then: g95 myprog.f90 This creates the executable program called a.out Y ou can give a mor e meaningful name to your executable by using the -o option:  g95 myprog.f90 -o myprog This creates the executable program called myprog The path of include files can be given with the -I option, for example: g95 myprog.f90 -o myprog -I/home/fred/fortran/inc or g95 myprog.f90 -o myprog -I$MYINC  wher e the environ ment v ariable MYINC is set with: MYINC=/home/fred/fortran/inc/ 4. Compiler Options As for any Fortran compiler, g95 provides many options that control its behaviour. The basic and recommended options are given here, a more detailed description can be found at http://www.g95.org/docs.shtml . Speed optimisation Th e -Olevel option performs some optimisation of the executable and can lead to significant increases in executation speed. Example: g95 myprog.f90 -o myprog -O2 Warning options Th e -Wlevel option enables most warning messages that can be switched on by the programmer. Such messages are generated at compile- time warning the programmer of, for example, unused or unset variables. Example: g95 myprog.f90 -o myprog -O2 -Wall It is advisable to use -Olevel option as some warning options rely on flow analysis provided by this option. Runtime options Various run-time options can be selected, these options cause extra code to be added to the executable and so can cause significant decreases in execution speed. However these options can be very useful during program development and debugging. Example g95 myprog.f90 -o myprog -O2 -fbounds-check This causes the executable to check for "array index out of bounds conditions". Recommended options For teaching students Fortran programming, and during program development in the research environment, I advise something like the following options as they help the programmer to program with safety in mind.

Upload: vivek-singh

Post on 03-Apr-2018

212 views

Category:

Documents


0 download

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.

[email protected]

19/12/2005

small update 04/11/2008