
Fortran code modernization Dr. Reinhold Bader Leibniz Supercomputing Centre This work is licensed under the Creative Commons Attribution Non-Commercial 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ When attributing this work, please use the following text block: Fortran code modernization, Leibniz Supercomputing Centre, 2018. Available under a Creative Commons Attribution Non-Commercial 3.0 Unported License. Workshop's aims correlations and speed of anticorrelations exist execution Need: 1. experience 2. compromise 3. intelligence 4. diligence Improve Performance (not necessarily in that order) (Time to solution) speed of development speed of build process process © 2015-18 LRZ Modernizing Fortran Legacy Codes 2 How can the aims be achieved? • replace obsolescent / unsuitable features by modern ones language features • follow best practices in using advanced features correctness of code contributes to development speed • edit, document tools • build, debug, profile, tune • I/O processing and its design data handling • visualization • scalability in multiple facets parallelism • proper choice of programming model • reduce problem complexity order while maintaining algorithms efficiency of execution expect tradeoff © 2015-18 LRZ Modernizing Fortran Legacy Codes 3 Assumptions on Audience Good working knowledge of Fortran 77 semantics Knowledge about the most relevant Fortran 90/95 concepts modules, array processing, dynamic memory Basic experience with C programming Basic experience with parallel programming using OpenMP, MPI or both Useful: some conceptual knowledge about object-oriented programming (single inheritance, virtual methods, interface classes) © 2015-18 LRZ Modernizing Fortran Legacy Codes 4 Assumptions on pre-existing code Language features are used that date from Fortran 77 or earlier were never standardized, but are supported in many compilers How you proceed depends on the specifics of code reuse: run without (or at most minor isolated) modifications as a standalone program → no refactoring required „never change a running system“ + Fortran (mostly) backward compatible use as library facility → no full refactoring may be needed, but it is likely desirable to create explicit interfaces further maintenance (bug fixes with possibly non-obvious effects) or even further development is needed → refactoring is advisable © 2015-18 LRZ Modernizing Fortran Legacy Codes 5 History of Fortran Fortran – the oldest portable Generations of standards programming language Fortran 66 ancient first compiler developed by John Fortran 77 (1980) traditional Backus at IBM (1957-59) Fortran 90 (1991) large revision design target: generate code with speed comparable to Fortran 95 (1997) small revision assembly programming, i.e. Fortran 2003 (2004) large revision for of compiled efficiency Fortran 2008 (2010) mid-size revision executables TS 29113 (2012) extends C interop targeted at scientific / TS 18508 (2015) extends parallelism engineering (high performance) computing Fortran 2018 (2018) next revision Fortran standardization TS Technical Specifications ISO/IEC standard 1539-1 „mini-standards“ targeted for future inclusion (modulo bug-fixes) repeatedly updated © 2015-18 LRZ Modernizing Fortran Legacy Codes 6 Conventions and Flags used in these talks Standards conformance Legacy code Recommend replacement by a more modern feature Recommended practice OBS obsolescent feature Standard conforming, but DEL deleted feature considered questionable Implementation style dependencies Dangerous practice, likely to introduce bugs and/or non- Processor dependent conforming behaviour behaviour (may be unportable) Performance Gotcha! Non-conforming and/or definitely buggy language feature for / against performance © 2015-18 LRZ Modernizing Fortran Legacy Codes 7 Why Fortran? SW engineering aspects Key language features good ratio of learning effort to dynamic (heap) memory productivity management since , much more powerful in good optimizability encapsulation and code reuse compiler correctness checks via modules since object based and object- (constraints and restrictions) oriented features Ecosystem array processing many existing legacy libraries versatile I/O processing existing scientific code bases abstraction features: overloaded may determine what language and user-defined operators to use interoperability with C using tools for diagnosis of FP exception handling correctness problems is parallelism sometimes advisable © 2015-18 LRZ Modernizing Fortran Legacy Codes 8 When not to use Fortran When programming an embedded system these sometimes do not support FP arithmetic implementation of the language may not be available When working in a group/project that uses C++, Java, Eiffel, Haskell, … as their implementation language synergy in group: based on some – usually technically justified – agreement minor exception: library code for which a Fortran interface is desirable – use C interoperability features to generate a wrapper © 2015-18 LRZ Modernizing Fortran Legacy Codes 9 Some references Modern Fortran explained (8th edition incorporates ) Michael Metcalf, John Reid, Malcolm Cohen, OUP, 2018 The Fortran 2003 Handbook J. Adams, W. Brainerd, R. Hendrickson, R. Maine, J. Martin, B. Smith. Springer, 2008 Guide to Fortran 2008 programming (introductory text) W. Brainerd. Springer, 2015 Modern Fortran – Style and Usage (best practices guide) N. Clerman, W. Spector. Cambridge University Press, 2012 Scientific Software Design – The Object-Oriented Way Damian Rouson, Jim Xia, Xiaofeng Xu, Cambridge, 2011 © 2009-18 LRZ Advanced Fortran Topics - LRZ section 10 References cont'd Design Patterns – Elements of Reusable Object-oriented Software E. Gamma, R. Helm, R. Johnson, J. Vlissides. Addison-Wesley, 1994 Modern Fortran in Practice Arjen Markus, Cambridge University Press, 2012 Introduction to High Performance Computing for Scientists and Engineers G. Hager and G. Wellein © 2009-18 LRZ Advanced Fortran Topics - LRZ section 11 Dealing with legacy language features © 2015-18 LRZ Modernizing Fortran Legacy Codes 12 Legacy code: Fixed source form Source code stored in files with extension for use with C-style .f .for .ftn .F preprocessing Layout of code looks something like this C 1 2 3 4 5 6 7 8 *2345678901234567890123456789012345678901234567890123456789012345678901234567890 PROGRAM M Y = 1.0 X = 1.5 X = X + 2.0 +Y IF (X < 4.0) GOTO 20 WRITE(*,*) 'statement with continuation', 1 'line', X C comment line 20 CONTINUE END PROGRAM © 2015-18 LRZ Modernizing Fortran Legacy Codes 13 Technical reason for fixed source form … © 2015-18 LRZ Modernizing Fortran Legacy Codes 14 Legacy code: Fixed source form Fortran 77 (and earlier) language rules for layout: statements must start at column 7 processor extensions to 132 columns exist. Beware unnoticed errors must end at column 72 if compiler option is not toggled continuation line: single non-blank / non-zero character in column 6 limit of 19 continuation lines comment must have the characters C or * in column 1 labels must be in columns 1-5 C 1 2 3 4 5 6 7 8 *2345678901234567890123456789012345678901234567890123456789012345678901234567890 PROGRAM M Y = 1.0 X = 1.5 X = X + 2.0 +Y IF (X < 4.0) GOTO 20 WRITE(*,*) 'statement with continuation', might get ignored if 1 'line', X line length extension C comment line is not toggled 20 CONTINUE END PROGRAM © 2015-18 LRZ Modernizing Fortran Legacy Codes 15 Legacy code: Fixed source form Further pitfall: Insignificance of embedded blanks S = 0.0 C = 0.0 DO 10 I=1.5 AA = 2.0 S = S+I BB = 2.0 10 CONTINUE IF (AA .EQ. BB) THEN C = AA + BB WRITE(*,*) 'S=',S WRITE(*,*) 'C=',C END END Both codes are conforming, but deliver results that might surprise you ... Quiz: Which language feature conspires with the embedded blanks to produce this surprise? © 2015-18 LRZ Modernizing Fortran Legacy Codes 16 The new way: Rules for free source form Program line Comments: upper limit of 132 characters after statement on same line: arbitrary indentation allowed WRITE(*,*) 'Hello' ! produce output Continuation line separate comment line: indicated by ampersand: WRITE(*,fmt=*) & WRITE(*,*) 'Hello' 'Hello' ! produce output variant for split tokens: The art of commenting code: WRITE(*,fmt=*) 'Hel& concise &lo' informative non-redundant upper limit: 255 consistent Multiple statements (maintenance issue) semicolon used as separator File extension .f90 .F90 a = 0.0; b = 0.0; c = 0.0 unrelated to language level © 2015-18 LRZ Modernizing Fortran Legacy Codes 17 Tooling options Open-source software NAG compiler convert tool by Michael supports =polish as an Metcalf option for converting between to_f90 tool by Alan Miller fixed and free format your mileage may vary additional suboptions are further similar tools exist available © 2015-18 LRZ Modernizing Fortran Legacy Codes 18 Implicit and explicit typing of variables (1) If no type declaration Example program: statement appears: PROGRAM declarations without an IMPLICIT REAL :: ip statement, typing of entities xt = 5 ! xt is real is performed implicitly, i = 2.5 ! i is integer ip = 2.5 ! ip is real based on first letter of the WRITE(*,*) x, i, ip variable‘s name: END PROGRAM a,...,h and o,...,z become default real Note: entities newer (scripting) languages i,...,n become default perform auto-typing by integer entities context this is not possible in Fortran © 2015-18 LRZ Modernizing Fortran Legacy Codes 19 Implicit and explicit typing of variables (2) Modify implicit typing scheme IMPLICIT statement:
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages252 Page
-
File Size-