Given that column major order is used:
- Thus consecutive memory locations (words) store elements like this: A, a, A.......etc.
One element is of 4 words and one cache block is of 8 words size.
- So whenever a cache miss occur, consecutive 8 words ,( ie 2 elements) are fetched and placed in cache.
In given code
for(i=0; i<10; i++)
for(j=0; j<10; j++);
A[i][j] = A[i][j]+10;
- In A[i][j] = A[i][j]+10;
- A is miss -> A is searched in memory and fetched consecutive 8 words,
- ie, A and A is in memory.
Now while writing A[i][j] = A[i][j]+10;
- cache hit occurs because A is already in cache
But next i=0 and j=1 and we need A.
- But we have A and A in cache.
- Again cache miss occur.
Thus while reading every new element cache miss occur. While writing back cache hit occur.
Each loop contain one read (always miss) and one write (always hit).
- Total 10*10 =100 times loop works.
- Thus 2*100=200 memory references and 100 reads and 100 writes
cache hit ratio = no of hits / total no of accesses. = no of writes/ no of accesses = 100/2*100 = 1/2
Cache hit ratio can be improved if ROW MAJOR order is used
Correct me if I am wrong!