Advanced Fortran Programming

Similar documents
Sami Ilvonen Pekka Manninen. Introduction to High-Performance Computing with Fortran. September 19 20, 2016 CSC IT Center for Science Ltd, Espoo

Advanced Parallel Programming

CSC Summer School in HIGH-PERFORMANCE COMPUTING

CSC Summer School in HIGH-PERFORMANCE COMPUTING

Introduction to C. Sami Ilvonen Petri Nikunen. Oct 6 8, CSC IT Center for Science Ltd, Espoo. int **b1, **b2;

CSC Summer School in HIGH-PERFORMANCE COMPUTING

Performance Engineering: Lab Session

Lecture V: Introduction to parallel programming with Fortran coarrays

OO Fortran Exercises

Domain Decomposition: Computational Fluid Dynamics

Parallel Programming with Fortran Coarrays: Exercises

Fortran classes and data visibility

Domain Decomposition: Computational Fluid Dynamics

Exercise: Calling LAPACK

Domain Decomposition: Computational Fluid Dynamics

Reusing this material

Appendix D. Fortran quick reference

Advanced Threading and Optimization

Student Outcomes. Lesson Notes. Classwork. Opening Exercise (3 minutes)

Technical Report on further interoperability with C

CFD exercise. Regular domain decomposition

Chapter 4. Fortran Arrays

Jussi Enkovaara Martti Louhivuori. Python in High-Performance Computing. CSC IT Center for Science Ltd, Finland January 29-31, 2018

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

PROGRAMMING MODEL EXAMPLES

- HALF YEARLY EXAM ANSWER KEY DEC-2016 COMPUTER SCIENCE ENGLISH MEDIUM

Python in High-Performance Computing

Message-Passing Programming with MPI. Message-Passing Concepts

The G3 F2PY for connecting Python to Fortran 90 programs

Compiling applications for the Cray XC

Programming Languages

More Coarray Features. SC10 Tutorial, November 15 th 2010 Parallel Programming with Coarray Fortran

Hydro-dynamics in CUDA Performance optimizations in MOHID

TS Further Interoperability of Fortran with C WG5/N1917

Technical Specification on further interoperability with C

PACKAGE SPECIFICATION HSL 2013

OUTCOMES BASED LEARNING MATRIX

Fortran Coarrays John Reid, ISO Fortran Convener, JKR Associates and Rutherford Appleton Laboratory

Sharpen Exercise: Using HPC resources and running parallel applications

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

Interfacing With Other Programming Languages Using Cython

Programming in C First meeting

The new features of Fortran 2003

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

ME1107 Computing Y Yan.

Python Scripting for Computational Science

Parallel Programming in Fortran with Coarrays

Co-arrays to be included in the Fortran 2008 Standard

Scientific Programming in C X. More features & Fortran interface

Exercise 6.2 A generic container class

Sharpen Exercise: Using HPC resources and running parallel applications

MPI Case Study. Fabio Affinito. April 24, 2012

Advanced Fortran Topics - Hands On Sessions

Message Passing Programming. Designing MPI Applications

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Page 1 of 7. Date: 1998/05/31 To: WG5 From: J3/interop Subject: Interoperability syntax (Part 1) References: J3/98-132r1, J3/98-139

Fortran 2008: what s in it for high-performance computing

Goals for This Lecture:

Reflection Seismology (SCPY 482) An Introduction to Fortran 90 Programming

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

OpenMP - exercises -

Programming in C week 1 meeting Tiina Niklander

AMath 483/583 Lecture 24. Notes: Notes: Steady state diffusion. Notes: Finite difference method. Outline:

Goals for This Lecture:

AMath 483/583 Lecture 24

Python Scripting for Computational Science


Parallel Programming. Libraries and implementations

ISO/IEC : TECHNICAL CORRIGENDUM 2

Properties of an identifier (and the object it represents) may be set at

Class Information ANNOUCEMENTS

Fortran 2003 and Beyond. Bill Long, Cray Inc. 17-May-2005

Fortran Bill Long, Cray Inc. 21-May Cray Proprietary

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

Practical Exercise 1 Question 1: The Hello World Program Write a Fortran 95 program to write out Hello World on the screen.

Compiling environment

AMath 483/583 Lecture 8

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

ParaFEM Coding Standard for Fortran 90. Contents. 1.0 Introduction. 2.0 Documentation. 2.1 External Documentation

Migrating A Scientific Application from MPI to Coarrays. John Ashby and John Reid HPCx Consortium Rutherford Appleton Laboratory STFC UK

Complex data structures. Cedric Saule

OpenMP - exercises - Paride Dagna. May 2016

Short Notes of CS201

COMP-520 GoLite Tutorial

CS201 - Introduction to Programming Glossary By

CS201- Introduction to Programming Current Quizzes

An Introduction to Fortran

FORTRAN TO PYTHON INTERFACE GENERATOR WITH AN APPLICATION TO AEROSPACE ENGINEERING

NAGWare f95 Recent and Future Developments

Index. classes, 47, 228 coarray examples, 163, 168 copystring, 122 csam, 125 csaxpy, 119 csaxpyval, 120 csyscall, 127 dfetrf,14 dfetrs, 14

Fundamental Concepts and Definitions

Numerical Modelling in Fortran: day 4. Paul Tackley, 2017

An introduction to Fortran. Daniel Price School of Physics and Astronomy Monash University Melbourne, Australia

Overloading, abstract classes, and inheritance

Parallel Programming Patterns Overview and Concepts

Chip Multiprocessors COMP Lecture 9 - OpenMP & MPI

Goals for This Lecture:

Parallel Programming. Libraries and Implementations

Lecture 12 Level Sets & Parametric Transforms. sec & ch. 11 of Machine Vision by Wesley E. Snyder & Hairong Qi

Transcription:

Sami Ilvonen Pekka Manninen Advanced Fortran Programming March 20-22, 2017 PRACE Advanced Training Centre CSC IT Center for Science Ltd, Finland type revector(rk) integer, kind :: rk real(kind=rk), allocatable :: data(:) contains procedure :: sum => vecsum generic :: operator(+) => sum end type revector type, extends(revector) :: imvector real(kind=rk), allocatable :: imdata(:) contains procedure :: sum => imvecsum end type imvector contains function vecsum(x,y) result(z) implicit none class(revector(dp)), intent(in) :: x,y class(revector(dp)), allocatable :: z integer :: i select type(y)

All material (C) 2013-2017 by CSC IT Center for Science Ltd. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License, http://creativecommons.org/licenses/by-nc-sa/3.0/

Agenda Monday 9:00-9:30 Course introduction 9:30-10:00 Useful new features beyond F95 10:00-10:15 Coffee break 10:15-10:45 Exercises 10:45-11:30 Useful new features cont d 11:30-12:00 Exercises 12:00-13:00 Lunch break 13:00-14:00 Interoperability with C 14:00-14:45 Exercises 14:45-15:00 Coffee break 15:00-16:00 Exercises Tuesday 9:00-9:45 Introduction to Fortran coarrays 9:45-10:00 Coffee break 10:00-11:15 Exercises 11:15-12:00 More coarray features 12:00-13:00 Lunch break 13:00-14:00 Exercises 14:00-14:45 Advanced topics in coarrays 14:45-15:00 Coffee break 15:00-16:00 Exercises Wednesday 9:00-10:00 Types and procedure pointers 10:00-10:15 Coffee break 10:15-11:00 Exercises 11:00-11:45 Type extensions 11:45-12:30 Lunch break 12:30-13:00 Procedure pointers 13:00-14:00 Exercises 14:00-15:00 Advanced object-oriented Fortran 15:00-15:15 Coffee break 15:15-16:00 Exercises

Web resources CSC s Fortran95/2003 Guide (in Finnish) for free http://www.csc.fi/csc/julkaisut/oppaat Fortran wiki: a resource hub for all aspects of Fortran programming http://fortranwiki.org About Fortran standard evolution over few decades http://fortranwiki.org/fortran/show/fortran+2008 http://fortranwiki.org/fortran/show/fortran+2003 http://fortranwiki.org/fortran/show/fortran+95 http://fortranwiki.org/fortran/show/fortran+90 http://fortranwiki.org/fortran/show/fortran+77 http://fortranwiki.org/fortran/show/fortran+66 GNU Fortran online documents, version 5.3.0 https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gfortran/ Cray documentation, where to search for Fortran compiler reference http://docs.cray.com Intel Fortran compilers http://software.intel.com/en-us/fortran-compilers G95 Fortran compiler and about its coarray support http://www.g95.org http://www.g95.org/coarray.shtml Fortran code examples http://www.nag.co.uk/nagware/examples.asp http://www.personal.psu.edu/jhm/f90/progref.html Mistakes in Fortran 90 Programs That Might Surprise You http://www.cs.rpi.edu/~szymansk/oof90/bugs.html Coarrays references http://www2.hpcl.gwu.edu/pgas09/tutorials/caf_tut.pdf http://www.co-array.org ftp://ftp.nag.co.uk/sc22wg5/n1801-n1850/n1824.pdf http://en.wikipedia.org/wiki/coarray_fortran http://gcc.gnu.org/wiki/coarray

EXERCISE ASSIGNMENTS

General exercise instructions Computing servers We will use CSC s Cray XC40 supercomputer Sisu for the exercises. Log onto Sisu using the provided tnrgxx username and password, e.g. % ssh X trng10@sisu.csc.fi For editing program source files you can use e.g. Emacs editor with or without (the option nw) X-Windows: emacs nw prog.f90 emacs prog.f90 Also other popular editors (vim, nano) are available. Simple compilation and execution Compilation and execution are done via the ftn (and cc for C programs as needed) wrapper commands and the aprun scheduler: % ftn o my_exe test.f90 % cc o my_c_exe test.c % aprun n 1./my_exe We will use the default Cray compiling environment. There are also other compilers (GNU and Intel) available on Sisu, which can be changed (for example) as % module swap PrgEnv-cray PrgEnv-gnu Use the commands module list and module avail to see the currently loaded and available modules, respectively. When requiring multiple cores (in case of coarrays exercises), just increment the n switch for aprun, e.g. n 4 for running with 4 images. Skeleton codes For most of the exercises, skeleton codes are provided in the exercises folder, under a corresponding subdirectory. Generally, you should look for sections marked with TODO for completing the exercises.

Useful new features 1. Operating system utilities a) Get all command line arguments into a string and print it. b) Get number of arguments, get them one-by-one (also command itself) and print them. c) Find out the value of environment variable $HOME. d) Execute a Unix-command 'echo $HOME' and check the exit status. 2. New allocatable features Take two integer arrays and try Automatic allocation Automatic resizing Allocating an array in a function Moving an allocation from an allocated an initialized array to an unallocated array. Give a try also for the allocatable scalar feature by defining an allocatable character string and allocating and initializing it in one go. See answers/ex2/alloc.f90. 3. New pointer features and contiguous attribute Run the program ex3/pointer.f90 to see how the new pointer features work. Then fix ex3/contiguous.f90 such that you get the diagonal as intended. 4. Experimenting with asynchronous I/O Compile the program ex4/async.f90 with Cray compiler and run it to see, whether asynchronous I/O is in fact asynchronous on Sisu. Do the same with Intel and GNU compilers.

Language interoperability 5. Call C function from Fortran Supply the correct C binding interface block for the C function DotProduct. 6. Accessing global C data from Fortran a) Modify Fortran module file (ex6/globalmod.f90) to map C variables correctly to Fortran representation. b) Repeat run with Intel compiler, too and realize that the C struct may not be optimal from data alignment point of view. Fix the alignment in C-code make sure Fortran gets corrected, too. c) Fix the Fortran main program (ex6/global.f90) to get a correct C-to-F pointer mapping. 7. (BONUS) Implement mygetenv-function This mygetenv-function should take a null-terminated Fortran string as input and return a Fortran string. Yet it should call the getenv-function from C-library directly. This can be accomplished by use of assignment operator that maps TYPE(c_ptr) i.e., char * in C-language, to Fortran character strings. You will need to provide the interface definition to enable hassle-free getenv-calls from Fortran the rest is already implemented.

Fortran coarrays 8. Hello World revisited Write the hello world program demonstrated in the lectures, but revising it such that the images print their messages in consecutive order, i.e., Hello! From Image 1 Hello! From Image 2... 9. Reading input data Write a test program where a parameter is being read in by the image #1 from the keyboard and then distributed to other images. 10. Global maximum Write a program where all images generate a set of (dfferent) random numbers, and find the maximum value of them. Then the rank #1 finds the global maximum out of the local maxima and prints it out.

11. Parallelizing a heat equation solver with coarrays Here we will parallelize a 2D heat equation solver (see the Appendix) using coarrays. We will start from a complete serial Fortran implementation of the solver (exercises/ex11, heat_serial.f90 and heat_serial_main.f90). Take your time to get acquainted with the program and how to build ( make serial ) and run it. The approach is to partition the grid into domains column-wise and have the whole grid declared as a coarray. The images are able to make the update in their domains independently (i.e. simultaneously) besides the outermost (domain-bordering) grid points, for which data has to be read from the neighboring images. We will then parallelize it with coarrays as follows: Declare the temperature grids to be coarrays throughout the code. We can restrict ourselves to have the grid width being evenly dividable by the number of images (if you want some challenge, you can lift this restriction

for a general case). Major modifications to the initialize subroutine will be needed to have a matching initial conditions with the serial implementation. Implement a new routine for halo swap i.e. performing the update of the ghost layers columns (see above). Let us call it exchange. The output routine needs to be adapted for either doing the single-writer I/O (i.e. gathering the full grid to one image), or alternatively, all images can print their local portions of the grid. Time and energy permitting, also reading in an input file (read_input) can be converted to support coarrays parallelization. A reference coarray implementation is being provided in the answers/ex11 folder, heat_coarray.f90 and heat_coarray_main.f90. You can build it by make parallel.

Types and procedure pointers 12. Parametrization of a derived type Modify the skeleton exercises/ex12/partype.f90 and create a type abstraction for a vector. The implementation should allow the parametrization of the real KIND and LEN. 13. Abstract interfaces and procedure pointers The skeleton for this exercise is provided in exercises/ex13/absif.f90 file. Do the following modifications: a) Create two abstract interfaces. One for a function that takes two vectors as an input arguments and returns a vector and another for a function that takes same arguments, but returns a scalar. b) Create actual implementations for functions computing the sum, elementwise product and dot product of two vectors. c) Initialize two vectors v1=(1,2,3,4,5) and v2=(5,4,3,2,1) and compute v3=v1+v2, v3=v1.*v2 (elementwise product) and v3=v1*v2 (dot product). Do the type constructors work as expected? d) Create functions vecfun(fun,v1,v2) and scalfun(fun,v1,v2) which take as their first argument scalar or vector functions with the interface created in the part a) of this exercise. Is it possible to compute the results of part c) of this exercise by using the functions vecfun and scalfun?

Objects 14. Type-bound procedures Skeleton files together with a Makefile can be found from exercise folder exercises/ex14. a) Add functions created in the exercise 13 b) as type-bound procedures of the given vector type. b) Implement operators for the type-bound procedures that you implemented in part a). Note that you can also use operator names like.prod. in addition to *. 15. Type extensions Extend the vector type of exercise 14 to have a separate imaginary part and make operators for summing two imaginary vectors and imaginary vector to a real vector. Advanced objects 16. Abstract classes a) Construct an abstract vector class which contains deferred type-bound procedures and operators for sums, elementwise products and dot products of two vectors. b) Create a double precision vector class implementation of the abstract vector class. 17. Type component visibility and type creation a) Make type components of the given vector class private. Create a constructor which wraps the type constructor and allows creation of vector types of given length and type. b) Create a module generic with the same name as the vector type and map it to the type constructor created in part a).

Appendix: Heat equation solver The heat equation is a partial differential equation that describes the variation of temperature in a given region over time where u(x, y, z, t) represents temperature variation over space at a given time, and α is a thermal diffusivity constant. We limit ourselves to two dimensions (plane) and discretize the equation onto a grid. Then the Laplacian can be expressed as finite differences as Where x and y are the grid spacing of the temperature grid u(i,j). We can study the development of the temperature grid with explicit time evolution over time steps t: There are a solver for the 2D equation implemented with Fortran (including some C for printing out the images). You can compile the program by adjusting the Makefile as needed and typing make. The solver carries out the time development of the 2D heat equation over the number of time steps provided by the user. The default geometry is a flat rectangle (with grid size provided by the user), but other shapes may be used via input files - a bottle is give as an example. Examples on how to run the binary: 1../heat (no arguments - the program will run with the default arguments: 256x256 grid and 500 time steps) 2../heat bottle.dat (one argument - start from a temperature grid provided in the file bottle.dat for the default number of time steps) 3../heat bottle.dat 1000 (two arguments - will run the program starting from a temperature grid provided in the file bottle.dat for 1000 time steps) 4../heat 1024 2048 1000 (three arguments - will run the program in a 1024x2048 grid for 1000 time steps) The program will produce a.png image of the temperature field after every 100 iterations. You can change that from the parameter image_interval.