Memory and Caches in Computer Systems, Study notes of Computer Networks

The topics of memory, data, integers, floats, machine code, C, Java, x86 assembly, procedures, stacks, arrays, structs, caches, virtual memory, and more. It also talks about the problem of processor-memory bottleneck and the solution of caches. plots and diagrams to explain the concepts. It is a useful resource for computer science students studying memory and caches in computer systems.

Typology: Study notes

2022/2023

Uploaded on 05/11/2023

ekayavan
ekayavan 🇺🇸

4.6

(39)

258 documents

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University*of*Washington*
Roadmap*
1*
car *c = malloc(sizeof(car));
c->miles = 100;
c->gals = 17;
float mpg = get_mpg(c);
free(c);
Car c = new Car();
c.setMiles(100);
c.setGals(17);
float mpg =
c.getMPG();
get_mpg:
pushq %rbp
movq %rsp, %rbp
...
popq %rbp
ret
Java:*
C:*
Assembly*
language:*
Machine*
code:*
0111010000011000
100011010000010000000010
1000100111000010
110000011111101000011111
Computer*
system:*
OS:*
Memory*&*data*
Integers*&*floats*
Machine*code*&*C*
x86*assembly*
Procedures*&*stacks*
Arrays*&*structs*
Memory*&*caches*
Processes*
Virtual*memory*
Memory*allocaJon*
Java*vs.*C*
Spring*2014* Memory*and*Caches*
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

Partial preview of the text

Download Memory and Caches in Computer Systems and more Study notes Computer Networks in PDF only on Docsity!

Roadmap

*car c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100); c.setGals(17); float mpg = c.getMPG(); get_mpg: pushq %rbp movq %rsp, %rbp ... popq %rbp ret

C: Java:

Assembly

language:

Machine

code:

0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111

Computer

system:

OS:

Memory & data Integers & floats Machine code & C x86 assembly Procedures & stacks Arrays & structs Memory & caches Processes Virtual memory Memory allocaJon Java vs. C

How does execuJon Jme grow with SIZE?

int array[SIZE]; int A = 0; for (int i = 0 ; i < 200000 ; ++ i) { for (int j = 0 ; j < SIZE ; ++ j) { A += array[j]; } }

SIZE

TIME

Plot

Making memory accesses fast!

¢ Cache basics ¢ Principle of locality ¢ Memory hierarchies ¢ Cache organizaJon ¢ Program opJmizaJons that consider caches

Problem: Processor-­‐Memory Bo[leneck

Main

Memory

CPU Reg Processor performance doubled about every 18 months Bus bandwidth evolved much slower Core 2 Duo: Can process at least 256 Bytes/cycle Core 2 Duo: Bandwidth 2 Bytes/cycle Latency 100 cycles Problem: lots of wai4ng on memory 5

cycle = single fixed-­‐Jme

Spring 2014 machine step Memory and Caches

Cache

¢ English definiJon: a hidden storage space for provisions, weapons, and/or treasures ¢ CSE definiJon: computer memory with short access Jme used for the storage of frequently or recently used instrucJons or data (i-­‐cache and d-­‐cache) more generally, used to opJmize data transfers between system elements with different characterisJcs (network interface cache, I/O cache, etc.)

General Cache Mechanics

Cache^8 9 14 Memory Larger, slower, cheaper memory viewed as parJJoned into “blocks” or “lines” Data is copied in block-­‐sized transfer units Smaller, faster, more expensive memory caches a subset of the blocks (a.k.a. lines)

General Cache Concepts: Miss

Cache^8 9 14 Memory

Request: 12 Data in block b is needed

Block b is not in cache:

Miss!

Block b is fetched from

memory

Request: 12 12

Block b is stored in cache

  • Placement policy: determines where b goes
  • Replacement policy: determines which block gets evicted (vicAm)

Why Caches Work

¢ Locality: Programs tend to use data and instrucJons with addresses near or equal to those they have used recently

Why Caches Work

¢ Locality: Programs tend to use data and instrucJons with addresses near or equal to those they have used recently ¢ Temporal locality: § Recently referenced items are likely

to be referenced again in the near future

¢ SpaJal locality? block

Why Caches Work

¢ Locality: Programs tend to use data and instrucJons with addresses near or equal to those they have used recently ¢ Temporal locality: § Recently referenced items are likely

to be referenced again in the near future

¢ SpaJal locality: § Items with nearby addresses tend

to be referenced close together in Ame

§ How do caches take advantage of this? block block

Locality Example

int sum_array_rows(int a[M][N]) { int i, j, sum = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) sum += a[i][j]; return sum; } a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

Locality Example

int sum_array_rows(int a[M][N]) { int i, j, sum = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) sum += a[i][j]; return sum; } a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] 1: a[0][0] 2: a[0][1] 3: a[0][2] 4: a[0][3] 5: a[1][0] 6: a[1][1] 7: a[1][2] 8: a[1][3] 9: a[2][0] 10: a[2][1] 11: a[2][2] 12: a[2][3] stride-­‐

Locality Example

int sum_array_cols(int a[M][N]) { int i, j, sum = 0; for (j = 0; j < N; j++) for (i = 0; i < M; i++) sum += a[i][j]; return sum; } a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] 1: a[0][0] 2: a[1][0] 3: a[2][0] 4: a[0][1] 5: a[1][1] 6: a[2][1] 7: a[0][2] 8: a[1][2] 9: a[2][2] 10: a[0][3] 11: a[1][3] 12: a[2][3] stride-­‐N

Locality Example

int sum_array_3d(int a[M][N][N]) { int i, j, k, sum = 0; for (i = 0; i < N; i++) for (j = 0; j < N; j++) for (k = 0; k < M; k++) sum += a[k][i][j]; return sum; } ¢ What is wrong with this code? ¢ How can it be fixed?