126
��Computer Systems: A Programmer’s Perspective

Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

����������

�����������������������Computer Systems: A Programmer’s Perspective����������������

Page 2: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������

�����������������

������

���������

���������

���������

���������������

...

������

���������

���������

���������

���������������

����������������������������������

�����������

�����������������������Computer Systems: A Programmer’s Perspective����������������

Page 3: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

����������������

���������

�����������������

��������������

�����������������

��������������

���������������������

��������������

������������������

�����������������

�������������������

�����������������������������������

���������������������������

��������������������������������������

����������������������

��������������������� ��������������������������

��������������������������������������������������� �����������������������Computer Systems: A Programmer’s Perspective����������������

Page 4: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������������

�����������������������Computer Systems: A Programmer’s Perspective����������������

Page 5: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

�������������������������int�����������������

��� �������� �������

���� �������� ���������� ��������� ��������� ������� ������� ������� ������� �������� ����

Page 6: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

�������������������������int�����������������

��� �������� �������

���� �������� ���������� ��������� ��������� ������� ������� ������� ������� �������� ����

������������������������

������������������������

����������������������

����������������������

Page 7: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

�����������������������������int�����������������

��� �������� �������

���� �������� ���������� ���������� ���������� �������� �������� �������� �������� ��������� �����

������������������������

������������������������

����������������������

����������������������

Page 8: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

��

��

�����������

0x765428 42

0x8005C0 79

Page 9: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765428 42

0x8005C0 79

Page 10: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765428 42

0x8005C0 79

��

Page 11: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765428 42

0x8005C0 79

��

Page 12: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765428 42

0x8005C0 79

��

Page 13: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 14: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 15: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

42��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 16: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?

temporal locality

��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 17: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 18: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

42��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 19: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x8005C0?��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 20: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x8005C0?��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 21: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x8005C0?��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 22: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x8005C0?��

0x765428 42

��

0x765428 42

�����������

0x765428 42

0x8005C0 79

��

Page 23: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x8005C0?��

0x765428 42

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 24: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x8005C0?��

0x8005C0 79

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 25: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

79��

0x8005C0 79

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 26: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

0x8005C0 79

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 27: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

0x8005C0 79

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 28: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

0x8005C0 79

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 29: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

0x765428 42

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 30: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

42��

0x765428 42

��

0x765428 42

0x8005C0 79

�����������

0x765428 42

0x8005C0 79

��

Page 31: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

��

��

�����������

0x765420 42 5

0x8005C0 79

��

Page 32: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765420 42 5

0x8005C0 79

��

Page 33: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765420 42 5

0x8005C0 79

��

Page 34: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765420 42 5

0x8005C0 79

��

Page 35: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

�����������

0x765420 42 5

0x8005C0 79

��

Page 36: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

��

0x765420 42 5

�����������

0x765420 42 5

0x8005C0 79

��

Page 37: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765428?��

0x765420 42 5

��

0x765420 42 5

�����������

0x765420 42 5

0x8005C0 79

��

Page 38: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

42��

0x765420 42 5

��

0x765420 42 5

�����������

0x765420 42 5

0x8005C0 79

��

Page 39: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765430?

spatial locality

��

0x765420 42 5

��

0x765420 42 5

�����������

0x765420 42 5

0x8005C0 79

��

Page 40: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

0x765430?��

0x765420 42 5

��

0x765420 42 5

�����������

0x765420 42 5

0x8005C0 79

��

Page 41: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������

5��

0x765420 42 5

��

0x765420 42 5

�����������

0x765420 42 5

0x8005C0 79

��

Page 42: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

�����������������������������int�����������������

��� �������� �������

���� �������� ���������� ���������� ���������� �������� �������� �������� �������� ��������� �����

����������������������������������

����������������������������������

��������������������������������

����������������������

���������int�

��

Page 43: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������

Temporal locality

A previously used address is likely to be used again soon

Cache hierarchy������������������������������������������������������

Spatial locality

Newly used addresses are likely to be near recently usedaddresses

Blocking�����������������������������������������������������

�����

Page 44: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������������

long data[MAXELEMS]; int test(int elems, int stride) { long i, sx2 = stride*2, sx3 = stride*3, sx4 = stride*4; long acc0 = 0, acc1 = 0, acc2 = 0, acc3 = 0; long length = elems; long limit = length - sx4; for (i = 0; i < limit; i += sx4) { acc0 = acc0 + data[i]; acc1 = acc1 + data[i+stride]; acc2 = acc2 + data[i+sx2]; acc3 = acc3 + data[i+sx3]; } for (; i < length; i++) { acc0 = acc0 + data[i]; } return ((acc0 + acc1) + (acc2 + acc3));}

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 45: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 46: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���

���

��� ��� ��� ���

���

��������0x765428

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 47: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���

���

��� ��� ��� ���

���

�����������������

��������

�� �

��������������

�������������

������

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 48: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���

���

��� ��� ��� ���

���

�����������������

��������

����������

��������������

�������������

������

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 49: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���

���

��� ��� ��� ���

���

�����������������

��������

����������

��������������

�������������

������

���cache line

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 50: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���������������������

�����

� ����� �

���

���

��� ��� ��� ���

���

�����������������

��������

��� �

������

��� �

������

�������������

������

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 51: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���������������������

�����

� ����� �

���

���

��� ��� ��� ���

���

�����������������

��������

��� �

������

��� �

������

�������������

������

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 52: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���������������������

�����

� ����� �

���

���

��� ��� ��� ���

���

�����������������

��������

��� �

������

��� �

������

�������������

������

�-way associative cache �������direct-mapped cache

�������������������������������������������Computer Systems: A Programmer’s Perspective�����������������

Page 53: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���������������������

�����

� ����� �

���

���

��� ��� ��� ���

���

�����������������

��������

��� �

������

��� �

������

�������������

������

���������

��� ... ��������

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 54: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

���������������������

�����

� ����� �

���

���

��� ��� ��� ���

���

�����������������

��������

��� �

������

��� �

������

�������������

������

���������

��� ... ��������

�����������������������Computer Systems: A Programmer’s Perspective�����������������

Page 55: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

1 1 0 0 1 1

1 0 1 0 1 0

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 56: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

1 1 0 0 1 1

1 0 1 0 1 0

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 57: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

1 1 0 0 1 1

1 0 1 0 1 0

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 58: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

�����

1 1 0 0 1 1

1 0 1 0 1 0

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

�������

��

Page 59: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

�����

1 1 0 0 1 1

1 0 1 0 1 0

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

�������

int�����������

��

Page 60: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

1 1 0 0 1 1

1 1 1 1 1 1

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 61: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

1 1 0 0 1 1

1 1 1 1 1 1

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 62: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

�����

1 1 0 0 1 1

1 1 1 1 1 1

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

�������������

��

Page 63: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�������������������������������

�������

1 1 0 0 1 1

0 0 1 0 1 0

0

1 1 0 1 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 64: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�����������������������

1 1 0 0 1 1 0

1 0 1 0 1 0 1 1 1 0 1 1

0 1 1 1 0 0 0

1 1 0 1 1 0 1 1 0 0 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 65: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�����������������������

1 1 0 0 1 1 0

1 0 1 0 1 0 1 1 1 0 1 1

0 1 1 1 0 0 0

1 1 0 1 1 0 1 1 0 0 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 66: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�����������������������

1 1 0 0 1 1 0

1 0 1 0 1 0 1 1 1 0 1 1

0 1 1 1 0 0 0

1 1 0 1 1 0 1 1 0 0 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 67: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

�����������������������

1 1 0 0 1 1 0

1 0 1 0 1 0 1 1 1 0 1 1

0 1 1 1 0 0 0

1 1 0 1 1 0 1 1 0 0 1 0

��������0 1 0 1 0 0 1 1 0 0

��

Page 68: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

����������������������

�����������miss�������������������������������������

� ���������������������������������

� �����������������������������������

�����������������������Computer Systems: A Programmer’s Perspective��������������������

Page 69: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������

�����������������������������������������������������������������

What to do on a write-hit?Write-through������������������������������Write-back���������������������������������������

What to do on a write-miss?Write-allocate�����������������������������������No-write-allocate���������������������������

������������������������� ���������������������������������� ��������������������������� ← good assumption

�����������������������Computer Systems: A Programmer’s Perspective��������������������

Page 70: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

�������������������� ������������������������ ������������ �������������������� ���������������������������

struct { int x; int y;} grid[16][16];

sizeof(grid[0][0])���2*sizeof(int)������grid����������������������������

sizeof(grid)���16*16*2*sizeof(int)���������������grid���������������������������

�����

Page 71: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

��

Page 72: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

��

Page 73: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

i���� j���� ����

��

Page 74: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

i���� j���� ����

��

Page 75: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

i���� j���� ����i���� j���� ���

��

Page 76: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

i���� j���� ����i���� j���� ���i���� j���� ���

��

Page 77: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

i���� j���� ����i���� j���� ���i���� j���� ���i���� j���� ���

��

Page 78: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

i���� j���� ����i���� j���� ���i���� j���� ���i���� j���� ���i���� j���� ����

��

Page 79: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

���i���� j����� ���

��

Page 80: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

���i���� j����� ���i���� j���� ����

��

Page 81: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

� � � �

� � � �

� � �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

� � � �

� � � �

� � �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

���i���� j����� ���i���� j���� ����

��

Page 82: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

���i���� j����� ���i���� j���� ����

��

Page 83: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x;

Miss rate:����

��

Page 84: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

�����j���������

��

Page 85: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

i���� j���� ����

��

Page 86: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

i���� j���� ����i���� j���� ����

��

Page 87: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

i���� j���� ����i���� j���� ����i���� j���� ����

��

Page 88: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

���i���� j���� ����

��

Page 89: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

���i���� j���� ����i���� j���� ����

��

Page 90: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

Miss rate:�����

��

Page 91: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�������������������

���grid�������������������������

�����������������

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

����������������⇒column as inner loop

��

Page 92: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������

���grid�������������������������

����������64

������������

�����

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

���i���� j���� ����i���� j���� ����

���

Page 93: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������

���grid�������������������������

����������64�����

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

���i����� j���� ����

���

Page 94: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������

���grid�������������������������

����������64�����

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

���i����� j���� ����i���� j���� ���

���

Page 95: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������

���grid�������������������������

����������64�����

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

������������������⇒��������������������

���

Page 96: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������

� � � �

� � � �

� � �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

� � � �

� � � �

� � �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

�� �� �� ��

���grid�������������������������

����������������2

for (j = 0; j < 16; j++) for (i = 0; i < 16; i++) total_x += grid[i][j].x;

�������������������������������������������������

���

Page 97: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x; for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_y += grid[i][j].y;

���

Page 98: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_x += grid[i][j].x; for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) total_y += grid[i][j].y;

Miss rate:����

���

Page 99: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) { total_x += grid[i][j].x; total_y += grid[i][j].y; }

���

Page 100: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) { total_x += grid[i][j].x; total_y += grid[i][j].y; }

i���� j���� ����

���

Page 101: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) { total_x += grid[i][j].x; total_y += grid[i][j].y; }

i���� j���� ����i���� j���� ���

���

Page 102: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) { total_x += grid[i][j].x; total_y += grid[i][j].y; }

i���� j���� ����i���� j���� ���i���� j���� ���

���

Page 103: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) { total_x += grid[i][j].x; total_y += grid[i][j].y; }

i���� j���� ����i���� j���� ���i���� j���� ���i���� j���� ���

���

Page 104: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) { total_x += grid[i][j].x; total_y += grid[i][j].y; }

Miss rate:������

���

Page 105: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

��������������

���grid�������������������������

�����������������

for (i = 0; i < 16; i++) for (j = 0; j < 16; j++) { total_x += grid[i][j].x; total_y += grid[i][j].y; }

���������������⇒fuse to improve locality

���

Page 106: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������

����������������⇒�column as inner loop

������������������⇒���������������������

���������������⇒�fuse to improve locality

���������������������������������

��������������������1

������������������

�������

Page 107: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];

i

k

A

� k

j

B

� i

j

C

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 108: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];

i

k

A

� k

j

B

� i

j

C

�����������

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 109: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];

i

k

A

� k

j

B

� i

j

C

������������

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 110: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];

i

k

A

� k

j

B

� i

j

C

������������������ ����������������� ��������������������������

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 111: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];

i

k

A[i][★]

k

j

B[★][j]

i

j

C[i][j]

������������������������������������������

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 112: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j];

i

k

A[i][★]

k

j

B[★][j]

i

j

C[i][j]

���������� 0.25 � 1.0 � 0.0 ��1.25

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 113: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������ijk

for (i = 0; i < n; i++) for (j = 0; j < n; j++) { sum = 0.0; for (k = 0; k < n; k++) sum += A[i][k] * B[k][j]; C[i][j] += sum; }

i

k

A[i][★]

k

j

B[★][j]

i

j

C[i][j]

���������� 0.25 � 1.0 � 0.0 ��1.25

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 114: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������jik

for (j = 0; j < n; j++) for (i = 0; i < n; i++) { sum = 0.0; for (k = 0; k < n; k++) sum += A[i][k] * B[k][j]; C[i][j] += sum; }

���������������������

i

k

A[i][★]

k

j

B[★][j]

i

j

C[i][j]

���������� 0.25 � 1.0 � 0.0 ��1.25

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 115: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������kij

for (k = 0; k < n; k++) for (i = 0; i < n; i++) { a = A[i][k]; for (j = 0; j < n; j++) C[i][j] += a * B[k][j]; }

i

k

A[i][k]

k

j

B[k][★]

i

j

C[i][★]

���������� 0.0 � 0.25 � 0.25 ��0.5

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 116: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������ikj

for (i = 0; i < n; i++) for (k = 0; k < n; k++) { a = A[i][k]; for (j = 0; j < n; j++) C[i][j] += a * B[k][j]; }

���������������������

i

k

A[i][k]

k

j

B[k][★]

i

j

C[i][★]

���������� 0.0 � 0.25 � 0.25 ��0.5

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 117: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������jki

for (j = 0; j < n; j++) for (k = 0; k < n; k++) { b = B[k][j]; for (i = 0; i < n; i++) C[i][j] += A[i][k] * b; }

i

k

A[★][k]

k

j

B[k][j]

i

j

C[★][j]

���������� 1.0 � 0.0 � 1.0 ��2.0

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 118: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

�����������������������kji

for (k = 0; k < n; k++) for (j = 0; j < n; j++) { b = B[k][j]; for (i = 0; i < n; i++) C[i][j] += A[i][k] * b; }

���������������������

i

k

A[★][k]

k

j

B[k][j]

i

j

C[★][j]

���������� 1.0 � 0.0 � 1.0 ��2.0

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 119: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������������

���

Page 120: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (k = 0; k < n; k++) for (i = 0; i < n; i++) { a = A[i][k]; for (j = 0; j < n; j++) C[i][j] += a * B[k][j]; }

i

k

A[i][k]

k

j

B[k][★]

i

j

C[i][★]

���

Page 121: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (k = 0; k < n; k++) for (i = 0; i < n; i++) { a = A[i][k]; for (j = 0; j < n; j++) C[i][j] += a * B[k][j]; }

i

k

A[i][k]

k

j

B[k][★]

i

j

C[i][★]

���������������������������B�����������������

���

Page 122: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

���������������������

for (k = 0; k < n; k++) for (i = 0; i < n; i++) { a = A[i][k]; for (j = 0; j < n; j++) C[i][j] += a * B[k][j]; }

i

k

A[i][k]

k

j

B[k][★]

i

j

C[i][★]

���������������������������������� ����� ����C��������������������������B

���

Page 123: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������������������������

for (i = 0; i < n; i += W) for (j = 0; j < n; j += W) for (k = 0; k < n; k += W) for (ii = i; ii < i+W; ii++) for (jj = j; jj < j+W; jj++) for (kk = k; kk < k+W; kk++) C[ii][jj] += A[ii][kk] * B[kk][jj];

���W�W������ i

k

A

k

j

B

i

j

C

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 124: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������������������������

for (i = 0; i < n; i += W) for (j = 0; j < n; j += W) for (k = 0; k < n; k += W) for (ii = i; ii < i+W; ii++) for (jj = j; jj < j+W; jj++) for (kk = k; kk < k+W; kk++) C[ii][jj] += A[ii][kk] * B[kk][jj];

���W�W������ i

k

A

k

j

B

i

j

C

������������������������⇒�0.25����������

�����������������������Computer Systems: A Programmer’s Perspective������������������

Page 125: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������������������������

for (i = 0; i < n; i += W) for (j = 0; j < n; j += W) for (k = 0; k < n; k += W) for (ii = i; ii < i+W; ii++) for (jj = j; jj < j+W; jj++) for (kk = k; kk < k+W; kk++) C[ii][jj] += A[ii][kk] * B[kk][jj];

���W�W������ i

k

A

k

j

B

i

j

C

�����������������������������0.25����������

�������������� ������������������

�����������������������Computer Systems: A Programmer’s Perspective����������������������

Page 126: Temporal locality A previously used address is likely to be used again soon Cache hierarchy What to do on a write-hit? Write-through Write-back

������������������������������

for (i = 0; i < n; i += W) for (j = 0; j < n; j += W) for (k = 0; k < n; k += W) for (ii = i; ii < i+W; ii++) for (jj = j; jj < j+W; jj++) for (kk = k; kk < k+W; kk++) C[ii][jj] += A[ii][kk] * B[kk][jj];

���W�W������ i

k

A

k

j

B

i

j

C

0.25���� �������� �

������ �

��

⇒� �������������

�����������������������Computer Systems: A Programmer’s Perspective����������������������