How to calculate Cache hit ratio from 2-way set associate cache - caching

On my assignment we have 2 questions: we have a 2-way set associative cache. The cache has four sets in total. Main memory consists of 4K blocks of 8 words each and word addressing is used.
Part a) ask to demonstrate the address format, which I've solved to be word = 3 bit set =2 bit and field = 7 bit. The problem im having is in part b):
Compute the hit ratio for a program that loops 3 times from location 8 to location 51. In other words, think of this as an assembly language program that runs from the opcode at location 8 to the opcode at location 51m then loops back to location 8. It does three such iterations in total.
Now to my understanding after the research I've done there's a standard normally some sort of speed or hit rate that is given? I was wondering how do i calculate the hit ratio if i don't know a miss rate, a miss penalty a cache speed or anything?

I think we're in the same class lol I have the exact same question on assignment due tonight.. Anyway I did some research and found this answer to a similar question on chegg:
a. Given that memory contains 2K blocks of eight words.
2K can be distributed as 2K * 23 = 211* 23 = 214 so we have 14-bit addresses with 9 bits
in the tag field, 2 bits in the set field and 3 in the word field
b. First iteration of the loop:
→ Address 8 is a miss, then entire block brought into Set 1.9-15 are then hits.
→ 16 is a miss, entire block brought into Set 2, 17-23 are hits.
→ 24 is a miss, entire block brought into Set 3, 25-31 are hits.
→ 32 is a miss, entire block brought into Set 0, 33-39 are then hits.
→ 40 is a miss, entire block brought into Set 1 41-47 are hits.
→ 48 is a miss, entire block brought into Set 2, 49-51 are hits.
For the first iteration of the loop, we have 6 misses, and 5*7 + 3 hits, or 38 hits.
On the remaining iterations, we have 5*8+4 hits, or 44 hits each,for 88 more hits.
Therefore, we have 6 misses and 126 hits, for a hit ratio of 126/132, or 95.45%.
Hope this helps, good luck!

Related

Calculating the Values stored in the TAG field

Question:
Consider a computer system that has a cache with 4096 block. Each block can store 16 bytes. What will be the value stored in the TAG field of the cache block that holds the memory block containing the address 0xABCDEF.
a. if it is Direct Mapped Cache
b. if it is 16 way set associate cache
c. if it is fully associative cache
Here is my work/logic below:
We know that each block can store 16 bytes. So that 2^4. Meaning our block offset is 4
ABCDE is 24 bits, b/c 4 bits per piece
4096 blocks is 2^12
a. is it is directly mapped then 24 - 20 - 4 --> 0
b. if its 16 way then our calculation are 24-16(index) - 4(offset) --> 4
c. if it fully associate then we do have an index and its just 24 - 4 --> 20
I am not sure if I am approaching the question the right way. Any help would be much appreciated!
I am using this illustration as my reference for how cache is represented:
http://csillustrated.berkeley.edu/PDFs/handouts/cache-3-associativity-handout.pdf
Ok, so I figured it out.
a. Since our cache is directly mapped. We have 2^12 block blocks. That means the index will be 12, and with an offset of 4. The TAG value will be 8
b. Now since is 16 way associative. 2^4 = 16. So we do 2^12/ 2^4 ==> 2^8
This means the 8 will be in our index slot and the TAG value will be 12
c. If it is fully associative, that mean we won't have to account for the index slow. So its just 24 - 20 ==> 4
Hope this helps anyone who bumps into this

Mapping 512KB main memory into 1KB cache homework question

I'm sorry if I made an error in posting this. Please let me know if I need to change anything.
I've received my computer architecture homework back and I missed this question. My professor's explanation didn't make sense to me, and I disagree with what he told me, so I am here asking what you guys think.
Here is the question:
A computer uses 16-bit memory addresses. Main memory is 512KB, and the cache is 1KB with 32B per block. Given each of the following mapping functions, calculate the number of bits in each field of the memory address.
Here is how I worked through the direct mapping part of the problem:
Cache memory: 1KB (2^10), 16-bit memory addresses (1 word = 2B) -> 1024B/2B = 512 words, 16 words per block (32B) -> 512/16 = 32 cache memory blocks.
Main memory: 512 KB (2^19), 16-bit memory addresses (1 word = 2B) -> 524288B/2B = 256K words, 16 words per block (32B) -> 256K/16 = 16384 or 16K main memory blocks.
I understand the word tag as such: 32B per block allows for 16 16-bit memory addresses per block. This (I believe) supports that: 1 word = 16 bits = 2 B -> 32B/2B = 16 words in each block. This equates to 2^4 = 4 bits for determining which word in the block, leaving 12 bits for tag and block bits in the memory address.
Now, in order to map 16K main memory blocks directly into 32 cache memory blocks, there will have to be 512 main memory blocks mapped to each cache memory block. So 512/16K blocks per 1/32 blocks.
Here is where I am confused. Doesn't this require 9 tag bits, as 2^9 = 512 (main memory blocks possibly mapped into one cache memory block)?
For the block bits, which point to a particular block in the cache, this requires 5 bits. 2^5 = 32, blocks in cache memory.
This would require 18 bits in the memory address.
Here is my professor's answer for this question:
2^5 = 32 -> 5 Word bits
(1KB)/(32B) = 32 blocks -> 5 Block bits
16 – 5 – 5 = 6 Tag bits
I did not realize I could simply subtract the required block and word bits to get the tag bits. But it still doesn't make sense to me. 2^6 = 64 blocks per cache block. 64*32 gives 2048. I can't wrap my head around this. Can someone please help?
Okay, the terminology that i learnt is slightly different but the principal should be the same for this explanation.
So cache will have multiple sets (sort of like a cell). And each set will have 1 cache line (containing 1 block of data) or multiple cache lines (each contain 1 block of data) (direct mapping or n-associativity mapping).
In mapping the main memory blocks to the cache, the main memory address (16 bit) is divided into 3 fields: tag, index bits and offset bits. A memory cell is 1 byte and a block is made up of a few cells
Offset bits are used to access the individual bytes of a memory block. Think of it as the offset on top of the block base address to get the byte you want (i assume your memory should be byte-addressable rather than word-addressable as it doesn't make sense to access 2B word as this would be inflexible) And here your prof/textbook call it as word bit. Hence if a block has 32 Bytes, there would be log2(block size) = 5 bits needed to access the individuals cells in the mapped block.
Index bits (in direct mapped cache is called block bits too as the number of set is the same as the number of blocks in the cache) is used to identify which set/cache line/ cache block that the main memory block is mapped to the cache. There are 1KB/32B = 32 cache blocks in the cache. As direct mapping is used, each set contain only 1 cache block and therefore there will be 32 set in this cache. Thus to access the correct set in cache, 5 bits is needed and therefore index bits = 5 bits
Tag is a name to determine if the data block in cache is the correct one we are looking one from the main memory. As the address of main memory is 16 bit and we already know index and offset fields, it is easy to deduce that tag will need 16 - 5 - 5 6 bits. How we determine the tag is not really a concern as the block size and cache size (and hence no. of sets in cache is given here).

How do I map a memory address to a block when there is an offset in a direct-mapped cache?

To start off, the first cache has 16 one-word blocks. As an example I will use 0x03 memory reference. The index has 4 bits (0011). It is clear that the bits equal 3mod16 (0011 = 0x03 = 3). However I am getting confused using this mod equation to determine block location in a cache with offset bits.
The second cache has a total size of eight two-word blocks. This means that there is 1 offset bit. Since there are now 8 blocks, there are only 3 index bits. As an example, I will take the same memory reference of 0x03. However now I am having trouble mapping to the block using the mod equation I used before. I try 3mod8 which is 3, however in this case, since there is an offset bit, the index bits are 001. 001 is not equal to 3 so what did I do wrong? Does mod not work when there are offset bits? I was under the impression that the mod equation would always equal the index bits.
Its all in the address. You get the address, then mask off number of bits from the end, for following reasons.
Number of words in the cacheline. If you've got 2 word cacheline (take a bit out, 4 word - 2 bts etc)
Then how many cacheline entries you have. (If is a 1024 cacheline, you takeout 10 bits. This 10 bits is your index, remaining bits are for your Tag)
Now, you also need to consider 'WAY' as well. If its a direct mapped cache, above applies. If its a 2 way set associative cache, you dont have 1024 lines, what you have a 512 blocks with each having 2 lines in them. Which means you only need 9 bits to determine the index of the block. If its 4 way, you've got 256 blocks with 4 lines in them, meaning you only need 8 bits for your index.
In a set associative cache, index are there to choose a block, once a block is chosen, use can use a policy like LRU to fill an entry in case of a cache miss. Hits are determined by comparing the tag in the selected block.
Bottom line, block location is not determined by the address, only a block is selected by the address and thereafter its Tag comparison to find the data.

How exactly to count the hit rate of a direct mapped cache?

We got a cache given with 8 frames and it's directly mapped. Following access sequence on main memory blocks has been observed:
2 5 0 13 2 5 10 8 0 4 5 2
Count the hit rate of this organized cache.
Solution:
I understand how and why the numbers are placed in the table like that. But I don't understand why 2 and 5 have been bold-printed and why we got hit rate of 17%.
This has been solved by our professor but I don't understand it completely.
Like was mentioned by #Margaret Bloom in the comments, the numbers in bold refer to cache-hits. Non-bold refer to cache misses.
You might understand it better by using this simulator: cachesimulator.com
The Simulator works with WORD-instructions only, so a little conversion of your assignment need to be made in order to simulate it:
cache-size: 32 bytes (8 rows)
block-size: 4 bytes (one word per row)
associativity: 1 (direct-mapped cache)
replacement algorithm: LRU
memory size: (any number larger than (14*4) works) for example: 1024
Now since the simulator works with WORD-instructions you need to convert your access sequence by multiplying each number by 4, also, in the simulator you enter addresses in hexadecimal so after you have multiplied by 4 you convert to hexadecimal, then you get:
8 14 0 34 8 14 28 20 0 10 14 8
In the simulator you enter instructions on the form:
<operationtype><space><register><space><address>
In your case the operationtype is LOAD and the register does'nt matter. So you can use any register, for example:
LOAD 1 8
LOAD 1 14
LOAD 1 0
LOAD 1 34
LOAD 1 8
LOAD 1 14
LOAD 1 28
LOAD 1 20
LOAD 1 0
LOAD 1 10
LOAD 1 14
LOAD 1 8
Enter the instructions above in the text-area of the simulator and click run. You can then see the cache hits and misses in real-time and when the simulation is finnished you can analyze the results by looking at the content of the cache memory and the list of instruction-results. You can view the main-memory address that each element in the cache refers to by hovering over it.
I understand how and why the numbers are placed in the table like that.
So you understand which how addresses map to cache lines, and that the vertical axis is time.
But I don't understand why 2 and 5 have been bold-printed and why we got hit rate of 17%.
The table entries are bold (cache hit) when the previous access to the same cache line was to the same address. A different address that maps to the same cache line causes a cache miss (evicting the old contents).
Visually / graphically: look vertically upwards in the same column to see which data is currently hot in the cache line.
Obviously once you know how many cache hits there were, calculating the hit rate is easy.
Normally you should just ask your professor extremely basic questions like this. However, your diagram was really easy to understand, so it made this trivial question easy to understand and answer.

Understanding Direct Mapped Cache

I'm trying to understand direct mapped cache, but it is a very complex concept. I have written what I think I understand so far, but I am unsure whether I am correct or not. Can somebody please verify if the explanation below is correct?
E.g, for a made up computer, just for the sake of this question, there 1024 memory locations (cells) in the RAM. This equals 2^10 so the address for each of these memory locations must be 10 bits long.
The CPU is asked to get data from the RAM memory address 1100100111. However the CPU doesn't access the data directly from this memory address in the RAM. The RAM stores this data to cache memory and then the CPU gets the data from the cache memory.
There are different ways of doing this, one being direct mapped cache. The cache memory and ram memory are divided up into blocks, where the number of cells in the blocks in each memory must be the same. The number of blocks in the RAM and cache must also be a power of 2.
In this example lets say there are 2^6 = 64 blocks in the RAM, so there are 1024/64 = 16 cells in each block. Lets say there are 2^2 = 4 blocks in the cache, so the cache has 64 cells. The "6" and "2" in the exponents of these numbers are important later on.
Because the The number of blocks in the RAM and cache is a power of 2, it makes the calculations easy. In our address 1100100111 the last 6 bits mark the offset 100111 (the 6 comes from the fact that 2^6 = 64), and the remaining 4 bits 1100 mark the RAM block number the data is stored in. Within this block number are two other important numbers. First the cache block number; this is the cache block that that RAM block would store to. This is the first 2 bits after the offset, so it will be 00 (The 2 comes from the fact that There are 2^2 = 4 blocks in the cache). The remaining 2 numbers in the address mark the tag. This will be 11.
So when the CPU is asked to get data from memory address 1100100111 it will look for this data in cache block number 00. It will compare the tag of the address 11 to the tag saved in the cache, which is a separate piece of memory used to store information about where from the RAM the data has come from. If the tags are the same this is a hit and this is the data the CPU is looking for. If the tag of the address and the tag in the memory are different, then this is a miss, and the data isn't stored in the cache.
If this is the case, the cache controller will get the data from block number 1100 in the RAM and store it in the cache block number 00, and update the tag in this block to 11. The CPU can now get the data in this block.
Is this all correct? I need to understand this before I can start to try and understand associative and set associative memory.
Thanks!
You have the right idea, but your numbers went wrong somewhere. In your example you have a direct-mapped cache of 4 blocks/lines of 16 bytes/cells each. The address 1100100111 will be divided up as follows. You use the least significant four bits 0111 as the offset because it refers to which cell of a particular block you want. I think you accidentally included the block number as part of the offset. Anyway, the next least significant two bits 10 will be the block number and the most significant four bits 1100 will be the tag.
Your understanding seems to be fine. One thing more that is necessary is a bit to indicate if the cache block is valid or not. Good luck with the associative stuff!

Resources