Log In
8 votes

Consider an array A[100] and each element occupies 4 word. A 32 word cache is used and divided into 8 word blocks. What is the hit ratio for the following statement.


What mapping is going to be used in the Solution ?

in CO and Architecture
retagged by

4 Answers

10 votes
Best answer

According to the question, a block can have 2 elements. And each element has size of 4 words.

Now, the looping is given as follows ==>


This means that for each value of i, we have to read the element first,and +10 shows that after reading , we have to add this constant value to the array element .

Hence, we will consider read and write cases now. Also, of the question was about just reading and no updating , then would have considered only read cases .

Now, first iteration will read first A[0] value, which is not there in the cache ( we have assumed cache to be initially empty), it will be a cache miss . 

Now, A[0] and A[1] cache block will be brought to the cache , and now write reference for A[0] is a hit, whereas, for A[1] ,both read and write access will be hits .

Hence, for a particular cache block , we have 1 miss and 3 hits .

Hence, hit ratio = 75% ..

selected by

@kapil your explanation is great but clear my this doubt

a/c to the problem statement we can have 2 element in a cache block .....thats over now ;ets come to this statement    for(i=0;i<100;i++)
                   A[i]=A[i]+10      in this the loop is from i=0 to <100 rt .

to execute this loop which is asking us  to take i=0 first we should have considered only a[0]   but we are also getting a[1] into picture..........

i assumed i must be the index of the array and if we are running the loop then a/c to this statement  A[i]=A[i]+10   we must go at 10 indexe's beyond what we are working on right now means if we have taken i=0 then at RHS A[i] should have value A[i]+10 (value of i plus 10) please explain this.....

@shekhar sir, if value at i = 0 , is 5 .

Then, it will just add the value ,

Also , if 1 word = 1 byte , then element size is 32 bits.... It won't affect next element...
@kapil you men loop is on index not value if value is incrementing it is not going to effect the loop
Yes sir, it won't affect, as we are just changing our elements.. We are providing them new value...
@shekhar sir,

The answer could change , if we consider write cases ,i.e , whether our cache is write back or write through. We can also consider them, but as such nothing is given like this in the question , so I didn't considered them. I just made it simple...
@kapil you did not say anything  about why a[1] getting fetched along with a[0] when we are working on a[0] at first iteration ......i understood that part that cache block can hold 2 elements at the same time but it is not necessary condition here that we should fetch a[1] along with a[0] evenif we have space to place it in block ..........
Sir, it is neccesary, as we can only transfer blocks from memory to cache . we cannot pass only 1 element. Cache line acts as a basic building block to transfer information between memory levels...
@kapil thanks this is point i was looking for ...everything is clear now .
@Kapil In case, if each element has size of 8 words, it will not fetch it's adjacent element and hit rate will be 25% right ?
We don't fill half line/block at a time (one element of the said array is equal to half the line/block size). we fetch whole block/page from memory. and the block in memory are also  8 word size and due to contiguous memory locations of consecutive array elements a[0] and a[1] will be present in same block in MM.

@Kapil @shekhar chauhan @Arjun

What will happen once all the Cache Blocks get occupied? 

A complete Block is replaced by a new Block. Correct me, if i am wrong.

1 vote

Each cache block is of 8 words.

One array element is of 4 word size

Hence each block can store 2 array elements.

In the for loop, 

When i=0, cache miss, A[0] and A[1] are fetched

When i=1,cache hit

When i=2,cache miss, A[2] and A[3] are fetched

When i=3 cache hit


When i=98, cache miss, A[98] and A[99] are fetched

When i=99 cache hit

So cache hit and miss come alternatively.hit ratio is 50% and set associative mapping is used

@shiva for i=0 it is okay that we are fetching a[0] but why a[1] ?

A/c to the statement A[i] = A[i] +10

if we consider i = 0 then there is no way we are getting a[1] any where in first iteration .
first of all here for each iteration memory is accessed for 2 times- one for read and another for write. Now since block size is = 8 words and element size is = 4 words so two elements will come into block.

So for i= 0 , a[0] and a[1] is in the 0th cache block. read a[0] - miss and write a[0] - hit.

     for i=1 , as a[1] is already in the cache block , so both a[1] - hit and write a[1] - hit.

so for i= 0 and i= 1 .. total memory access = 4 where hit = 3 and miss = 1.

Similarly, for i= 2 and 3 , total memory access = 4 where hit = 3 and miss= 1.

So for i=0 to 99 , total memory access= 100 * 2 = 200 , where hit = 150 and miss = 50 ,

Hit ratio = 150/200 = 3/4 = 0.75
@vijay does it has nothing to do with the statement how many memory location going to to accessed in each iteration .as you said there will be one read one write  at first place. ?

in your explanation you did not say anything about  this part of increment "+10". how is it going to effect the answer ?
1 vote

Since cache clock size=8 words 

Each array element size is 4words 

Now in each cache block there exist two array elements like below way

A[0]A[1] A[2]A[3]   ........ A A[94]A[94] A[96]A[97] A[98]A[99]

So when ever we access A[0] then we can access both A[0]A[1]

A[0]     R(Miss)    W(Hit)

A[1]     R(Hit)         W(Hit)



0 votes

According to the question size of block 8 words=3bits

No of  blocks=32/8 =22 = 2

Array A has 100 elements =400 words

Each block contain 8 words

So  in a block 1st one is hit and next 7 words are miss.

No of miss here 400/8 =50

Number of hits =350

So, hit ratio 350/400⨉100=87.5%

Related questions

4 votes
4 answers
Consider an array A[200] and each element occupies 8-words. A 64-word cache is used and divided into 16-word blocks. What is the hit ratio for the following code segment: for(int i=0; i<200; i++) A[i] = A[i]+5 0.85 0.65 0.95 0.75
asked Nov 10, 2017 in CO and Architecture Parshu gate 596 views
5 votes
1 answer
Consider an array has 100 elements and each element occupies 4 words .A 32 bit word cache is used and divided into a block of 8 words .What is the hit rate of this for(i=0; i<10; i++) for(j=0; j<10; j++); A[i][j] = A[i][j]+10; Column major is used here. How to improve Hit rate ?
asked Jul 21, 2016 in CO and Architecture shekhar chauhan 1.2k views
0 votes
1 answer
In some problems we multiply only with the second part of the equation with (1-H1) component and leave the first part. Whereas in other cases we multiply with cache hit and miss. Is there any patterns for this or could be explained.
asked Mar 17, 2018 in CO and Architecture nirupama thakur 727 views
2 votes
3 answers
Consider a paging system uses, TLB’s access time is 30 ns and memory access time is 200ns. If the effective memory access time is 150ns, what will be the hit ratio of TLB?
asked Nov 8, 2016 in Operating System Veerendra V 3.6k views