User has memory machine. It has one layer for data storage and another layer for cache. User has stored an array with length N in the first layer.
When CPU needs data, it immediately checks in cache memory whether it has data or not. If data is present it results in CACHE HITS, else CACHE MISS, i.e., data is not in cache memory so it retrieves data from main memory and inserts a block of data into cache layer.
The question that arises is : How many times machine will need to load a block into cache layer, i.e. determine number of CACHE MISS?
Let us assume an array and denote its elements by A0, A1, … , AN?
Machine loads array in blocks with size B :
Assume block size is 4.
1 2 3 4 comes in Block 1, 5 6 7 8 comes in Block 2 and 9,10 comes in Block 3.
A0, A1, … , AB? 1 form a block; AB, AB+1, … , A2B? 1 form another block, and so on. The last block may contain less than B elements of user’s array.
Cache may only contain at most one block at a time. Whenever user tries to access an element Ai, machine checks if block where Ai is located is already in cache, and if it is not, loads this block into cache layer, so that it can quickly access any data in it.
However, as soon as user tries to access an element that is outside currently loaded block in cache, block that was previously loaded into cache is removed from cache, since machine loads a new block containing element that is being accessed.
User has a sequence of elements Ax1, Ax2, …, AxM which he wants to access, in this order. Initially, cache is empty. We need to find out how many times machine will need to load a block into cache layer.
Input Format :
- First line of each test case contains three space-separated integers N, B and M.
- Second line contains M space-separated integers x1, x2, …, xM.
5 3 3 0 3 4
Machine stores elements [A0, A1, A2] in one block and [A3, A4] in another block. When accessing A0, block [A0, A1, A2] is loaded. Then, accessing A3 removes previous block from cache and loads block [A3, A4]. Finally, when user accesses A4, a new block is not loaded, since block containing A4 is currently loaded in cache.
- Initially cache hit occurs because cache layer is empty and we find next multiplier and starting element.
- Obtain user value and find next multiplier number which is divisible by block size.
- Find starting elements of current block.
- If user value is greater than next multiplier and lesser than starting element then cache hit occurs.
Time Complexity : O(m)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Locality of Reference and Cache Operation in Cache Memory
- Cache Memory in Computer Organization
- Difference between Virtual memory and Cache memory
- Cache Organization | Set 1 (Introduction)
- Computer Organization | Locality and Cache friendly code
- Cache Memory Design
- Differences between Associative and Cache Memory
- Concept of Cache Memory Design
- Difference between Cache Memory and Register
- 2D and 2.5D Memory organization
- Random Access Memory (RAM) and Read Only Memory (ROM)
- Introduction to memory and memory units
- Difference between Byte Addressable Memory and Word Addressable Memory
- Difference between Random Access Memory (RAM) and Content Addressable Memory (CAM)
- Difference between Uniform Memory Access (UMA) and Non-uniform Memory Access (NUMA)
- What's difference between CPU Cache and TLB?
- Multilevel Cache Organisation
- Cache Coherence Protocols in Multiprocessor System
- Write Through and Write Back in Cache
- Types of Cache Misses
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.