Difference between revisions of "Modern Fortran"
m |
|||
Line 652: | Line 652: | ||
==Modules== | ==Modules== | ||
{| | {| | ||
+ | |- valign="top" | ||
| | | | ||
* Easiest to show by example | * Easiest to show by example | ||
Line 658: | Line 659: | ||
* “Use” goes before “implicit none” | * “Use” goes before “implicit none” | ||
| | | | ||
− | samples/procedures/simplemod/simplemod.f90 | + | <source lang="fortran"> |
+ | module gravity | ||
+ | implicit none | ||
+ | real, parameter :: G = 6.67e-11 ! MKS units | ||
+ | |||
+ | contains | ||
+ | real function gravforce(x1,x2,m1,m2) | ||
+ | implicit none | ||
+ | real, dimension(3), intent(in) :: x1,x2 | ||
+ | real, intent(in) :: m1, m2 | ||
+ | real :: dist | ||
+ | |||
+ | dist = sqrt(sum((x1-x2)**2)) | ||
+ | gravforce = G * m1 * m2 / dist**2 | ||
+ | end function gravforce | ||
+ | end module gravity | ||
+ | |||
+ | program simplemod | ||
+ | use gravity | ||
+ | implicit none | ||
+ | |||
+ | print *, 'Gravitational constant = ', G | ||
+ | print *, 'Force between 2 1kg masses at [1,0,0] & | ||
+ | &and [0,0,1] is' | ||
+ | |||
+ | print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.) | ||
+ | |||
+ | end program simplemod | ||
+ | </source> | ||
+ | (from samples/procedures/simplemod/simplemod.f90) | ||
| | | | ||
|} | |} | ||
Line 664: | Line 694: | ||
==Compiling & Running== | ==Compiling & Running== | ||
{| | {| | ||
+ | |- valign="top" | ||
| | | | ||
* When compiling the code a gravity.mod file is created | * When compiling the code a gravity.mod file is created | ||
Line 669: | Line 700: | ||
* Not compatible between different compilers, versions. | * Not compatible between different compilers, versions. | ||
| | | | ||
+ | <source lang="bash"> | ||
+ | $ ls | ||
+ | simplemod.f90 | ||
+ | |||
+ | $ gfortran -o simplemod simplemod.f90 -Wall | ||
+ | |||
+ | $ ls | ||
+ | gravity.mod simplemod simplemod.f90 | ||
+ | |||
+ | $ ./simplemod | ||
+ | Gravitational constant = 6.6700000E-11 | ||
+ | Force between 2 1kg masses at [1,0,0] and [0,0,1] is | ||
+ | 3.3350003E-11 | ||
+ | </source> | ||
|} | |} | ||
==Modules== | ==Modules== | ||
{| | {| | ||
+ | |- valign="top" | ||
| | | | ||
* function gravforce can “see” the modulewide parameter defined above. | * function gravforce can “see” the modulewide parameter defined above. | ||
* So can main program, through use statement. | * So can main program, through use statement. | ||
| | | | ||
− | samples/procedures/simplemod/simplemod.f90 | + | <source lang="fortran"> |
+ | <source lang="fortran"> | ||
+ | module gravity | ||
+ | implicit none | ||
+ | real, parameter :: G = 6.67e-11 ! MKS units | ||
+ | |||
+ | contains | ||
+ | real function gravforce(x1,x2,m1,m2) | ||
+ | implicit none | ||
+ | real, dimension(3), intent(in) :: x1,x2 | ||
+ | real, intent(in) :: m1, m2 | ||
+ | real :: dist | ||
+ | |||
+ | dist = sqrt(sum((x1-x2)**2)) | ||
+ | gravforce = G * m1 * m2 / dist**2 | ||
+ | end function gravforce | ||
+ | end module gravity | ||
+ | |||
+ | program simplemod | ||
+ | use gravity | ||
+ | implicit none | ||
+ | |||
+ | print *, 'Gravitational constant = ', G | ||
+ | print *, 'Force between 2 1kg masses at [1,0,0] & | ||
+ | &and [0,0,1] is' | ||
+ | |||
+ | print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.) | ||
+ | |||
+ | end program simplemod | ||
+ | </source> | ||
+ | (from samples/procedures/simplemod/simplemod.f90) | ||
|} | |} | ||
==use module, only :== | ==use module, only :== | ||
{| | {| | ||
+ | |- valign="top" | ||
* Best practice is to only pull in from the module what you need | * Best practice is to only pull in from the module what you need | ||
* Otherwise, everything. | * Otherwise, everything. | ||
Line 687: | Line 764: | ||
* (Note syntax for continuation of a string...) | * (Note syntax for continuation of a string...) | ||
| | | | ||
+ | <source lang="fortran"> | ||
+ | module gravity | ||
+ | implicit none | ||
+ | real, parameter :: G = 6.67e-11 ! MKS units | ||
+ | |||
+ | contains | ||
+ | real function gravforce(x1,x2,m1,m2) | ||
+ | implicit none | ||
+ | real, dimension(3), intent(in) :: x1,x2 | ||
+ | real, intent(in) :: m1, m2 | ||
+ | real :: dist | ||
+ | |||
+ | dist = sqrt(sum((x1-x2)**2)) | ||
+ | gravforce = G * m1 * m2 / dist**2 | ||
+ | end function gravforce | ||
+ | end module gravity | ||
+ | |||
+ | program simplemod2 | ||
+ | use gravity, only : G, gravforce | ||
+ | implicit none | ||
+ | |||
+ | print *, 'Gravitational constant = ', G | ||
+ | print *, 'Force between 2 1kg masses at [1,0,0] & | ||
+ | &and [0,0,1] is' | ||
+ | |||
+ | print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.) | ||
+ | |||
+ | end program simplemod2 | ||
+ | </source> | ||
samples/procedures/simplemod/simplemod2.f90 | samples/procedures/simplemod/simplemod2.f90 | ||
|} | |} | ||
Line 692: | Line 798: | ||
==Modules usually get their own files== | ==Modules usually get their own files== | ||
{| | {| | ||
+ | |- valign="top" | ||
| | | | ||
* For encapsulation | * For encapsulation | ||
Line 699: | Line 806: | ||
* (Main program hasn’t changed much). | * (Main program hasn’t changed much). | ||
| | | | ||
− | samples/procedures/multifilemod/gravity.f90 | + | <source lang="fortran"> |
+ | module gravity | ||
+ | implicit none | ||
+ | private | ||
+ | |||
+ | character (len=8), parameter, public :: massunit="kilogram" | ||
+ | character (len=8), parameter, public :: forceunit="Newton" | ||
+ | public :: gravforce | ||
+ | |||
+ | real, parameter :: G = 6.67e-11 ! MKS units | ||
+ | |||
+ | contains | ||
+ | real function distance(x1,x2) | ||
+ | implicit none | ||
+ | real, dimension(3), intent(in) :: x1, x2 | ||
+ | |||
+ | distance = sqrt(sum((x1-x2)**2)) | ||
+ | end function distance | ||
+ | |||
+ | real function gravforce(x1,x2,m1,m2) | ||
+ | implicit none | ||
+ | real, dimension(3), intent(in) :: x1,x2 | ||
+ | real, intent(in) :: m1, m2 | ||
+ | real :: dist | ||
+ | |||
+ | dist = distance(x1,x2) | ||
+ | gravforce = G * m1 * m2 / dist**2 | ||
+ | end function gravforce | ||
+ | end module gravity | ||
+ | </source> | ||
+ | (from samples/procedures/multifilemod/gravity.f90 ) | ||
|} | |} | ||
==Modules usually get their own files== | ==Modules usually get their own files== | ||
{| | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Compiling gravity.f90 now gives both an .o file (containing the code) and the .mod file as before. | * Compiling gravity.f90 now gives both an .o file (containing the code) and the .mod file as before. | ||
* Compiling the main program (multifilemod.f90) requires the .mod file. | * Compiling the main program (multifilemod.f90) requires the .mod file. | ||
| | | | ||
− | samples/procedures/multifilemod/Makefile | + | <source lang="make"> |
+ | FC=gfortran | ||
+ | FFLAGS=-O3 -Wall | ||
+ | |||
+ | multifilemod: multifilemod.o gravity.o | ||
+ | $(FC) -o $@ multifilemod.o gravity.o | ||
+ | |||
+ | %.mod: %.f90 | ||
+ | $(FC) $(FFLAGS) -c $< | ||
+ | |||
+ | multifilemod.o: multifilemod.f90 gravity.mod | ||
+ | $(FC) $(FFLAGS) -c $< | ||
+ | |||
+ | clean: | ||
+ | rm -f *.o *~ *.mod multifilemod | ||
+ | </source> | ||
+ | (from samples/procedures/multifilemod/Makefile) | ||
|} | |} | ||
==.mod needed for compilation== | ==.mod needed for compilation== | ||
{| | {| | ||
+ | |- valign="top" | ||
| | | | ||
* ...because needs the type information of the constants, | * ...because needs the type information of the constants, | ||
Line 717: | Line 873: | ||
* Can’t compile without these | * Can’t compile without these | ||
| | | | ||
− | samples/procedures/multifilemod/multifilemod.f90 | + | <source lang="fortran"> |
+ | program simplemod2 | ||
+ | use gravity, only : gravforce, massunit, forceunit | ||
+ | implicit none | ||
+ | |||
+ | print *, 'Force between 2 1 ', massunit ,' masses ', & | ||
+ | ' at [1,0,0] and [0,0,1] is' | ||
+ | |||
+ | print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.), forceunit | ||
+ | |||
+ | end program simplemod2 | ||
+ | </source> | ||
+ | (from samples/procedures/multifilemod/multifilemod.f90) | ||
|} | |} | ||
==.o needed for linking== | ==.o needed for linking== | ||
{| | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Linking, however, doesn’t require the .mod file | * Linking, however, doesn’t require the .mod file | ||
* Only requires the .o file from the module code. | * Only requires the .o file from the module code. | ||
* .mod file analogous (but better than) .h files for C code. | * .mod file analogous (but better than) .h files for C code. | ||
| | | | ||
− | samples/procedures/multifilemod/Makefile | + | <source lang="make"> |
+ | FC=gfortran | ||
+ | FFLAGS=-O3 -Wall | ||
+ | |||
+ | multifilemod: multifilemod.o gravity.o | ||
+ | $(FC) -o $@ multifilemod.o gravity.o | ||
+ | |||
+ | %.mod: %.f90 | ||
+ | $(FC) $(FFLAGS) -c $< | ||
+ | |||
+ | multifilemod.o: multifilemod.f90 gravity.mod | ||
+ | $(FC) $(FFLAGS) -c $< | ||
+ | |||
+ | clean: | ||
+ | rm -f *.o *~ *.mod multifilemod | ||
+ | </source> | ||
+ | (from samples/procedures/multifilemod/Makefile) | ||
|} | |} | ||
==Compiling and running== | ==Compiling and running== | ||
{| | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* So compile files with modules first, so those that need them have the .mod files | * So compile files with modules first, so those that need them have the .mod files | ||
* Link the .o files | * Link the .o files | ||
+ | | | ||
+ | <source lang="bash"> | ||
+ | $ make | ||
+ | gfortran -O3 -Wall -c gravity.f90 | ||
+ | gfortran -O3 -Wall -c multifilemod.f90 | ||
+ | gfortran -o multifilemod multifilemod.o gravity.o | ||
+ | reposado-$ ./multifilemod | ||
+ | Force between 2 1 kilogram masses at [1,0,0] and [0,0,1] is | ||
+ | 3.33500033E-11 Newton | ||
+ | </source> | ||
|} | |} | ||
==Private and public== | ==Private and public== | ||
{| | {| | ||
− | |- | + | |- valign="top" |
| | | | ||
* Not all of a module’s content need be public | * Not all of a module’s content need be public | ||
Line 744: | Line 942: | ||
* Allows hiding implementation specific routines | * Allows hiding implementation specific routines | ||
| | | | ||
+ | <source lang="fortran"> | ||
+ | module gravity | ||
+ | implicit none | ||
+ | private | ||
+ | |||
+ | character (len=8), parameter, public :: massunit="kilogram" | ||
+ | character (len=8), parameter, public :: forceunit="Newton" | ||
+ | public :: gravforce | ||
+ | |||
+ | real, parameter :: G = 6.67e-11 ! MKS units | ||
+ | |||
+ | contains | ||
+ | real function distance(x1,x2) | ||
+ | implicit none | ||
+ | real, dimension(3), intent(in) :: x1, x2 | ||
+ | |||
+ | distance = sqrt(sum((x1-x2)**2)) | ||
+ | end function distance | ||
+ | |||
+ | real function gravforce(x1,x2,m1,m2) | ||
+ | implicit none | ||
+ | real, dimension(3), intent(in) :: x1,x2 | ||
+ | real, intent(in) :: m1, m2 | ||
+ | real :: dist | ||
+ | |||
+ | dist = distance(x1,x2) | ||
+ | gravforce = G * m1 * m2 / dist**2 | ||
+ | end function gravforce | ||
+ | end module gravity | ||
+ | </source> | ||
( from samples/procedures/multifilemod/gravity.f90 ) | ( from samples/procedures/multifilemod/gravity.f90 ) | ||
|} | |} | ||
Line 749: | Line 977: | ||
==Procedures== | ==Procedures== | ||
{| | {| | ||
− | |- | + | |- valign="top" |
| | | | ||
* We’ve already seen procedures defined in new style; let’s look more closely. | * We’ve already seen procedures defined in new style; let’s look more closely. | ||
Line 759: | Line 987: | ||
* Also documentation of a sort. | * Also documentation of a sort. | ||
| | | | ||
+ | <source lang="fortran"> | ||
+ | module procedures | ||
+ | |||
+ | contains | ||
+ | |||
+ | function square(x) result(xsquared) | ||
+ | implicit none | ||
+ | real :: xsquared | ||
+ | real, intent(IN) :: x | ||
+ | |||
+ | xsquared = x*x | ||
+ | end function square | ||
+ | |||
+ | function cube(x) | ||
+ | implicit none | ||
+ | real :: cube | ||
+ | real, intent(IN) :: x | ||
+ | |||
+ | cube = x*x*x | ||
+ | end function cube | ||
+ | |||
+ | subroutine squareAndCube(x, squarex, cubex) | ||
+ | implicit none | ||
+ | real, intent(in) :: x | ||
+ | real, intent(out) :: squarex | ||
+ | real, intent(out) :: cubex | ||
+ | |||
+ | squarex = square(x) | ||
+ | cubex = cube(x) | ||
+ | end subroutine squareAndCube | ||
+ | |||
+ | end module procedures | ||
+ | </source> | ||
( from samples/procedures/funcsub/procedures.f90 ) | ( from samples/procedures/funcsub/procedures.f90 ) | ||
|} | |} | ||
Line 764: | Line 1,025: | ||
==Functions== | ==Functions== | ||
{| | {| | ||
− | |- | + | |- valign="top" |
| | | | ||
* Can be typed a couple of ways. | * Can be typed a couple of ways. | ||
Line 772: | Line 1,033: | ||
* Function return values don’t take intent - always out | * Function return values don’t take intent - always out | ||
| | | | ||
+ | <source lang="fortran"> | ||
+ | function square(x) result(xsquared) | ||
+ | implicit none | ||
+ | real :: xsquared | ||
+ | real, intent(IN) :: x | ||
+ | |||
+ | xsquared = x*x | ||
+ | end function square | ||
+ | |||
+ | function cube(x) | ||
+ | implicit none | ||
+ | real :: cube | ||
+ | real, intent(IN) :: x | ||
+ | |||
+ | cube = x*x*x | ||
+ | end function cube | ||
+ | |||
+ | subroutine squareAndCube(x, squarex, cubex) | ||
+ | implicit none | ||
+ | real, intent(in) :: x | ||
+ | real, intent(out) :: squarex | ||
+ | real, intent(out) :: cubex | ||
+ | |||
+ | squarex = square(x) | ||
+ | cubex = cube(x) | ||
+ | end subroutine squareAndCube | ||
+ | </source> | ||
( from samples/procedures/funcsub/procedures.f90 ) | ( from samples/procedures/funcsub/procedures.f90 ) | ||
|} | |} | ||
Line 777: | Line 1,065: | ||
==Procedure interfaces== | ==Procedure interfaces== | ||
{| | {| | ||
− | |- | + | |- valign="top" |
| | | | ||
* The interface to a procedure consists of | * The interface to a procedure consists of | ||
Line 786: | Line 1,074: | ||
* Like a C prototype, but more detailed info | * Like a C prototype, but more detailed info | ||
* .mod files contain explicit interfaces to all public module procedures. | * .mod files contain explicit interfaces to all public module procedures. | ||
+ | | | ||
+ | <source lang="fortran"> | ||
+ | function square(x) result(xsquared) | ||
+ | implicit none | ||
+ | real :: xsquared | ||
+ | real, intent(IN) :: x | ||
+ | |||
+ | xsquared = x*x | ||
+ | end function square | ||
+ | |||
+ | function cube(x) | ||
+ | implicit none | ||
+ | real :: cube | ||
+ | real, intent(IN) :: x | ||
+ | |||
+ | cube = x*x*x | ||
+ | end function cube | ||
+ | |||
+ | subroutine squareAndCube(x, squarex, cubex) | ||
+ | implicit none | ||
+ | real, intent(in) :: x | ||
+ | real, intent(out) :: squarex | ||
+ | real, intent(out) :: cubex | ||
+ | |||
+ | squarex = square(x) | ||
+ | cubex = cube(x) | ||
+ | end subroutine squareAndCube | ||
+ | </source> | ||
+ | ( from samples/procedures/funcsub/procedures.f90 ) | ||
|} | |} | ||
Line 886: | Line 1,203: | ||
|} | |} | ||
− | + | =Fortran arrays= | |
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Fortran made for dealing with scientific data | * Fortran made for dealing with scientific data | ||
* Arrays built into language | * Arrays built into language | ||
− | * The type information associated with an | + | * The type information associated with an array includes rank (# of dimension), size, element type, stride.. |
− | array includes rank (# of dimension), size, | + | * Enables powerful optimizations, programmer-friendly features. |
− | element type, stride.. | + | * Can be manipulated like simple scalar variables |
− | * Enables powerful optimizations, | + | * Elementwise addition, multiplication.. samples/arrays/basic.f90 |
− | programmer-friendly features. | + | | |
− | + | |} | |
− | |||
− | * Can be manipulated | ||
− | like simple scalar | ||
− | variables | ||
− | * Elementwise | ||
− | addition, | ||
− | multiplication.. | ||
− | samples/arrays/basic.f90 | ||
==Array constructors== | ==Array constructors== | ||
− | * Can have array | + | {| |
− | constants like | + | |- valign="top" |
− | numerical constants | + | | |
− | * use [] or (/ /), then | + | * Can have array constants like numerical constants |
− | comma-separated | + | * use [] or (/ /), then comma-separated list of values. |
− | list of values. | + | * Implied do loops can be used in constructors |
− | * Implied do loops | + | * (Variables have to be defined) |
− | can be used in | + | | |
− | constructors | ||
− | * (Variables have to | ||
− | be defined) | ||
− | |||
[1,2,3,4,5] or (/1,2,3,4,5/) | [1,2,3,4,5] or (/1,2,3,4,5/) | ||
[ (i,i=1,5)] | [ (i,i=1,5)] | ||
[ ((i*j,j=1,3),i=1,5)] | [ ((i*j,j=1,3),i=1,5)] | ||
+ | |} | ||
==Elementwise operations== | ==Elementwise operations== | ||
− | * Elementwise operations | + | {| |
− | can be */+-, or | + | |- valign="top" |
− | application of an | + | | |
− | elemental function. | + | * Elementwise operations can be */+-, or application of an elemental function. |
− | * Math intrinsics are all | + | * Math intrinsics are all elemental - applied to array, applies to every element. |
− | elemental - applied to | + | * Order of execution undefined - allows vectorization, parallelization. |
− | array, applies to every | + | | |
− | element. | ||
− | * Order of execution | ||
− | undefined - allows | ||
− | vectorization, | ||
− | parallelization. | ||
− | |||
samples/arrays/elementwise.f90 | samples/arrays/elementwise.f90 | ||
+ | |} | ||
==Elemental Functions== | ==Elemental Functions== | ||
− | * User can create their | + | {| |
− | own elemental functions | + | |- valign="top" |
− | * Label any scalar function | + | | |
− | with “elemental” should (until recently, | + | * User can create their own elemental functions |
− | must) be pure, so can be | + | * Label any scalar function with “elemental” should (until recently, must) be pure, so can be applied everywhere at same time. |
− | applied everywhere at | ||
− | same time. | ||
* Faster than in loop. | * Faster than in loop. | ||
− | * Can also take multiple | + | * Can also take multiple arguments: eg z = addsquare(x,y) |
− | arguments: eg | + | | |
− | z = addsquare(x,y) | ||
− | |||
samples/arrays/elemental.f90 | samples/arrays/elemental.f90 | ||
+ | |} | ||
==Array comparisons== | ==Array comparisons== | ||
− | * Array comparisons | + | {| |
− | return an array of | + | |- valign="top" |
− | logicals of the same size | + | | |
− | of the arrays. | + | * Array comparisons return an array of logicals of the same size of the arrays. |
− | * Can use any and all to | + | * Can use any and all to see if any or all of those logicals are true. |
− | see if any or all of those | + | | |
− | logicals are true. | ||
samples/arrays/compare.f90 | samples/arrays/compare.f90 | ||
+ | |} | ||
==Array masks== | ==Array masks== | ||
− | * These logical arrays can | + | {| |
− | be used to mask several | + | |- valign="top" |
− | operations | + | | |
− | * Only do sums, mins, etc | + | * These logical arrays can be used to mask several operations |
− | where the mask is true | + | * Only do sums, mins, etc where the mask is true |
− | * Eg, only pick out positive | + | * Eg, only pick out positive values. |
− | values. | + | * Many array intrinsics have this mask option |
− | * Many array intrinsics | + | | |
− | have this mask option | ||
− | |||
samples/arrays/mask.f90 | samples/arrays/mask.f90 | ||
+ | |} | ||
==Where construct== | ==Where construct== | ||
− | * The where construct | + | {| |
− | can be used to easily | + | |- valign="top" |
− | manipulate sections of | + | | |
− | array based on arbitrary | + | * The where construct can be used to easily manipulate sections of array based on arbitrary comparisons. |
− | comparisons. | + | * Where construct => for whatever indices the comparison is true, set values as follow; otherwise, set other values. |
− | * Where construct => for | + | | |
− | whatever indices the | ||
− | comparison is true, set | ||
− | values as follow; | ||
− | otherwise, set other | ||
− | values. | ||
− | |||
samples/arrays/where.f90 | samples/arrays/where.f90 | ||
+ | |} | ||
==Forall construct== | ==Forall construct== | ||
− | * Forall is an array | + | {| |
− | assignment statement | + | |- valign="top" |
− | * Each line in forall has to be | + | | |
− | independent. All done “at | + | * Forall is an array assignment statement |
− | once” - no guarantees as | + | * Each line in forall has to be independent. All done “at once” - no guarantees as to order |
− | to order | + | * If (say) 2 lines in the forall, all of the first line is done, then all of the second. |
− | * If (say) 2 lines in the forall, | + | * Any functions called must be pure |
− | all of the first line is done, | + | * Can be vectorized or parallelized by compiler |
− | then all of the second. | + | | |
− | * Any functions called must | ||
− | be pure | ||
− | * Can be vectorized or | ||
− | parallelized by compiler | ||
− | |||
samples/arrays/forall.f90 | samples/arrays/forall.f90 | ||
+ | |} | ||
==Array Sections== | ==Array Sections== | ||
− | * Generalization of array | + | {| |
− | indexing | + | |- valign="top" |
− | * Familiar to users of | + | | |
− | Matlab, IDL, Python.. | + | * Generalization of array indexing |
− | * Can use “slices” of an | + | * Familiar to users of Matlab, IDL, Python.. |
− | array using “index | + | * Can use “slices” of an array using “index triplet” |
− | triplet” | ||
* [start]:[end][:step] | * [start]:[end][:step] | ||
− | * Default start=1, default | + | * Default start=1, default end=size, default step=1. |
− | end=size, default step=1. | + | * Can be used for each index of multid array |
− | * Can be used for each | + | | |
− | index of multid array | + | <source lang="fortran"> |
− | |||
a([start]:[end][:step]) | a([start]:[end][:step]) | ||
a = [1,2,3,4,5,6,7,8,9,10] | a = [1,2,3,4,5,6,7,8,9,10] | ||
Line 1,030: | Line 1,319: | ||
a(2) == 2 | a(2) == 2 | ||
a(:) == [1,2,3,4,5,6,7,8,9,10] | a(:) == [1,2,3,4,5,6,7,8,9,10] | ||
+ | </source> | ||
+ | |} | ||
==Array Sections== | ==Array Sections== | ||
− | * This sort of thing is very | + | {| |
− | handy in numerical | + | |- valign="top" |
− | computation | + | | |
− | * Replace do-loops with | + | * This sort of thing is very handy in numerical computation |
− | clearer, shorter, possibly | + | * Replace do-loops with clearer, shorter, possibly vectorized array operations |
− | vectorized array | + | * Bigger advantage for multidimensional arrays |
− | operations | + | | |
− | * Bigger advantage for | ||
− | multidimensional arrays | ||
− | |||
samples/arrays/derivative.f90 | samples/arrays/derivative.f90 | ||
+ | |} | ||
==Array Sections== | ==Array Sections== | ||
− | * The previous sorts of array | + | {| |
− | sections - shifting things leftward | + | |- valign="top" |
− | and rightward - are so common | + | | |
− | there are intrinsics for them | + | * The previous sorts of array sections - shifting things leftward and rightward - are so common there are intrinsics for them |
− | * +ve shift shifts elements | + | * +ve shift shifts elements leftwards (or array bounds rightwards). |
− | leftwards (or array bounds | + | * cshift does circular shift - shifting off the end of the array “wraps around”. |
− | rightwards). | + | * eoshift fills with zeros, or optional filling. |
− | * cshift does circular shift - shifting | ||
− | off the end of the array “wraps | ||
− | around”. | ||
− | * eoshift fills with zeros, or | ||
− | optional filling. | ||
* Can work on given dimension | * Can work on given dimension | ||
− | + | | | |
+ | <source lang="fortran"> | ||
a = [1,2,3,4,5] | a = [1,2,3,4,5] | ||
cshift(a,1) == [2,3,4,5,1] | cshift(a,1) == [2,3,4,5,1] | ||
Line 1,064: | Line 1,349: | ||
eoshift(a,1) ==[2,3,4,5,0] | eoshift(a,1) ==[2,3,4,5,0] | ||
eoshift(a,-1)==[0,1,2,3,4] | eoshift(a,-1)==[0,1,2,3,4] | ||
+ | </source> | ||
+ | |} | ||
− | ==Other important array== | + | ==Other important array intrinsics== |
− | + | {| | |
+ | |- valign="top" | ||
+ | | | ||
* minval/maxval - finds min, max element in an array. | * minval/maxval - finds min, max element in an array. | ||
* minloc/maxloc - finds location of min/max element | * minloc/maxloc - finds location of min/max element | ||
* product/sum - returns product/sum of array elements | * product/sum - returns product/sum of array elements | ||
* reshape - Adjusts shape of array data. Eg: | * reshape - Adjusts shape of array data. Eg: | ||
− | 1,4 | + | | |
+ | <source lang="fortran"> | ||
+ | 1,4 | ||
reshape([1,2,3,4,5,6],[3,2]) == 2,5 | reshape([1,2,3,4,5,6],[3,2]) == 2,5 | ||
− | 3,6 | + | 3,6 |
+ | </source> | ||
+ | |} | ||
==Linear algebra in Fortran== | ==Linear algebra in Fortran== | ||
− | * Comes built in with transpose, matmul, | + | {| |
− | dot_product for dealing with arrays. | + | |- valign="top" |
+ | | | ||
+ | * Comes built in with transpose, matmul, dot_product for dealing with arrays. | ||
* matmul also does matrix-vector multiplication | * matmul also does matrix-vector multiplication | ||
− | * Either use these or system-provided BLAS | + | * Either use these or system-provided BLAS libraries - never, ever write yourself. |
− | libraries - never, ever write yourself. | + | | |
+ | |} | ||
==Matmul times== | ==Matmul times== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
+ | <source lang="bash"> | ||
$ ./matmul 2500 | $ ./matmul 2500 | ||
Experiment with matrix size | Experiment with matrix size | ||
− | 2500 | + | 2500 |
Triple-loop time: | Triple-loop time: | ||
− | 149.63400 | + | 149.63400 |
matmul intrinsic time: | matmul intrinsic time: | ||
− | 10.370000 | + | 10.370000 |
SGEMM lapack call time: | SGEMM lapack call time: | ||
− | 1.4809999 | + | 1.4809999 |
− | + | </source> | |
− | (gfortran 4.6, compiled -O3 -march=native | + | (gfortran 4.6, compiled -O3 -march=native using Intel MKL 10.3 for sgemm) |
− | using Intel MKL 10.3 for sgemm) | + | | |
− | |||
samples/arrays/matmul.f90 | samples/arrays/matmul.f90 | ||
+ | |} | ||
==Linear algebra in Fortran== | ==Linear algebra in Fortran== | ||
− | + | {| | |
+ | |- valign="top" | ||
+ | | | ||
samples/arrays/matrix.f90 | samples/arrays/matrix.f90 | ||
+ | | | ||
+ | |} | ||
==Array sizes and Assumed Shape== | ==Array sizes and Assumed Shape== | ||
− | * Printmat routine here is | + | {| |
− | interesting - don’t pass | + | |- valign="top" |
− | (a,rows,cols), just a. | + | | |
− | * Can assume a rank-2 array, | + | * Printmat routine here is interesting - don’t pass (a,rows,cols), just a. |
− | and get size at runtime. | + | * Can assume a rank-2 array, and get size at runtime. |
− | * Simplifies call, and eliminates | + | * Simplifies call, and eliminates possible inconsistency: what if rows, cols is wrong? |
− | possible inconsistency: what if | + | * size(array,dim) gets the size of array in the dim dimension. |
− | rows, cols is wrong? | + | | |
− | * size(array,dim) gets the size of | ||
− | array in the dim dimension. | ||
− | |||
samples/arrays/matrix.f90 | samples/arrays/matrix.f90 | ||
+ | |} | ||
==Array sizes and Assumed Shape== | ==Array sizes and Assumed Shape== | ||
− | * Assumed shape arrays (eg, | + | {| |
− | dimension(:,:)) much better | + | |- valign="top" |
− | than older ways of passing | + | | |
− | arrays: | + | * Assumed shape arrays (eg, dimension(:,:)) much better than older ways of passing arrays: |
integer nx, ny | integer nx, ny | ||
+ | |||
integer a(nx,ny) | integer a(nx,ny) | ||
+ | |||
or worse, | or worse, | ||
+ | |||
integer a(*,ny) | integer a(*,ny) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | * Information is thrown away, possibility of inconsistency. | ||
+ | * Here, (:,:) means we know the rank, but don’t know the size yet. | ||
+ | | | ||
samples/arrays/matrix.f90 | samples/arrays/matrix.f90 | ||
+ | |} | ||
==Allocatable Arrays== | ==Allocatable Arrays== | ||
− | * So far, all our programs have had fixed-size | + | {| |
− | arrays, set at compile time. | + | |- valign="top" |
− | * To change problem size, have to edit code, | + | | |
− | recompile. | + | * So far, all our programs have had fixed-size arrays, set at compile time. |
− | * Has some advantages (optimization, | + | * To change problem size, have to edit code, recompile. |
− | determinism) but very inflexible. | + | * Has some advantages (optimization, determinism) but very inflexible. |
− | * Would like to be able to request memory | + | * Would like to be able to request memory at run time, make array of desired size. |
− | at run time, make array of desired size. | + | * Allocatable arrays are arguably most important addition to Fortran. |
− | * Allocatable arrays are arguably most | + | | |
− | important addition to Fortran. | + | |} |
==Allocate(), Deallocate()== | ==Allocate(), Deallocate()== | ||
− | * Give array a deferred size (eg, | + | {| |
− | dimension(:)) and the attribute | + | |- valign="top" |
− | allocatable. | + | | |
− | * When time to allocate it, use | + | * Give array a deferred size (eg, dimension(:)) and the attribute allocatable. |
− | allocate(a(n)). | + | * When time to allocate it, use allocate(a(n)). |
* Deallocate with deallocate(a). | * Deallocate with deallocate(a). | ||
+ | * In between, arrays can be used as any other array. | ||
+ | | | ||
samples/arrays/allocatable.f90 | samples/arrays/allocatable.f90 | ||
− | + | |} | |
− | |||
==Allocate(), Deallocate()== | ==Allocate(), Deallocate()== | ||
− | * If allocation fails (not enough memory available for | + | {| |
− | request), program will exit. | + | |- valign="top" |
− | * Can control this by checking for an optional error code, | + | | |
− | allocate(a(n),stat=ierr) | + | * If allocation fails (not enough memory available for request), program will exit. |
− | * Can then test if ierr>0 (failure condition) and handle | + | * Can control this by checking for an optional error code, allocate(a(n),stat=ierr) |
− | gracefully. | + | * Can then test if ierr>0 (failure condition) and handle gracefully. |
− | * In scientific programming, the default behaviour is often | + | * In scientific programming, the default behaviour is often fine, if abrupt - you either have enough memory to run the problem, or you don’t. |
− | fine, if abrupt - you either have enough memory to run | + | | |
− | the problem, or you don’t. | + | |} |
==get_command_argument()== | ==get_command_argument()== | ||
− | * Previous version still | + | {| |
− | depended on a compiled-in | + | |- valign="top" |
− | number. | + | | |
− | * Can read from file or from | + | * Previous version still depended on a compiled-in number. |
− | console, but Fortran now has | + | * Can read from file or from console, but Fortran now has standard way to get command-line arguments |
− | standard way to get | + | * Get the count of arguments, and if there’s at least one argument there, get it, read it as integer, and allocate array. |
− | command-line arguments | + | | |
− | * Get the count of arguments, | ||
− | and if there’s at least one | ||
− | argument there, get it, read it | ||
− | as integer, and allocate array. | ||
samples/arrays/allocatable2.f90 | samples/arrays/allocatable2.f90 | ||
+ | |} | ||
==get_command_argument()== | ==get_command_argument()== | ||
− | + | {| | |
+ | |- valign="top" | ||
+ | | | ||
samples/arrays/allocatable2.f90 | samples/arrays/allocatable2.f90 | ||
+ | | | ||
+ | |} | ||
==Hands on #3== | ==Hands on #3== | ||
− | * Use array functionality to simplify hydro code | + | {| |
− | -- don't need to pass, array size, and can | + | |- valign="top" |
− | simplify mathematics using array operations. | + | | |
− | * In workedexamples/arrays, have modified | + | * Use array functionality to simplify hydro code -- don't need to pass, array size, and can simplify mathematics using array operations. |
− | hydro to allocate u, and pbm to just take array. | + | * In workedexamples/arrays, have modified hydro to allocate u, and pbm to just take array. |
− | * Do the same with the fluid dynamic routines in | + | * Do the same with the fluid dynamic routines in solver.f90 |
− | solver.f90 | ||
* ~30 min | * ~30 min | ||
+ | | | ||
+ | |} | ||
− | + | =Fortran Pointers= | |
− | * Pointers, or references, | + | {| |
− | refer to another | + | |- valign="top" |
− | variable. | + | | |
− | * Eg, p does not contain a | + | * Pointers, or references, refer to another variable. |
− | real value, but a | + | * Eg, p does not contain a real value, but a reference to another real variable. |
− | reference to another | + | * Once associated with another variable, can read/write to it as if it were stored “in” p. |
− | real variable. | + | | |
− | * Once associated with | + | <source lang="fortran"> |
− | another variable, can | ||
− | read/write to it as if it | ||
− | were stored “in” p. | ||
− | |||
real, target :: x = 3.2 | real, target :: x = 3.2 | ||
real, pointer:: p | real, pointer:: p | ||
p => x | p => x | ||
+ | |||
p | p | ||
x | x | ||
3.2 | 3.2 | ||
− | + | </source> | |
− | |||
− | |||
samples/pointers/ptr1.f90 | samples/pointers/ptr1.f90 | ||
+ | |} | ||
==Fortran Pointers== | ==Fortran Pointers== | ||
− | * Pointers are either | + | {| |
− | associated, null, or | + | |- valign="top" |
− | undefined; start out life | + | | |
− | undefined. | + | * Pointers are either associated, null, or undefined; start out life undefined. |
− | * Can associate them to | + | * Can associate them to a variable with => , or mark them as not associated with any valid variable by pointing it to null(). |
− | a variable with => , or | + | * Reading value from or writing value to a null pointer will cause errors, probably crash. |
− | mark them as not | + | * Fortran pointers can’t point just anywhere. |
− | associated with any | + | * Must reference a variable with the same type, that has the target attribute. |
− | valid variable by | + | | |
− | pointing it to null(). | + | <source lang="fortran"> |
− | |||
real, target :: x = 3.2 | real, target :: x = 3.2 | ||
real, pointer:: p | real, pointer:: p | ||
Line 1,238: | Line 1,540: | ||
x | x | ||
+ | </source> | ||
+ | | | ||
+ | |} | ||
==Fortran Pointers== | ==Fortran Pointers== | ||
− | + | {| | |
− | + | |- valign="top" | |
− | * | + | | |
− | + | * Pointers can reference other pointers. | |
− | + | * Actually references what they’re pointing to. | |
− | + | | | |
− | + | <source lang="fortran"> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | * | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
real, target :: x = 3.2 | real, target :: x = 3.2 | ||
real, pointer:: p1, p2 | real, pointer:: p1, p2 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
p1 => x | p1 => x | ||
p2 => p1 | p2 => p1 | ||
Line 1,284: | Line 1,561: | ||
x | x | ||
3.2 | 3.2 | ||
+ | </source> | ||
+ | |} | ||
==Allocating a pointer== | ==Allocating a pointer== | ||
− | * Pointer doesn’t | + | {| |
− | necessarily have to have | + | |- valign="top" |
− | another variable to | + | | |
− | target | + | * Pointer doesn’t necessarily have to have another variable to target |
− | * Can allocate memory | + | * Can allocate memory for p to point to that does not belong to any other pointer. |
− | for p to point to that | + | * Must deallocate it when done |
− | does not belong to any | + | | |
− | other pointer. | + | <source lang="fortran"> |
− | * Must deallocate it when | ||
− | |||
− | |||
real, pointer:: p | real, pointer:: p | ||
allocate(p) | allocate(p) | ||
Line 1,302: | Line 1,578: | ||
p | p | ||
7.9 | 7.9 | ||
− | + | | | |
− | + | </source> | |
− | |||
samples/pointers/ptr2.f90 | samples/pointers/ptr2.f90 | ||
+ | |} | ||
− | ==What are they good | + | ==What are they good for? (1)== |
− | for? (1) | + | {| |
− | * Pointers are essential for | + | |- valign="top" |
− | creating, maintaining | + | | |
− | dynamic data structures | + | * Pointers are essential for creating, maintaining dynamic data structures |
* Linked lists, trees, heaps.. | * Linked lists, trees, heaps.. | ||
− | * Some of these can be | + | * Some of these can be sort-of implemented in arrays, but very awkward |
− | sort-of implemented in | + | * Adaptive meshes, treebased particle solvers need these structures. |
− | arrays, but very | + | | |
− | awkward | ||
− | * Adaptive meshes, treebased particle solvers | ||
− | need these structures. | ||
− | |||
http://en.wikipedia.org/wiki/File:Singly-linked-list.svg | http://en.wikipedia.org/wiki/File:Singly-linked-list.svg | ||
− | |||
http://en.wikipedia.org/wiki/File:Max-Heap.svg | http://en.wikipedia.org/wiki/File:Max-Heap.svg | ||
+ | |} | ||
− | ==What are they good | + | ==What are they good for? (2)== |
− | for? (2) | + | {| |
− | * A pointer can be of | + | |- valign="top" |
− | array type, not just | + | | |
− | scalar | + | * A pointer can be of array type, not just scalar |
− | * Fortran pointers + | + | * Fortran pointers + fortran arrays are quite interesting; can create “views” of subarrays |
− | fortran arrays are quite | + | | |
− | interesting; can create | + | <source lang="fortran"> |
− | “views” of subarrays | ||
− | |||
real, target, dimension(7) :: x | real, target, dimension(7) :: x | ||
real, pointer:: p(:) | real, pointer:: p(:) | ||
Line 1,353: | Line 1,623: | ||
7 | 7 | ||
− | + | </source> | |
− | |||
− | |||
samples/pointers/views.f90 | samples/pointers/views.f90 | ||
+ | |} | ||
==Hands on #4== | ==Hands on #4== | ||
− | * Use pointers to provide views into subsets of | + | {| |
− | the arrays in solver.f90 to clarify the functions. | + | |- valign="top" |
− | * In workedexamples/pointers, have started | + | | |
− | the process with cfl, hydroflux; try tackling | + | * Use pointers to provide views into subsets of the arrays in solver.f90 to clarify the functions. |
− | tvd1d, others. | + | * In workedexamples/pointers, have started the process with cfl, hydroflux; try tackling tvd1d, others. |
* ~30 min | * ~30 min | ||
+ | | | ||
+ | |} | ||
− | + | =Derived Types and Objects= | |
− | * Often, groups of | + | {| |
− | variables naturally go | + | |- valign="top" |
− | together to represent a | + | | |
− | larger structure | + | * Often, groups of variables naturally go together to represent a larger structure |
− | * Whenever you find | + | * Whenever you find yourself passing the same group of variables to several routines, a good candidate for a derived type. |
− | yourself passing the | + | <source lang="fortran"> |
− | same group of variables | ||
− | to several routines, a | ||
− | good candidate for a | ||
− | derived type. | ||
− | |||
type griddomain | type griddomain | ||
− | real :: xmin, xmax | + | real :: xmin, xmax |
− | real :: ymin, ymax | + | real :: ymin, ymax |
− | real :: nx, ny | + | real :: nx, ny |
− | real, dimension(:,:) :: u | + | real, dimension(:,:) :: u |
endtype griddomain | endtype griddomain | ||
+ | |||
type(griddomain) :: g | type(griddomain) :: g | ||
g % xmin = -1 | g % xmin = -1 | ||
g % xmax = +1 | g % xmax = +1 | ||
+ | </source> | ||
+ | |} | ||
==Derived Types and Objects== | ==Derived Types and Objects== | ||
− | * Consider interval | + | {| |
− | arithmetic (good for | + | |- valign="top" |
− | quantification of | + | | |
− | uncertainties, etc). | + | * Consider interval arithmetic (good for quantification of uncertainties, etc). |
− | * An interval inherently | + | * An interval inherently has two values associated with it - the end points. |
− | has two values | ||
− | associated with it - the | ||
− | end points. | ||
* Can make this a type. | * Can make this a type. | ||
− | + | * Note can access the fields in the type with “%” | |
− | + | * typename (field1val,field2val..) initializes a value of that type. | |
− | + | * Can pass values of this type to functions, etc., just like a built-in type. | |
− | * Note can access the | + | | |
− | fields in the type with | + | (from samples/derivedtypes/simple/intervalmath.f90 ) |
− | “%” | + | | |
− | * typename | + | |} |
− | (field1val,field2val..) | ||
− | initializes a value of that | ||
− | type. | ||
− | * Can pass values of this | ||
− | type to functions, etc., | ||
− | just like a built-in type. | ||
− | samples/derivedtypes/simple/intervalmath.f90 | ||
==Procedures using types== | ==Procedures using types== | ||
− | + | {| | |
− | + | |- valign="top" | |
− | + | | | |
− | + | * Can start creating library of routines that operate on these new interval types. | |
− | + | * Procedures can take the new type as arguments, functions can return the new type. | |
− | + | | | |
− | + | (from samples/derivedtypes/intervalfunctions/intervalmath.f90 ) | |
− | + | |} | |
− | |||
− | |||
− | |||
− | |||
− | * Can start creating | ||
− | library of routines that | ||
− | operate on these new | ||
− | interval types. | ||
− | * Procedures can take | ||
− | the new type as | ||
− | arguments, functions | ||
− | can return the new | ||
− | type. | ||
− | |||
− | samples/derivedtypes/intervalfunctions/ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==Procedures using types== | ==Procedures using types== | ||
{| | {| | ||
+ | |- valign="top" | ||
| | | | ||
+ | * Would prefer not to have to treat integer and real intervals so differently in main program | ||
+ | * Different types, but adding should be similar. | ||
* Would like to be able to call “addintervals” and have language call the right subroutine, do the right thing. | * Would like to be able to call “addintervals” and have language call the right subroutine, do the right thing. | ||
* Similar to how intrinsics work - sin() works on any kind of real, matmult() works on integer, real, or complex matricies. | * Similar to how intrinsics work - sin() works on any kind of real, matmult() works on integer, real, or complex matricies. | ||
Line 1,459: | Line 1,695: | ||
==Generic Interfaces== | ==Generic Interfaces== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
{| | {| | ||
* Generic Interfaces | * Generic Interfaces | ||
Line 1,469: | Line 1,708: | ||
==Generic Interfaces== | ==Generic Interfaces== | ||
− | * Note that everything is | + | {| |
− | private except what is | + | |- valign="top" |
− | explicitly made public. | + | | |
+ | * Note that everything is private except what is explicitly made public. | ||
* Types are public. | * Types are public. | ||
− | * Generic interfaces are | + | * Generic interfaces are public. |
− | public. | + | * Type specific routines are not. |
− | * Type specific routines | + | * Program using interval math sees only the generic interfaces. |
− | are not. | + | | |
− | * Program using interval | ||
− | math sees only the | ||
− | generic interfaces. | ||
samples/derivedtypes/genericintervals/intervalmath.f90 | samples/derivedtypes/genericintervals/intervalmath.f90 | ||
+ | |} | ||
==Generic interfaces== | ==Generic interfaces== | ||
− | * Call to addintervals or | + | {| |
− | subtract intervals goes | + | |- valign="top" |
− | to the correct typespecific routine. | + | | |
+ | * Call to addintervals or subtract intervals goes to the correct typespecific routine. | ||
* As does print interval. | * As does print interval. | ||
− | * Could create routines | + | * Could create routines to add real to int interval, etc and add to the same interface. |
− | to add real to int | + | | |
− | interval, etc and add to | ||
− | the same interface. | ||
samples/derivedtypes/genericintervals/interval2.f90 | samples/derivedtypes/genericintervals/interval2.f90 | ||
+ | |} | ||
==Operator overloading== | ==Operator overloading== | ||
− | * An infix operator is | + | {| |
− | really just “syntactic | + | |- valign="top" |
− | sugar” for a function | + | | |
− | which takes two | + | * An infix operator is really just “syntactic sugar” for a function which takes two operands and returns a third. |
− | operands and returns a | + | | |
− | third. | + | <source lang="fortran" |
− | |||
a = b (op) c | a = b (op) c | ||
=> | => | ||
function op(b,c) | function op(b,c) | ||
returns a | returns a | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
a=b | a=b | ||
=> | => | ||
subroutine assign(a,b) | subroutine assign(a,b) | ||
+ | </source> | ||
+ | |} | ||
==Operator overloading== | ==Operator overloading== | ||
− | * Here, we’ve defined | + | {| |
− | two subroutines which | + | |- valign="top" |
− | set intervals based on | + | | |
− | an array - 2 ints for an | + | * Here, we’ve defined two subroutines which set intervals based on an array - 2 ints for an integer interval, or 2 reals for a real interval |
− | integer interval, or 2 | + | | |
− | reals for a real interval | ||
− | |||
samples/derivedtypes/intervaloperators/intervalmath.f90 | samples/derivedtypes/intervaloperators/intervalmath.f90 | ||
+ | |} | ||
==Generic interfaces== | ==Generic interfaces== | ||
− | * Once this is done, can | + | {| |
− | use assignment | + | |- valign="top" |
− | operator, | + | | |
− | * Or add, subtract | + | * Once this is done, can use assignment operator, |
− | multiply intervals. | + | * Or add, subtract multiply intervals. |
− | * Can even compose | + | * Can even compose them in complex expressions! Functions automatically composed. |
− | them in complex | + | | |
− | expressions! Functions | ||
− | automatically | ||
− | composed. | ||
samples/derivedtypes/intervaloperators/interval3.f90 | samples/derivedtypes/intervaloperators/interval3.f90 | ||
+ | |} | ||
==Type bound procedures== | ==Type bound procedures== | ||
− | * Types can have not only | + | {| |
− | variables, but | + | |- valign="top" |
− | procedures. | + | | |
− | * Takes us from a type to | + | * Types can have not only variables, but procedures. |
− | what is usually called a | + | * Takes us from a type to what is usually called a class. |
− | class. | + | | |
− | + | (from samples/derivedtypes/intervaltypebound/intervalmath.f90 ) | |
− | samples/derivedtypes/intervaltypebound/ | + | |} |
− | intervalmath.f90 | ||
==Type bound procedures== | ==Type bound procedures== | ||
− | * Called like one accesses | + | {| |
− | a field - % | + | |- valign="top" |
− | * Operates on data of | + | | |
− | the particular variable it | + | * Called like one accesses a field - % |
− | is invoked from | + | * Operates on data of the particular variable it is invoked from |
− | + | | | |
samples/derivedtypes/intervaltypebound/interval3.f90 | samples/derivedtypes/intervaltypebound/interval3.f90 | ||
+ | |} | ||
==Type bound procedures== | ==Type bound procedures== | ||
− | * It is implicitly passed as | + | {| |
− | it’s first parameter the | + | |- valign="top" |
− | variable itself. | + | | |
− | * Can take other | + | * It is implicitly passed as it’s first parameter the variable itself. |
− | arguments as well. | + | * Can take other arguments as well. |
+ | | | ||
+ | samples/derivedtypes/intervaltypebound/intervalmath.f90 | ||
+ | |} | ||
− | + | ==Object oriented programming== | |
− | + | {| | |
+ | |- valign="top" | ||
+ | | | ||
+ | * F2003 onwards can do full object oriented programing. | ||
+ | * Types can be derived from other types, inheriting their fields and type-bound procedures, and extending them. | ||
+ | * Goes beyond scope of today, but this is the starting-off point. | ||
+ | | | ||
+ | |} | ||
− | == | + | =Interoperability with other languages= |
− | + | {| | |
− | + | |- valign="top" | |
− | + | | | |
− | + | * Large scientific software now frequently uses multiple languages, either within a single code or between codes. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | * Large scientific software now frequently uses | ||
− | multiple languages, either within a single code | ||
− | or between codes. | ||
* Right tool for the job! | * Right tool for the job! | ||
− | * Need to know how to make software | + | * Need to know how to make software interact. |
− | interact. | + | * Here we’ll look at C/Fortran code calling each other, and calling Fortran code from python. |
− | * Here we’ll look at C/Fortran code calling | + | | |
− | each other, and calling Fortran code from | + | |} |
− | python. | ||
==C-interoperability== | ==C-interoperability== | ||
− | * iso_c_binding module contains definitions for | + | {| |
− | interacting with C | + | |- valign="top" |
+ | | | ||
+ | * iso_c_binding module contains definitions for interacting with C | ||
* Types, ways to bind procedures to C, etc. | * Types, ways to bind procedures to C, etc. | ||
− | * Allows you to call C routines, or bind Fortran | + | * Allows you to call C routines, or bind Fortran routines in a way that they can be called by C. |
− | routines in a way that they can be called by C. | + | | |
+ | |} | ||
− | ==Calling a C routine== | + | ==Calling a C routine from Fortran== |
− | + | {| | |
− | * As with the case of | + | |- valign="top" |
− | calling a passed-in | + | | |
− | function, need an | + | * As with the case of calling a passed-in function, need an explicit prototype. |
− | explicit prototype. | + | * Tells compiler what to do with “sqrtc” function when called. |
− | * Tells compiler what | + | | |
− | to do with “sqrtc” | ||
− | function when called. | ||
samples/C-interop/call-c-fn/main.f90 | samples/C-interop/call-c-fn/main.f90 | ||
+ | |} | ||
− | ==Calling a C routine== | + | ==Calling a C routine from Fortran== |
− | + | {| | |
− | * BIND(C) - tells compiler/ | + | |- valign="top" |
− | linker will have a C-style, | + | | |
− | rather than fortran-style | + | * BIND(C) - tells compiler/ linker will have a C-style, rather than fortran-style name in object file. |
− | name in object file. | + | * Can optionally supply another name; otherwise, will default to procedure name. |
− | * Can optionally supply | + | * Here we’re calling it sqrtc to avoid Fortran sqrt() function. |
− | another name; otherwise, | + | * The value the function takes and returns are C doubles; that is, reals of kind(c_double). |
− | will default to procedure | + | * Also defined: c_float, integers of kind c_int, c_long, etc. |
− | name. | + | | |
− | * Here we’re calling it sqrtc | + | |} |
− | to avoid Fortran sqrt() | ||
− | function. | ||
− | ==Calling a C routine== | + | ==Calling a C routine from Fortran== |
− | + | {| | |
− | + | |- valign="top" | |
− | + | | | |
− | + | * C function arguments by default are passed by value; Fortran by default are passed by reference. | |
− | + | * Passed by value - values copied into function | |
− | + | * Passed by reference pointers to values copied in. | |
− | + | * value attribute for C-style arguments. | |
− | + | * Compiling - just make sure to link in C library you’re calling | |
− | |||
− | |||
− | |||
− | * C function arguments by | ||
− | default are passed by | ||
− | value; Fortran by default | ||
− | are passed by reference. | ||
− | * Passed by value - values | ||
− | copied into function | ||
− | * Passed by reference pointers to values copied | ||
− | in. | ||
− | * value attribute for C-style | ||
− | arguments. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | * Compiling - just make | ||
− | sure to link in C library | ||
− | you’re calling | ||
* And that’s it. | * And that’s it. | ||
− | + | | | |
+ | <source lang="bash"> | ||
$ make | $ make | ||
gfortran -c main.f90 | gfortran -c main.f90 | ||
Line 1,666: | Line 1,872: | ||
2.0000000000000000 | 2.0000000000000000 | ||
C | C | ||
− | sqrt(x) = | + | sqrt(x) = 1.4142135623730951 |
− | 1.4142135623730951 | + | Fortran sqrt(x) = 1.4142135623730951 |
− | Fortran sqrt(x) = | + | </source> |
− | 1.4142135623730951 | + | |} |
==C strings== | ==C strings== | ||
− | * When using C | + | {| |
− | strings, you have to | + | |- valign="top" |
− | remember that C | + | | |
− | terminates strings | + | * When using C strings, you have to remember that C terminates strings with a special character |
− | with a special | ||
− | character | ||
* C_NULL_CHAR | * C_NULL_CHAR | ||
− | * Dealing with functions | + | * Dealing with functions that return strings is hairier, as they return a pointer, not an array. |
− | that return strings is | + | | |
− | hairier, as they return | + | <source lang="bash"> |
− | a pointer, not an array. | ||
− | |||
$ make | $ make | ||
gfortran-mp-4.4 -c main.f90 | gfortran-mp-4.4 -c main.f90 | ||
Line 1,690: | Line 1,892: | ||
1234 = | 1234 = | ||
1234 | 1234 | ||
− | + | </source> | |
− | samples/C-interop/c-strings/main.f90 | + | (from samples/C-interop/c-strings/main.f90 ) |
+ | |} | ||
==Calling Fortran from C== | ==Calling Fortran from C== | ||
− | * To write Fortran | + | {| |
− | routines callable from | + | |- valign="top" |
− | C, bind the subroutine | + | | |
− | to a C name | + | * To write Fortran routines callable from C, bind the subroutine to a C name |
− | * Can optionally give it a | + | * Can optionally give it a different name, as above |
− | different name, as above | + | * And make sure arguments are of C types. |
− | * And make sure | + | | |
− | arguments are of C | ||
− | types. | ||
− | |||
samples/C-interop/c-call-fortran/froutine.f90 | samples/C-interop/c-call-fortran/froutine.f90 | ||
+ | |} | ||
==Calling Fortran from C== | ==Calling Fortran from C== | ||
− | * Here, we’ll do | + | {| |
− | something a little | + | |- valign="top" |
− | trickier and pass C | + | | |
− | dynamic arrays | + | * Here, we’ll do something a little trickier and pass C dynamic arrays |
− | + | * In Fortran, we accept the arguments as C pointers | |
− | + | * We then associate them with fortran pointers to arrays of shape [nx] (1d arrays here) | |
− | + | * Then we can do the usual Fortran array operations with them. | |
− | + | * The single scalar argument passed back we just treat as an intent(out) variable | |
− | * In Fortran, we accept | ||
− | the arguments as C | ||
− | pointers | ||
− | * We then associate | ||
− | them with fortran | ||
− | pointers to arrays of | ||
− | shape [nx] (1d arrays | ||
− | here) | ||
− | * Then we can do the | ||
− | usual Fortran array | ||
− | operations with them. | ||
− | |||
− | |||
− | |||
− | |||
− | * The single scalar | ||
− | argument passed back | ||
− | we just treat as an | ||
− | intent(out) variable | ||
* Of type c_float. | * Of type c_float. | ||
− | samples/C-interop/c-call-fortran/froutine.f90 | + | | |
+ | (from samples/C-interop/c-call-fortran/froutine.f90 ) | ||
+ | |} | ||
==More advanced== | ==More advanced== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Handling arbitrary C code is possible | * Handling arbitrary C code is possible | ||
− | * Passing C structs -- create a Fortran derived | + | * Passing C structs -- create a Fortran derived type with the same fields, use BIND(C) in defining the type. |
− | type with the same fields, use BIND(C) in | + | * C “multidimensional arrays” - have to be careful, they may not be contiguous! Follow pointers. |
− | defining the type. | ||
− | * C “multidimensional arrays” - have to be | ||
− | careful, they may not be contiguous! Follow | ||
− | pointers. | ||
* Even taking passed function pointers to C | * Even taking passed function pointers to C | ||
− | functions is possible (samples/C-interop/ | + | functions is possible |
− | functionptr) | + | | |
− | + | (from samples/C-interop/functionptr ) | |
− | + | |} | |
− | |||
− | |||
− | |||
+ | ==Fortran calling C, which calls Fortran== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
samples/C-interop/valueref/driver.f90 | samples/C-interop/valueref/driver.f90 | ||
+ | | | ||
+ | |} | ||
==samples/C-interop/valueref/croutine.c== | ==samples/C-interop/valueref/croutine.c== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
+ | | | ||
+ | |} | ||
==samples/C-interop/valueref/froutine.f90== | ==samples/C-interop/valueref/froutine.f90== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
+ | | | ||
+ | |} | ||
==$ ./main== | ==$ ./main== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
+ | <source lang="bash"> | ||
+ | $ ./main | ||
(1) In the FORTRAN routine before call to C | (1) In the FORTRAN routine before call to C | ||
(1) i = | (1) i = | ||
Line 1,771: | Line 1,972: | ||
30.000000000000000 | 30.000000000000000 | ||
(1) In the FORTRAN routine after call to C | (1) In the FORTRAN routine after call to C | ||
− | (1) i = | + | (1) i = 15 x = 3.0000000000000000 prod = 30.000000000000000 |
− | 15 x = | + | </source> |
− | 3.0000000000000000 | + | | |
− | prod = | + | |} |
− | 30.000000000000000 | ||
==samples/C-interop/valueref/Makefile== | ==samples/C-interop/valueref/Makefile== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
+ | | | ||
+ | |} | ||
==F2py== | ==F2py== | ||
− | * Comes with scipy, | + | {| |
− | a widely-installed | + | |- valign="top" |
− | (by scientists) | + | | |
− | python package. | + | * Comes with scipy, a widely-installed (by scientists) python package. |
− | * Wraps fortran in | + | * Wraps fortran in python, allows you to easily interface. |
− | python, allows you | + | * fwrap is another option |
− | to easily interface. | + | | |
− | * fwrap is another | ||
− | option | ||
− | |||
http://www.scipy.org/F2py | http://www.scipy.org/F2py | ||
+ | |} | ||
==* Will only use solver module.== | ==* Will only use solver module.== | ||
− | * Unfortunately, f2py isn’t quite smart enough to understand | + | {| |
− | using parameters to size arrays, so global replace ‘nvars’=4 | + | |- valign="top" |
+ | | | ||
+ | * Unfortunately, f2py isn’t quite smart enough to understand using parameters to size arrays, so global replace ‘nvars’=4 | ||
* module load use.experimental gcc python/2.7.1 intel/12 | * module load use.experimental gcc python/2.7.1 intel/12 | ||
* “f2py -m hydro_solver -h hydro_solver.pyf solver.f90” | * “f2py -m hydro_solver -h hydro_solver.pyf solver.f90” | ||
+ | | | ||
+ | |} | ||
==* generates the following header file (hydro_solver.pyf)== | ==* generates the following header file (hydro_solver.pyf)== | ||
− | + | {| | |
+ | |- valign="top" | ||
+ | | | ||
* Comment out stuff we don’t need (lower-level routines) | * Comment out stuff we don’t need (lower-level routines) | ||
* f2py -c --fcompiler=gfortran hydro_solver.pyf solver.f90 | * f2py -c --fcompiler=gfortran hydro_solver.pyf solver.f90 | ||
+ | | | ||
+ | |} | ||
==$ ipython== | ==$ ipython== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
+ | <source lang="bash"> | ||
In [1]: import hydro_solver | In [1]: import hydro_solver | ||
In [2]: hydro_solver.solver. | In [2]: hydro_solver.solver. | ||
Line 1,829: | Line 2,044: | ||
...dt = hydro_solver.solver.timestep(u) | ...dt = hydro_solver.solver.timestep(u) | ||
In [8]: pylab.imshow(u[1,:,:]) | In [8]: pylab.imshow(u[1,:,:]) | ||
+ | </source> | ||
+ | |} | ||
− | + | =Coarrays= | |
− | * In F2008, objects | + | {| |
− | can also have a | + | |- valign="top" |
− | “codimension”. | + | | |
− | * An object with a | + | * In F2008, objects can also have a “codimension”. |
− | codimension can be | + | * An object with a codimension can be “seen” across processes |
− | “seen” across | + | * Right now, only intel v 12 supports this |
− | processes | + | | |
− | * Right now, only intel | ||
− | v 12 supports this | ||
samples/coarrays/simple.f90 | samples/coarrays/simple.f90 | ||
+ | |} | ||
==Coarrays== | ==Coarrays== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
1 | 1 | ||
Line 1,863: | Line 2,082: | ||
independant | independant | ||
processes | processes | ||
+ | | | ||
+ | |} | ||
==Coarrays== | ==Coarrays== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
1 | 1 | ||
Line 1,891: | Line 2,115: | ||
Independent, parallel tasks can | Independent, parallel tasks can | ||
“see” each other’s data as easily as an array index. | “see” each other’s data as easily as an array index. | ||
+ | | | ||
+ | |} | ||
==Sychronization== | ==Sychronization== | ||
− | * When accessing other | + | {| |
− | processor’s data, must | + | |- valign="top" |
− | ensure that tasks are | + | | |
− | synchronized | + | * When accessing other processor’s data, must ensure that tasks are synchronized |
− | * Don’t want to read | + | * Don’t want to read task 1’s data before read is complete! |
− | task 1’s data before | ||
− | read is complete! | ||
* sync all | * sync all | ||
+ | | | ||
samples/coarrays/broadcast.f90 | samples/coarrays/broadcast.f90 | ||
+ | |} | ||
==Sychronization== | ==Sychronization== | ||
− | * Other synchronization | + | {| |
− | tools: | + | |- valign="top" |
− | * sync images([..]) syncs | + | | |
− | only images in the list | + | * Other synchronization tools: |
− | provided | + | * sync images([..]) syncs only images in the list provided |
− | * critical - only one | + | * critical - only one image can enter block at a time |
− | image can enter block | + | * lock - finer-grained control than critical. |
− | at a time | ||
− | * lock - finer-grained | ||
− | control than critical. | ||
* atomic operations. | * atomic operations. | ||
− | + | | | |
samples/coarrays/broadcast.f90 | samples/coarrays/broadcast.f90 | ||
+ | |} | ||
==1d Diffusion Eqn== | ==1d Diffusion Eqn== | ||
− | * Calculate heat | + | {| |
− | diffusion along a bar | + | |- valign="top" |
− | * Finite difference; | + | | |
− | calculate second | + | * Calculate heat diffusion along a bar |
− | derivative using | + | * Finite difference; calculate second derivative using neighbours |
− | neighbours | + | * Get new temperature from old temperatures |
− | * Get new | ||
− | temperature from | ||
− | old temperatures | ||
− | |||
1 -2 1 | 1 -2 1 | ||
+ | | | ||
+ | |} | ||
==1d Diffusion Eqn== | ==1d Diffusion Eqn== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Initial conditions: | * Initial conditions: | ||
− | * Use pointers to | + | * Use pointers to point to new, old temperatures |
− | point to new, old | + | * 1..totpoints+2 (pts 1, totpoints+1 “ghost cells) |
− | temperatures | + | * Setup x, orig temperature, expected values |
− | * 1..totpoints+2 (pts 1, | + | | |
− | totpoints+1 “ghost | ||
− | cells) | ||
− | * Setup x, orig | ||
− | temperature, expected | ||
− | values | ||
− | |||
samples/coarrays/diffusion/diffusion.f90 | samples/coarrays/diffusion/diffusion.f90 | ||
+ | |} | ||
==1d Diffusion Eqn== | ==1d Diffusion Eqn== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Evolution: | * Evolution: | ||
* Apply BCs | * Apply BCs | ||
− | * Apply finite | + | * Apply finite difference stencil to all real data points |
− | difference stencil to | + | | |
− | all real data points | ||
samples/coarrays/diffusion/diffusion.f90 | samples/coarrays/diffusion/diffusion.f90 | ||
+ | |} | ||
==1d Diffusion Eqn== | ==1d Diffusion Eqn== | ||
− | * Output calculated | + | {| |
− | values and theory to | + | |- valign="top" |
− | file output.txt | + | | |
− | * Note: Fortran2008, | + | * Output calculated values and theory to file output.txt |
− | can use “newunit” | + | * Note: Fortran2008, can use “newunit” to find, use an unused IO unit |
− | to find, use an | + | | |
− | unused IO unit | ||
− | |||
samples/coarrays/diffusion/diffusion.f90 | samples/coarrays/diffusion/diffusion.f90 | ||
+ | |} | ||
==$ ./diffusion== | ==$ ./diffusion== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
[..] | [..] | ||
$ gnuplot | $ gnuplot | ||
Line 1,970: | Line 2,195: | ||
gnuplot> plot 'output.txt' using 1:2 with points title 'Simulated', | gnuplot> plot 'output.txt' using 1:2 with points title 'Simulated', | ||
'output.txt' using 1:3 with lines title 'Theory' | 'output.txt' using 1:3 with lines title 'Theory' | ||
+ | | | ||
+ | |} | ||
==Coarray Diffusion== | ==Coarray Diffusion== | ||
− | * Now we’ll try this in | + | {| |
− | parallel | + | |- valign="top" |
− | * Each image runs it’s part | + | | |
− | of the problem | + | * Now we’ll try this in parallel |
− | (totpoints/num_images()) | + | * Each image runs it’s part of the problem (totpoints/num_images()) |
− | * Communications is like | + | * Communications is like boundary condition handling - except boundary data must be obtained from neighbouring image. |
− | boundary condition | + | | |
− | handling - except | + | |} |
− | boundary data must be | ||
− | obtained from | ||
− | neighbouring image. | ||
==Coarray Diffusion== | ==Coarray Diffusion== | ||
− | * Everything’s the same | + | {| |
− | except we are only | + | |- valign="top" |
− | responsible for locpoints | + | | |
− | points, starting at start. | + | * Everything’s the same except we are only responsible for locpoints points, starting at start. |
− | * Once calculated, we | + | * Once calculated, we never need totpoints again. All arrays are of size (locpoints), etc. |
− | never need totpoints | + | * For simplicity, we assume here everything divides evenly. |
− | again. All arrays are of | + | | |
− | size (locpoints), etc. | ||
− | * For simplicity, we | ||
− | assume here everything | ||
− | divides evenly. | ||
− | |||
samples/coarrays/diffusion/diffusion-coarray.f90 | samples/coarrays/diffusion/diffusion-coarray.f90 | ||
+ | |} | ||
==Coarray Diffusion== | ==Coarray Diffusion== | ||
− | * Boundary exchange; if we | + | {| |
− | have interior neighbours, | + | |- valign="top" |
− | get updated data from | + | | |
− | them so we can calculate | + | * Boundary exchange; if we have interior neighbours, get updated data from them so we can calculate our new data |
− | our new data | + | * Note: can’t use pointers here, coarrays can’t be targets. |
− | * Note: can’t use pointers | + | * Sync all around boundary exchange a little heavy handed; could just sync neighbours. |
− | here, coarrays can’t be | + | | |
− | targets. | ||
− | * Sync all around boundary | ||
− | exchange a little heavy | ||
− | handed; could just sync | ||
− | neighbours. | ||
samples/coarrays/diffusion/diffusion-coarray.f90 | samples/coarrays/diffusion/diffusion-coarray.f90 | ||
+ | |} | ||
==Coarray Diffusion== | ==Coarray Diffusion== | ||
− | + | {| | |
+ | |- valign="top" | ||
+ | | | ||
+ | <source lang="fortran"> | ||
temperature()[leftneigh] | temperature()[leftneigh] | ||
Line 2,024: | Line 2,243: | ||
lnpts+2 | lnpts+2 | ||
+ | </source> | ||
+ | |} | ||
==Coarray Diffusion== | ==Coarray Diffusion== | ||
− | * Everything else exactly | + | {| |
− | the same. | + | |- valign="top" |
− | * For I/O, we each output | + | | |
− | our own file, prefixed by | + | * Everything else exactly the same. |
− | our image number | + | * For I/O, we each output our own file, prefixed by our image number |
* (eg, 1-output.txt, 2output.txt...) | * (eg, 1-output.txt, 2output.txt...) | ||
− | + | | | |
samples/coarrays/diffusion/diffusion-coarray.f90 | samples/coarrays/diffusion/diffusion-coarray.f90 | ||
+ | |} | ||
==$ export FOR_COARRAY_NUM_IMAGES=3== | ==$ export FOR_COARRAY_NUM_IMAGES=3== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
+ | <source lang="fortran"> | ||
# 3 images | # 3 images | ||
$ ./diffusion-coarray | $ ./diffusion-coarray | ||
Line 2,043: | Line 2,269: | ||
gnuplot> plot '1-ics.txt' using 1:2, '2-ics.txt' using 1:2, '3-ics.txt' | gnuplot> plot '1-ics.txt' using 1:2, '2-ics.txt' using 1:2, '3-ics.txt' | ||
using 1:2 | using 1:2 | ||
+ | </source> | ||
+ | |} | ||
==gnuplot> plot '1-output.txt' using 1:2, '2-output.txt' using 1:2, '3output.txt' using 1:2, '1-output.txt' using 1:3 with lines title 'Theory',== | ==gnuplot> plot '1-output.txt' using 1:2, '2-output.txt' using 1:2, '3output.txt' using 1:2, '1-output.txt' using 1:3 with lines title 'Theory',== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
'2-output.txt' using 1:3 with lines title 'Theory', '3-output.txt' using 1:3 | '2-output.txt' using 1:3 with lines title 'Theory', '3-output.txt' using 1:3 | ||
with lines title 'Theory' | with lines title 'Theory' | ||
+ | | | ||
+ | |} | ||
==Parallel Matrix Multiplication== | ==Parallel Matrix Multiplication== | ||
− | * Consider matrix | + | {| |
− | operations, where | + | |- valign="top" |
− | matrix is | + | | |
− | decomposed onto | + | * Consider matrix operations, where matrix is decomposed onto images in blocks |
− | images in blocks | + | * A given image has corresponding blocks in A, B, responsible for that block in C. |
− | * A given image has | ||
− | corresponding blocks | ||
− | in A, B, responsible | ||
− | for that block in C. | ||
A | A | ||
Line 2,065: | Line 2,294: | ||
C | C | ||
= | = | ||
+ | | | ||
+ | |} | ||
==Parallel Matrix Multiplication== | ==Parallel Matrix Multiplication== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Block matrix multiply exactly like matrix multiply, except each operation is a submatrix operation | * Block matrix multiply exactly like matrix multiply, except each operation is a submatrix operation | ||
* matmul instead of “*”. | * matmul instead of “*”. | ||
Line 2,077: | Line 2,311: | ||
C | C | ||
= | = | ||
+ | | | ||
+ | |} | ||
==Parallel Matrix Multiplication== | ==Parallel Matrix Multiplication== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Each image gets its own block of a, b, c. | * Each image gets its own block of a, b, c. | ||
* Note [nrows,*]. Coarrays can have any corank(eg, number of codimensions) | * Note [nrows,*]. Coarrays can have any corank(eg, number of codimensions) | ||
Line 2,085: | Line 2,324: | ||
samples/coarrays/blockmatrix.f90 | samples/coarrays/blockmatrix.f90 | ||
+ | | | ||
+ | |} | ||
==Parallel Matrix Multiplication== | ==Parallel Matrix Multiplication== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Allocation, deallocation of coarrays is like a sync all | * Allocation, deallocation of coarrays is like a sync all | ||
* Forces synchronization across all images | * Forces synchronization across all images | ||
Line 2,092: | Line 2,336: | ||
samples/coarrays/blockmatrix.f90 | samples/coarrays/blockmatrix.f90 | ||
+ | | | ||
+ | |} | ||
==Parallel Matrix Multiplication== | ==Parallel Matrix Multiplication== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* This is the entire parallel multiplication operation. | * This is the entire parallel multiplication operation. | ||
* a[myrow,k] gets the entire a block from image at myrow,k. | * a[myrow,k] gets the entire a block from image at myrow,k. | ||
Line 2,099: | Line 2,348: | ||
samples/coarrays/blockmatrix.f90 | samples/coarrays/blockmatrix.f90 | ||
+ | | | ||
+ | |} | ||
==Coarray Summary== | ==Coarray Summary== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Coarrays are powerful, simple-to-use addition to parallel programming models available | * Coarrays are powerful, simple-to-use addition to parallel programming models available | ||
* Will be widely available soon | * Will be widely available soon | ||
Line 2,106: | Line 2,360: | ||
* Typically implemented with MPI under the hood; can give performance similar to MPI with fewer lines of code. | * Typically implemented with MPI under the hood; can give performance similar to MPI with fewer lines of code. | ||
* Other languages have similar extensions (UPC based on C, Titanium based on Java) but are not part of languages’ standard. | * Other languages have similar extensions (UPC based on C, Titanium based on Java) but are not part of languages’ standard. | ||
+ | | | ||
+ | |} | ||
==Closing Hints== | ==Closing Hints== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* Always give the compiler info it needs to help you by being as explicit as possible | * Always give the compiler info it needs to help you by being as explicit as possible | ||
* implicit none, end [construct] [name], parameters for constants, intent in/out, use only, etc. | * implicit none, end [construct] [name], parameters for constants, intent in/out, use only, etc. | ||
* Always get as much info from compiler as possible - always use -Wall (gfortran) or -warn all (ifort). | * Always get as much info from compiler as possible - always use -Wall (gfortran) or -warn all (ifort). | ||
+ | | | ||
+ | |} | ||
==Useful Resources== | ==Useful Resources== | ||
+ | {| | ||
+ | |- valign="top" | ||
+ | | | ||
* http://fortranwiki.org/ | * http://fortranwiki.org/ | ||
** Reference source; has all standards; Fortran2003/2008 status of major compilers | ** Reference source; has all standards; Fortran2003/2008 status of major compilers | ||
Line 2,119: | Line 2,383: | ||
* http://stackoverflow.com/questions/tagged/fortran | * http://stackoverflow.com/questions/tagged/fortran | ||
** ( Programmers Questions & Answers | ** ( Programmers Questions & Answers | ||
+ | |} |
Revision as of 10:03, 14 August 2012
Modern Fortran for FORTRAN77 users
Jonathan Dursi
This is the Wiki-fied version of the slides used for the Modern Fortran course at SciNet. To follow along, with both the examples and the hands on, you will want to download the source code and make sure you have a working Fortran 2003 compiler.
Course Overview
|
A Brief History of Fortran
- Only major compiled programming language designed specifically for scientific programming.
- Powerful array operations; many mathematical functions (Bessel functions!) built in; designed to enable compiler optimizations for fast code
- Oldest (54-57 yrs) still-used programming language.
- Most people come to Fortran via being given old code by someone.
- Can’t understand the old code, or quirks of modern language, without understanding it’s history
- 1957 - J.W. Backus et al. In Proceedings Western Joint Computer Conference, Los Angeles, California, February 1957.
- IBM 704
- (Arguably) first modern compiled programming language.
- Idea of compilers at all was controversial at time.
FORTRAN (1957)
|
Incremental changes
|
FORTRAN66
|
FORTRAN77
- The most common to see “in the wild” of old code today
- if/else/endif, better do loops, control of implicit typing
- Character strings, saved variables, IO improvements
- Approved in 1978, beginning long tradition of “optimistic” naming of standards by year.
The interregnum
- Programming languages and techniques were moving quite quickly
- Several attempts were made to make new version, but standardization process very slow, failed repeatedly.
- Absent new real standard, implementations began to grow in many different directions
- Some extensions became quasi-standard, many were peculiar to individual compilers.
Fortran90
- Enormous changes; the basis of modern Fortran (not FORTRAN!)
- Free form, array slices, modules, dynamic memory allocation, derived types...
- Changes so major that took several years for compilers to catch up.
- Modern fortran
And since...
- Fortran95 - modest changes to Fortran90, killed off some deprecated F77 constructs.
- Fortran 2003 - bigger change; object-oriented, C interoperability. Most compilers have pretty good F2003 support.
- Fortran 2008 - mostly minor changes, with one big addition (coarray), other parallel stuff. Compiler-writers getting there.
F90, F95, F2003, F2008..
- We won’t distinguish between versions; we’ll just show you a lot of useful features of modern fortran.
- Will only show widely-implemented features from 2003 and 8, with exception of coarrays; these are being implemented and are very important.
New Format, New Syntax
Free Format: some highlights
|
<source lang="fortran"> program example implicit none integer, parameter :: npts = 10000 real, parameter :: startx=0., endx=1. real, parameter :: dx = (endx-startx)/npts real :: integral, xleft, xright, xmid integer :: i if (npts < 2) then print *,'Too few points!' else integral = 0. ! Simpson's Rule xleft = 0. int: do i=0,npts-1 xright = (i+1)*dx xmid = (xleft+xright)/2. integral = integral + (dx/6.)*(f(xleft) + 4.*f(xmid) + & f(xright)) xleft = xright end do int print *,'Numerical integral is ', integral print *,'Exact soln is ', (endx-startx)/2. - & (sin(2*endx)-sin(2*startx))/4. endif
function f(x) implicit none real :: f real, intent(in) :: x f = sin(x)**2 end function f end program example </source> |
Variable Declarations
<source lang="fortran"> integer i parameter (i=5) </source>
|
<source lang="fortran"> implicit none integer, parameter :: npts = 10000 real, parameter :: startx=0., endx=1. real, parameter :: dx = (endx-startx)/npts real :: integral, xleft, xright, xmid integer :: i </source> |
Variable Initialization
|
<source lang="fortran"> |
...
subroutine testvarinit implicit none integer :: i = 5 print '(A,I3)', 'On entry; i = ', i i = 7 print '(A,I3)', 'Now set; i = ', i end subroutine testvarinit |
...
use inittest |
...
call testvarinit call testvarinit |
...
end program initialization </source> ( From samples/variables/initialization/initialization.f90) <source lang="bash"> $ gfortran initialization.f90 -o initialization $ ./initialization On entry; i = 5 Now set; i = 7 On entry; i = 7 Now set; i = 7 </source> |
---|
Real Kinds
|
<source lang="fortran"> program realkinds use iso_fortran_env implicit none real :: x real(kind=real32) :: x32 real(kind=real64) :: x64 real(kind=real128):: x128 real(kind=selected_real_kind(6)) :: y6 real(kind=selected_real_kind(15)):: y15 print *,'Default:' print *, precision(x), range(x) print *,'Real32:' print *, precision(x32), range(x32) print *,'Real64:' print *, precision(x64), range(x64) print *,'Real128:' print *, precision(x128), range(x128) print *, print *,'Selected Real Kind 6:' print *, precision(y6), range(y6) print *,'Selected Real Kind 15:' print *, precision(y15), range(y15) end program realkinds </source> (from samples/variables/kinds/realkinds.f90) <source lang="bash"> $ ./realkinds Default: 6 37 Real32: 6 37 Real64: 15 307 Real128: 18 4931 Selected Real Kind 6: 6 37 Selected Real Kind 15: 15 307 </source> |
Integer Kinds
|
<source lang="fortran"> program integerkinds use iso_fortran_env implicit none integer :: i integer(kind=int8) :: i8 integer(kind=int16) :: i16 integer(kind=int32) :: i32 integer(kind=int64) :: i64 integer(kind=selected_int_kind(6)) :: j6 integer(kind=selected_int_kind(15)):: j15 print *,'Default:' print *, huge(i) print *,'Int8:' print *, huge(i8) print *,'Int16:' print *, huge(i16) print *,'Int32:' print *, huge(i32) print *,'Int64:' print *, huge(i64) print *, print *,'Selected Integer Kind 6:' print *, huge(j6) print *,'Selected Integer Kind 15:' print *, huge(j15) end program integerkinds </source> (from samples/variables/kinds/intkinds.f90) <source lang="bash"> $ ./intkinds Default: 2147483647 Int8: 127 Int16: 32767 Int32: 2147483647 Int64: 9223372036854775807 Selected Integer Kind 6: 2147483647 Selected Integer Kind 15: 9223372036854775807 </source> |
Strings
|
<source lang="fortran"> program strings implicit none character(len=20) :: hello character(len=20) :: world character(len=30) :: helloworld hello = "Hello" world = "World!" helloworld = trim(hello) // " " // trim(world) print *, helloworld if (hello < world) then print *, '<', hello, '> is smaller.' else print *, '<', world, '> is larger.' endif end program strings </source> (from samples/variables/strings/strings.f90) <source lang="bash"> $ ./strings Hello World! <Hello > is smaller. </source> |
Array declarations
|
<source lang="fortran"> program arrays implicit none real, dimension(3) :: x, y x = [1,2,3] y = 2*x print *, x print *, y end program arrays </source> ( from samples/variables/arrays/arrays.f90) <source lang="bash"> $ ./arrays 1.0000000 2.0000000 3.0000000 2.0000000 4.0000000 6.0000000 </source> |
Do loops
|
<source lang="fortran"> program doi implicit none integer :: i do i=1,10 print *, i, i**2, i**3 enddo end program doi </source> ( from samples/variables/doloops/doi.f90 ) <source lang="bash"> $ ./doi 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 </source> |
Named loops
|
<source lang="fortran"> program nameddo implicit none integer :: i, j outer: do i=1,3 inner: do j=1,3 print *, i, j, i*i+j*j enddo inner end do outer end program nameddo </source> ( from samples/variables/doloops/nameddo.f90 ) <source lang="bash"> $ ./nameddo 1 1 2 1 2 5 1 3 10 2 1 5 2 2 8 2 3 13 3 1 10 3 2 13 3 3 18 </source> |
Cycle/exit
|
<source lang="fortran"> program cycleexit implicit none integer :: i do print *, 'Enter a number between 1-13' read *, i if (i>=1 .and. i<=13) exit print *, 'Wrong; try again.' enddo print *, 'Good; you entered ', i end program cycleexit </source> ( from samples/variables/doloops/cycleexit.f90 ) <source lang="bash"> $ more cycleexit-out.txt $ ./cycleexit Enter a number between 1-13 23 Wrong; try again. Enter a number between 1-13 -1 Wrong; try again. Enter a number between 1-13 12 Good; you entered 12 </source> |
Do while
|
<source lang="fortran"> program dowhile implicit none integer :: i i = -1 do while (i < 1 .or. i > 13) print *, 'Enter a number between 1-13' read *, i if (i<1 .or. i>13) print *, 'Wrong; try again.' enddo print *, 'Good; you entered ', i end program dowhile </source> ( from samples/variables/doloops/dowhile.f90 ) |
Hands On 1
- In workedexample/f77 is a simplified, F77ized version of a fluid-dynamics code from Ue-Li Pen, CITA, U of Toronto (http://www.cita.utoronto.ca/~pen/MHD/)
- For the purposes of this class, we've turned it from a perfectly good f90 code to something that looks more like something your supervisor would dust off and give to you.
- Today we’ll be translating this version into a very modern Fortran
- Compile (using make) and run (./hydro)
- Outputs a .pbm file; use “display dens.pbm” to see the result of dense blob of fluid moving through a light medium.
- In workedexamples/freeform, have partly converted the program to new freeform format, with enddos, ending procedures, implicit nones, and new variable declaration syntax.
- Finish doing so - just need to do program hydro, subroutine color, subroutine outputpbm, function cfl. Fix indenting (Don't need to start at col 7 anymore).
- ~1 hr (for getting logged in and everything working)
Procedures and Modules
|
Modules
|
<source lang="fortran"> module gravity implicit none real, parameter :: G = 6.67e-11 ! MKS units contains real function gravforce(x1,x2,m1,m2) implicit none real, dimension(3), intent(in) :: x1,x2 real, intent(in) :: m1, m2 real :: dist dist = sqrt(sum((x1-x2)**2)) gravforce = G * m1 * m2 / dist**2 end function gravforce end module gravity program simplemod use gravity implicit none print *, 'Gravitational constant = ', G print *, 'Force between 2 1kg masses at [1,0,0] & &and [0,0,1] is' print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.) end program simplemod </source> (from samples/procedures/simplemod/simplemod.f90) |
Compiling & Running
|
<source lang="bash"> $ ls simplemod.f90 $ gfortran -o simplemod simplemod.f90 -Wall $ ls gravity.mod simplemod simplemod.f90 $ ./simplemod Gravitational constant = 6.6700000E-11 Force between 2 1kg masses at [1,0,0] and [0,0,1] is 3.3350003E-11 </source> |
Modules
|
<source lang="fortran"> <source lang="fortran"> module gravity implicit none real, parameter :: G = 6.67e-11 ! MKS units contains real function gravforce(x1,x2,m1,m2) implicit none real, dimension(3), intent(in) :: x1,x2 real, intent(in) :: m1, m2 real :: dist dist = sqrt(sum((x1-x2)**2)) gravforce = G * m1 * m2 / dist**2 end function gravforce end module gravity program simplemod use gravity implicit none print *, 'Gravitational constant = ', G print *, 'Force between 2 1kg masses at [1,0,0] & &and [0,0,1] is' print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.) end program simplemod </source> (from samples/procedures/simplemod/simplemod.f90) |
use module, only :
- Best practice is to only pull in from the module what you need
- Otherwise, everything.
- Adds some clarity and documentation, good for maintainability
- (Note syntax for continuation of a string...)
<source lang="fortran"> module gravity implicit none real, parameter :: G = 6.67e-11 ! MKS units contains real function gravforce(x1,x2,m1,m2) implicit none real, dimension(3), intent(in) :: x1,x2 real, intent(in) :: m1, m2 real :: dist dist = sqrt(sum((x1-x2)**2)) gravforce = G * m1 * m2 / dist**2 end function gravforce end module gravity program simplemod2 use gravity, only : G, gravforce implicit none print *, 'Gravitational constant = ', G print *, 'Force between 2 1kg masses at [1,0,0] & &and [0,0,1] is' print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.) end program simplemod2 </source> samples/procedures/simplemod/simplemod2.f90 |
Modules usually get their own files
|
<source lang="fortran"> module gravity implicit none private character (len=8), parameter, public :: massunit="kilogram" character (len=8), parameter, public :: forceunit="Newton" public :: gravforce real, parameter :: G = 6.67e-11 ! MKS units contains real function distance(x1,x2) implicit none real, dimension(3), intent(in) :: x1, x2 distance = sqrt(sum((x1-x2)**2)) end function distance real function gravforce(x1,x2,m1,m2) implicit none real, dimension(3), intent(in) :: x1,x2 real, intent(in) :: m1, m2 real :: dist dist = distance(x1,x2) gravforce = G * m1 * m2 / dist**2 end function gravforce end module gravity </source> (from samples/procedures/multifilemod/gravity.f90 ) |
Modules usually get their own files
|
<source lang="make"> FC=gfortran FFLAGS=-O3 -Wall multifilemod: multifilemod.o gravity.o $(FC) -o $@ multifilemod.o gravity.o %.mod: %.f90 $(FC) $(FFLAGS) -c $< multifilemod.o: multifilemod.f90 gravity.mod $(FC) $(FFLAGS) -c $< clean: rm -f *.o *~ *.mod multifilemod </source> (from samples/procedures/multifilemod/Makefile) |
.mod needed for compilation
|
<source lang="fortran"> program simplemod2 use gravity, only : gravforce, massunit, forceunit implicit none print *, 'Force between 2 1 ', massunit ,' masses ', & ' at [1,0,0] and [0,0,1] is' print *, gravforce([1.,0.,0.],[0.,0.,1.],1.,1.), forceunit end program simplemod2 </source> (from samples/procedures/multifilemod/multifilemod.f90) |
.o needed for linking
|
<source lang="make"> FC=gfortran FFLAGS=-O3 -Wall multifilemod: multifilemod.o gravity.o $(FC) -o $@ multifilemod.o gravity.o %.mod: %.f90 $(FC) $(FFLAGS) -c $< multifilemod.o: multifilemod.f90 gravity.mod $(FC) $(FFLAGS) -c $< clean: rm -f *.o *~ *.mod multifilemod </source> (from samples/procedures/multifilemod/Makefile) |
Compiling and running
|
<source lang="bash"> $ make gfortran -O3 -Wall -c gravity.f90 gfortran -O3 -Wall -c multifilemod.f90 gfortran -o multifilemod multifilemod.o gravity.o reposado-$ ./multifilemod Force between 2 1 kilogram masses at [1,0,0] and [0,0,1] is 3.33500033E-11 Newton </source> |
Private and public
|
<source lang="fortran"> module gravity implicit none private character (len=8), parameter, public :: massunit="kilogram" character (len=8), parameter, public :: forceunit="Newton" public :: gravforce real, parameter :: G = 6.67e-11 ! MKS units contains real function distance(x1,x2) implicit none real, dimension(3), intent(in) :: x1, x2 distance = sqrt(sum((x1-x2)**2)) end function distance real function gravforce(x1,x2,m1,m2) implicit none real, dimension(3), intent(in) :: x1,x2 real, intent(in) :: m1, m2 real :: dist dist = distance(x1,x2) gravforce = G * m1 * m2 / dist**2 end function gravforce end module gravity </source> ( from samples/procedures/multifilemod/gravity.f90 ) |
Procedures
|
<source lang="fortran"> module procedures contains function square(x) result(xsquared) implicit none real :: xsquared real, intent(IN) :: x xsquared = x*x end function square function cube(x) implicit none real :: cube real, intent(IN) :: x cube = x*x*x end function cube subroutine squareAndCube(x, squarex, cubex) implicit none real, intent(in) :: x real, intent(out) :: squarex real, intent(out) :: cubex squarex = square(x) cubex = cube(x) end subroutine squareAndCube end module procedures </source> ( from samples/procedures/funcsub/procedures.f90 ) |
Functions
|
<source lang="fortran"> function square(x) result(xsquared) implicit none real :: xsquared real, intent(IN) :: x xsquared = x*x end function square function cube(x) implicit none real :: cube real, intent(IN) :: x cube = x*x*x end function cube subroutine squareAndCube(x, squarex, cubex) implicit none real, intent(in) :: x real, intent(out) :: squarex real, intent(out) :: cubex squarex = square(x) cubex = cube(x) end subroutine squareAndCube </source> ( from samples/procedures/funcsub/procedures.f90 ) |
Procedure interfaces
|
<source lang="fortran"> function square(x) result(xsquared) implicit none real :: xsquared real, intent(IN) :: x xsquared = x*x end function square function cube(x) implicit none real :: cube real, intent(IN) :: x cube = x*x*x end function cube subroutine squareAndCube(x, squarex, cubex) implicit none real, intent(in) :: x real, intent(out) :: squarex real, intent(out) :: cubex squarex = square(x) cubex = cube(x) end subroutine squareAndCube </source> ( from samples/procedures/funcsub/procedures.f90 ) |
Procedure interfaces
|
http://en.wikipedia.org/wiki/File:Trapezoidal_rule_illustration_small.svg |
Procedure interfaces
|
(from samples/procedures/interface/integrate.f90 ) |
Recursive procedures
|
( from samples/procedures/recursive/integrate.f90) |
Pure procedures
|
(from samples/procedures/purity/purity.f90) |
Optional Arguments
|
(from samples/procedures/optional/integrate.f90) |
Keyword Arguments
|
(from samples/procedures/optional/optional.f90) |
Procedures and Modules Summary
|
Hands On 2
|
Fortran arrays
|
Array constructors
|
[1,2,3,4,5] or (/1,2,3,4,5/) [ (i,i=1,5)] [ ((i*j,j=1,3),i=1,5)] |
Elementwise operations
|
samples/arrays/elementwise.f90 |
Elemental Functions
|
samples/arrays/elemental.f90 |
Array comparisons
|
samples/arrays/compare.f90 |
Array masks
|
samples/arrays/mask.f90 |
Where construct
|
samples/arrays/where.f90 |
Forall construct
|
samples/arrays/forall.f90 |
Array Sections
|
<source lang="fortran"> a([start]:[end][:step]) a = [1,2,3,4,5,6,7,8,9,10] a(7:) == [7,8,9,10] a(:3) == [1,2,3] a(2:4) == [2,3,4] a(::3) == [1,4,7,10] a(2:4:2) == [2,4] a(2) == 2 a(:) == [1,2,3,4,5,6,7,8,9,10] </source> |
Array Sections
|
samples/arrays/derivative.f90 |
Array Sections
|
<source lang="fortran"> a = [1,2,3,4,5] cshift(a,1) == [2,3,4,5,1] cshift(a,-1) == [5,1,2,3,4] eoshift(a,1) ==[2,3,4,5,0] eoshift(a,-1)==[0,1,2,3,4] </source> |
Other important array intrinsics
|
<source lang="fortran"> 1,4 reshape([1,2,3,4,5,6],[3,2]) == 2,5 3,6 </source> |
Linear algebra in Fortran
|
Matmul times
<source lang="bash"> $ ./matmul 2500 Experiment with matrix size 2500 Triple-loop time: 149.63400 matmul intrinsic time: 10.370000 SGEMM lapack call time: 1.4809999 </source> (gfortran 4.6, compiled -O3 -march=native using Intel MKL 10.3 for sgemm) |
samples/arrays/matmul.f90 |
Linear algebra in Fortran
samples/arrays/matrix.f90 |
Array sizes and Assumed Shape
|
samples/arrays/matrix.f90 |
Array sizes and Assumed Shape
integer nx, ny integer a(nx,ny) or worse, integer a(*,ny)
|
samples/arrays/matrix.f90 |
Allocatable Arrays
|
Allocate(), Deallocate()
|
samples/arrays/allocatable.f90 |
Allocate(), Deallocate()
|
get_command_argument()
|
samples/arrays/allocatable2.f90 |
get_command_argument()
samples/arrays/allocatable2.f90 |
Hands on #3
|
Fortran Pointers
|
<source lang="fortran"> real, target :: x = 3.2 real, pointer:: p p => x p x 3.2 </source> samples/pointers/ptr1.f90 |
Fortran Pointers
|
<source lang="fortran"> real, target :: x = 3.2 real, pointer:: p p => null() p x </source> |
Fortran Pointers
|
<source lang="fortran"> real, target :: x = 3.2 real, pointer:: p1, p2 p1 => x p2 => p1 p1 p2 x 3.2 </source> |
Allocating a pointer
|
<source lang="fortran"> real, pointer:: p allocate(p) p = 7.9 p 7.9 |
</source> samples/pointers/ptr2.f90 |
What are they good for? (1)
|
http://en.wikipedia.org/wiki/File:Singly-linked-list.svg http://en.wikipedia.org/wiki/File:Max-Heap.svg |
What are they good for? (2)
|
<source lang="fortran"> real, target, dimension(7) :: x real, pointer:: p(:) p => x(2:6) p x 1 2 3 4 5 6 7 </source> samples/pointers/views.f90 |
Hands on #4
|
Derived Types and Objects
<source lang="fortran"> type griddomain real :: xmin, xmax real :: ymin, ymax real :: nx, ny real, dimension(:,:) :: u endtype griddomain type(griddomain) :: g g % xmin = -1 g % xmax = +1 </source> |
Derived Types and Objects
|
(from samples/derivedtypes/simple/intervalmath.f90 ) |
Procedures using types
|
(from samples/derivedtypes/intervalfunctions/intervalmath.f90 ) |
Procedures using types
|
samples/derivedtypes/genericintervals/interval2.f90 |
Generic Interfaces
Generic Interfaces
Generic interfaces
Operator overloading
Operator overloading
Generic interfaces
Type bound procedures
Type bound procedures
Type bound procedures
Object oriented programming
Interoperability with other languages
C-interoperability
Calling a C routine from Fortran
Calling a C routine from Fortran
Calling a C routine from Fortran
C strings
Calling Fortran from C
Calling Fortran from C
More advanced
Fortran calling C, which calls Fortran
samples/C-interop/valueref/croutine.csamples/C-interop/valueref/froutine.f90$ ./main
samples/C-interop/valueref/MakefileF2py
* Will only use solver module.
* generates the following header file (hydro_solver.pyf)
$ ipython
Coarrays
Coarrays
Coarrays
Sychronization
Sychronization
1d Diffusion Eqn
1d Diffusion Eqn
1d Diffusion Eqn
1d Diffusion Eqn
$ ./diffusion
Coarray Diffusion
Coarray Diffusion
Coarray Diffusion
Coarray Diffusion
Coarray Diffusion
$ export FOR_COARRAY_NUM_IMAGES=3
gnuplot> plot '1-output.txt' using 1:2, '2-output.txt' using 1:2, '3output.txt' using 1:2, '1-output.txt' using 1:3 with lines title 'Theory',
Parallel Matrix Multiplication
Parallel Matrix Multiplication
Parallel Matrix Multiplication
Parallel Matrix Multiplication
Parallel Matrix Multiplication
Coarray Summary
Closing Hints
Useful Resources
|