170 likes | 238 Views
Problem 6.15. 1024-byte direct-mapped cache 16-byte blocks sizeof(int) = 4 struct algae_position{ int x; int y; }; Struct algae_position grid[16][16]; grid begins at memory address 0 Some code: int total_x = 0, total_y = 0; int i, j; for (i = 0; i < 16; i++){ for (j=0; j < 16; j++){
E N D
Problem 6.15 • 1024-byte direct-mapped cache • 16-byte blocks • sizeof(int) = 4 • struct algae_position{ int x; int y; }; • Struct algae_position grid[16][16]; • grid begins at memory address 0 • Some code: int total_x = 0, total_y = 0; int i, j; 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; } }
Problem 6.15 • What is the total number of reads? • What is the total number of reads that miss in the cache? • What is the miss rate?
Problem 6.15 • Total number of reads:
Problem 6.15 • Total number of reads: • One read per iteration of the each loop. • 16*16 = 256 Iterations in loop 1 • 16*16 = 256 iterations in loop 2 • 256+256 = 512 reads total
Problem 6.15 • What is the total number of reads that miss the cache? • What does our cache look like?
Problem 6.15 • 1024-byte direct-mapped cache • 16-byte blocks • There are 1024 bytes / (16 bytes/set) = 64 blocks • sizeof(int) = 4 • struct algae_position{ int x; int y; }; • sizeof(algae_position) = 8; • Each block can hold two algae_position’s • Struct algae_position grid[16][16]; • grid begins at memory address 0
Problem 6.15 • struct algae_position{ int x; int y; }; • sizeof(algae_position) = 8; • Each block can hold two algae_position’s • Struct algae_position grid[16][16]; • grid begins at memory address 0 • Access to grid[0][0] or grid[0][1] will cause both ([0][0] and [0][1]) to be stored in block 0. • Access to grid[0][2] or grid[0][3] will cause both to be stored in block 1. • Etc…
Problem 6.15 • 1024-byte direct-mapped cache • 16-byte blocks • There are 1024 bytes / (8 bytes/block) = 128 blocks struct algae_position{ int x; int y; }; • sizeof(algae_position) = 8; • Each block can hold two algae_position’s • Struct algae_position grid[16][16]; • grid begins at memory address 0 • Access to grid[0][0] or grid[0][1] will cause both ([0][0] and [0][1]) to be stored in block 0. • There are 64 blocks total, each block can hold 2 algae_position’s. • The first row of grid[][] will take up the first 8 blocks. • The cache will be full after we read the first 128 / 8 = 16 rows of grid[][]. • Any reads after that will evict previously stored items in the cache.
Problem 6.15 • There are 128 blocks total, each block can hold 2 algae_position’s. • The first row of grid[][] will take up the first 8 blocks. • The cache will be full after we read the first 128/8 = 16 rows of grid[][]. • Any reads after that will evict previously stored items in the cache. • This means that we can consider each loop separately since all the first 16 rows the second loop is accessing will have been evicted by the later reads (of the last 16 rows) of the first loop.
Problem 6.15 /* First loop */ for (i = 0; i < 16; i++){ for (j = 0; j < 16; j++){ total_x += grid[i][j].x; } } • In the first loop: • Access grid[0][0].x -- Miss • Both grid[0][0] and grid[0][1] are stored in block 0. • Access grid[0][1].x -- Hit • Access grid[0][2].x –- Miss • Access grid[0][3].x –- Hit • ... • 256 reads, 128 of them are misses.
Problem 6.15 /* Second loop */ for (i = 0; i < 16; i++){ for (j = 0; j < 16; j++){ total_y += grid[i][j].y; } } • In the first loop: • Access grid[0][0].y -- Miss • Both grid[0][0] and grid[0][1] are stored in block 0. • Access grid[0][1].y -- Hit • Access grid[0][2].y –- Miss • Access grid[0][3].y –- Hit • ... • 256 reads, 128 of them are misses.
Problem 6.15 • What is the total number of reads? • 512 • What is the total number of reads that miss in the cache? • 128 (first loop) + 128 (second loop) = 256 • What is the miss rate? • 256 misses/ 512 reads = 50%
Problem 6.16 • What if our loop looked like this? for (i =0; i < 16; i++){ for (j = 0; j < 16; j++){ total_x += grid[j][i].x; total_y += grid[j][i].y; } }
Problem 6.16 • What if our loop looked like this? for (i =0; i < 16; i++){ for (j = 0; j < 16; j++){ total_x += grid[j][i].x; total_y += grid[j][i].y; } }
Problem 6.16 • What if our loop looked like this? for (i =0; i < 16; i++){ for (j = 0; j < 16; j++){ total_x += grid[j][i].x; total_y += grid[j][i].y; } } • We are going down column by column to sum the coordinates. • The cache can only hold half of the elements in the array, so that means that a read to grid[8][0] will evict the block that was loaded when we read grid[0][0]. Since this block also contained grid[0][1], the first read of grid[0][1] will be a miss. • Each iteration will have one hit and one miss. • This means we have 256 hits and 256 misses (50% miss rate).
Problem 6.17 • What if our loop looked like this? for (i =0; i < 16; i++){ for (j = 0; j < 16; j++){ total_x += grid[i][j].x; total_y += grid[i][j].y; } }
Problem 6.17 • What if our loop looked like this? for (i =0; i < 16; i++){ for (j = 0; j < 16; j++){ total_x += grid[i][j].x; total_y += grid[i][j].y; } } • Stride-one reference pattern. • We will read grid[0][0].x (cold miss), grid[0][0].y (hit), grid[0][1].x (hit), grid[0][1].y (hit). • The next two iterations will have the same number of hits/misses. • 25% of the 512 reads will be misses.