New CPS615 Foils-- 28 August 95 Introduction to Fortran90 for CPS615 Fall 95 Nancy McCracken Geoffrey Fox NPAC Syracuse University 111 College Place Syracuse NY 13244-4100 Abstract of Fortran90 Overview for CPS615 This introduces array notation and describes basic array operators Array Constructors and Array Sections The Where Construct Forall available in some compilers and critical for parallelism Subroutines and Interfaces Intrinsic Functions A simple Gauss-Jordan Matrix Inversion is used as an example A Brief Description of Fortran 90 History Designed by X3J3 Standards committee between 1979 and 1991. The committee was part of the International Standards Organization and is also accredited by ANSI, the American National Standards Institute. In 1991, the committee had 46 members: universities 5 user groups 6 international groups 7 government organizations 6 vendors 22 Fortran90 extends Fortran77 -- A summary of new features: Array operations Pointers Improved facilities for numerical computation including a set of numeric inquiry functions Parameterization of the intrinsic types, to permit processors to support short integers, very large character sets, etc. User-defined derived data types composed of arbitrary data structures and operations on those structures. Modules, for global data definitions and procedure libraries. Source form, more appropriate for the terminal New control constructs, forms of CASE and DO Recursive procedures Optional and keyword arguments Dynamic storage allocation Improved 1/0facilities Additional intrinsic procedures Evolution of the language by labelling some features "obsolescent" Elementwise Operations in Fortran90: Addition of Arrays Let a and b be conformable (same shape) integer arrays a + b creates a new array of the same shape i.e. the same size and number of dimensions Elementwise Operations in Fortran90: Array Assignment b=a copies data from array a to array b Global Operations in Fortran90: Reduction Let a be an array of integer or real numbers of any size and shape Sum reduction(a) = S ai where sum over i runs over index set of a Sum is the Fortran90 function for sum reduction of the entire array a Example of sum reduction -- Numerical Integration How to create Arrays in Fortran90 [1:N] creates an array of contiguous numbers (Standard syntax: ( i, i = 1,N )) Creating arrays: Constants (Spread or broadening) No special syntax is needed -- just set array of desired shape to the constant 3 Completing the Integration Example Calculate the function fval for a particular polynomial Note in a real implementation, integrate should be made into a subroutine with fval as a parameter Use uniform interval widths so that width is not an array but a scalar constant Array Expressions Creating a subset of a one-dimensional array Given an array fval of length N+1, fval(2:N+1) gives an array containing last N values. Any arrays can be used together in an expression if they are "conformable" Consider Integration Example again Selection: Conditional Evaluation of Array Operations A Boolean array -- here called L -- can determine a "context" in which a data-parallel operation is evaluated This example gives a final array with undefined elements and corresponds to Fortran 90 selection statement where L a=a+b Integration Again! Simpson's Rule More General Elementwise Operations One can code very general index sets for elementwise expressions using the forall statement Note that the body of a forall statement is executed "in parallel". There is NO dependence between different forall statement indices. This important difference from DO loops More Details on Array Operations in Fortran90 --- The Parts of a Fortran Program The same program in Free Form syntax How to declare Array Properties A number of properties are established by this declaration: The type of the elements is integer. Other possible types are real, logical, char, real*4, real*8 etc. The rank of the array is two (two-dimensional) The extent of the first dimension (the number of elements) is N. The extent of the second dimension is 9 If the lower bound is 1, 1:N can be abbreviated as N The shape of the array is [N,9] -- a vector consisting of the extents of the dimensions. The following Fortran77 declaration is equivalent Array Indexing Indexing individual elements or Array objects i.e. whole arrays Array Constructors for Array Objects Note syntax [ and ] can be replaced by (/ and /) Other Types of arrays can be constructed: Array Sections for Array Objects Use of Subscript Triples to Specify array Sections How to Specify Array Sections with Vector Valued Subscripts uses the values of a one-dimensional array as subscripts to another array Using Arrays in Expressions and Statements: Arrays must be same shape(conformable) Basic Array Assignment Assigning to one row of a two dimensional array. The remaining array values are unchanged Restriction on array assignment Assignment Collisions are NOT allowed! The result is undefined and runtime errors may result Sample Program Using Array Sections Simpson's Rule for Integration using array sections to select different weightings The Where Construct Single Assignment where statement with conformable arrays Multiple Assignment Where Construct The Forall Construct Not in the Fortran90 standard language but is in HPF and many Fortran90 compilers provide it Single Assignment Forall Statements Forall statements where individual values depend on the index variables Scan (Parallel Prefix) -- Another Global Operation Let a be an array of integer or real numbers of any shape. Define the inclusive scan by: With example: Example Application Using Scans -- Calculation of Binomial Coefficients Calculate an array, bin, of binomial coefficients This simple program isn't very good -- it gets arithmetic overflow except at small n Can you formulate a better algorithm in Fortran90 ? Arguments for Procedures In Fortran90, each subroutine, function and (main) program are compiled independently Default implies that cannot refer to variables from one procedure in another One can pass arguments and these are passed by reference(address) not value as in Fortran77 Common blocks or modules can be used for global declaration of variables that can be referenced in all subroutines Interface Blocks for Called Subroutines Interface blocks are used in calling procedure to allow compiler to check the syntax and argument types in subroutine calls Intrinsic Functions -- Optional and Keyword Arguments Intrinsic functions are part of the language and not part of library. They have a function and not an operator notation The general form of these built in functions is: Optional arguments and keyword arguments are also allowed for user-defined functions The Elemental and Inquiry Functions All standard functions of arithmetic, trigonometry and type conversion etc. are extended in a natural way to operate elementwise on arrays Here is a teensy sample! There are also a new set of inquiry functions in intrinsic set Array Transformation Functions There are a set of reduction functions already exemplied by sum Here is another example of sum reducing over first dimension (columns) and over second dimension(rows) The Arrray Location Functions These are new for Fortran90 and find location of particular elements. They return a vector of array indices for the location of an element. If more than one element satisfies the criteria, as in more than one minimum or maximum element, the first such element (as defined by column major order) is returned. Array Construction Functions Array construction functions Here is an example of reshaping a one dimensional array into a two dimensional form with shape 3 by 8 An Example of Use of Intrinsic Functions Shift Intrinsic Transformation Functions Array shift functions and an example Some Simple Matrix Manipulation Intrinsic Functions Note matmul is multiplication of arrays as matrices a * b is multiplication of corresponding elements Libraries such as IBM ESSL or HPCC SCALAPACK give you more general matrix operations such solution of equations The Algorithm for Gauss-Jordan Matrix Inversion The array operations needed before and after pivot row handled separately The part of Gauss-Jordan addressing first set of rows before row i For all rows j before i'th row: A similar strategy is used for the rows after row i and between them these two processes complete row transformations for i'th column The Fortran90 Gauss Jordan Program This is a simplified code which only works for "nice" matrices where you do not need to check for zero's on the diagonal and use pivoting for robust results