Goals for This Lecture:

Similar documents
Goals for This Lecture:

Goals for This Lecture:

ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

Chapter 4. Fortran Arrays

Goals for This Lecture:

AMath 483/583 Lecture 8

Review More Arrays Modules Final Review

Goals for This Lecture:

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

Introduction to Programming with Fortran 90

Lecture V: Introduction to parallel programming with Fortran coarrays

Allocating Storage for 1-Dimensional Arrays

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

INTRODUCTION TO FORTRAN PART II

CSE 262 Spring Scott B. Baden. Lecture 4 Data parallel programming

Fortran 90 - A thumbnail sketch

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

SHAPE Returns the number of elements in each direction in an integer vector.

Appendix D. Fortran quick reference

Types II. Hwansoo Han

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


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

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

CS 1313 Spring 2000 Lecture Outline

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

Declaration and Initialization

Subroutines, Functions and Modules

Computer Science & Engineering 150A Problem Solving Using Computers

Reusing this material

AMath 483/583 Lecture 21

Arrays and ArrayLists. Ananda Gunawardena

NAGWare f95 Recent and Future Developments

7. Procedures and Structured Programming

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

A Brief Introduction to Fortran of 15

An Introduction to Fortran

PACKAGE SPECIFICATION HSL 2013

AMath 483/583 Lecture 7

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Fortran90 Bindings for Charm++

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

6.1 Expression Evaluation. 1 of 21

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

Chapter 3. Fortran Statements

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

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

LF Fortran 95 Language Reference. Revision G.02

Introduction to Modern Fortran

eccodes BUFR decoding

(Refer Slide Time 01:41 min)

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

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

Dynamic Memory Allocation

01. Function Description and Limitation 02. Fortran File Preparation 03. DLL File Preparation 04. Using the USSR material model in midas FEA

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Programming for Electrical and Computer Engineers. Pointers and Arrays

NAGWare f95 and reliable, portable programming.

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

AMath 483/583 Lecture 7. Notes: Notes: Changes in uwhpsc repository. AMath 483/583 Lecture 7. Notes:

Programming Languages

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Bits, Bytes, and Precision

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Message Passing Interface: Basic Course

Homework #3 CS2255 Fall 2012

Scientific Programming in C VI. Common errors

Introduction [1] 1. Directives [2] 7

CS201- Introduction to Programming Current Quizzes

CS201 Latest Solved MCQs

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Module 7.2: nag sym fft Symmetric Discrete Fourier Transforms. Contents

The new features of Fortran 2003

FORM 2 (Please put your name and form # on the scantron!!!!)

CS 230 Programming Languages

Object-Oriented Principles and Practice / C++


Vector and Free Store (Pointers and Memory Allocation)

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

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

Goals for This Lecture:

Programming Languages

Module 28.3: nag mv rotation Rotations. Contents

Old Questions Name: a. if b. open c. output d. write e. do f. exit

How to declare an array in C?

CS2255 HOMEWORK #1 Fall 2012

Computational Methods of Scientific Programming

Software Development Products. Product Errata. Intel Fortran Compiler for Linux* and Windows* 10th February 2003 DISCLAIMER

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

Computer Programming: C++

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

Technical Specification on further interoperability with C

Storage and Sequence Association

MPI: A Message-Passing Interface Standard

Program Development (SAS IML)

Advanced Fortran Programming

CPSC 3740 Programming Languages University of Lethbridge. Data Types

41391 High performance computing: Miscellaneous parallel programmes in Fortran

Transcription:

Goals for This Lecture: Learn about multi-dimensional (rank > 1) arrays Learn about multi-dimensional array storage Learn about the RESHAPE function Learn about allocatable arrays & the ALLOCATE and DEALLOCATE statements See an example of allocatable arrays in use

Multi-dimensional Arrays Type and shape of a multi-dimensional (rank > 1) array can be declared in two forms: real :: data(9,1:2,0:257) real, dimension(9,1:2,0:257) :: data Arrays can have up to seven dimensions You may not be able to fit a multidimensional array on a given machine! Example: real :: data(1000,1000,1000,1000) This array requires at least 4 10 12 bytes of storage. There are not many platforms with > 4 Terabytes of memory around!

Multi-dimensional Array Storage a(1,1) In FORTRAN, multi-dimensional arrays are ordered in column-major order Memory layout a(2,1) a(3,1) a(1,2) a(2,2) a(3,2) Elements are stored in memory in a sequence where the left-most index varies most rapidly This is the reverse of other languages like C & C++ where elements are stored in row-major order The order in which array elements are accessed controls how fast the code executes a(1,3) a(2,3) a(3,3) Index layout a(1,1) a(2,1) a(3,1) a(1,2) a(2,2) a(3,2) a(1,3) a(2,3) a(3,3)

Initializing Multi-dimensional Arrays It would be nice if we could initialize multi-dimensional arrays in declaration statements with an array constructor such as: real :: identity(3,3)=(/1.,0.,0.,0.,1.,0.,0.,0.,1./) However this is illegal as the array constructor on the right-hand-side of the assignment operator is not conformable with the rank-2 array on the lefthand-side This can be dealt with with the RESHAPE function: real :: identity(3,3)=reshape((/1.,0.,0.,0.,1.,0.,0.,0.,1./),(/3,3/)) The RESHAPE function converts the 9-element rank-1 array (the first argument) into a rank-2 array (specified by the array in the second argument) of the same size.

The RESHAPE function The reshape function accepts two arguments The first argument is the array to be reshaped It can be of any type Arrays can be variable or constants (built with an array constructor) The second argument is an integer rank-1 array that specifies the shape the resulting array will have The number of elements in the integer array is the dimensionality of the reshaped matrix The value of each element is the extent of the corresponding dimension

Allocatable Arrays Thus far we have been working with arrays that are statically allocated The size of the array is determined at the time of compilation Must know the needed size prior to compiling the program. A better solution is to dynamically allocate the array once the size is known (at the time of program execution) This can be done with allocatable, a.k.a. deferred shape arrays Example: real, allocatable :: data(:) integer :: size size = 15 allocate(data(size)) deallocate(data) Allocatable arrays should always be deallocated after the program ceases to use them

ALLOCATED Statement Allocatable arrays can not be used in any way until memory for it has been allocated! You can test to see if memory for an array has been allocated by using the ALLOCATED statement The ALLOCATED statement returns a value of.true. if memory has already been allocated and.false. if it has not been allocated. Example: real, allocatable :: data(:) integer :: size size = 15 if(.not.allocated(data)) allocate(data(size)) data(1:size) = 1.0 deallocate(data)

! Purpose: Find max & min values of an arbitrary data set! Author: F. Douglas Swesty! Date: 10/21/2005 program maxmin implicit none! Turn off implicit typing integer, parameter :: lun1=11! Define an LUN for the output file integer :: npts=0! Number of data points real, allocatable :: x(:)! Define array to hold data integer :: i! Loop index integer :: ierror=0! I/O error status variable real :: max_val, min_val! Maximum & minimum values integer :: max_loc, min_loc! Maximum & minimum location! Open the file open(unit=lun1,file= experiment.dat,status= OLD,iostat=ierror) if(ierror /= 0) then write(*,*) Could not read in file! stop endif! Find out how many points there are do while(ierror == 0)! Do while no read error read(lun1,*) max_val! Read in data into a temp. variable npts = npts+1! Increment counter enddo allocate(x(npts))! Allocate space in array rewind(lun1)! Rewind the file do i=1,npts,1! Rewind the data points read(lun1,*) x(i) enddo close(unit=lun1)! Close the file max_val = x(1)! Initialize maximum min_val = x(1)! Initialize minimum max_loc = 1! Initialize maximum location min_loc = 1! Initialize minimum location do i=2,npts! Loop over rest of data set if(x(i) > max_val) then! If x(i) is a new maximum max_val = x(i)! Reset maximum max_loc = i! Reset maximum location endif if(x(i) < min_val) then! If x(i) is a new minimum min_val = x(i)! Reset minimum min_loc = I! Reset minimum location endif enddo! Write out results write(*,*) Maximum,max_val; at,max_loc write(*,*) Minimum,min_val; at,min_loc deallocate(x)! deallocate space for array stop! Halt execution end program maxmin

Reading Assignment Read Sections 7.1