-1

i know hit rate is the percentage of found data in cache. but i have no idea how to find the hit for an algorithm. i was thinking that for code 1 i will have 11 blocks each with 4 elements and for code 2 i'll have 4 blocks each with 11 elements and each time i see 4 elements missed. not sure if that make sense at all. any advise is welcomed

Suppose a 2-dimensional array A with 11 rows by 4 columns, stored in memory like this [0][0], [0][1], [0][2], [0][3], [1][0], [1][1], …[10][2], [10][3]

Also suppose a fully associative single level cache of 10 memory blocks, with each memory block holding 4 bytes, and a FIFO replacement policy.

Each row fits exactly into one cache block and rather unluckily, the whole array cannot fit into cache. the cache is one row too small...

Now given the 2 following codes, 1- how do i calculate the hit rate 2- given that cache access time is 5ns and memory access time is 70ns, and assuming overlapping access to memory and cache, how do i calculate the EAT for each code?

Code 1:

for (int row = 0; row < 11; row ++)
{
   for (int column = 0; column < 4; column ++)
   {
      myByte = A [ row, column ];
   }
}

Code 2:

for (int column = 0; column < 4; column ++)
   {
   for (int row = 0; row < 11; row ++)
   {
      myByte = A [ row, column ];
   }
}

Any help is appreciated. Thanks

dddx
  • 41
  • 4

1 Answers1

0

Well we dont calculate hit rate for an algorithm. We write algorithm in such a way which utilizies the concept of cache i.e to get maximum hit rate. Your question actually belongs to Computer Organization and Architecture.

Anyways, your 2D - array is stored in memory in a row major order. So, for code 1, with first inner for loop a block would be fetched from memory containing four element since nothing was found in the cache (as it is a miss) (i.e memory access time 70ns).Now subsequent three element i.e [0][1],[0][2],[0][3] would be fetched from cahce(i.e 3*5 = 15ns) i.e total time for accessing first four element = 70+3*5 = 85ns.

The above process would repeat for ten rows of your array w.r.t you 10 cache block. but for the last block using FIFO concept swaping of block would take place but the time would remain same. so total time = 85*11 = 935ns

for code 2,

for every iteration of inner for loop a block would be fetched. So, every time you are accessing memory and in that case you are not utilizing the concept of cache i.e its a bad piece of code. The code 2 would work best if your array is stored in column major order in memory.

Siddhartha Gupta
  • 507
  • 3
  • 11