movq %rax, 8(%rsp)
movq 8(%rsp), %rax
A bus is a collection of parallel wires that carry address, data, and control signals
Buses are typically shared by multiple devices
Example: movq A, %rax
CPU places address A on the memory bus
Example: movq A, %rax
Main memory reads A from the memory bus, retrieves word x, and places it on the bus
Example: movq A, %rax
CPU read word x from the bus and copies it into register %rax
Example: movq %rax, A
CPU places address A on the memory bus; main memory reads it and waits for the corresponding data word to arrive
Example: movq %rax, A
CPU places data word y on the bus
Example: movq %rax, A
Main memory reads data word y from the bus and stores it at address A
Principle of Locality: Programs tend to use data and instructions with addresses near or equal to those they have used recently
sum = 0;
for (i = 0; i < n; i++) {
sum += a[i];
}
return sum
sum
each iteration (temporal)Claim: being able to look at code and get a qualitative sense of its locality is a good skill for a professional programmer
Question: Does this function have good locality with respect to array a
?
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;
}
Question: can you permute the loops so that the function scans the 3D array with a stride-1 reference pattern (and thus have good spatial locality)?
int sum_array_3d(int a[M][N][N]) {
int i, j, k, sum = 0;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) }
for (k = 0; k < M; k++) {
sum += a[k][i][j];
}
}
}
return sum;
}
These fundamental properties complement each other beautifully
The suggest an approach for organizing memory and storage systems known as a memory hierarchy
Cache: A smaller, faster storage device that acts as a staging area for a subset of the data in a larger, slower device
Big Idea (Ideal): The memory hierarchy creates a large pool of storage that costs as much as the cheap storage near the bottom, but serves data to programs at the rate of the fast storage near the top
A cache hit is when the data in block \(b\) is needed and is in the cache
A cache miss is when the data in block \(b\) is needed and is in not the cache