Difference between revisions of "FortranIntro"

 
Line 1: Line 1:
{{Needs Update}}
 
 
=Introduction to Fortran=  
 
=Introduction to Fortran=  
  
Line 24: Line 23:
 
On NCI Fortran programs are compiled using the ifort compiler.
 
On NCI Fortran programs are compiled using the ifort compiler.
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
$ module load intel-fc
+
$ module load intel-compiler
 
$ ifort -warn all -warn errors hello.f90
 
$ ifort -warn all -warn errors hello.f90
 
$ ./a.out
 
$ ./a.out
Line 30: Line 29:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The <span style="font-family:monospace">module}} command makes ifort available for use. The default program name when you compile using ifort is {{a.out}}, you can change what the filename is by adding {{-o OUTPUT}} to the ifort command. The warning flags {{-warn all -warn errors</span> makes sure that ifort checks for any programming errors, make sure to fix them!
+
The 'module' command makes ifort available for use. The default program name when you compile using ifort is 'a.out', you can change what the filename is by adding '-o OUTPUT' to the ifort command. The warning flags '-warn all -warn errors' makes sure that ifort checks for any programming errors, make sure to fix them!
  
 
==Variables==  
 
==Variables==  
Line 39: Line 38:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<span style="font-family:monospace">type}} is the variable type, usually one of {{integer}}, {{real}} or {{logical}}. To change how many bytes are used in a variable change the kind, for instance {{real(kind=8)</span> is a 64 bit (8 byte) ieee floating point number. The kind is optional, the default value is platform specific but should be at least 4 bytes. Usually you won't worry about kinds.
+
'type' is the variable type, usually one of 'integer', 'real' or 'logical'. To change how many bytes are used in a variable change the kind, for instance 'real(kind=8)' is a 64 bit (8 byte) IEEE floating point number. The kind is optional, the default value for ifort is 4 bytes. Usually you won't worry about kinds.
  
Various options are possible, for instance <span style="font-family:monospace">dimension(2,3)}} makes the variable a 2x3 array, {{parameter</span> ensures the variable is unchanged by the program (it's value must be set in the declaration)
+
Various options are possible, for instance 'dimension(2,3)' makes the variable a 2x3 array, 'parameter' ensures the variable is unchanged by the program (it's value must be set in the declaration)
  
Fortran programs and functions have two main sections, variable declaration and the program proper. All variables used should be declared, the command <span style="font-family:monospace">implicit none</span> at the start of a function ensures this. Variable declaration cannot be mixed with program statements.
+
<syntaxhighlight lang=fortran>
 +
integer, allocatable :: foo(:)
 +
real(kind=4), dimension(2,3) :: bar
 +
integer(kind=8), parameter :: buz = 12
 +
</syntaxhighlight>
 +
 
 +
Fortran programs and functions have two main sections, variable declaration and the program proper. All variables used should be declared, the command 'implicit none' at the start of a function ensures this. Variable declaration cannot be mixed with program statements.
  
 
<syntaxhighlight lang=fortran>
 
<syntaxhighlight lang=fortran>

Latest revision as of 00:29, 23 January 2020

Introduction to Fortran

In the world of high-performance computing there are two main choices of programming language, C and Fortran. Highly tuned compilers are available for both languages, along with function libraries which help in creating parallel programs.

Each language has different strengths, Fortran's array syntax allows for computations over large datasets to be done efficiently and trivially parallelised, while C gives you control over memory to make use of more complicated data structures. Both languages are continuing to be improved, the last release of the Fortran standard was in 2008, mainly adding co-arrays for shared memory programming.

Many models in the Earth Sciences are programmed in Fortran, including the Met Office Unified Model and MOM amongst others.

Compiling

A basic Fortran program is

! hello.f90
program hello
implicit none

write(*,*) 'Hello'
end program

This program simply writes 'Hello' to the console.

On NCI Fortran programs are compiled using the ifort compiler.

$ module load intel-compiler
$ ifort -warn all -warn errors hello.f90
$ ./a.out
Hello

The 'module' command makes ifort available for use. The default program name when you compile using ifort is 'a.out', you can change what the filename is by adding '-o OUTPUT' to the ifort command. The warning flags '-warn all -warn errors' makes sure that ifort checks for any programming errors, make sure to fix them!

Variables

Variables in Fortran are declared like

type(kind=4), options :: name

'type' is the variable type, usually one of 'integer', 'real' or 'logical'. To change how many bytes are used in a variable change the kind, for instance 'real(kind=8)' is a 64 bit (8 byte) IEEE floating point number. The kind is optional, the default value for ifort is 4 bytes. Usually you won't worry about kinds.

Various options are possible, for instance 'dimension(2,3)' makes the variable a 2x3 array, 'parameter' ensures the variable is unchanged by the program (it's value must be set in the declaration)

integer, allocatable :: foo(:)
real(kind=4), dimension(2,3) :: bar
integer(kind=8), parameter :: buz = 12

Fortran programs and functions have two main sections, variable declaration and the program proper. All variables used should be declared, the command 'implicit none' at the start of a function ensures this. Variable declaration cannot be mixed with program statements.

program variables
implicit none
integer(kind=8) :: run_count
real, dimension(4) :: x, y
real, dimension(4,4) :: temperature
real, parameter :: pi = 3.1415926

...
end program

Types

type field
integer         :: nx,ny
real, allocable :: x(:), y(:)
real, allocable :: data(:,:)
end type

Functions & Subroutines

Modules