Jump to content

  • Log In with Google      Sign In   
  • Create Account

Regarding CPU cache and RAM and Main memory


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1506

Like
2Likes
Like

Posted 13 February 2014 - 05:53 PM

Why does cache on CPU require both data and address of program's instruction?

 

Why does loading an instruction from a program sequential require RAM or main memory?

 

It seems every a load instruction takes place it consume main memory but I am not sure why.

 

I know the facts. But I do not know the reasons for why the facts hold.



Sponsor:

#2 King Mir   Members   -  Reputation: 2050

Like
1Likes
Like

Posted 13 February 2014 - 06:21 PM

1) It needs the data to decode the instruction. It needs the address to figure out which instruction is next.

2) A program is stored in RAM.

3) I have no clue what you mean by a load consuming memory.

#3 warnexus   Prime Members   -  Reputation: 1506

Like
0Likes
Like

Posted 13 February 2014 - 07:27 PM

1) It needs the data to decode the instruction. It needs the address to figure out which instruction is next.2) A program is stored in RAM.3) I have no clue what you mean by a load consuming memory.


I mean the load operation the one that has 5 steps: fetch encode execute memory read. The load operation requires RAM in order to operate right?

#4 Ravyne   GDNet+   -  Reputation: 8146

Like
0Likes
Like

Posted 13 February 2014 - 09:20 PM

I don't know if it speaks directly to your questions, but there seems to be some confusion about the relationship between cache, RAM, and main memory.

 

Conceptually, your program only knows about address space -- that address space might be mapped to the RAM modules installed in your computer, it might be mapped to memory or registers on a peripheral device (like a graphics card), or it might be mapped to virtual memory that's currently swapped out to sectors on your hard disk drive.

 

Cache isn't separate, it just mirrors the contents of the address space in a small amount of fast memory that's closer to the CPU itself (on the CPU silicon, even). Caches aren't magic, all they do is operate on the principle that if you access word 0x0FFFFF04, you're more likely to access 0x0FFFFF08 soon than any other memory. When you read just one byte from the address space, it isn't just one byte that comes back -- to the CPU, you only see the byte you asked for, but the data path between the cache and RAM is an entire cache-line wide (usually 32 bytes or more, IIRC) and it all goes into the cache -- so when you ask for the next byte, you don't walk all the way out to main memory and back, you just walk to the cache and back. Of course, the cache is smaller than the address space, so eventually it gets full and you need to start over-writing what's in the cache. There's different structures and ways a cache can work, but that's the basic principle of it. All of its automatic and you don't usually have to worry about it. All you really need to care about, if memory bandwidth or latency begins to bottleneck your application, is arranging your data structures in such a way that memory that gets used together in next to each other (that oversimplifies a bit, but is the basic idea) so that its all in the cache at the same time.



#5 frob   Moderators   -  Reputation: 22714

Like
4Likes
Like

Posted 13 February 2014 - 09:40 PM

First off, you've asked a lot of hardware questions and mostly they've been for personal learning, but if I find out it is for homework I'll hunt you down. :-)


Most CPU instructions require a register, a memory address, a constant, or a combination of them.

opcode register
opcode address
opcode address register
opcode register address
opcode register constant
and so on.

Older machines (and sometimes modern microprocessors) did not have any memory cache on the processor. If the opcode required a memory address then everything stops and waits for the memory's content to be moved to or from the processor.

So now moving on to the questions.

>>Why does cache on CPU require both data and address of program's instruction?

A cache is just a local copy so the wait doesn't take as long. You need the address (where the copy is supposed to be from) and the data (the stuff at that address).

Many modern processors include caches dedicated to the data and instructions separately because they have different access patterns. Instruction caches make sense to keep the contents of functions in place. Data caches make sense to keep source and target buffers in place. Different access patterns mean different algorithms are helpful.
 
>> Why does loading an instruction from a program sequential require RAM or main memory?

I'm going to have to guess at the meaning because either some words were left out or there is an English translation issue.

Instructions are generally run sequentially. The first instruction is run, then the second, then the third. There are a few instructions that cause it to jump to a different place; some are conditional jumps and others are unconditional jumps. You write conditional jumps in high level languages with if statements, loops, and anything that says "if a condition is true, do something different". An unconditional jump simply moves to a new location, like a function call or a function return; no questions asked, you just go there.

>> It seems every a load instruction takes place it consume main memory but I am not sure why.

I want to give an answer but do not understand the question.
 


Because of all the questions you keep asking, you might enjoy looking at some old 6502 programs. There are quite a few games for the Atari 2600, the most popular game console at the end of the 1970s, that could be fun to study. Google turns up hundreds of good emulators, and also a large number of good tutorial sites that can help build simple games fairly easily. You won't be writing million-copy blockbusters, but just learning simple things like how to cycle through the screen colors on an old 6502-chip system is both very easy and very instructive.

I've seen people pick up some of the old 6502-chip systems where the first week is trying to figure out what is going on, the second week is experimenting with crazy ideas, and the third week is pushing out minimalist clones of pong, ski and other ancient games. Considering programmers of that era often only had 40 to 60 working days to create blockbuster titles, with today's shared information and super-large storage there are hobby developers who put together some amazing games on that generation's hardware. I had a co-worker who was very into those, he had programmable Atari 2600 cartridges, programmable Vectrex cartridges, and a few others with rather amazing homebrew games on them.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#6 warnexus   Prime Members   -  Reputation: 1506

Like
0Likes
Like

Posted 13 February 2014 - 10:59 PM


It needs the address to figure out which instruction is next.

 

I thought that was the job of PC(Program Counter)



#7 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 13 February 2014 - 11:01 PM

 

1) It needs the data to decode the instruction. It needs the address to figure out which instruction is next.2) A program is stored in RAM.3) I have no clue what you mean by a load consuming memory.


I mean the load operation the one that has 5 steps: fetch encode execute memory read. The load operation requires RAM in order to operate right?

 

In case the steps of executing an instruction are the part that is confusing you, the steps are based on the most complex instruction's needs and all instructions use the same steps. This is because the CPU is pipelined, so one instruction is executing its fetch while another instruction is executing its encode, etc.

Fetch is just getting the instruction (from cache, or RAM if not in cache, or generating a fault that results in a page load from disk if not in RAM).

Encode (usually referred to as decode) is decoding the instruction so the CPU knows what to do and assigning the registers that will get used.

Execute is where math and such happens, perhaps calculating the full address from an offset for a load operation.

Memory is where reads and writes to memory occur (again, from cache when possible, RAM if not in cache, etc.). This is where the actual loading of data from memory (RAM) into a register happens.

Read I assume is supposed to be write-back. There is no reading that occurs at that point, it's just saving the virtual registers back to the physical register file. I don't think this stage even exists on CPUs that lack register renaming.

 

It's worth noting that loading a constant (when such instructions exist) does not require data loaded from RAM as the constant is encoded in the instruction. So the "memory" stage of the pipeline would be a noop when loading a constant.



#8 warnexus   Prime Members   -  Reputation: 1506

Like
0Likes
Like

Posted 13 February 2014 - 11:06 PM


First off, you've asked a lot of hardware questions and mostly they've been for personal learning, but if I find out it is for homework I'll hunt you down. :-)

 

I am not sure if hardware questions are allowed in the programming section. I only post these questions because the knowledge of what goes underneath the hood of a computer might benefit me as a programmer and a self taught game programmer. It is not for homework but more to fill the gaps of what I am not sure of or have doubts about. I am taking a Computer Architecture class and these questions came up when I was typing up the notes for this class.



#9 TheChubu   Crossbones+   -  Reputation: 4756

Like
1Likes
Like

Posted 14 February 2014 - 01:24 AM

I'll just say: Don't get discouraged if you get downvotes for your questions, its okay to ask things.

 

Think of it as trading reputation points for knowledge :D


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#10 Ryan_001   Prime Members   -  Reputation: 1457

Like
1Likes
Like

Posted 14 February 2014 - 05:39 AM

I'll just say: Don't get discouraged if you get downvotes for your questions, its okay to ask things.

 

Think of it as trading reputation points for knowledge biggrin.png

 

Its sad but true, I find Gamedev to be a rather hostile forum.



#11 warnexus   Prime Members   -  Reputation: 1506

Like
0Likes
Like

Posted 14 February 2014 - 07:19 AM

I'll just say: Don't get discouraged if you get downvotes for your questions, its okay to ask things.
 
Think of it as trading reputation points for knowledge :D


One of the funniest statements I have read on this forum.

#12 Ravyne   GDNet+   -  Reputation: 8146

Like
1Likes
Like

Posted 14 February 2014 - 02:39 PM


Its sad but true, I find Gamedev to be a rather hostile forum.

 

I don't think its overly hostile unless you ask overtly silly questions, or take a wrong-headed opinion and really stick to your guns. I do think people are unfairly down-voted on occasion, and when I see people in the negative for it I take a look through the thread, and at the post and if they don't seem to be in the wrong I'm happy to upvote even if they're incorrect about something.

 

People can make honest mistakes or be genuinely confused and shouldn't fear being punished for it. And there does seem to be a piling-on effect that's really not helpful or productive.

 

But I think the system mostly works -- Its pretty rare that I downvote anyone. When I do they're objectively wrong and unwilling to admit it, or rude. I also tend to follow an axiom that I don't downvote unless I or someone else has/can correct the information. It seems unfair to downvote without letting its target know why, or how they're wrong.



#13 King Mir   Members   -  Reputation: 2050

Like
0Likes
Like

Posted 14 February 2014 - 02:44 PM

It needs the address to figure out which instruction is next.

 
I thought that was the job of PC(Program Counter)

The program counter is the address of the instruction.

1) It needs the data to decode the instruction. It needs the address to figure out which instruction is next.2) A program is stored in RAM.3) I have no clue what you mean by a load consuming memory.


I mean the load operation the one that has 5 steps: fetch encode execute memory read. The load operation requires RAM in order to operate right?

Of course it does; it's job is to load data from memory.

Edited by King Mir, 14 February 2014 - 02:45 PM.


#14 warnexus   Prime Members   -  Reputation: 1506

Like
0Likes
Like

Posted 14 February 2014 - 02:51 PM


Of course it does; it's job is to load data from memory.

 

Oh I get it now! Thanks for clearing up my doubt with all the questions. Everyone else did a great job too.



#15 Ryan_001   Prime Members   -  Reputation: 1457

Like
1Likes
Like

Posted 14 February 2014 - 04:09 PM

Its sad but true, I find Gamedev to be a rather hostile forum.

 
I don't think its overly hostile unless you ask overtly silly questions, or take a wrong-headed opinion and really stick to your guns. I do think people are unfairly down-voted on occasion, and when I see people in the negative for it I take a look through the thread, and at the post and if they don't seem to be in the wrong I'm happy to upvote even if they're incorrect about something.
 
People can make honest mistakes or be genuinely confused and shouldn't fear being punished for it. And there does seem to be a piling-on effect that's really not helpful or productive.
 
But I think the system mostly works -- Its pretty rare that I downvote anyone. When I do they're objectively wrong and unwilling to admit it, or rude. I also tend to follow an axiom that I don't downvote unless I or someone else has/can correct the information. It seems unfair to downvote without letting its target know why, or how they're wrong.

 
I wish that were true, my experience has been the complete opposite.  For example I got down-voted on this post: http://www.gamedev.net/topic/653308-how-alive-is-oop/#entry5130883.  I didn't insult or denigrate anyone.  Every contentious statement I prepositioned with 'I feel' or 'I personally think'.  No one in subsequent posts even addressed the issues I mentioned.  But just stating the opinion that 'C++ isn't a true OOP language' is enough.  In my university days these are the things I'd have long debates about with my colleagues.  Is encapsulation sufficient for OOP or is inheritance a requirement?  There are legitimate arguments on both sides, and it can be a very interesting discussion.  But that back-n-forth can never happen on GameDev.  Its why I like the TigSource.com forums.  They're certainly less knowledgeable, but far more accommodating to differing opinions and ideas.
 
That's just a minor, albeit recent, example.  There are many more egregious situations that have occurred in the past.  TBH, I don't really care about the ranking.  I've learned long ago to tread cautiously in GameDev, and to never express an opinion.  Its just sad IMHO that that's the way it is.
 
I'll leave it at that, this has become quite tangential to the OP...

Edited by Ryan_001, 14 February 2014 - 04:09 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS