GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPUTER ORGANIZATION & ARCHITECTURE

Trace array-loop locality and cache conflicts

3 questions solved the same way, newest first. Back to Computer Organization & Architecture

How these are solved

Translate row-major array indices to byte addresses and cache blocks, count cold spatial misses, and for the ratio variant also evaluate the column-stride conflict behavior.

2 marks · MCQ
Report issue

Row-major traversal cache misses

Cache Memory

Question 80, GATE 2006: A CPU has a 32 KB direct-mapped cache with 128-byte blocks. C array A[512][512] uses 8-byte elements. The two programs execute separately from the same initially cold cache; i,j,x are in registers.
P1: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[i][j];
P2: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[j][i];
Let M1,M2 be their cache-miss counts. Use the conventional block-aligned base address assumed by the listed answers. Find M1.
Options:
(A) 0
(B) 2048
(C) 16384
(D) 262144
Question 80, GATE 2006: A CPU has a 32 KB direct-mapped cache with 128-byte blocks. C array A[512][512] uses 8-byte elements. The two programs execute separately from the same initially cold cache; i,j,x are in registers.
P1: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[i][j];
P2: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[j][i];
Let M1,M2 be their cache-miss counts. Use the conventional block-aligned base address assumed by the listed answers. Find M1.
Options:
(A) 0
(B) 2048
(C) 16384
(D) 262144
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. In C, A is row-major. Each 128-byte cache block holds 128/8=16 elements.

2. P1 visits all 512×512 elements sequentially, so one compulsory miss serves each group of 16 elements. For a block-aligned A, M1=512×512/16=16384, C; each later element in the same block hits.

3. Teaching check: C’s rightmost subscript varies over contiguous elements. Each cache block supplies sixteen consecutive doubles, causing one miss followed by fifteen hits in a sequential scan. The whole array is much larger than the cache, but immediate within-block reuse still occurs.

Original paper, page 12

2 marks · MCQ
Report issue

Row versus column traversal miss ratio

Cache Memory

Question 81, GATE 2006: A CPU has a 32 KB direct-mapped cache with 128-byte blocks. C array A[512][512] uses 8-byte elements. The two programs execute separately from the same initially cold cache; i,j,x are in registers.
P1: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[i][j];
P2: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[j][i];
Let M1,M2 be their cache-miss counts. Use the conventional block-aligned base address assumed by the listed answers. Find M1/M2.
Options:
(A) 0
(B) 1/16
(C) 1/8
(D) 16
Question 81, GATE 2006: A CPU has a 32 KB direct-mapped cache with 128-byte blocks. C array A[512][512] uses 8-byte elements. The two programs execute separately from the same initially cold cache; i,j,x are in registers.
P1: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[i][j];
P2: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[j][i];
Let M1,M2 be their cache-miss counts. Use the conventional block-aligned base address assumed by the listed answers. Find M1/M2.
Options:
(A) 0
(B) 1/16
(C) 1/8
(D) 16
Question 81, GATE 2006: A CPU has a 32 KB direct-mapped cache with 128-byte blocks. C array A[512][512] uses 8-byte elements. The two programs execute separately from the same initially cold cache; i,j,x are in registers.
P1: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[i][j];
P2: for(i=0;i<512;i++) for(j=0;j<512;j++) x+=A[j][i];
Let M1,M2 be their cache-miss counts. Use the conventional block-aligned base address assumed by the listed answers. Find M1/M2.
Options:
(A) 0
(B) 1/16
(C) 1/8
(D) 16
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. P1 has 16384 misses under the block-aligned assumption. P2 accesses A[j][i] with stride 512×8=4096 bytes=32 cache lines.

2. The cache has 32768/128=256 lines, so successive row accesses cycle through only 256/gcd(256,32)=8 cache indices. With 512 rows, every would-be reused block is evicted before the next column accesses it.

3. All 512²=262144 P2 accesses miss. M1/M2=16384/262144=1/16, B.

4. The last options are on physical page 13.

5. Teaching check: A 4096-byte stride advances by 32 cache indices modulo 256. Only eight indices participate in a column, repeatedly overwriting each other. Thus the next column cannot reuse the previous column’s blocks, even though adjacent elements within a row occupy the same block.

Original paper, page 12

Descriptive
Report issue

Count reverse-scan cache misses

Cache Memory

Question 10(b), GATE 2002: A C program declares float A[2048], with four bytes per element and A starting at address 0x00000000. It runs on a computer with an 8-Kbyte direct-mapped data cache and 16-byte cache blocks (lines). Starting with an empty cache, access each array element once in reverse order, from the last element through the first. Ignore all other data and instruction accesses. How many data-cache misses occur? Briefly justify.
Question 10(b), GATE 2002: A C program declares float A[2048], with four bytes per element and A starting at address 0x00000000. It runs on a computer with an 8-Kbyte direct-mapped data cache and 16-byte cache blocks (lines). Starting with an empty cache, access each array element once in reverse order, from the last element through the first. Ignore all other data and instruction accesses. How many data-cache misses occur? Briefly justify.
Question 10(b), GATE 2002: A C program declares float A[2048], with four bytes per element and A starting at address 0x00000000. It runs on a computer with an 8-Kbyte direct-mapped data cache and 16-byte cache blocks (lines). Starting with an empty cache, access each array element once in reverse order, from the last element through the first. Ignore all other data and instruction accesses. How many data-cache misses occur? Briefly justify.
Tap to enlarge
Shared context

float A[2048] begins at address 0; each element occupies 4 bytes. The direct-mapped data cache has 8 Kbytes = 8192 bytes and 16-byte lines. Part (b) scans from A[2047] to A[0], starting with an empty cache and no other accesses.

Answer & solution

Answer: 512 cache misses (no official key was published)

1. Count the blocks touched. The array holds 2048 four-byte elements, so its total size is 8192 bytes. With 16-byte lines, there are 8192/16 = 512 distinct array blocks, each containing four consecutive elements.

2. Trace the first reversed block. The first access, A[2047], misses because the cache is empty and loads the entire block A[2044…2047]. The next accesses A[2046], A[2045], A[2044] therefore hit. Crossing to A[2043] causes the next miss and loads the next group of four.

3. Generalize the pattern. In every group of four reversed accesses, the first is a miss and the other three are hits. No accesses outside the array intervene. The 512 array blocks have distinct cache indices, although even a smaller cache would still preserve this immediate three-hit pattern for a single uninterrupted sequential scan.

4. Multiply the counts: misses = 512×1 = 512, hits = 512×3 = 1536, total accesses = 512+1536 = 2048. All these misses are compulsory first references, not conflict misses.

5. Reverse traversal changes which element first brings in each block, but not the number of blocks touched. A direct-mapped simulation from A[2047] down to A[0] confirms the count of 512 misses.

Original paper, page 8