Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A)...

24
Fundamentals of Fundamentals of Computational Computational Science Science CISC 810 CISC 810 Dr Rob Thacker Dr Rob Thacker Dept of Physics (308A) Dept of Physics (308A) thacker@physics thacker@physics

Transcript of Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A)...

Page 1: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Fundamentals of Fundamentals of Computational Computational

ScienceScience CISC 810 CISC 810Dr Rob ThackerDr Rob Thacker

Dept of Physics (308A)Dept of Physics (308A)

thacker@physicsthacker@physics

Page 2: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Today’s LectureToday’s Lecture

Review of assignment 1Review of assignment 1 C/Fortran programming overview C/Fortran programming overview

Page 3: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Assignment 1 solutionsAssignment 1 solutions

Printed copies will be handed out on Printed copies will be handed out on Oct 13Oct 13

An electronic version will not be An electronic version will not be made availablemade available

Page 4: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Humour – more FORTRAN Humour – more FORTRAN jokesjokes

Joke, circa 1980 (following the standardization of Joke, circa 1980 (following the standardization of FORTRAN 77):FORTRAN 77): "Q: What will the scientific programming language of "Q: What will the scientific programming language of the year 2000 look like?”the year 2000 look like?”““... A: Nobody knows, but its name will be ... A: Nobody knows, but its name will be FORTRAN."FORTRAN."

““Consistently separating words by spaces became a Consistently separating words by spaces became a general custom about the tenth century A. D., and general custom about the tenth century A. D., and lasted until about 1957, when FORTRAN abandoned lasted until about 1957, when FORTRAN abandoned the practice."the practice." ——Sun FORTRAN Reference Manual Sun FORTRAN Reference Manual

““FORTRAN, the infantile disorder, by now nearly 20 FORTRAN, the infantile disorder, by now nearly 20 years old, is hopelessly inadequate for whatever years old, is hopelessly inadequate for whatever computer application you have in mind today: it is computer application you have in mind today: it is now too clumsy, too risky, and too expensive to use.”now too clumsy, too risky, and too expensive to use.” Edsger W. Dijkstra, circa 1970. Edsger W. Dijkstra, circa 1970.

Page 5: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

A note on these A note on these programming lecturesprogramming lectures

In two lectures I can’t possibly tell you all these In two lectures I can’t possibly tell you all these is to know about C/FORTRAN programmingis to know about C/FORTRAN programming You learn programming by doingYou learn programming by doing Sit down at a terminal and write a programSit down at a terminal and write a program

Then a more complicated program and so on…Then a more complicated program and so on… Please be prepared to look up additional material Please be prepared to look up additional material

on the webon the web If you have not done any programming in If you have not done any programming in

C/FORTRAN before don’t be intimidatedC/FORTRAN before don’t be intimidated It isn’t hard (C just looks harder because of the It isn’t hard (C just looks harder because of the

“strange” syntax)“strange” syntax) I’ll try and teach them together where possible, I’ll try and teach them together where possible,

this may mean being convoluted in some placesthis may mean being convoluted in some places

Page 6: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

StyleStyle When reading and writing code, you have to keep a When reading and writing code, you have to keep a

bunch of facts straight for a short period of timebunch of facts straight for a short period of time What do this function's parameters mean?What do this function's parameters mean? What does this loop's index refer to?What does this loop's index refer to?

The more odds and ends readers have to keep track of, The more odds and ends readers have to keep track of, the more errors they will makethe more errors they will make Goal of style rules is therefore to reduce the number of things Goal of style rules is therefore to reduce the number of things

the reader has to juggle mentallythe reader has to juggle mentally The greater a difference is, the more likely we are to The greater a difference is, the more likely we are to

notice itnotice it So every semantic difference ought to be visually distinct…So every semantic difference ought to be visually distinct… ……and every difference in naming or layout ought to mean and every difference in naming or layout ought to mean

somethingsomething Most important thing is to Most important thing is to be consistentbe consistent

Anything consistent is readable after a whileAnything consistent is readable after a while Just watch kids learning to read French, Punjabi, and KoreanJust watch kids learning to read French, Punjabi, and Korean

Page 7: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

C/FORTRAN issuesC/FORTRAN issues Remember:Remember:

C is case sensitive: C is case sensitive: HELPHELP is not the same as is not the same as helphelp FORTRAN isn’tFORTRAN isn’t

FORTRAN coding style is idiosyncraticFORTRAN coding style is idiosyncratic f77 standard requires you keep line lengths to 72 f77 standard requires you keep line lengths to 72

characters(!)characters(!) Must also start each line in the 7Must also start each line in the 7thth column(!) column(!) f90 repealed these issuesf90 repealed these issues

C is more syntatically sophisticated than C is more syntatically sophisticated than FORTRANFORTRAN C requires you place a semi-colon at the end of each C requires you place a semi-colon at the end of each

command linecommand line Allows you to write very long single linesAllows you to write very long single lines

Page 8: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

NamingNaming Names of files, classes, methods, variables, and other things Names of files, classes, methods, variables, and other things

are the most visible clue to purposeare the most visible clue to purpose A variable called temperature shouldn't be used to store the A variable called temperature shouldn't be used to store the

number of pottery shards found at a dig sitenumber of pottery shards found at a dig site Choose names that are both Choose names that are both meaningfulmeaningful and and readablereadable

current_surface_temperature_of_probecurrent_surface_temperature_of_probe is meaningful, but not is meaningful, but not readablereadable

cstpcstp is easier to read, but hard to understand… is easier to read, but hard to understand… ……and easy to confuse with and easy to confuse with ctspctsp

If you must abbreviate, be consistentIf you must abbreviate, be consistent curr_ave_tempcurr_ave_temp instead of instead of current_average_temperaturecurrent_average_temperature is OK… is OK… ……but only if no one else is using but only if no one else is using curnt_av_tmpcurnt_av_tmp

The smaller the scope of a name, the more compact it can beThe smaller the scope of a name, the more compact it can be It's OK to use i and j for indices in tightly-nested for loopsIt's OK to use i and j for indices in tightly-nested for loops But But notnot OK if the loop bodies are several pages long OK if the loop bodies are several pages long

Of course, they shouldn't be anyway…Of course, they shouldn't be anyway… The wider the scope of a name, the more descriptive it has to The wider the scope of a name, the more descriptive it has to

bebe

Page 9: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Good practicesGood practicesRuleRule GoodGood BadBad

No whitespace immediately No whitespace immediately inside parentheses inside parentheses

max(candidates[sublist]) max(candidates[sublist]) max( candidates[ sublist ] ) max( candidates[ sublist ] )

……or before the parenthesis or before the parenthesis starting indexing or slicing starting indexing or slicing

max (candidates [sublist] ) max (candidates [sublist] )

No whitespace immediately No whitespace immediately before comma or colon before comma or colon

if limit > 0: print minimum, limitif limit > 0: print minimum, limit if limit > 0 : print minimum , limitif limit > 0 : print minimum , limit

Use space around arithmetic Use space around arithmetic and in-place operators and in-place operators

x += 3 * 5 x += 3 * 5 x+=3*5 x+=3*5

Never use names that are Never use names that are distinguished only by "l", distinguished only by "l", "1", "0", or "O" "1", "0", or "O"

tempo_long and tempo_init tempo_long and tempo_init tempo_l and tempo_1 tempo_l and tempo_1

Upper case with underscores Upper case with underscores for constants for constants

TOLERANCE or MAX_AREA TOLERANCE or MAX_AREA Tolerance or MaxAreaTolerance or MaxArea

Lowercase with underscores Lowercase with underscores for function for function

divide_region divide_region divRegion divRegion

Page 10: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

CommentingCommenting This is an extremely valuable part of any coding This is an extremely valuable part of any coding

projectproject Don’t leave it out because you are lazyDon’t leave it out because you are lazy Don’t think that your code “reads like a book” and Don’t think that your code “reads like a book” and

that comments are unnecessarythat comments are unnecessary Don’t ignore it because you don’t have timeDon’t ignore it because you don’t have time

Well placed and thought out comments…Well placed and thought out comments… Document the development of the code (in addition Document the development of the code (in addition

to any repository comments you make)to any repository comments you make) Help out from a functional perspective (you can have Help out from a functional perspective (you can have

“To do” lists embedded within the code)“To do” lists embedded within the code) Explain code, functions and the control flow of the Explain code, functions and the control flow of the

programprogram

Page 11: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Explanative comment Explanative comment hierarchyhierarchy

/***************************************/***************************************

******************************************************************************

*** This really gets your attention ****** This really gets your attention ***

*** use it for warning messages ****** use it for warning messages ***

******************************************************************************

***************************************/***************************************/

/*----> Another important message <----*//*----> Another important message <----*/

/***************************************/***************************************

* Use boxed comments at the start ** Use boxed comments at the start *

* of code sections or the program ** of code sections or the program *

* ** *

* Here we explain important details ** Here we explain important details *

* about the code and algorithm ** about the code and algorithm *

***************************************/***************************************/

/*/*

* A medium-level comment explaining the* A medium-level comment explaining the

* next few lines of code. Can still use* next few lines of code. Can still use

* * characters to **emphasize** words.* * characters to **emphasize** words.

*/*/

/* This comment explains the next line *//* This comment explains the next line */

! ***************************************! ***************************************

! ***************************************! ***************************************

! *** This really gets your attention ***! *** This really gets your attention ***

! *** use it for warning messages ***! *** use it for warning messages ***

! ***************************************! ***************************************

! ***************************************! ***************************************

! ----> Another important message <----! ----> Another important message <----

! ***************************************! ***************************************

! * Use boxed comments at the start *! * Use boxed comments at the start *

! * of code sections or the program *! * of code sections or the program *

! * *! * *

! * Here we explain important details *! * Here we explain important details *

! * about the code and algorithm *! * about the code and algorithm *

! ***************************************! ***************************************

!!

! A medium-level comment explaining the! A medium-level comment explaining the

! next few lines of code. Can still use! next few lines of code. Can still use

! * characters to **emphasize** words.! * characters to **emphasize** words.

!!

! This comment explains the next line ! This comment explains the next line

C FORTRAN

If you use FORTRAN get into the habitof using ! for comments

Page 12: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Good practiceGood practice At the beginning of each subroutine At the beginning of each subroutine

include a comment that describes each of include a comment that describes each of the inputsthe inputs Do the same for functionsDo the same for functions

If multiple people are working on the same If multiple people are working on the same project add your initials to each commentproject add your initials to each comment e.g. e.g. !rjt I added this code for a reason!rjt I added this code for a reason

If you borrow code from other projects If you borrow code from other projects make sure this is noted as well – helps make sure this is noted as well – helps others to track down bugs and gives credit others to track down bugs and gives credit where it is duewhere it is due

Page 13: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Resist the temptation to Resist the temptation to be cleverbe clever

Don’t worry that you don’t understand the details of the following Don’t worry that you don’t understand the details of the following code, the concepts should be clearcode, the concepts should be clear

The following C example is unclear even to reasonable The following C example is unclear even to reasonable programmers:programmers:

while (‘\n’, != *p++ = *q++);while (‘\n’, != *p++ = *q++);

It would have been much better to write this asIt would have been much better to write this as

while(1) {while(1) { *dest_ptr = *source_ptr;*dest_ptr = *source_ptr; if (*dest_ptr == ‘\n’)if (*dest_ptr == ‘\n’) break; /* Exit loop if done */break; /* Exit loop if done */ dest_ptr++;dest_ptr++; source_ptr++;source_ptr++;

}}

Page 14: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Compiler command line Compiler command line namesnames

C – either cc or gccC – either cc or gcc gcc is the GNU C gcc is the GNU C

compilercompiler cc is frequently cc is frequently

linked to gcc, but linked to gcc, but this is platform this is platform dependentdependent

the Intel C compiler the Intel C compiler is often denoted icc is often denoted icc

Usage: cc myfile.c –o myprog Usage: cc myfile.c –o myprog

FORTRAN – many FORTRAN – many typestypes f77 – usually links to f77 – usually links to

FORTRAN 77 FORTRAN 77 compilercompiler

f90 – usually links to f90 – usually links to FORTRAN 90 FORTRAN 90 compilercompiler

gfortran – gnu fortrangfortran – gnu fortran ifort – Intel compilerifort – Intel compiler

Usage: f77 myfile.f –o myprogUsage: f77 myfile.f –o myprog

What is the difference between compilers? Speed. The Intel compilers aretypically between 5 and 20% faster than the GNU compilers (but it varies).

Page 15: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Hello WorldHello World

CC

#include <stdio.h>#include <stdio.h>

main()main()

{{

printf(“Hello World! \n”);printf(“Hello World! \n”);

}}

FortranFortran

program helloprogram hello

print *,”Hello World!”print *,”Hello World!”

endend

main is always the name of thefirst function to be executed.{} must enclose the code tobe executed

Semi-colon ends astatement

Indented into 7th column

No format isselected by *so just printsfree form

New line character

Standard IO header

Page 16: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

C headersC headers The The #include#include forces the compiler to include forces the compiler to include

the file the file stdio.hstdio.h It will find it by checking the linking pathIt will find it by checking the linking path

Header files contain Header files contain prototypesprototypes, which are , which are abstract function definitionsabstract function definitions

stdio.hstdio.h is by far the most common, however is by far the most common, however math.hmath.h contains many mathematical contains many mathematical function definitions (log, trig etc) function definitions (log, trig etc)

Lots of headers available to, including time Lots of headers available to, including time functions, string functions, exception functions, string functions, exception handling …handling …

Page 17: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Mathematical variable Mathematical variable typestypes

CCint a; int a; Can specify whether Can specify whether

signed signed or unsignedor unsigned

Can also specify Can also specify whether short (2 whether short (2 bytes) or long (8 bytes) or long (8

bytes)bytes)

float b; float b; 4 byte floating-4 byte floating-pointpoint

double c;double c; 8 byte floating-8 byte floating-pointpoint

FORTRANFORTRANinteger a integer a signed integersigned integer**

real b real b floating-pointfloating-point**

double precision c double precision c floating-pointfloating-point** with twice with twice

the the precision precision

real*4 real*4 4 byte floating-point4 byte floating-point

real*8 real*8 8 byte floating-point8 byte floating-point

complex complex 8 byte complex pair8 byte complex pair

complex*16 complex*16 16 byte complex pair16 byte complex pair

*99 times out of 100, these correspondto 4 byte words for integer and real andan 8 byte word for double precision.Every now and again you may use a platformwhere the native words are 8 bytes

Page 18: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Character & non-Character & non-numerical typesnumerical types

CCchar a;char a;a = ‘S’;a = ‘S’;

Strings are created Strings are created by declaring arrays by declaring arrays of characters:of characters: e.g.e.g. char a[4];char a[4];

FORTRANFORTRANcharacter acharacter aa=“S”a=“S”

logicallogical type is type is available but is available but is essentially same as essentially same as integerinteger

Strings are handle Strings are handle by a character type by a character type whose length you whose length you define define e.g.e.g.

character*4 acharacter*4 a

Page 19: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Declaring ArraysDeclaring Arrays

C - exampleC - exampleint data[3];int data[3]; Indices run from 0 to Indices run from 0 to

22 For multiple For multiple

dimensionsdimensionsint data[3][int data[3][22];]; Fastest changing Fastest changing

index in memory is the index in memory is the lastlast one one

This is called Row-This is called Row-major ordermajor order

FORTRAN – exampleFORTRAN – example integer data(3)integer data(3)

Indices run from 1 to 3Indices run from 1 to 3 For multiple For multiple

dimensionsdimensions Integer data(Integer data(33,2),2)

Fastest changing Fastest changing index in memory is the index in memory is the first onefirst one

This is called column This is called column major ordermajor order

This is a very important distinction!

Page 20: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Avoid implicit variable Avoid implicit variable definitions in FORTRANdefinitions in FORTRAN

For economy, early versions of FORTRAN For economy, early versions of FORTRAN assumed if you did not declare a variable thatassumed if you did not declare a variable that Variables beginning with i-n are assumed to be integerVariables beginning with i-n are assumed to be integer Variables beginning with a-h, o-z are assumed to be realVariables beginning with a-h, o-z are assumed to be real

Do NOT use this conventionDo NOT use this convention If you don’t type a variable name correctly the If you don’t type a variable name correctly the

machine creates a new variable for you – can machine creates a new variable for you – can create horrendous bugscreate horrendous bugs

Add the statement “Add the statement “IMPLICIT NONEIMPLICIT NONE” to the ” to the beginning of your codebeginning of your code Forces you to declare all variable namesForces you to declare all variable names Same is true in F90 unfortunatelySame is true in F90 unfortunately

Thankfully C forces you to do thisThankfully C forces you to do this

Page 21: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

LoopsLoops

CCint i;int i;

for (i=1; i<=10; i++) {for (i=1; i<=10; i++) {

printf(“Iteration %d\n”,i);printf(“Iteration %d\n”,i);

}}

i++i++ means means i=i+1i=i+1

%d%d means print an means print an integerinteger

Note the general loop Note the general loop structurestructurefor (for (initial statement; initial statement; condition; iteration statementcondition; iteration statement) ) {{

bodybody

}}

FORTRANFORTRANinteger iinteger i

do i=1,10do i=1,10

print*,”Iteration “,iprint*,”Iteration “,i

end doend do

Can also specify loop Can also specify loop iteration:iteration:

do i=1,10,2do i=1,10,2 Would increment in Would increment in

steps of 2steps of 2

Page 22: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

FORTRAN 90 Array FORTRAN 90 Array SyntaxSyntax

Really useful feature for minimizing the Really useful feature for minimizing the length of vector operationslength of vector operations

Suppose we wish to add two vectors of Suppose we wish to add two vectors of length 100length 100

Normally this would require a loop:Normally this would require a loop:do i=1,100do i=1,100 a(i)=b(i)+b(i)a(i)=b(i)+b(i)end doend do

Can replace this with Can replace this with a=b+ca=b+c You can also specify bounds for the You can also specify bounds for the

operationoperation a(1:4)=b(1:4)+c(1:4)a(1:4)=b(1:4)+c(1:4) Adds elements Adds elements b(1)+c(1)b(1)+c(1) through to through to b(4)+c(4)b(4)+c(4)

Page 23: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

SummarySummary Pick a style and use it – it will pay offPick a style and use it – it will pay off Comment your code – not excessively, but Comment your code – not excessively, but

make sure it is clear and concisemake sure it is clear and concise C requires you include header filesC requires you include header files Simple programs to try if you haven’t Simple programs to try if you haven’t

written any in C/FORTRAN before:written any in C/FORTRAN before: Sum the first n numbersSum the first n numbers Sum up the values of a 2d grid of numbers Sum up the values of a 2d grid of numbers

where each grid entry a(i,j) is i*jwhere each grid entry a(i,j) is i*j Try doing this with a while loop that exits when the Try doing this with a while loop that exits when the

sum exceeds a given value sum exceeds a given value

Page 24: Fundamentals of Computational Science CISC 810 Dr Rob Thacker Dept of Physics (308A) thacker@physics.

Next LectureNext Lecture

more on C/Fortran programmingmore on C/Fortran programming