Lecture 24: Memory and Locality
CSE 373: Data Structures and Algorithms
1
Announcements
UW IT Course Evaluations - 5 EC points for everyone if at least 90% fill out
P4 due today
Final Exam
Friday’s lecture
CSE 373 22 SP – CHAMPION
2
Review: Binary, Bits and Bytes
CSE 373 SP 18 - KASEY CHAMPION
3
Thought experiment
CSE 373 SP 18 - KASEY CHAMPION
4
What do these two methods do?
What is the big-Θ
Θ(n*m)
Check in question: tinyurl.com/Summer373L24
Incorrect Assumptions
CSE 373 SP 18 - KASEY CHAMPION
5
Lies!
RAM (Random-Access Memory)
- RAM goes by a ton of different names: memory, main memory, RAM are all names for this same thing.
CSE 373 SP 19 - KASEY CHAMPION
6
RAM can be represented as a huge array
CSE 373 SP 19 - KASEY CHAMPION
7
=
This is a main takeaway
If you’re interested in deeper than this : https://www.youtube.com/watch?v=fpnE6UAfbtU or take some EE classes?
RAM:
Arrays
A rough view of arrays and linked lists
CSE 373 SP 19 - KASEY CHAMPION
8
int[] array = new int[3];
array[0] = 3;
array[1] = 7;
array[2] = 3;
Node front = new Node(3);
front.next = new Node(7);
front.next.next = new Node(3);
3
7
3
3
7
3
(drawing singly linked list instead of doubly because drawings are hard / the two are similar)
Memory Architecture
CSE 373 SP 18 - KASEY CHAMPION
9
CPU Register
L1 Cache
L2 Cache
RAM
Disk
What is it? | Typical Size | Time |
The brain of the computer! | 32 bits | ≈free |
Extra memory to make accessing it faster | 128KB | 0.5 ns |
Extra memory to make accessing it faster | 2MB | 7 ns |
Working memory, what your programs need | 8GB | 100 ns |
Large, longtime storage | 1 TB | 8,000,000 ns |
Memory Architecture
- accessing the disk is very slow
Computer Design Decisions
CSE 373 SP 18 - KASEY CHAMPION
10
Locality
CSE 373 SP 18 - KASEY CHAMPION
11
Leveraging Spatial Locality
CSE 373 SP 18 - KASEY CHAMPION
12
How memory is used and moves around
CSE 373 SP 19 - KASEY CHAMPION
13
CSE 373 SP 19 - KASEY CHAMPION
14
CSE 373 SP 19 - KASEY CHAMPION
15
CSE 373 SP 19 - KASEY CHAMPION
16
CSE 373 SP 19 - KASEY CHAMPION
17
CSE 373 SP 19 - KASEY CHAMPION
18
CSE 373 SP 19 - KASEY CHAMPION
19
CSE 373 SP 19 - KASEY CHAMPION
20
CSE 373 SP 19 - KASEY CHAMPION
21
CSE 373 SP 19 - KASEY CHAMPION
22
Solution to Mercy’s traveling problem
CSE 373 SP 19 - KASEY CHAMPION
23
CSE 373 SP 19 - KASEY CHAMPION
24
CSE 373 SP 19 - KASEY CHAMPION
25
CSE 373 SP 19 - KASEY CHAMPION
26
CSE 373 SP 19 - KASEY CHAMPION
27
RAM
CPU
CPU – kind of like the home / brain of your computer. Pretty much all computation is done here and data needs to move here to do anything significant with it (math, if checks, normal statement execution).
Data travels between RAM and the CPU, but it’s slow
Before
CSE 373 SP 19 - KASEY CHAMPION
28
RAM
CPU
Cache!
Bring a bunch of data back when you go all the way to RAM
Bring a bunch of food back when you go all the way to the store
After
Cache
CSE 373 SP 19 - KASEY CHAMPION
29
CSE 373 SP 19 - KASEY CHAMPION
30
RAM
CPU
Cache!
Bring a bunch of data back when you go all the way to RAM
Bring a bunch of food back when you go all the way to the store
After
This is a big idea!
How is a bunch of memory taken from RAM?
CSE 373 SP 19 - KASEY CHAMPION
31
This is a big idea (continued)!
How is a bunch of memory taken from RAM?�(continued)
CSE 373 SP 19 - KASEY CHAMPION
32
cache
original data (the 1) we wanted to look up gets passed back to the cpu
CPU
all the data from the block gets brought to the cache
How does this pattern of memory grabbing affect our programs?
Imagine that the below memory is just an entire array of length 13, with some data in it.
CSE 373 SP 19 - KASEY CHAMPION
33
Just by accessing one element we bring the nearby elements back with us to the cache. In this case, it’s almost all of the array!
Leveraging Temporal Locality
CSE 373 SP 18 - KASEY CHAMPION
34
Moving Memory
CSE 373 SP 18 - KASEY CHAMPION
35
Thought Experiment
CSE 373 SP 18 - KASEY CHAMPION
36
Why does sum1 run so much faster than sum2?
sum1 takes advantage of spatial and temporal locality
0 | 1 | 2 | 3 | 4 |
| | | | |
0 | 1 | 2 |
‘a’ | ‘b’ | ‘c’ |
0 | 1 | 2 |
‘d’ | ‘e’ | ‘f’ |
0 | 1 | 2 |
‘g’ | ‘h’ | ‘i’ |
0 | 1 | 2 |
‘j’ | ‘k’ | ‘l’ |
0 | 1 | 2 |
‘m’ | ‘n’ | ‘o’ |
Java and Memory
CSE 373 SP 18 - KASEY CHAMPION
37
What happens when you create a new array?
What happens when you create a new object?
What happens when you read an array index?
What happens when we open and read data from a file?
Array v Linked List
CSE 373 SP 18 - KASEY CHAMPION
38