ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh - - PowerPoint PPT Presentation

engn2219 comp6719
SMART_READER_LITE
LIVE PREVIEW

ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh - - PowerPoint PPT Presentation

ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh Sankaranarayana Semester 1, 2020 1 Course Revision 2 Final exam Final exam details and practice exams are up on the course website 3 Examinable material The examinable


slide-1
SLIDE 1

ENGN2219/COMP6719

Computer Architecture & Simulation Ramesh Sankaranarayana Semester 1, 2020

1

slide-2
SLIDE 2

Course Revision

2

slide-3
SLIDE 3

Final exam

Final exam details and practice exams are up on the course website

3

slide-4
SLIDE 4

Examinable material

The examinable material comprises the contents of the lectures and labs. This includes the lectures on statistics for simulation. The lectures in Week 11 on communication and architectures are not examinable. There will be a mix of theoretical and lab based questions, on a rough 50-50

  • split. There will be 5-6 main questions in the exam. A sample exam paper

will be provided. Look at the sample paper, the mid-semester exam and the labs/labtests for a guide on questions of a practical nature. Roughly 60% of the questions will be on Architecture and 40% on Simulation.

4

slide-5
SLIDE 5

Exam environment

The exam environment will be similar to the one that you had for the mid- semester exam - so gitlab based, open book. There will be no use of Proctorio. The gitlab environment is itself being upgraded, to minimize the risk of a repeat of the forking/pushing issues that took place during the midsem. This will be stress tested before the actual exam.

5

slide-6
SLIDE 6

In addition, we are going to provide you with exams ahead of time, but

  • encrypted. The encryption key will only be provided at the scheduled start
  • f the exam. This will ensure that all of you have forked and cloned the repo

well ahead of the scheduled exam time. As for pushing, the upgrades to the environment will minimize any latency

  • issues. In any case, what is important is that you commit your exams to

your local repo by the scheduled time. It does not matter if the actual push to your gitlab fork gets delayed a bit. However, it is really important that you commit and push regularly. Do so after you complete each question (or major subquestion), or every 30 mins.

6

slide-7
SLIDE 7

Revision Part 1 - Simulation

7

slide-8
SLIDE 8

Content

Questions will cover the content covered in the following topics: Week 7: Simulation Introduction Week 8: Simulation methods, Monte Carlo, Discrete Event Week 9: Dynamics Systems, System Dynamics, Verification and Validation Week 10: Statistics for Simulation

8

slide-9
SLIDE 9

Programming

Programming questions will cover the following topics: Simple Monte Carlo, Discrete Event. eg, coin toss Visualisation of plots Simple dynamical systems. eg, random walk Interpolation/Extrapolation Numerical Integration/Differentiation Use of odeint You will not be asked to do statistics related programming.

9

slide-10
SLIDE 10

Sample questions - Theory

  • 1. What are the objectives of simulation?
  • 2. Differentiate between the Eulerian and Lagrangian approaches to

modeling space. Give an example for each.

  • 3. What are the main features of Discrete-Event simulation?
  • 4. What is a dynamical system? How are such systems represented?
  • 5. What is system dynamics? How are systems represented in this context?
  • 6. What are the four components of Boyd’s OODA loop? What happens if

your competitor is quicker in going around the loop than you?

10

slide-11
SLIDE 11
  • 7. Differentiate between verification and validation. What are some of the

difficulties of verification and validation?

  • 8. Briefly describe the three common measures of central tendency.
  • 9. What are the five values included in a five number summary? What is a

common approach taken to represent this summary?

  • 10. Suppose you have paired bivariate data about temperature and

electricity use. You want to find out if there is any significant relationship between the two, and if so, use it to assist you in making

  • predictions. Would you use correlation or regression? Briefly explain.

11

slide-12
SLIDE 12

Sample questions - Problems

12

slide-13
SLIDE 13

Coin toss

How would you use the Monte Carlo approach to calculate the probability

  • f an unbiased coin landing on its head?

Use repeated iterations Use a random number between 0 and 1 to imitate a coin toss. If the number is, say, , it is heads, else tails. Average the total number of heads over the number of iterations

<= 0.5

13

slide-14
SLIDE 14

cointoss.py

import random # Function takes in the probability of heads as a number betwee # 0 and 1, and the number of tosses. Returns the average of the # number of heads over the number of tosses. def cointoss(headsprob, numtosses): heads = 0 for toss in range(numtosses): currentoss = random.random() if (currentoss <= headsprob): heads += 1 return(heads/numtosses)

14

slide-15
SLIDE 15

cointoss.py (contd)

# Print this value for say, 1000 tosses print (cointoss(0.5, 1000))

15

slide-16
SLIDE 16

Boiling temperature of water

The boiling temperature of water at various altitudes is given below: 600 1500 2300 3000 6100 7900 100 98.8 95.1 92.2 90 81.2 75.6 Use linear interpolation to calculate the boiling temperature at 5000m. Draw a plot of the points. Now, do the same using cubic spline interpolation. What if you needed to find the temperature at 8500m?

TB h h(m) T C) (∘

16

slide-17
SLIDE 17

water.py

import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt height = [0,600,1500,2300,3000,6100,7900]; temp = [100,98.8,95.1,92.2,90,81.2,75.6]; flinear = interp1d(height,temp) fcubic = interp1d(height,temp,kind='cubic') heightnew = np.linspace(0,7900,num=100) plt.plot(height,temp,'o',heightnew, flinear(heightnew), '-', he plt.legend(['data', 'linear', 'cubic-spline'], loc='best') plt.title('Linear and Cubic Spline Interpolation') plt.show()

17

slide-18
SLIDE 18

water.py

18

slide-19
SLIDE 19

Sunflower growth

The growth data of a sunflower plant is given below: 1 3 5 7 9 11 13 22 51 127 202 227 248 252 Use regression to curve-fit the data with a second-order polynomial. Use the polynomial to estimate the height in week 6. Draw a plot of the data and the fitted curve. Now do the same using a third-order polynomial and a tenth-order polynomial. In each case, calculate the error using the sum of squares approach.

Week Height(cm)

19

slide-20
SLIDE 20

sunflower.py

import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt from pylab import * week = [1,3,5,7,9,11,13]; height = [22,51,127,202,227,248,252]; z2 = np.polyfit(week,height,2) # second order polynomial f2 = poly1d(z2) heightfit2 = f2(week) sse2 = round(((height - heightfit2)**2).sum(),2) z3 = np.polyfit(week,height,3) # third order polynomial f3 = poly1d(z3)

20

slide-21
SLIDE 21

sunflower.py (contd.)

heightfit3 = f3(week) sse3 = round(((height - heightfit3)**2).sum(),2) z10 = np.polyfit(week,height,10) # tenth order polynomial f10 = poly1d(z10) heightfit10 = f10(week) sse10 = round(((height - heightfit10)**2).sum(),2) plt.plot(week,height,'o',week, heightfit2,'-',week,heightfit3,' plt.legend(['data', 'second', 'third', 'tenth'], loc='best') plt.title('Sunflower Growth') plt.text(1,1,f'SSE2 = {sse2}') plt.text(5,1,f'SSE3 = {sse3}') plt.text(9,1,f'SSE10 = {sse10}') plt.show()

21

slide-22
SLIDE 22

sunflower.py

22

slide-23
SLIDE 23

Numerical Differentiation

Let be a vector with values ranging from to . Let be a function defined as follows: Find the derivative of the above function using forward differentiation. Plot the function and against x, on the same graph with blue and red colors,

  • respectively. Give the graph an appropriate title and labels.

x −2π 2π y y = 50 ∗ sin(x) + 3 ∗ tan(x) − 5 ∗ − x − 30 x2 y y′

23

slide-24
SLIDE 24

numerical-differentiation.py

import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt from pylab import * x = linspace(-2*np.pi,2*np.pi,180); y = 50*np.sin(x) + 3*np.cos(x) - 5*x**2 - x - 30; deriv_y = np.diff(y)/np.diff(x);

24

slide-25
SLIDE 25

numerical-differentiation.py (contd.)

xd = x[0:-1]; # forward difference x values plt.plot(x,y,'b',xd,deriv_y,'r') plt.title('Polynomial-Trig Function') plt.ylabel("y and y'"); plt.xlabel('x') plt.legend(['y',"y'"],loc='best') plt.show();

25

slide-26
SLIDE 26

numerical-differentiation.py

26

slide-27
SLIDE 27

Potential energy

The variation of gravitational acceleration with altitude is given by: where = 6371 km is the radius of the earth and = 9.81 is the gravitational acceleration at sea level. The change in the gravitational potential energy, , of an object with mass kg that is raised from the earth to a height km is given by:

g y g = R2 (R + y)2 g0 R g0 m/s2 ΔU m h ΔU = mgdy ∫

h

27

slide-28
SLIDE 28

Determine the change in the potential energy of a satellite with a mass of 500kg that is raised from the surface of the earth to a height of 800 km. Hint: Use the function.

quad

28

slide-29
SLIDE 29

potentialEnergyIntegration.py

import numpy as np from scipy import integrate import matplotlib.pyplot as plt from pylab import * def gravity(altitude): # Gravitational acceleration function # altitude in km; gravity in ms^-2 # Earth's radius (km) R = 6371; # Mean gravitational acceleration @ ground-level g0 = 9.81; return((R**2)*g0)/((R+altitude)**2);

29

slide-30
SLIDE 30

potentialEnergyIntegration.py (contd.)

# given parameters # mass (kg) of satellite m = 500; # height (km) from Earth's surface h = 800; gpevariation = integrate.quad(gravity,0,h); gpeChange = m*gpevariation[0]; #print(m,h); #print(gpeChange); print("The difference in gravitational potential energy of the

30

slide-31
SLIDE 31

Velocity of a falling object

The velocity of an object that falls freely due to the earth’s gravity can be modeled by the equation: where is the mass of the object, = 9.81 , and is a constant. Solve the equation for for the case = 5 kg, = 0.05 kg/m, s and (0) = 0 m/s. Make a plot of as a function of time.

v m = − mg + k dv dt v2 m g m/s2 k v m k 0 ≤ t ≤ 15 v v

31

slide-32
SLIDE 32

fallingObject.py

mport numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt #from pylab import * # given relationship: # m * dv/dt = -mg + kv^2 # rearrange into dv/dt = (-mg + kv^2)/m # m=5kg, g=9.81ms^-2, k=0.05 # def velocityOfObject(v,t): return ((-5*9.81)+(0.05*(v**2)))/5;

32

slide-33
SLIDE 33

fallingObject.py (contd.)

v0 = 0 t = np.linspace(0,15) v = odeint(velocityOfObject,v0,t) plt.plot(t,v) plt.title('Velocity of Falling Object') plt.xlabel('t') plt.ylabel('v') plt.show();

33

slide-34
SLIDE 34

fallingObject.py

34

slide-35
SLIDE 35

info

Study event run by CSSA next week: Monday, from 6pm onwards, in Hancock West Details will be posted on Wattle

35

slide-36
SLIDE 36

Revision Part 2 - Architecture

36

slide-37
SLIDE 37

Content

Questions will cover the content covered in the following topics: Week 1: Introduction and Logic Week 2: ALU Operations Week 3: Memory Operations Week 4: Control Structures Week 5: Functions Week 6: Data Structures Week 11: Sequential circuits (no architecture/networking)

37

slide-38
SLIDE 38

Programming

Programming questions will cover the following topics: All of the assembly language programming covered in the lectures/labs/labtests

38

slide-39
SLIDE 39

Sample questions - Theory

  • 1. Using an example, show how using twos complement representation

makes it easy to do subtraction.

  • 2. What is the main difference between combinational and sequential

logic circuits? Why do we need sequential logic circuits?

  • 3. What are the main components of the CPU and what are their roles?
  • 4. Why is there a memory hierarchy, where you have registers, cache,

main memory and secondary memory? Briefly explain.

39

slide-40
SLIDE 40
  • 5. How would the number 0xD5 be stored at memory addresses a and

a+1 in big-endian and little-endian formats?

  • 6. What is the difference between register and immediate instructions? If

you have a situation where you can use either one, which one would you use and why?

  • 7. What is the load/store architecture? What is the advantage of this

approach?

  • 8. How might you implement the absolute value function y=abs(x) using

if-then-else statement labels in assembly? Assume that x is in register

r0 and the result y should be in the same register r0.

40

slide-41
SLIDE 41
  • 9. How would you pass parameters by reference to a function in

assembly?

  • 10. What is the need for a function calling convention? What are the things

that you do at the beginning of a function (prologue) and at the end of a function (epilogue)?

41

slide-42
SLIDE 42

Sample questions - Problems

42

slide-43
SLIDE 43

Party Disagreement Score

.syntax unified .global main @ READ ALL INSTRUCTIONS CAREFULLY @ You've been provided a data structure below for characters @ in a fantasy video game, similar to the ones presented in @ lecture slides. The data structure is as follows: @ Party size 2 bytes (stores the number of players in the pa

43

slide-44
SLIDE 44

@ Followed by a structure for each player, as follows: @ HP 2 bytes (stores health points of the character @ Mana 2 bytes (stores the mana points of the character us @ Alignment 4 bytes (stores the alignment of the player as a SI @ (-3 = Very Evil, -2 = Evil, -1 = Weak Evil, 0 = Neutral, 1 = @ Name 16 bytes (stores the name of the character as a nul @ are added so the length is always @ Class 16 bytes (stores the class of the player as a null @ are added so the length is always @ Each player takes exactly 40 bytes in total to store.

44

slide-45
SLIDE 45

@ Each player has a player ID, starting from zero, based on the @ In the example below: @ Player: ID: @ Astrid 0 @ Ulric 1 @ Zendril 2 @ Azar 3 @ See the .data section for examples

45

slide-46
SLIDE 46

@ Question 1 : Party Disagreement Score @ Party members of vastly different alignment don't get along w @ a party comprised of both good-aligned and evil-aligned party @ can affect how well the party can work together as a team. @ The party disagreement score is defined to be the difference @ between the highest alignment and lowest alignment party memb @ For example, given the party in the .data section below, @ The highest alignment is Azar, with an alignment of 3 (Very G @ The lowest alignment is Zendril, with an alignment of -1 (Wea @ The disagreement score is therefore 3 - (-1) = 4

46

slide-47
SLIDE 47

@ Write a function "disagreement" @ INPUTS: (in r0) the address of the party data in memory @ OUTPUTS: (in r0) the disagreement score for that party @ Your function should work for any party, of any size. @ DO NOT HARDCODE THE ANSWER main: ldr r0, =party bl disagreement end: nop b end

47

slide-48
SLIDE 48

disagreement: @ put your code here nop bx lr

48

slide-49
SLIDE 49

@ You may NOT add labels in the data section @ to make it easier to read information directly out. .data party: .hword 4 @party_size .hword 21 @ HP .hword 117 @ MANA .word 2 @ Alignment .ascii "Astrid Gold\0\0\0\0\0" @Player ID 0 .ascii "Priestess\0\0\0\0\0\0\0"

49

slide-50
SLIDE 50

.hword 0 @ HP .hword 0 @ MANA .word 0 @ Alignment .ascii "Ulric Strongarm\0" @Player ID 1 .ascii "Mercenary\0\0\0\0\0\0\0" .hword 67 @ HP .hword 31 @ MANA .word -1 @ Alignment .ascii "Zendril Silver\0\0" @Player ID 2 .ascii "Dark Elf\0\0\0\0\0\0\0\0"

50

slide-51
SLIDE 51

.hword 84 @ HP .hword 12 @ MANA .word 3 @Alignment .ascii "Azar the Worthy\0" @Player ID 3 .ascii "Paladin\0\0\0\0\0\0\0\0\0"

51

slide-52
SLIDE 52

Sample solution

.syntax unified .global main main: ldr r0, =party bl disagreement end: nop b end disagreement:

52

slide-53
SLIDE 53

Assumptions?

@ Note that for this code, the disagreement score for a par @ Doesn't really matter, as the behaviour can be undefined @ The score is only defined for parties with one or more pl

53

slide-54
SLIDE 54

@ r0 = address of current player, and at the end, return va @ r1 = current lowest alignment @ r2 = current highest alignment @ r3 = party size @ r4 = scratch mov r1, 100 @ dummy value always higher than any alignment mov r2, -100 @ dummy value always lower than any alignment ldrh r3, [r0], 2 @ r0 now points at start of first playe

54

slide-55
SLIDE 55

disagreement_loop: cmp r3, 0 @ if no more players beq disagreement_calculate @ compute disagreement score ldr r4, [r0, 4] @ load player alignment cmp r4, r1 bgt skip_set_low_align @ skip if player alignment > lo mov r1, r4 skip_set_low_align: cmp r4, r2 blt skip_set_high_align @ skip if player alignment < hi mov r2, r4

55

slide-56
SLIDE 56

skip_set_high_align: add r0, 40 @ move to next player sub r3, 1 @ decrement player counter b disagreement_loop disagreement_calculate: sub r0, r2, r1 @ score = highest - lowest bx lr

56

slide-57
SLIDE 57

Greatest common divisor

The greatest common divisor of two numbers and is defined to be the largest integer that divides both and . We denote the greatest common divisor of and as . An algorithm for computing the greatest common divisor of two positive numbers is shown below.

a > 0 b > 0 a b a b gcd(a, b)

function gcd(a,b): while(a != b): if(a > b): a := a - b else: b := b - a return a

57

slide-58
SLIDE 58

Write a program that computes the greatest common divisor of two non- negative numbers. You may assume that in memory, under the label numbers, there will be two different positive numbers stored as bytes. You have two tasks to complete. Task 1: Load one of the numbers into R0, and the other number into R1. The

  • rder doesn’t matter.

Task 2: You should compute the greatest common divisor of the two numbers, and save the result into R0. Your code should work for any two strictly positive inputs. Do not hardcode your answer to the given numbers.

58

slide-59
SLIDE 59

.syntax unified .global main main: @ Task 1: Read the bytes from `numbers` and save them into R0 nop gcd: @ Task 2: Compute the gcd of R0 and R1, store the result in R nop end: b end .data numbers: @Make sure you code works for other numbers too! @ You may change the numbers here. .byte 15, 21 @ gcd(15,21) = 3

59

slide-60
SLIDE 60

Sample solution

.syntax unified .global main main: @ Task 1: Read the bytes from `numbers` and save them into R0 ldr r2, =numbers ldrb r0, [r2] ldrb r1, [r2, #1] gcd: @ Task 2: Compute the gcd of R0 and R1, store the result in R @ gcd: @ r0 := a @ r1 := b cmp r0, r1 @ while(a != b) beq end bpl change_a @ if(a >= b) then goto change_a

60

slide-61
SLIDE 61

change_b: @ change_b: sub r1, r1, r0 @ b := b - a b gcd @ goto gcd change_a: @ change_a: sub r0, r0, r1 @ a := a - b b gcd @ goto gcd end: @result is already in r0 b end .data numbers: @Make sure you code works for other numbers too! .byte 15,21 @ gcd(15,21) = 3

61

slide-62
SLIDE 62

Sum of numbers

Write a program that computes the sum of all numbers in a given range. You may assume that in memory, under the label numbers, there will be two different positive numbers stored as half-words. You have two tasks to complete. Task 1: Store the smaller number into R1, and the larger number into R2. You may assume the numbers will always be different. Task 2: You should compute the sum of all numbers from the smaller number to the bigger number, inclusive. Save the result into R0. The numbers given in the code below are only an example, and your code should work for any inputs satisfying the constraints above. Do not hardcode your answer to the given numbers.

62

slide-63
SLIDE 63

.syntax unified .global main main: task1: @ Task 1: Read from memory address 'range', @ and store the numbers into R1 and R2. nop task2: @ Task 2: Compute the sum from R1 to R2, and save the resul nop

63

slide-64
SLIDE 64

end: b end .data range: @ You can modify these numbers to test your code. .hword 4, 20 @ R0 should contain 4 + 5 + … + 19 + 20 = 204 @.hword 1, 5 @ R0 should contain 1 + 2 + 3 + 4 + 5 = 15 @.hword 3, 7 @ R0 should contain 3 + 4 + 5 + 6 + 7 = 25

64

slide-65
SLIDE 65

Sample solution

.syntax unified .global main main: task1: @ Task 1: Read from memory address 'range', @ and store the numbers into R1 and R2. nop @ r0 will hold the result mov r0, 0

65

slide-66
SLIDE 66

@ Read the numbers ldr r0, =range ldrh r1, [r0] ldrh r2, [r0, #2] @ ensure that r1 has the lower value cmp r1, r2 @ continue to task2 if r1 <= r2 ble task2 @ Interchange the values in r1 and r2 mov r3,r1 mov r1,r2 mov r2,r3

66

slide-67
SLIDE 67

task2: @ Task 2: Compute the sum from R1 to R2, and save the resul @ Add the current number in r1 to r0 add r0, r1 @ Check if we are done cmp r1, r2 beq end @ Else, add 1 to r1 and go to task2 add r1, #1 b task2

67

slide-68
SLIDE 68

end: b end .data range: @ You can modify these numbers to test your code. .hword 4, 20 @ R0 should contain 4 + 5 + … + 19 + 20 = 204 @.hword 1, 5 @ R0 should contain 1 + 2 + 3 + 4 + 5 = 15 @.hword 3, 7 @ R0 should contain 3 + 4 + 5 + 6 + 7 = 25

68

slide-69
SLIDE 69

That’s all, folks!!!

69

slide-70
SLIDE 70

Thanks much and good luck with your exams!!!!

70