
Some training course practicalities Each lecture is about 45min – 1h Between lectures usually an exercise session, ca. 1h A training day ends at always 4pm Lunch breaks at noon with lectures/exercises continuing at 1pm Prerequisites: good knowledge of Fortran Introduction to Advanced Fortran and Unix/Linux Exercises will be carried out using CSC’s Cray XC40 (Sisu) supercomputer 1 2 Introduction to Advanced Fortran A brief overview of this 3-day training course A brief overview of the contents of this training course Advanced Fortran concentrates on F2003/F2008 aspects How do we teach? – some Fortran95 less well known features are covered, too – An example session with “Operator overloading” Our major F2003/F2008 subjects in this course are A quick history of Fortran standards over the last decades – Object Oriented Fortran (OOF) – From FORTRAN66 to Fortran 2008 – Co-Array Fortran (CAF) Parallel processing aspects that often improve program Minor subjects cover performance: paving way to High Performance Computing – Developments in ALLOCATABLE and POINTER variables – Co-Array Fortran (CAF) is part of F2008 standard – Interfacing with C—programs / libraries (ISO C bindings) § With a fun demo-session on explicit dynamic linking 3 4 How do we teach? An example session: “Operator overloading” We will try to make potentially difficult things look To improve readability of Fortran source code it is relatively easy to learn and understand possible to overload existing operators – or even create your own ones (not covered here) We tend to skip items that – we think – have less significance in everyday modern Fortran programming Operator overloading applies to the mathematical operations like ‘+’, ‘-’, ‘*’, ‘/’ and assignment ‘=’ A typical lecture contains many smaller subjects, which Binary operators involve two operands – residing in the often contain the syntactical point of view followed by left and right hand sides (LHS & RHS) of the operator simplified code snippets – Unary operator (e.g. minus sign) only has one operator An exercise session followed by lecture session, allow (RHS) students to practice with the just learned stuff Typical operator overloading usages are found in the field of vector and matrix computation 5 6 Operator overloading ... Overloading summation operator ‘+’ for vectors A practical implementation of operator overloading Whilst it is usually easier to code this directly without any usually involve creating a Fortran MODULE –file where all extra code for implementing operator overloading, it is the necessary components are placed pedagogically a useful case to go through here A MODULE contains derived data TYPE definition of the To start with, lets introduce a simple vector type: TYPE vector_t associated elements and creation of appropriate REAL, allocatable :: v(:) INTERFACE blocks to enable compiler to map the END TYPE vector_t references to the new operators In a more generalized case this TYPE may contain many Operator overloading can optionally be implemented to more components than shown here cover several (intrinsic) data types, or a mixture of types – We may also want to sum over mixture of various REAL precisions (“kinds”) and/or perhaps over INTEGERs, too 7 8 Overloading summation operator ‘+’... Overloading summation operator ‘+’ ... MODULE overload TYPE vector_t All the necessary code fractions The next step is to introduce a summation function, real, allocatable :: x(:) are now placed in a MODULE –file END TYPE vector_t … and referenced from the user which does the actual adding up of two vectors INTERFACE operator(+) MODULE PROCEDURE add source code, e.g. : – We need to introduce the summation OPERATOR itself END INTERFACE INTERFACE assignment(=) PROGRAM main and INTERFACE blocks to enable the access MODULE PROCEDURE asgn_int USE overload END INTERFACE TYPE(vector_t) :: x1, x2, out We also would like have a flexible initialization of the CONTAINS FUNCTION add(v1, v2) x1 = [ 1, 2, 3 ]; vectors to be summed up – we take an opportunity to TYPE(vector_t) :: add x2 = [ 10, 20, 30 ]; TYPE(vector_t), intent(in) :: v1, v2 out = x1 + x2 introduce an assignment OPERATOR, too add%x = v1%x + v2%x print '(a,3(1x,g0))','out=',out%x END FUNCTION add END PROGRAM main – In the example our initial values are all INTEGERs, so we SUBROUTINE asgn_int(out,in) TYPE(vector_t), intent(out) :: out only need to map them to our vector TYPE INTEGER, intent(in) :: in(:) The output is: out%x = in out= 11.00000 22.00000 33.00000 END SUBROUTINE asgn_int END MODULE overload 9 10 A bit of Fortran history Evolution of Fortran standards over the few decades As many know the name “Fortran” stands for “Formula Mid-60’s the most significant version was labeled as ANSI translating system” standard, called FORTRAN66 (“Fortran” in capital letters J) – The 1st version became standard by John Backus (IBM/1957) In 1978 the former “de-facto” FORTRAN77 standard was Well suited for numerical and scientific computing approved and quickly became popular, contained e.g.: – Often emphasis in HPC – High Performance Computing – IMPLICIT statement A language for computationally intensive areas like – IF – THEN – ELSE IF – ELSE – ENDIF – Computational physics & computational chemistry – CHARACTER data type – Finite element analysis & computational fluid dynamics – PARAMETER statement for specifying constants – Numerical weather prediction & climate research – Generic names for intrinsic functions 11 12 Evolution of Fortran standards… Fortran90 An important extension to FORTRAN77 was release of A major step in keeping Fortran programming alive was “military standard” Fortran enhancements (also in 1978) introduction of Fortran90 standard in ’91 (ISO), ’92 (ANSI) by US DoD, and adopted by most FORTRAN77 compilers – Free format source input (up to 132 characters per line) – IMPLICIT NONE – Dynamic memory handling via – DO – ENDDO § ALLOCATE / DEALLOCATE for ALLOCATABLE –arrays § Automatic array sizing in procedures from arguments / modules – INCLUDE statement – Bit-manipulation functions Marked a number of features obsolescent, but did not delete any features – yet, e.g.: All these were eventually incorporated to the next major – Arithmetic IF and computed GOTO –statements official standard release – Fortran90 – Alternate RETURN, and use of H(ollerith) in FORMAT –statements 13 14 Fortran95 Fortran 2003 (F2003) A minor revision in 1997 (ISO) some features were taken A significant revision of the Fortran95 standard (in 2004) from High Performance Fortran (HPF) specification, e.g. – Parameterized derived TYPEs – FORALL and nested WHERE clauses to improve vectorization – Object-Oriented Programming (OOP), e.g. – User-defined PURE and ELEMENTAL procedures § Type extension, (single) inheritance, polymorphism, … – Automatic DEALLOCATE when ALLOCATABLE out of scope – Procedure POINTERs – POINTER and TYPE components default initializations – Interoperability with C (and other) languages Deleted some features previously marked as obsolescent – O/S interfacing: command line arguments & env. variables – – Use of non-INTEGERs as DO-loop parameters ALLOCATABLE improvements – – H(ollerith) edit descriptor in FORMATs POINTER improvements – – ASSIGN and assigned GOTO I0 (eye-zero) edit descriptor for INTEGERs in FORMAT statements 15 16 Fortran 2008 (F2008) Recap A minor revision of Fortran 2003 – in 2010 The Fortran standard has evolved from its early days, late – Certain new features still not fully supported by MOST 1950s, and contains many modern language features, like compiler makers L Object Oriented Programming (OOP – with a twist “called” New features include OOF) as well as Co-Array Fortran (CAF) to express (massive) – Co-arrays parallelism – Sub-modules Interfacing with C-language is now standardized, so is the – CONTIGUOUS –attribute interface with operating system (command line arguments, – BLOCK –construct obtaining environment variables and running Unix- – newunit= in OPEN –statement commands from Fortran) – DO CONCURRENT –construct Developments in ALLOCATABLE and POINTER variables are – G0 edit descriptor for REALs in FORMAT statements also noticeable improvements 17 18 Useful features since Fortran90 Interfacing the operating system – Accessing command line arguments and environment variables – Running operating system commands from Fortran Enhancements in use of ALLOCATABLE –variables – Derived data type can have ALLOCATABLE components – Automatic DEALLOCATE when variable goes out of scope – ALLOCATABLEs as dummy arguments and function can return ALLOCATABLEs – Automatic resizing of ALLOCATABLE variables – ALLOCATABLE scalars: variable length CHARACTER strings – Two essential POINTER assignment improvements Useful new features CONTIGUOUS attribute Asynchronous I/O I0 and G0 edit descriptors allow dynamic output FORMATting 19 20 Command line arguments Command line arguments … Parameters to a program are very often given to programs as Access separate command line arguments command line arguments call get_command_argument(number[,value][,length][,status]) – Input file(s), modify program behavior, etc. – number is of type integer and denotes which argument to get – value is of type character string and contains the value of the requested Fortran 2003 has (finally!) a standardized method for reading argument on return. If the actual argument is too short or long it is padded command line arguments with blanks or truncated, respectively (optional) – No need to use extensions such as GETARG and IARGC – length is of type integer and contains the length
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages68 Page
-
File Size-