// Before: 2 sequential arrays //
int val[SIZE];
int key[SIZE];
//After: 1 array of stuctures //
struct merge {
int val;
int key;
};
struct merge merged_array[SIZE];
Reducing conflicts between val & key (they might conflict for a single block in the cache); improve spatial locality when they are accessed in a interleaved fashion
store first colum, then second colum, then third….
Example:
– 6x6 array A
– Each element 1 word
– Block size: 4 word
– For i=1 to 6; X+= a[1,i]; how many blocks are loaded into cache
For final
you need to know how many misses in this pic
and prof will give the code

store first row, then second row, then third….
Example:
– 6x6 array A
– Each element 1 word
– Block size: 4 word
– For i=1 to 6; X+= a[1,i]; how many blocks are loaded into cach

Assume the array storage is in row order
for (k = 0; k < 100; k = k+1)
for (j = 0; j < 100; j = j+1)
for (i = 0; i < 5000; i = i+1)
x[i][j] = 2 * x[i][j];
/* After */
for (k = 0; k < 100; k = k+1)
for (i = 0; i < 5000; i = i+1)
for (j = 0; j < 100; j = j+1)
x[i][j] = 2 * x[i][j];
/* Before */
for (i = 0; i < N; i = i+1)
for (j = 0; j < N; j = j+1)
a[i][j] = 1/b[i][j] * c[i][j];
for (i = 0; i < N; i = i+1)
for (j = 0; j < N; j = j+1)
d[i][j] = a[i][j] + c[i][j];
/* After */
for (i = 0; i < N; i = i+1)
for (j = 0; j < N; j = j+1)
{ a[i][j] = 1/b[i][j] * c[i][j];
d[i][j] = a[i][j] + c[i][j];}
행렬 곱셈처럼 큰 데이터를 쓰는 작업에서, 행이나 열을 끝까지 쭉 훑지 않고 캐시에 쏙 들어가는 작은 정사각형 조각(블록) 을 하나 정해요. 그 조각을 여러 번 다 쓴 다음에 다음 조각으로 넘어가요.
비유: 시험공부할 때 책 10권을 동시에 펴서 한 줄씩 왔다 갔다 하지 않고, 한 챕터를 책상에 펴 놓고 끝낸 뒤 다음 챕터로 가는 것.
Idea: fetch data into the cache before processors request them

필요해지기 전에 미리 캐시로 가져오는 것이에요. 프로그래머, 컴파일러, 하드웨어 누구든 할 수 있어요. 처음 쓰는 데이터라서 어차피 나는 미스(cold miss)도 막을 수 있어요.
You only prefetch data that are truly needed
- Avoid bandwidth waste
You issue prefetch requests early enough
- To hide the memory latency
You don’t issue prefetch requests too early
- To avoid cache pollution
Prefetching relies on having extra memory bandwidth that can be used without penalty
Useful Prefetch
Useless Prefetch
Harmful Prefetch