58
Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer Science, The University of Auckland, New Zealand Iolanthe II drifts off Waiheke Island near Auckland, New Zealand

Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Embed Size (px)

Citation preview

Page 1: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Computer Program

Design

Dataflow Models

John MorrisFaculty of Engineering,Mahasarakham University

previously

Electrical & Computer Enginering/Computer Science, The University of Auckland,New Zealand

Iolanthe II drifts off Waiheke Island near Auckland, New Zealand

Page 2: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Self Introduction

• John Morris, PhD(Sydney)

• Before coming to Mahasarakham ..Assoc Professor, Electrical and Computer Engineering/Computer Science, University of Auckland, New Zealand (now part-time)

• Current study

Page 3: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Brief CV• PhD – University of Sydney, Australia – optical spectroscopy

• Post-doctoral:

• National Research Council of Canada, Ottawa

• University of Tokyo ( 東京大学 ), Japan

• University positions

• University of Melbourne, Physical Chemistry

• University of Tasmania, Computer Science

• University of Western Australia, Electrical Engineering

• University of Auckland, Electrical and Computer Engineering/Computer Science

• Visiting

• Institute for Molecular Science (分子科学研究所 ), Okazaki, Japan

• Osaka University ( 大阪大学 ), Japan

• MIT, Cambridge, USA

• Case Western Reserve University, Cleveland, USA

• ChungAng University ( 중앙대학교 ), Seoul, Korea

• Kyung-pook University ( 경북대학교 ), Daegu, Korea

• Shandong University ( 山东大学 ), Jinan, China

• Kaohsiung National University, Kaohsiung, Taiwan

Page 4: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Research Interests Just in case you’re interested

• Real-time stereophotogrammetry• Attempting to mimic capabilities of human-eye

brain combination • Use two cameras connected to an FPGA or a

GPU to generate high resolution 3D ‘images’ of a scene in real-time

• Accelerating computations with attached processors – FPGA or GPU• Photo-realistic rendering for special effects in

movies

Page 5: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

When not working …

Iolanthe II

Hauraki Gulf,Auckland

Channel Island,Entrance to Hauraki Gulf

Arriving Noumea,New Caledonia, 2012

Track – New Zealand toNew Caledonia, 2012

Page 6: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Iolanthe IIISingapore – Malaysia – Koh Samui – Koh Tao – Sattahip, January 2-20, 2013

in Ocean Marina, Jomtien now!

Page 7: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Racing …

Iolanthe III

Iolanthe III races every month from Jomtien, ChonburiNext race: August 9 – Sign up now!

Page 8: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

My Thai is very limited …

• These words are not very useful for talking about computers ..

• So my lectures are 99.9% English!

Page 9: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Advanced Thai

Beware: Do not use this word in my class – your ajarn will understand it

Page 10: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Questions?

• Ask questions at any time!• There are NO silly questions!!

• Only failing to ask a question is silly!• If you didn’t understand, there are certainly other

students in the room that didn’t understand too!• They will be very happy for you to ask!!

• Not confident in English?• You will get better if you try!• Ask in Thai anyway – someone will be able to help!

• To encourage you to ask questions, the student who asks the first question about the material of the course will receive a bottle of excellent Australian red wine!

Page 11: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Computer Structure

• Modern computers are based on the von Neumann or stored program architecture

• ‘Architecture’ of a computer describes • its structure and • how the parts are linked together

Basic components of a

von Neumann machine

Page 12: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

John von Neumann• 1903 – born as Neumann János

Lajos, December 28 in Budapest, Austria-Hungary

• 1933 - Professor in Mathematics at Princeton University, New Jersey, USA changed his name to more American, John von Neumann

• Many contributions to mathematics and quantum physics

• Realized that a computer could store both program and data – the model for most modern computers!

Page 13: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Computer Structure

• In a von Neumann computer, the Central Processing Unit (CPU) has a memory which contains both instructions and data

• CPU fetches instructions which determine the calculations to be made on data (numbers) in the same memory

Basic components of a von Neumann machine

Page 14: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Computer Structure

• Memory is usually a linear array of binary bytes• Byte

• In modern systems, a byte is 8 binary bits

• Words• Bytes are grouped into words

eg 8 bytes = 64 binary bits in a modern system

• Address• Each byte is given a number (its address) from 0 to 2n-1• If n = 32, the memory contains 232 = 4,294,967,296

• Commonly called 4 GB (GigaBytes)

• 4 GB = 4 × 1024 × 1024 × 1024 = 4 × 210 × 210 × 210

Page 15: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Computer Memory• Linear array of binary bytes• Address Example

• Memory contains

• 220 = 1,048,574 bytes

• 1 MB (MegaBytes)

• 1 MB = 1 × 1024 × 1024 = 1 × 210 × 210 bytes

• Address range

• 0 to 1,048,575

• 0 to 220-1

• Each memory location holds• Instruction or

• Data0

2

4

6

8

1048574

1048572

1048570

1048568

1048566

Page 16: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Writing a program

Steps

1.Write a solution to your problem in a High Level Language such as C, C++ or Java

2.Use a compiler to convert the program to machine code

3.Transfer the machine code to memory

4.Tell the computer the start address of your program

5.Input data

6.Calculate ……………..

7.Collect results

Page 17: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

High Language Code

• Simple program to read a large set of numbers and calculate the average

• C language

#define B_LEN 16int main() { float x, sum, average, count; char buf[B_LEN]; count = 0.0; do { fgets(buf,B_LEN,stdin); x = atof( buf ); if ( x < 0.0 ) break; sum = sum+x; count = count + 1; } while ( x > 0.0 ); average = sum/count; fprintf( stdout, “Average: %f\n”, average );}

Page 18: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

High Language Code

• Most of this program is easy to understand• Comments have been added so that you can

understand the calculation

#define B_LEN 16int main() { float x, sum, average, count; char buf[B_LEN]; count = 0.0; // Set count to zero do { // Repeat reading numbers fgets(buf,B_LEN,stdin); // Read typed numbers x = atof( buf ); // Convert to binary if ( x < 0.0 ) break; // Check if finished sum = sum+x; // Add numbers in sum count = count + 1; // Count how many read } while ( x > 0.0 ); // Stop if –ve input average = sum/count; // Calculate average fprintf( stdout, “Average: %f\n”, average );} // Print average and end

Working storage

Page 19: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Loading program into memory

xsumcount

bufaverage0

847

000

#define B_LEN 16int main() { float x, sum, average, count; char buf[B_LEN]; count = 0.0; do { fgets(buf,B_LEN,stdin); x = atof( buf ); if ( x < 0.0 ) break; sum = sum+x; count = count + 1; } while ( x > 0.0 ); average = sum/count; fprintf( stdout, “Average: %f\n”,

average );}

Compiler

Text

Computerinstructions

Working storage

Data orVariables

Each variable allocated

to a memory location

Instructionstore

Datastore

MEMORY

Page 20: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Lab exercise

rldo woHell

int main() { printf( “Hello world\n” );}

Working storage

Data orVariables

Each variable allocated

to a memory location

Instructionstore

Datastore

MEMORY

TurboC Compiler

Text

Object

Computer

instructions

TurboC 2 steps1.Source object2.Object machine code

1

2

Words 32 bits or 4 bytesEach character 8 bitsSo 4 characters in each 32 bit word

Page 21: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Compilers

• Most compilers have two phases• Compiler

• Converts source (program text) to intermediate form

• One source file may only be part of a program, eg

• Factorial function

• Sine function

• …

• Linker

• ‘Link’ as in links in a chain ( โซ่� )• Linker links (joins) all the functions together

to make a program

First useful word I know in Thai

Page 22: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Designing programs

• Before building a bridge, aircraft, large building, factory, ….

• Engineers will carefully design all parts of the system

• Usually by preparing extensive diagrams of every part

• Computer programs must be designed in the same way!

Page 23: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Engineering Design

Simple parts

Complex components

All design starts with drawings ..

Page 24: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Engineering Design

Complete Structures

All design starts with drawings ..

Page 25: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Designing programs

• Computer programs must be designed in the same way!

• We use formal diagrams called

Dataflow diagrams

Page 26: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow diagrams

• Example

c = ( a2 + b2 )

• Inputs: a, b

• Output: c• Computation

a

b

cDataflow Diagram

Page 27: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow elements

• Dataflow • Data

• a and b

• flows into computation blocks

• ×, +, • and• out to the next

• Final output is result

c = ( a2 + b2 )

Dataflow Diagram

Page 28: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow elements

Dataflow DiagramInput dataVariables

Output dataResult

Data flows alongArcs

Circles areComputations

Page 29: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow elements

• Dataflow • Data

• a and b

• flows into computation blocks

• ×, +, • and• out to the next

• Final output is result

c = ( a2 + b2 )

Dataflow Diagram

a2 b2

a2 + b2

Copy a twice

(a2 + b2)

Page 30: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow elements• Example• Volume of a cone

• V = 1/3 r2h

•  

Dataflow Diagram

/3

r2

/3 r2

Copy r twice

/3 r2 h

Output: V

Constants: 0.333..,

Inputs: r, h

Page 31: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow components

• Low level (simple) diagrams• If we designed everything at

this low level, we’d never finish any design

• It would be like this ..

and need several tonnes of paper !!

(A bit like the Immigration Office!)

Problem:Find the engineer?

??

Page 32: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow components

• Diagrams can be simple to complex ..• Low level: +, -, ×, ...• Basic functions: sqrt, cos, …• Complex operations: integration, ..• System level: control a robot, simple phone,• Complex systems: aircraft systems, smart

building, smart phone, …

• We can build complex diagrams from simple ones ..

Page 33: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Computing sin(x)

• Many computations require mathematical functions• sin, cos, log, exp, ….

• Usually computed only when needed• sine computed by this equation

Usually, this expressionis truncated after ~10 termsbecause xn << n! for large n

Page 34: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Function operations

• First we need the factorials3!, 5!, 7!, ….

• We can show how to computethese with simple diagrams

• This diagramshows the usualcalculation of the factorials

• 2! 3! 4! 5! ….

Page 35: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Low level operations

• To add this to a more complex(higher level) diagram

• We hide the internal detail

• Just show the outputs

• 2! 3! 4! 5! ….

Factorial calculation

Factorial function(no internal detail!)

Page 36: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Calculating sin(x)

• sin function

Page 37: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow elements

• Example

• ex can be computed with the formula:

• Only 5 terms shown – more may be needed to obtain higher accuracy

Dataflow Diagram

ex

Page 38: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

High level function

• Again, we would builda high level function

• Hides the detail!• Allows designer to focus

on the important problem

• Re-use• Re-use = use + re- (again) = use again

• Now we have several example of the re-use idea!

• Solve a problem once, re-use that solution to solve another problem

Page 39: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Re-use and Patterns

• Re-use• Re-use = use + re- (again) = use again

• Now we have several example of the re-use idea!

• Solve a problem once, re-use that solution to solve another problem

• In English, we have a saying

‘Don’t re-invent the wheel’

Page 40: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Re-use and Patterns

• Patterns• Observe that the sin(x) function and the e(x)

function have similar structure!

• I built the e(x) DFD (data flow diagram) by

• Copying the sin(x) one and • Modifying it

• Re-use and patterns Save time Reduce probability of error More efficient programs

• Use an efficient solution to one problem to solve a similar one!

Page 41: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Decisions

• Many computations must chooseeg min or max?

• Input a, b ? • Which is min? max?

• Use a diamond surrounding a choice

• Example•Rectangle must be uprightfor the next step in a calculation•Diamond contains a condition – h > w ?

•Edges are marked Y or N

Page 42: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Repetitive operations

• One advantage of computers is• Precise, fast repetition of large numbers

(millions or more) of calculations which are• Boring• Complex• Repetitive• Error prone

• Simple diagrams shown already are not powerful enough for efficient design

• We need additional symbols!

• Summation example

• Sum n numbers and calculate max, min, average

Page 43: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Repetition

• Calculation of sum

• Iterative computation• Inputs

• Stream of x values

• n – number of inputs

• Values recycled through

+ modules

• Decision ‘cnt > n’terminates sum when cnt > n

• Final output: sum

Sum is re-cycledand becomes thenew input

Page 44: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Building larger blocks

• Iterations make functions like• sin, cos, ..

more compact

Page 45: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data – Sources and Sinks

• To complete a system diagram, add• Sources or Inputs of data

• Devices• Keyboards• Discs• Data acquisition devices• Communication links

• Networks

• Wireless

• Memory• Stored in the computer’s memory by

previous programs

Page 46: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data – Sources and Sinks

• Sinks or Outputs for data• Devices

• Displays• Discs• Motors, actuators, …

• Anything that controls a machine, vehicle, robot, …

• Communication links• Networks

• Wireless

• Memory• Stored in the computer’s memory for use by

the next program

Page 47: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Electrical problem

• You want a simple program to compute the voltage across a resistor when a current flows• Ohm’s Law: V = I * R

• So make a program that • Inputs

• I• R

• Outputs• V

• It should also tell the user when to input I and R

/*Ohm Law Program*/

#include

<stdio.h>#include<conio.h>main(){ float i,r;clrscr();printf("Ohm Law Program\n");printf("Enter Current =");scanf("%f",&i);printf("Enter Resistance =");scanf("%f",&r);printf("Voltage=i*r=%f  Volt",i*r);getch();}

Page 48: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Electrical problem

• DFD• Inputs from

keyboard• I• R

• Outputs to screen

• V• Prompts to

direct user to screen

Input and outputdevices

Page 49: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Electrical problem

/*Ohm Law Program*/#include <stdio.h>#include<conio.h>main(){ float i,r;clrscr();printf("Ohm Law Program\n");printf("Enter Current =");scanf("%f",&i);printf("Enter Resistance =");scanf("%f",&r);printf("Voltage=i*r=%f  Volt",i*r);getch();}

• In C, this DFD becomes this program – see lab this afternoon!

Page 50: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Sources and Sinks – Input and Output

All the rectangles or open boxes are sources or sinks

No standards for sources andsinks

Use an appropriate symbol!!

Pictures representing thedevice

Page 51: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

System dataflows

Diagrams can be Informal

• High level design requirements• Sketched by hand

or Formal

• Defined symbols• Correspond directly with program modules

Informal

Informal

Formal

Page 52: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Formal diagrams

• Informal diagrams are a good start!!

but

• Formal diagrams help make sure a design is correct

• Formal diagrams can be converted to actual program code in a high level languate like C, C++ or Java

Page 53: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Formal diagrams

• Formal diagrams help make sure a design is correct

• Diagrams reveal design faults• Missing data• Incorrect data• Missing operations

• Conversions• Summations• …

• Wrong orders

• Function g(x) applied

before function f(x)

Page 54: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Many design notations

• One automated tool

Page 55: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Data flow diagrams

• Many, many styles!!

Page 56: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Summary

• Design first!!• Diagrams show

• Data flow • Arcs – data• Boxes – data conversion

• Functions, transformations• Decisions

• Commonly diamonds• Selection boxes

• Multiplexors, selectors, ..

Page 57: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

SUMMARYProgram Design

Page 58: Computer Program Design Dataflow Models John Morris Faculty of Engineering, Mahasarakham University previously Electrical & Computer Enginering/ Computer

Summary

• Design first!!• Informal diagrams

• Useful• Explaining design to users

but

• Formal diagrams • Show design errors

• Missing data• Wrong data• etc

• Convert to actual program code in C, C++ or Java