r/AskComputerScience • • 5d ago

Which processes are unique to the RAM and CPU?

I know this probably sounds like a cliché question, but I've been handed 101 explanations of what RAM and the CPU actually do, and I've never managed to really get my head around it, since nobody ever gives concrete examples.

I get that the CPU has a small number of cores so it can handle complicated tasks very fast, while a graphics card has thousands so it can crank through lots of simple calculations quickly (like figuring out where an object is, shadows, and so on). The graphics card part makes sense to me, but what exactly are these specific complex tasks the CPU does?

So which particular processes do the CPU and RAM carry out, and where do the two overlap and interact?

0 Upvotes

18 comments sorted by

7

u/StaticCoder 5d ago

The CPU is generally better at handling lots of conditions, e.g. if this thing is greater than 10 and less than 30, then check if that other thing is 0, and if so do such and such. GPU are better at doing lots of unconditional things, especially in parallel, e.g. add these 32 pairs of numbers.

4

u/Obliviousnut_ 5d ago

As far as I’m concerned RAM does not carry out tasks or instructions it houses memory. Consider it a pile of books that you need to sort.
The CPU handles the tasks. It loads in, say, two books at a time and lets you evaluate their equivalence; I.e is Book A bigger than Book B. After which it might put book B in the stack of Big books and Book A in the other stack.
Considering this analogy, all that a CPU does is take something(s) from RAM and do some operation on them. This can be addition, subtraction, multiplication, comparisons etc, which is the fundamental basis of all computer programs.

Furthermore, what distinguishes a GPU and CPU is their architecture and intention. A CPU may have 4 cores - all of which can evaluate different bits of memory separately. A GPU may have 1024 cores, again which can evaluate different bits of memory separately. For things like matrix multiplication where you have lots of multiplications acting on what can be separable chunks of memory a GPu is optimal as, say you have 1024 matrices to multiply, each core can be in charge of the operation and finish in some amount of time. Whereas a CPU will only finish in say 256x that time.

1

u/webternet 4d ago

The pile of books is more like storage and your memories of reading them is more like RAM if you think about how things are accessed.

2

u/gigayoink 5d ago

Gonna assume you meant GPU and not RAM throughout your question.

The specific things CPUs do really well are:

  • Any kind of conditional and branching code. I.e. most application level code. CPUs are amazing at branch prediction and prefetching, which lets them do single tasks and sequential tasks like string processing much faster than any GPU.
  • Anything that requires a high degree of flexibility at runtime, such as debugging, instrumentation, instruction patching, runtime linking, hooks and events, tracing, ASAN/TSAN, fine grained locking.
  • Anything relating to system management, like setting up page tables, implementing system calls, managing process level permissions, managing interrupts, and implementing process switching.

2

u/Feldspar_of_sun 4d ago

Branch prediction is still one of those concepts that’s hard for me to wrap my head around on a hardware level, especially things like speculative execution. It’ll never cease to amaze me the insane things we can make rocks do with on/off switches

2

u/Discordbothost 4d ago

A loop is a good example. The last few trips through it tell the predictor that the jump back to the top usually happens. The CPU can start work on the next trip while the exit check is still running. If the check says the loop is done, it discards that work and picks up after the loop.

2

u/phoenix823 4d ago

RAM is used for data storage, both CPU instructions and data. The L1/L2/L3 cache on a CPU is storage as well. RAM and cache cannot do any computation themselves, they just store 0's and 1's.

The CPU requests data from memory, actually performs instructions on the data, and saves the data back to memory. It does all manner of math, comparisons, loops, jumps, etc.

The GPU does lots of parallel instructions at once because of the thousands of cores. If you want to render a 3D image there are billions of matrix algebra equations to solve every second. This is much more specialized than a CPU, which makes the GPU much faster, but the CPU is general purpose.

2

u/Mission-Landscape-17 4d ago

Ram stores data it does not do anything else.

The number of different things a cpu can do is potentially very large. X86 cpus support over 1000 different operations. And they can be combined in any order. Each operation works on one set of data values at a time.

In contrasts gpu's are good at doing the same thing thing to many sets of data values. Because it is doing the same thing over and over it can do more in parallell, and work through data faster. But the number of supported operations is much smaller.

2

u/TheElderGregg 4d ago

RAM exists because it wasn't possible to create a storage medium able to optimize for fast, large, and non volatile all at once.

So we have a smaller amount of really fast storage that loses all its data when you power it off (RAM) and a larger amount of slower storage we load into RAM as needed that can hold data across powering it on and off (all of the typical drives we store data on.)

1

u/SnugglyCoderGuy 5d ago

All RAM does is hold information, it could be data or it could be instructions themselves. The CPU tells RAM what to store and where to store it. The RAM just holds onto it until it loses power.

1

u/barkingcat 4d ago edited 4d ago

Cpu: anything with branching and decisions if else, while, any loops, etc

GPU tends to want the data and operation and ignores everything else. (The branching is done on the cpu, and the calculations are done on the GPU)

Even simpler terms if you have a racing car that can only go straight and bank, you have a NASCAR stock car - that's like a gpu

If you want a racing car that is also good at taking turns, that is an F1 racing car, that's like a Cpu 

Now is a NASCAR stock car "faster" than an F1? Totally arguable depending on the track. In a street racetrack or on the 1 mile drag strip?

Is a GPU faster than a cpu? At somethings  but for other things it sucks .

Ram is used for everything, it's working space

1

u/cakemates 4d ago edited 4d ago

the ram is an accessory of the cpu, ram cant do any work by itself, the ram is like a notebook for saving info for the cpu. The central processing unit does most of the work in a computer, and delegates specialized work to other devices.

1

u/petrov76 4d ago

Here's an analogy: your computer is a cubicle with an office worker processing data on a sheets of papar:

  1. In this analogy, the worker is the CPU. He can read one piece of paper at a time, and do some work on the data, before putting it back down on the desk.
  2. The desk is your RAM. It holds all the data that you have recently worked on. If you have more RAM, this is like getting a bigger desk that can hold more pieces of paper.
  3. The filing cabinet in the corner is your hard disk. When you are done with a piece of paper for awhile, you can save it away to filing cabinet, but it's slow to store and fetch things from there. When your desk fills up, you have a special drawer in the filing cabinet for active pages that you can use as RAM but it's slower (this is the page file).
  4. A GPU is like hiring an autistic coworker. He can't do general purpose tasks, but is really good at processing a bunch of pages with the exact same repetitive task. Things like highlight the 4th word, or add 2 to every page number, but do it to 16 or 128 pages at once. The general purpose worker will hand off tasks to him that he's good at.

1

u/Broad-Promise6954 5d ago

None.

Okay, that's intended as a controversial answer, but hold that thought for a while.

The divisions we make today aren't completely arbitrary but they're different from the divisions we made 10, 30, and 50+ years ago, and there will no doubt be changes again in the future. We live in a world where what I call the Wheel of Reinvention spins round and round, and stuff we did long ago re-appears in a new package that's labeled with the latest marketing buzzwords. What was exclusively a CPU operation moves to a GPU. Or, something that wasn't a memory operation ("fetch and add" for instance) now is, and then isn't tomorrow.

It's a game full of moving parts. Right now RAM is strictly storage and is addressed by bytes words no wait cache lines pages where'd my atomicity go oh the IO device isn't cache coherent wait now it's PIPT now it's VIVT etc.

You get to pick your divisions and labels up to some point, until the hardware changes for speed and/or cost reasons etc.

As a general rule, though, each part of a machine, from whatever era it's from, is specialized in some way that made sense at the time.

1

u/MasterGeekMX BSCS 5d ago edited 5d ago

Did a masters in CPU architecture, so I know a few things.

RAM can only do two things:

  1. Store some data in a given address
  2. Retrieve the data stored in a given address

That's it. RAM is just a massive warehouse of Bytes, where you can put data in and then get it back later.

In the case of the CPU, the exact set of operation varies as some CPU designs only do a handful of simple operations (a scheme called RISC) while others do lots of different complex operations (CISC), but at the end of the day a competent CPU must do these five kinds of operations:

  1. Arithmetic operations over two or more numbers (addition, subtraction, shifting the bits that make the number some amount of positions, etc).
  2. Logic operations over two or more numbers (checking if two numbers are equal or if one is bigger, sending the bits of each number to logic gates like AND, OR and XOR, etc)
  3. Load numbers from RAM into the CPU, and also store numbers from the CPU into RAM
  4. Jump to a given instruction in the code
  5. Jump to a given instruction in the code only if some condition is true

In most cases we consider that RAM isn't doing an operation, and instead it is simply a subordinate to the CPU, helping it to carry it's tasks

A GPU is not so different from the CPU and RAM you have as the main drivers, as both are in the end circuits that run calculations. The difference is that GPUs are built around doing lots of operations in parallel, while also having an obnoxiously fast and wide communication with RAM. I like to think it like CPUs being a sciences PhD with four scientific calculators, while a GPU is an army of middle schoolers armed with pocket calculators.

1

u/huuaaang 5d ago

The CPU is mainly for coordinating tasks. It gathers all the data that the GPU needs in RAM. It takes data from the hard drive, the shaders, the objects in a scene, the user input, the network, the physics calculations for how things should move and interact, and organizes it before finally sending what needs to be computed to the GPU. A single frame of a video game, for example. And then it says "Go!" and the GPU renders a frame to your screen. The GPU can't talk to your hard drive. It can't read the keyboard or mouse. It can't talk to your game server over the network. All it knows to do well is render an image for display. And it does it REALLY fast. Other things like running an AI model are similar to rendering graphics so it can do that well too.

1

u/Recycled5000 5d ago

For one thing, programs are too large to fit entirely within the CPU, so programs are stored in memory.

A copy of the program binary lives on the disc and gets loaded into RAM when the operating system is instructed to load and run a program.

One in RAM, the CPU then fetches pieces of the program into itself to begin execution. Again, because programs are too large in their entirety to fit inside a CPU, the code of the program instructs the CPU to store certain (computed) data into RAM and perhaps even to disc for later use.

In the meantime, the CPU executes new parts of the program, discarding older parts it is not using at the moment, knowing that it can recover them from memory as needed. This is the mechanism of caching.

The GPU is just another processor that has more specific parallel capabilities, which work well for certain workloads, such as graphics.