Difference between revisions of "HowTo:fortran"
(→Running MPI programs) |
(→Fortran (Programming Language)) |
||
(28 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
= Fortran (Programming Language) = | = Fortran (Programming Language) = | ||
− | FORTRAN, C, and C++ have a long history as the basic/main compiled languages for high performance computing. The key parallel computing packages, MPI and OpenMP, have been implemented in all of them from the beginning. While C and C++ have been extended for all programming purposes, FORTRAN originated from FORmular TRANslation, | + | FORTRAN, C, and C++ have a long history as the basic/main compiled languages for high performance computing. The key parallel computing packages, MPI and OpenMP, have been implemented in all of them from the beginning. While C and C++ have been extended for all programming purposes, FORTRAN, originated from FORmular TRANslation, developed with an emphasis on scientific computing. After the FORTRAN I-IV, 66, and 77 stages, the FORTRAN 90, 95, 2003, 2008, and 2015 versions have adopted many advanced features to become a true modern (object oriented) programming language, especially geared toward scientific computations. The following lists some of the most useful and prominent programming features of FORTRAN. |
{| style="border-spacing: 8px;" | {| style="border-spacing: 8px;" | ||
− | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:# | + | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#f7f7f7; border-radius:7px" | |
== Well Structured == | == Well Structured == | ||
− | FORTRAN is very well structured. All routines should have a clear beginning statement, and a corresponding ending one. For example (since case-in-sensitiveness, usually written in either lower or upper case | + | FORTRAN is very well structured. All routines should have a clear beginning statement, and a corresponding ending one. For example (since case-in-sensitiveness, usually written in either all lower or all upper case) |
<pre> | <pre> | ||
PROGRAM MY_VERY_USEFUL_CODE | PROGRAM MY_VERY_USEFUL_CODE | ||
− | ... | + | ... |
− | CALL PROBLEM_SOLVING (...) | + | CALL PROBLEM_SOLVING (...) |
− | ... | + | ... |
− | STOP | + | STOP |
END PROGRAM MY_VERY_USEFUL_CODE | END PROGRAM MY_VERY_USEFUL_CODE | ||
SUBROUTINE PROBLEM_SOLVING (...) | SUBROUTINE PROBLEM_SOLVING (...) | ||
− | ... | + | ... |
− | RESULT = AVERAGE_SCORE (...) | + | RESULT = AVERAGE_SCORE (...) |
− | RETURN | + | RETURN |
END SUBROUTINE PROBLEM_SOLVING | END SUBROUTINE PROBLEM_SOLVING | ||
FUNCTION AVERAGE_SCORE (...) | FUNCTION AVERAGE_SCORE (...) | ||
− | ... | + | ... |
− | RETURN | + | RETURN |
END FUNCTION AVERAGE_SCORE | END FUNCTION AVERAGE_SCORE | ||
</pre> | </pre> | ||
− | + | Even the DO loop and IF structure are also finished with an END statement. | |
<pre> | <pre> | ||
Line 50: | Line 50: | ||
== Modules == | == Modules == | ||
− | Similar to classes in C++, modules are very important and widely-used in FORTRAN. Theoretically modules are not classes, but usually | + | Similar to classes in C++, modules are very important and widely-used in FORTRAN. Modules, in the form of a separate code structure, may contain various definitions/declarations and can use other predefined modules. Theoretically modules are not classes, but usually used to provide some data structures (objects) for sharing, since in most scientific computations objects are known beforehand and the task is to manipulate them. Modules can also contain specific routines accessing the objects inside and accessible only when the module is used, similar to the encapsulation concept of classes. By using modules, the code can be written very concisely. Here is an example and its usage. |
+ | <pre> | ||
+ | MODULE MY_PARAMETERS | ||
+ | DOUBLE PRECISION, PARAMETER :: THE_EARTH_RADIUS = 6371.0D0 | ||
+ | END MODULE MY_PARAMETERS | ||
+ | |||
+ | SUBROUTINE EARTH_STORY (...) | ||
+ | USE MY_PARAMETERS | ||
+ | DOUBLE PRECISION:: THE_EARTH_DIAMETER | ||
+ | ... | ||
+ | THE_EARTH_DIAMETER = 2 * THE_EARTH_RADIUS | ||
+ | ... | ||
+ | RETURN | ||
+ | END SUBROUTINE EARTH_STORY | ||
+ | </pre> | ||
+ | |||
+ | |} | ||
+ | |||
+ | {| style="border-spacing: 8px;" | ||
+ | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#f7f7f7; border-radius:7px" | | ||
== Overloading == | == Overloading == | ||
− | As a modern language, FORTRAN also supports routine overloading. | + | As a modern language, FORTRAN also supports routine overloading: the ability to pick up the correct one from a group of routines with different unique interfaces by calling a fixed routine name. The routines are usually of the same functionality. |
<pre> | <pre> | ||
Line 88: | Line 107: | ||
Most FORTRAN compilers have built-in data types of very high precision, like quadruple precision | Most FORTRAN compilers have built-in data types of very high precision, like quadruple precision | ||
<pre> | <pre> | ||
− | REAL*16 :: VELOCITY(3,1000) | + | REAL*16 :: VELOCITY(3,1000) |
− | COMPLEX*32 :: | + | COMPLEX*32 :: HAMILTONIAN(1000, 1000) |
</pre> | </pre> | ||
+ | |||
+ | |} | ||
+ | |||
+ | {| style="border-spacing: 8px;" | ||
+ | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#f7f7f7; border-radius:7px" | | ||
+ | |||
+ | |||
+ | == Collective Operations == | ||
+ | FORTRAN supports collective operations on a whole array or a section of it. | ||
+ | <pre> | ||
+ | REAL*16 :: V1(3,100), V2(3,100), V3(3,100) | ||
+ | ... | ||
+ | V1 = 0.0Q0 | ||
+ | V1(2:3, 20:50) = 0.9Q0 | ||
+ | V2 = 0.8Q0 * V3 | ||
+ | </pre> | ||
+ | which assign all the "mentioned" elements with the corresponding values, without a need of loop(s). A pure array name means all elements. | ||
+ | |} | ||
+ | |||
+ | {| style="border-spacing: 8px;" | ||
+ | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#e1eaf1; border-radius:7px" | | ||
== Dynamic Memory Allocation == | == Dynamic Memory Allocation == | ||
− | Early versions of FORTRAN had a big drawback: they did not allow for dynamic memory allocation, forcing re-compilation array sizes | + | Early versions of FORTRAN had a big drawback: they did not allow for dynamic memory allocation, forcing re-compilation for array sizes changed. Newer versions of FORTRAN (since F90) support such operations even for many-dimensional arrays. |
<pre> | <pre> | ||
− | REAL*16, ALLOCATABLE :: | + | REAL*16, ALLOCATABLE :: COMPLICATED_DATA(:, :, :, :, :, :) |
− | + | INTEGER :: I1=3, I2=90, I3=80, I4, I5, I6=28 | |
+ | I4 = 24; I5 = 500 | ||
+ | ALLOCATE(COMPLICATED_DATA(I1, I2, I3, I4, I5, I6)) | ||
</pre> | </pre> | ||
Line 105: | Line 147: | ||
{| style="border-spacing: 8px;" | {| style="border-spacing: 8px;" | ||
− | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:# | + | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#f7f7f7; border-radius:7px" | |
== User Defined Data Types == | == User Defined Data Types == | ||
Line 126: | Line 168: | ||
{| style="border-spacing: 8px;" | {| style="border-spacing: 8px;" | ||
− | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:# | + | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#e1eaf1; border-radius:7px" | |
− | == | + | == Some Other Features == |
− | + | * FORTRAN also supports recursive routines calls and optional arguments for routines. | |
+ | * OpenMP and OpenAcc can easier understand and parallelize FORTRAN code. | ||
+ | * Compilers check FORTRAN code strictly based on grammars and point out any problems they find. | ||
+ | |} | ||
− | |||
− | |||
− | |||
− | + | {| style="border-spacing: 8px;" | |
+ | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#f7f7f7; border-radius:7px" | | ||
− | + | == Links and Further Reading == | |
− | + | * [http://www.j3-fortran.org/ Fortran Standard Technical Committee] | |
+ | * [https://en.wikipedia.org/wiki/Fortran Fortran Wikipedia Entry] with information about History, features, and variants of Fortran. | ||
+ | * [https://en.wikipedia.org/wiki/List_of_compilers#Fortran_compilers List of Fortran Compilers.] We are operating the [https://gcc.gnu.org/ GNU] and [https://software.intel.com/en-us/intel-compilers Intel] compilers on our systems, see [[HowTo:Compilers|our compiler help file]]. | ||
+ | * [https://www.amazon.ca/Fortran-90-Explained-Michael-Metcalf/dp/0198505582 FORTRAN 90/95 explained, by Michael Metcalf and John Reid.] A good introduction focussing on the 90 version that introduced many of the "modern" features. | ||
+ | |} | ||
− | |||
− | == | + | {| style="border-spacing: 8px;" |
− | + | | valign="top" width="50%" style="padding:1em; border:1px solid #aaaaaa; background-color:#e1eaf1; border-radius:7px" | | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
== Help == | == Help == | ||
[mailto:cac.help@queensu.ca Send email to cac.help@queensu.ca]. We have scientific programmers on staff who will probably be able to help you out. Of course, we can't do the coding for you but we do our best to get your code ready for parallel machines and clusters. | [mailto:cac.help@queensu.ca Send email to cac.help@queensu.ca]. We have scientific programmers on staff who will probably be able to help you out. Of course, we can't do the coding for you but we do our best to get your code ready for parallel machines and clusters. | ||
|} | |} |
Latest revision as of 17:00, 23 November 2016
Contents
Fortran (Programming Language)
FORTRAN, C, and C++ have a long history as the basic/main compiled languages for high performance computing. The key parallel computing packages, MPI and OpenMP, have been implemented in all of them from the beginning. While C and C++ have been extended for all programming purposes, FORTRAN, originated from FORmular TRANslation, developed with an emphasis on scientific computing. After the FORTRAN I-IV, 66, and 77 stages, the FORTRAN 90, 95, 2003, 2008, and 2015 versions have adopted many advanced features to become a true modern (object oriented) programming language, especially geared toward scientific computations. The following lists some of the most useful and prominent programming features of FORTRAN.
Well StructuredFORTRAN is very well structured. All routines should have a clear beginning statement, and a corresponding ending one. For example (since case-in-sensitiveness, usually written in either all lower or all upper case) PROGRAM MY_VERY_USEFUL_CODE ... CALL PROBLEM_SOLVING (...) ... STOP END PROGRAM MY_VERY_USEFUL_CODE SUBROUTINE PROBLEM_SOLVING (...) ... RESULT = AVERAGE_SCORE (...) RETURN END SUBROUTINE PROBLEM_SOLVING FUNCTION AVERAGE_SCORE (...) ... RETURN END FUNCTION AVERAGE_SCORE Even the DO loop and IF structure are also finished with an END statement. DO I = ISTART, IEND ... END DO IF (CONDITION) ... ELSE ... END IF |
ModulesSimilar to classes in C++, modules are very important and widely-used in FORTRAN. Modules, in the form of a separate code structure, may contain various definitions/declarations and can use other predefined modules. Theoretically modules are not classes, but usually used to provide some data structures (objects) for sharing, since in most scientific computations objects are known beforehand and the task is to manipulate them. Modules can also contain specific routines accessing the objects inside and accessible only when the module is used, similar to the encapsulation concept of classes. By using modules, the code can be written very concisely. Here is an example and its usage. MODULE MY_PARAMETERS DOUBLE PRECISION, PARAMETER :: THE_EARTH_RADIUS = 6371.0D0 END MODULE MY_PARAMETERS SUBROUTINE EARTH_STORY (...) USE MY_PARAMETERS DOUBLE PRECISION:: THE_EARTH_DIAMETER ... THE_EARTH_DIAMETER = 2 * THE_EARTH_RADIUS ... RETURN END SUBROUTINE EARTH_STORY |
OverloadingAs a modern language, FORTRAN also supports routine overloading: the ability to pick up the correct one from a group of routines with different unique interfaces by calling a fixed routine name. The routines are usually of the same functionality. MODULE MY_KINETICS INTERFACE GENERIC_KINETIC SUBROUTINE KINETIC_ROUTINE_A(...) ... END SUBROUTINE KINETIC_ROUTINE_A SUBROUTINE KINETIC_ROUTINE_B(...) ... END SUBROUTINE KINETIC_ROUTINE_B SUBROUTINE KINETIC_ROUTINE_C(...) ... END SUBROUTINE KINETIC_ROUTINE_C ... END INTERFACE GENERIC_KINETIC END MODULE MY_KINETICS After this module is cited USE MY_KINETICS with each of the specific routines available, the call CALL GENERIC_KINETIC(...) will invoke the specific routine with the matching unique interface. In C++, overloading is a type of class polymorphism. |
High PrecisionMost FORTRAN compilers have built-in data types of very high precision, like quadruple precision REAL*16 :: VELOCITY(3,1000) COMPLEX*32 :: HAMILTONIAN(1000, 1000) |
Collective OperationsFORTRAN supports collective operations on a whole array or a section of it. REAL*16 :: V1(3,100), V2(3,100), V3(3,100) ... V1 = 0.0Q0 V1(2:3, 20:50) = 0.9Q0 V2 = 0.8Q0 * V3 which assign all the "mentioned" elements with the corresponding values, without a need of loop(s). A pure array name means all elements. |
Dynamic Memory AllocationEarly versions of FORTRAN had a big drawback: they did not allow for dynamic memory allocation, forcing re-compilation for array sizes changed. Newer versions of FORTRAN (since F90) support such operations even for many-dimensional arrays. REAL*16, ALLOCATABLE :: COMPLICATED_DATA(:, :, :, :, :, :) INTEGER :: I1=3, I2=90, I3=80, I4, I5, I6=28 I4 = 24; I5 = 500 ALLOCATE(COMPLICATED_DATA(I1, I2, I3, I4, I5, I6)) in contrast to C/C++ where all arrays are allocated as one-dimensional. |
User Defined Data TypesFORTRAN also supports user defined data types: TYPE PERSON CHARACTER(LEN=10) :: NAME REAL :: AGE INTEGER :: ID END TYPE PERSON TYPE(PERSON) :: YOU, ME REAL :: DIFF YOU%ID = 12345 DIFF = YOU%AGE - ME%AGE |
Some Other Features
|
Links and Further Reading
|
HelpSend email to cac.help@queensu.ca. We have scientific programmers on staff who will probably be able to help you out. Of course, we can't do the coding for you but we do our best to get your code ready for parallel machines and clusters. |