Registers and Cache

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
Hi guys, I'm studying course of Computer Organization and still in doubt why we would use Cache if we are already having registers?
registers are faster than Cache ... any good explanation about why we would use Cache to let CPU works faster ? thanks

As a designer why would I use Cache and not registers to mapping from main memory? register is smaller and faster ..that's what we are actually look for no?
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,237
Hi guys, I'm studying course of Computer Organization and still in doubt why we would use Cache if we are already having registers?
registers are faster than Cache ... any good explanation about why we would use Cache to let CPU works faster ? thanks
Ask yourself the following questions when analyzing your question.
  • What is the magnitude of data locations (variables)?
  • How many registers are there?
  • How many locations in cache are there?
  • If data is an array of text, will that fit into registers
 

Papabravo

Joined Feb 24, 2006
22,078
Registers are used for data
Cache is used for instructions and data
You don't execute code from registers, but you can have a large data array in cache
Two different purposes
 

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
Registers are used for data
Cache is used for instructions and data
You don't execute code from registers, but you can have a large data array in cache
Two different purposes
So , lemme ask if registers are used for DATA , I could have instruction which it's as DATA, why not?
once again I'm not saying that the PC isn't working as what you described, but in aspect of designer why wouldn't I use a register for INSTRUCTIONS?
 

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
Ask yourself the following questions when analyzing your question.
  • What is the magnitude of data locations (variables)?
  • How many registers are there?
  • How many locations in cache are there?
  • If data is an array of text, will that fit into registers
Yes why not? if data is an array of text, lets assume data is about 45 instructions .. then why wouldn't I use registers? 45 registers I put on the CPU chip which at every register hold one instruction, now if I want to access one instruction, it's saved into register, the access to the register is faster than CACHE .. where's the problem?
 

Papabravo

Joined Feb 24, 2006
22,078
So , lemme ask if registers are used for DATA , I could have instruction which it's as DATA, why not?
once again I'm not saying that the PC isn't working as what you described, but in aspect of designer why wouldn't I use a register for INSTRUCTIONS?
In order for a register to be used as an instruction there has to be a pathway from a General Purpose Register to the Instruction Register where instructions are decoded and executed. Normally this pathway is from main memory to the Instruction Register. You could of course make this part of your design, but for what purpose, and at what cost?
 

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
In order for a register to be used as an instruction there has to be a pathway from a General Purpose Register to the Instruction Register where instructions are decoded and executed. Normally this pathway is from main memory to the Instruction Register. You could of course make this part of your design, but for what purpose, and at what cost?
HOOOOOP! HERE YOU ARE ACTUALLY HITTING MY POINT !

Well, lets assume I'm a designer .. so the "bridge" between CPU and MAIN MEMORY are registers (for instance lets assume 100 registers I have for getting data in parallel from main memory ) , I'm a designer fine with this ! but my question is, why in our days the bridge is not actually registers , it's CACHE .. here's my point ! , registers invested before cache and then the idea of the cache, so from your speech I could say that designer grab the idea of cache without any advantage on registers? impossible then we wouldn't see computers today using caches !

I'm not making it hard, but just want to know and confused why would I use cache if there would be registers and I can use them to save instructions, for instance 6000 registers? I use 6000 registers where's the problem?!
 

MrChips

Joined Oct 2, 2009
34,731
Do not confuse registers and memory.

Registers are your calculators. A CPU might have 1 to 64 registers depending on the CPU design. Read/write access time using registers is very fast.

System memory is for storing things, instructions as well as data. Total number of memory storage can be anything from 0 to 64GB. Memory read/write access time is much slower compared to that of registers.

Why use a cache?
A cache is used to pre-fetch instructions from slow memory so that when comes time to fetch new instructions or data the CPU have faster access to the instructions and data.
 

djsfantasi

Joined Apr 11, 2010
9,237
You’re conflating three different concepts.

Registers are not memory. They are physically part of a CPU. ADDING 6,000 registers may not be physically possible. There are design constraints within a CPU. For physical die trace lengths affect the CPU speed.

Cache are used for instructions and data. The cache goes to two physical memory locations. The instruction stack and memory (operands). It has a much faster fetch speed. However, it is too expensive to use as primary memory.

Memory is much larger... and since it is orders of magnitude larger, less expensive/ slower chips are used to be economically feasible.

So, you have to consider more than just movement of a bunch of bits. The design must match component speeds, function and cost.

Just imagine a Lamborghini that seats 6000 people, pulling a U-Haul. Would not be a practical vehicle. You can’t go from 0 to 100 mph immediately. Memory to cache to CPU is the CPU designers way of accelerating.
 

MrChips

Joined Oct 2, 2009
34,731
Let us take your design concept.

Suppose we made a CPU with 6000 registers (possible but at tremendous expense).

Now we prefetch instructions and data from system memory into the 6000 registers.
We now have a totally unmanageable collection of instructions and data.

How do we perform arithmetic operations on the data? Where is the data located? The program code does not know which register to perform the desired operation.

Bottom line is, memory and registers serve two different functions.
 

Papabravo

Joined Feb 24, 2006
22,078
HOOOOOP! HERE YOU ARE ACTUALLY HITTING MY POINT !

Well, lets assume I'm a designer .. so the "bridge" between CPU and MAIN MEMORY are registers (for instance lets assume 100 registers I have for getting data in parallel from main memory ) , I'm a designer fine with this ! but my question is, why in our days the bridge is not actually registers , it's CACHE .. here's my point ! , registers invested before cache and then the idea of the cache, so from your speech I could say that designer grab the idea of cache without any advantage on registers? impossible then we wouldn't see computers today using caches !

I'm not making it hard, but just want to know and confused why would I use cache if there would be registers and I can use them to save instructions, for instance 6000 registers? I use 6000 registers where's the problem?!
Not really. If you look at the history of computer architecture you have two different models. The "Harvard" model where data memory and instruction memory are separated, and may in fact be different sizes in terms of width and depth. Then you have the "Princeton" architecture where a single memory contains both instructions and data. One of the big drawbacks of early "Princeton" machines was their ability to generate and use self-modifying programs. This feature is normally deprecated in modern machines for reasons that must be manifestly obvious from a software security and integrity point of view. You might want to study where we have been before going off with bizarre ideas of what might be. Remember the law of unintended consequences.
 

WBahn

Joined Mar 31, 2012
32,784
Hi guys, I'm studying course of Computer Organization and still in doubt why we would use Cache if we are already having registers?
registers are faster than Cache ... any good explanation about why we would use Cache to let CPU works faster ? thanks

As a designer why would I use Cache and not registers to mapping from main memory? register is smaller and faster ..that's what we are actually look for no?
It might be what you are looking for provided you assume your customers don't care how much they are going to have to pay for your processor.

Registers are also insanely expensive. How many registers does a typical modern CPU have? It's on the scale of a dozen or two. How much memory does a typical level-1 cache have?
 

nsaspook

Joined Aug 27, 2009
16,289
Explicit registers need to be encoded in the cpu opcodes. Imagine the instruction decoder and extra space need in opcodes to address 6000 registers and how long it would take to save the complete register context in an interrupt. We do have a implicit register 'cache' in modern processors because of out-of-order execution but it's mainly invisible at the program level.

https://www.d.umn.edu/~gshute/arch/register-renaming.xhtml
A Brief History
The first renaming architecture was used for floating point instructions in late high performance models of the IBM 360 family of of computers [Tomasulo]. At that time it was not called register renaming. Tomasulo's architecture did not have value registers. Instead it used reservation station numbers for naming values. Each reservation slot had space for two source operand values. The reservation stations had circuitry to update the source operands when functional units produced result values.

The term "register renaming" was used by compiler writers for a technique that they used for code optimization. The term was later adopted by computer engineers.

Register renaming was used in many processors starting in the mid 1990s. Today it is used in almost every desktop and server processor.
 

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
Do not confuse registers and memory.

Registers are your calculators. A CPU might have 1 to 64 registers depending on the CPU design. Read/write access time using registers is very fast.

System memory is for storing things, instructions as well as data. Total number of memory storage can be anything from 0 to 64GB. Memory read/write access time is much slower compared to that of registers.

Why use a cache?
A cache is used to pre-fetch instructions from slow memory so that when comes time to fetch new instructions or data the CPU have faster access to the instructions and data.
I almost got the point.
for your question "why use a cache" .... then why we are using it? my cache is smaller than the main memory, otherwise we wouldn't call it a cache, and I demand every time a new instruction that's not found on the cache, but found in the main memory .. so the time for every new access is about a time of main memory (because in that Case I have many Cache MISS) .. so designing a cache isn't a good idea .. it's not working fine and faster than main memory if we have new instructions not found on cache !
as a result what's the purpose of using it?! if it's not giving me a faster time than main memory in every case... otherwise in specific cases(when there's cache hit) ........ so what its advantage over the main memory?!
 

Thread Starter

AndrieGnd

Joined Jun 25, 2019
52
You’re conflating three different concepts.

Registers are not memory. They are physically part of a CPU. ADDING 6,000 registers may not be physically possible. There are design constraints within a CPU. For physical die trace lengths affect the CPU speed.

Cache are used for instructions and data. The cache goes to two physical memory locations. The instruction stack and memory (operands). It has a much faster fetch speed. However, it is too expensive to use as primary memory.

Memory is much larger... and since it is orders of magnitude larger, less expensive/ slower chips are used to be economically feasible.

So, you have to consider more than just movement of a bunch of bits. The design must match component speeds, function and cost.

Just imagine a Lamborghini that seats 6000 people, pulling a U-Haul. Would not be a practical vehicle. You can’t go from 0 to 100 mph immediately. Memory to cache to CPU is the CPU designers way of accelerating.
but lets assume that I access the cache every time and the instruction I want to fetch isn't found on the cache (cache miss) .. so I will go to the main memory for getting that instruction .. so we didn't get the benefit of cache in that case ... the time isn't faster .. it's about the time of main memory . so using cache isn't giving me faster access time every time .. it may be not giving me faster time in a case of cache miss .
As a result, if cache isn't working faster at every case (including cache hit and miss cases) then it wouldn't be an advantage over the main memory, because if I have too many cache miss then every access I want to access the main memory .. where's the benefit of cache regarding to time access? we didn't get the advantage of it.... we implicitly will go to the main memory every case (cache miss) so we can say we didn't have cache "logically" but physically we have , so we just plug cache memory and not giving me any benefit in aspect of time access of cache miss(going to main memory every access because we don't have the data/instruction on cache)?!
 

MrChips

Joined Oct 2, 2009
34,731
At least you understand the limitations of the cache, i.e. problem of cache hit/miss.

How would you increase the likelihood of a cache hits?

1) Make the cache larger.

2) Optimize your code so that a program branch will be directed to a location within the cache, i.e. make your code tight.
 

Analog Ground

Joined Apr 24, 2019
460
Another aspect of cache vs main memory is in a symmetric multi-processing system which are very common. Cache is dedicated memory for a particular CPU and main memory is in common to multiple CPUs. A processor probably does not have to wait to access cache. However, it may have to wait to get to main memory. So, with multiple CPUs, cache is essential to keep all CPUs going, as much as possible, all the time.

In a four processor real-time system I designed, controlling the use of cache was essential in getting hard real time and the benefit of multiple processors.
 

djsfantasi

Joined Apr 11, 2010
9,237
but lets assume that I access the cache every time and the instruction I want to fetch isn't found on the cache (cache miss) .. so I will go to the main memory for getting that instruction .. so we didn't get the benefit of cache in that case ... the time isn't faster .. it's about the time of main memory . so using cache isn't giving me faster access time every time .. it may be not giving me faster time in a case of cache miss .
As a result, if cache isn't working faster at every case (including cache hit and miss cases) then it wouldn't be an advantage over the main memory, because if I have too many cache miss then every access I want to access the main memory .. where's the benefit of cache regarding to time access? we didn't get the advantage of it.... we implicitly will go to the main memory every case (cache miss) so we can say we didn't have cache "logically" but physically we have , so we just plug cache memory and not giving me any benefit in aspect of time access of cache miss(going to main memory every access because we don't have the data/instruction on cache)?!
And that is where the cache, firmware and operating system design are important.

It’s all about balance.

Cache is designed to optimize cache huts.

Software (firmware and OS) are also designed for the same objective. Optimize cache access. The difference in speed of cache versus main memory times the probability of cache hits is optimized to result in memory access times that are minimized. It’s much more complex than cache is 100 times faster than main memory or main memory 10o times slower (illustrative numbers only). If I access cache 200 times before a cache hit, I will have sped up memory access 2:1.
 

WBahn

Joined Mar 31, 2012
32,784
I almost got the point.
for your question "why use a cache" .... then why we are using it? my cache is smaller than the main memory, otherwise we wouldn't call it a cache, and I demand every time a new instruction that's not found on the cache, but found in the main memory .. so the time for every new access is about a time of main memory (because in that Case I have many Cache MISS) .. so designing a cache isn't a good idea .. it's not working fine and faster than main memory if we have new instructions not found on cache !
as a result what's the purpose of using it?! if it's not giving me a faster time than main memory in every case... otherwise in specific cases(when there's cache hit) ........ so what its advantage over the main memory?!
It doesn't have to give you faster access than main memory in EVERY case. It can't do that.

When I was working on F-15s we had several large tool kits in our shop. Those were "main memory". The tools we were actually using and had in our hands or setting on the bench were in "the registers", We also had a small tool kit that we would take out to the flight line with us which was our "cache". If we were out working on a bird and the tool we needed was not in the register (in our hand or on the near us because we had just used it) we would look in the tool box. If it was there, we could get it right away, if not, we had to walk back to the shop and get it. That's a cache miss.

So your claim basically comes down to saying that because not every tool that we might ever need is in the small tool box, there was no value in having it and we should simply walk back to the shop for every tool we needed. In reality, as long as the tool we needed was in the tool box frequently enough, it was a HUGE time saver having it there.

The same with cache memory. What makes it extremely useful are the properties of spatial and temporal locality of memory accesses. This merely means that when we access a certain memory location, there is a high likelihood that we will need to access that same memory location in the near future or that we will need to access nearby memory locations in the near future, so by loading the block of memory containing the location we are interested in right now, there is a high likelihood than many of the accesses we make in the near future will be found in that same block.
 
Top