Program Structure. r Memory management is designed to be transparent to the user program r As a...

13
Program Structure

Transcript of Program Structure. r Memory management is designed to be transparent to the user program r As a...

Page 1: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Program Structure

Page 2: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Program Structure

Memory management is designed to be transparent to the user program

As a programmer you typically do not think about how memory is organized.

Page 3: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Example 1

Consider the following code segments:

int data[128][128];for (j = 0; j <128; j++) for (i = 0; i < 128;i++)

data[i,j] = 0;

Does it matter which you use?

int data[128][128];for (i = 0; i <128; i++) for (j = 0; j < 128;j++)

data[i,j] = 0;

Page 4: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Example 1

data[1][0] data[1][1] data[1][2] …

… data[1][127]

With 128 word pages (1 word is one integer)

Each row is stored in one page (row major order)

data[0][0] data[0][1] data[0][2] …

… data[0][127]

data[127][0] data[127][1] data[127][2] …

… data[127][127]

Page 0

Page 1

Page 127

Page 5: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Example 1int data[128][128];

for (j = 0; j <128; j++) for (i = 0; i < 128;i++)

data[i,j] = 0; Access pattern when j is 0: data[0,0],

data[1,0], data[2,0] Are data[0,0] and data[1,0] on the same

page? No

So while j is 0 we have 128 page faults.

Page 6: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Example 1int data[128][128];

for (j = 0; j <128; j++) for (i = 0; i < 128;i++)

data[i,j] = 0; Access pattern when j is 1: data[0,1],

data[1,1], data[2,1] Are these on the same page? No Now what if the OS allocates fewer than

128 frames You will page fault on data[0,1]

Number of total page faults is 128*128 = 16,384

Page 7: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Example 2

Consider the following code segments:

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

for (k = 0; k < n; k++)

c[i,j] = c[i,j] + a[i,k] * b[k,j];

Which one?

for (i = 0; i < n; i++)

for (k = 0; k < n; k++)

for (j = 0; j < n; j++)

c[i,j] = c[i,j] + a[i,k] * b[k,j];

Page 8: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Example 2 for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

for (k = 0; k < n; k++)

c[i,j] = c[i,j] + a[i,k] * b[k,j];

k changes for each iteration Access pattern for b[k,j] when i is 0, j is 0 is:

b[0,0],b[1,0],b[2,0] etc By incrementing k we are skipping over an entire row of the

matrix

Access pattern for c[i,j] when i is 0, j is 0 and for any value of k is c[0,0]

Access pattern for a[i,k] when i is 0 is: a[0,0], a[0,1],…. All accesses come from the same row

Page 9: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Example 2 for (i = 0; i < n; i++)

for (k = 0; k < n; k++)

for (j = 0; j < n; ++)

c[i,j] = c[i,j] + a[i,k] * b[k,j];

j changes for each iteration Access pattern for b[k,j] when i is 0, k is 0 is:

b[0,0],b[0,1],b[0,2] etc All accesses are from the same row

Access pattern for c[i,j] when i is 0, k is 0 is: c[0,1], c[0,1], c[0,2] etc All accesses are from the same row

Access pattern for a[i,k] when i is 0, k is 0 and any j is: a[0,0] .

Page 10: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Program Structure

Careful selection of data structures and programming structures can increase locality and hence lower the page fault-rate

Page 11: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Singly-linked lists vs. 1D-arrays

Array Singly-linked list

Fixed size: Resizing is expensive Dynamic size

Insertions and Deletions are inefficient: Elements are usually shifted

Insertions and Deletions are efficient: No shifting

Random access i.e., efficient indexing No random access Not suitable for operations requiring accessing elements by index such as sorting

No memory waste if the array is full or almost full; otherwise may result in much memory waste.

Extra storage needed for references; however uses exactly as much memory as it needs

Sequential access is faster because of greater locality of references [Reason: Elements in contiguous memory locations]

Sequential access is slow because of low locality of references [Reason: Elements not in contiguous memory locations]

Page 12: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Other

Stack has good locality Hash table scatters references for poor

locality

Page 13: Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.

Summary

We have briefly reviewed issues related to program structure.