Chapter 4. Fortran Arrays

Similar documents
FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

Chapter 3. Fortran Statements

Chapter 6. A Brief Introduction to Fortran David A. Padua

ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

Goals for This Lecture:

A Brief Introduction to Fortran of 15

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

6.1 Expression Evaluation. 1 of 21

Technical Specification on further interoperability with C

Fortran 90 - A thumbnail sketch

Review More Arrays Modules Final Review

TS Further Interoperability of Fortran with C WG5/N1917

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

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

ISO/IEC : TECHNICAL CORRIGENDUM 2


Storage and Sequence Association

Technical Report on further interoperability with C

Information technology Programming languages Fortran Part 1: Base language

Co-arrays to be included in the Fortran 2008 Standard

Fortran Coding Standards and Style

INTRODUCTION TO FORTRAN PART II

Introduction to Programming with Fortran 90

Lecture V: Introduction to parallel programming with Fortran coarrays

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

The new features of Fortran 2003

Bits, Bytes, and Precision

NAGWare f95 Recent and Future Developments

Introduction to Fortran95 Programming Part II. By Deniz Savas, CiCS, Shef. Univ., 2018

Subroutines and Functions

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

Exam 3 Chapters 7 & 9

Introduction to Modern Fortran

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

Allocating Storage for 1-Dimensional Arrays

Parallel Programming in Fortran with Coarrays

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

AMath 483/583 Lecture 8

Subroutines, Functions and Modules

Size, Alignment, and Value Ranges of Data Types. Type Synonym Size Alignment Value Range. BYTE INTEGER*1 8 bits Byte

International Standards Organisation. Parameterized Derived Types. Fortran

Report from WG5 convener

Feature Proposals for Fortran A subset

Strategies & Obstacles in Converting a Large Production Application to FORTRAN 90

Evolution of Fortran. Presented by: Tauqeer Ahmad. Seminar on Languages for Scientific Computing

Fortran classes and data visibility

Appendix D. Fortran quick reference

Fortran 2003 Part 1. École normale supérieure L3 geosciences 2018/2019. Lionel GUEZ Laboratoire de météorologie dynamique Office E324

HPF commands specify which processor gets which part of the data. Concurrency is defined by HPF commands based on Fortran90

dbx90: Fortran debugger March 9, 2009

Declaration and Initialization

Goals for This Lecture:

COMPATIBILITY CONSIDERATIONS OF THE PL/1 IMPLEMENTATION

About 1. Chapter 1: Getting started with Fortran 2. Remarks 2. Versions 2. Examples 2. Installation or Setup 2. Hello, world 3. Quadratic equation 4

NAGWare f95 and reliable, portable programming.

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

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

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

The New Features of Fortran 2003

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Advanced Fortran Programming

Walt Brainerd s Fortran 90 programming tips

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

LF Fortran 95 Language Reference. Revision G.02

PGI Accelerator Programming Model for Fortran & C

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

Programming for High Performance Computing in Modern Fortran. Bill Long, Cray Inc. 17-May-2005

FORTRAN WATFIV. Mohammed Aslam CIS 24 Prof. Kopec Report 01 Date: 03/03/03

Array Abstract Data Type

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

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

7. Procedures and Structured Programming

SPECIFICATION 2 STATEMENTS

MPI: A Message-Passing Interface Standard

Fortran Annex to TR 24772, Guidance to Avoiding Vulnerabilities in Programming Languages through Language Selection and Use

Extrinsic Procedures. Section 6

Chapter 3:: Names, Scopes, and Bindings

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

NumPy. Daniël de Kok. May 4, 2017

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Homework #3 CS2255 Fall 2012

How to declare an array in C?

Fortran. (FORmula TRANslator) History

A Comparison of Co-Array Fortran and OpenMP Fortran for SPMD Programming

Cedar Fortran Programmer's Manual 1. Jay Hoeinger. Center for Supercomputing Research and Development. Urbana, Illinois

Control Flow February 9, Lecture 7

Fortran Annex to TR 24772, Guidance to Avoiding Vulnerabilities in Programming Languages through Language Selection and Use

Fortran90 Bindings for Charm++

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Modern Fortran OO Features

Introduction to Object-Oriented Concepts in Fortran95

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

Summer May 18, 2010

The New Features of Fortran 2000

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

ISO/IEC JTC 1/SC 22/OWGV N 0220

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

Programming with MPI

CS 1313 Spring 2000 Lecture Outline

Transcription:

Chapter 4. Fortran Arrays Fortran arrays are any object with the dimension attribute. In Fortran 90/95, and in HPF, arrays may be very different from arrays in older versions of Fortran. Arrays can have values assigned as a whole without specifying operations on individual array elements, and array sections can be accessed. Also, allocatable arrays that are created dynamically are available as part of the Fortran 90/95 and HPF standards. Arrays in HPF play a central role in data distribution and data alignment (refer to this chapter and The High Performance Fortran Handbook for details on working with arrays in HPF). This chapter describes some of the features of Fortran 90/95 and HPF arrays. The following example illustrates valid array operations. REAL(10,10) A,B,C A=12!Assign 12 to all elements of A B=3!Assign 3 to all elements of B C=A+B!Add each element of A to each of B Array Types Fortran supports four types of arrays: explicit-shape arrays, assumed-shape arrays, deferred-shape arrays and assumed-size arrays. Both explicit-shape arrays and deferred shape arrays are valid in a main program. Assumed shape arrays and assumed size arrays are only valid for arrays used as dummy arguments. Deferred shape arrays, where the storage for the array is allocated during execution, must be declared with either the ALLOCATABLE or POINTER attributes. Every array has properties of type rank, shape and size. The extent of an array s dimension is the number of elements in the dimension. The array rank is the number of dimensions in the array, up to a maximum of seven. The shape is the vector representing the extents for all dimensions. The size is the product of the extents. For some types of arrays, all of these properties are determined when the array is declared. For other types of arrays, some of these properties are determined when the array is allocated or when a procedure using the array is entered. For arrays that are dummy arguments, there are several special cases. Allocatable arrays are arrays that are declared but for which no storage is allocated until an allocate statement is executed when the program is running. Allocatable arrays provide Fortran 90/95 and HPF programs with dynamic storage. Allocatable arrays are declared with a rank specified with the ":" character rather than with explicit extents, and they are given the ALLOCATABLE attribute. 115

Array Specification Explicit Shape Arrays 116 Explicit shape arrays are those arrays familiar to FORTRAN 77 programmers. Each dimension is declared with an explicit value. There are two special cases of explicit arrays. In a procedure, an explicit array whose bounds are passed in from the calling program is called an automatic-array. The second special case, also found in a procedure, is that of an adjustable-array which is a dummy array where the bounds are passed from the calling program. Assumed Shape Arrays An assumed shape array is a dummy array whose bounds are determined from the actual array. Intrinsics called from the called program can determine sizes of the extents in the called program s dummy array. Deferred Shape Arrays A deferred shape array is an array that is declared, but not with an explicit shape. Upon declaration, the array's type, its kind, and its rank (number of dimensions) are determined. Deferred shape arrays are of two varieties, allocatable arrays and array pointers. Assumed Size Arrays An assumed size array is a dummy array whose size is determined from the corresponding array in the calling program. The array s rank and extents may not be declared the same as the original array, but its total size (number of elements) is the same as the actual array. This form of array should not need to be used in new Fortran programs. Array Specification Arrays may be specified in either of two types of data type specification statements, attribute-oriented specifications or entity-oriented specifications. Arrays may also optionally have data assigned to them when they are declared. This section covers the basic form of entity-based declarations for the various types of arrays. Note that all the details of array passing for procedures are not covered here; refer to The Fortran 95 Handbook for complete details on the use of arrays as dummy arguments. Explicit Shape Arrays Explicit shape arrays are defined with a specified rank, each dimension must have an upper bound specified, and a lower bound may be specified. Each bound is explicitly defined with a specification of the form: [lower-bound:] upper-bound An array has a maximum of seven dimensions. The following are valid explicit array declarations: INTEGER NUM1(1,2,3)!Three dimensions INTEGER NUM2(-12:6,100:1000)!Two dimensions with!lower and upper bounds INTEGER NUM3(0,12,12,12)!Array of size 0 INTEGER NUM3(M:N,P:Q,L,99)!Array with 4 dimensions Assumed Shape Arrays An assumed shape array is always a dummy argument. An assumed shape array has a specification of the form:

Chapter 4. Fortran Arrays [lower-bound] : The number of colons (:) determines the array s rank. An assumed shape array cannot be an ALLOCATABLE or POINTER array. Deferred Shape Arrays An deferred shape array is an array pointer or an allocatable array. An assumed shape array has a specification determines the array's rank and has the following form for each dimension: : For example: INTEGER, POINTER::NUM1(:,:,:,:) INTEGER, ALLOCATABLE::NUM2(:) Assumed Size Arrays An assumed size array is a dummy argument with an assumed size. The array s rank and bounds are specified with a declaration that has the following form: [explicit-shape-spec-list,][lower-bound :]* For example: SUBROUTINE YSUM1(M,B,C) INTEGER M REAL, DIMENSION(M,4,5,*) :: B,C Array Subscripts and Access There are a variety of ways to access an array in whole or in part. Arrays can be accessed, used, and assigned to as whole arrays, as elements, or as sections. Array elements are the basic access method, for example: INTEGER, DIMENSION(3,11) :: NUMB NUMB(3,1)=5 This assigns the value 5 to element 3,1 of NUMB The array NUMB may also be accessed as an entire array: NUMB=5 This assigns the value 5 to all elements of NUMB. Array Sections and Subscript Triplets Another possibility for accessing array elements is the array section. An array section is an array accessed by a subscript that represents a subset of the entire array's elements and is not an array element. An array section resulting from applying a subscript list may have a different rank than the original array. An array section's subscript list consists of subscripts, subscript triplets, and/or vector subscripts. For example using a subscript triplet and a subscript: NUMB(:,3)=6 117

Array Constructors 118 assigns the value 6 to all elements of NUMB with the second dimension of value 3 (NUMB(1,3), NUMB(2,3), NUMB(3,3) ). This array section uses the array subscript triplet and a subscript to access three elements of the original array. This array section could also be assigned to a rank one array with three elements, for example: INTEGER(3,11) NUMB INTEGER(3) NUMC NUMB(:,3)=6 NUMC=NUMB(:,3) Note that NUMC is rank 1 and NUMB is rank 2. This array section assignment illustrates how NUMC, an the array section of NUMB, has a shape that due to the use of the subscript 3, is of a different rank than the original array. The general form for an array's dimension with a vector subscript triplet is: [subscript] : [subscript] [:stride] The first subscript is the lower bound for the array section, the second is the upper bound and the third is the stride. The stride is by default one. If all values except the : are omitted, then all the values for the specified dimensions are included in the array section. For example, using NUMB above: NUMB(1:3:2,3)=7 assigns the value 7 to the elements NUMB(1,3) and NUMB(3,3). Array Sections and Vector Subscripts Vector-valued subscripts specify an array section by supplying a set of values defined in a one dimensional array (vector) for a dimension or several dimensions of an array section. For example: INTEGER J(2), I(2) INTEGER NUMB(3,6) I=(/1,2/) J=(/2,3/) NUMB(J,I)=7 This array section uses the vectors I and J to assign the value 7 to the elements NUMB(2,1), NUMB(2,2), NUMB(3,1), NUMB(3,2). Array Constructors An array constructor can be used to assign values to an array. Array constructors form one-dimensional vectors to supply values to a one-dimensional array, or one dimensional vectors and the RESHAPE function to supply values to arrays with more than one dimension. Array constructors can use a form of implied DO similar to that in a DATA statement. For example: INTEGER DIMENSION(4):: K = (/1,2,7,11/) INTEGER DIMENSION(20):: J = (/(I,I=1,40,2)/) CM Fortran Extensions The ARRAY Attribute The PGHPF compiler provides several extensions for handling arrays. The compiler handles the CM Fortran attribute ARRAY. The ARRAY attribute is similar to the DIMENSION attribute. Refer to for more details on the ARRAY statement.

Chapter 4. Fortran Arrays Array Constructors Extensions The PGHPF compiler supports an extended form of the array constructor specification. In addition to the (/../) specification for array constructors, PGHPF supports the notation where [ and ] begin and end, respectively, an array constructor. In addition, an array constructor item may be a 'subscript triplet' in the form of an array section where the values are assigned to the array: lower-bound : upper-bound [ : <stride> ] For the values i : j : k the array would be assigned values i, i+k, i+2k,..., j. If k is not present, stride is assumed to be 1. For example: INTEGER, DIMENSION(20):: K = [1:40:2] 119

120