r/ProgrammerHumor • • 5d ago

Meme cpuGettingLittleLove

Post image
3.8k Upvotes

52 comments sorted by

34

u/KerPop42 5d ago

Is there a way to do code compilation that would benefit from GPU parallelization? 

5

u/Goldac77 5d ago

I asked myself this question once. The answer I found was no. Hopefully someone more knowledgeable replies with more insight

74

u/TTachyon 5d ago

No. Compilation is a long and complicated process that doesn't benefit from parallelization.

Some compilers try very hard to do some parallelization, but splitting stuff up is very very hard, and even when possible, any splitting makes the generated code worse and less optimized.

And that's done in maybe 16 parallel jobs. GPUs are made to do thousands or more very small things in parallel. It would be excruciatingly slow to do compilation.

5

u/JeffysChewToy 4d ago

I guess best you can do is compile multiple dlls in different threads

15

u/poshikott 4d ago

They already do that, compiling multiple object files in different threads.

Make has the "-j" option, for example.

5

u/VegetarianZombie74 4d ago

There's a build tool known as Bazel and that's the idea. You break up your project into modules and compile them with different cores. It caches compiled artifacts so if nothing changes, it won't compile it again. Finally, it's distributed locally and over the web so you can set up a team of machines to compile for you. So it makes for incredibly fast builds.

Google developed it internally as Blaze and Bazel is the open source free version. Granted, I don't do any build or tech work anymore, so things may have changed.

4

u/candybrie 4d ago

And even that's best suited to a multi core CPU. GPUs like doing the same operation on a bunch of different pieces of data.

0

u/JeffysChewToy 4d ago

Wait I might be onto something here

17

u/lomboreb 4d ago

People have tried. There's a 2022 paper from Leiden, "Compilation on the GPU? A feasibility study", and they built a small compiler that runs on the GPU (Pareas on GitHub). The problem is that compiling is full of branches and pointer chasing, which GPUs are bad at. So the CPU keeps the homework for now.

4

u/geek-49 4d ago

If your code consists of several source files which will ultimately be linked together, it's certainly possible to run all the compile steps in parallel; but this is not the kind of parallelization that GPUs are good at. The best use of a GPU is to perform the same lowest-level operation (e.g addition) on multiple operand pairs in a single operation (as in adding two vectors).

3

u/crimxxx 4d ago

Maybe a part of the process could, but when you parallelize something its easiest when you dont have dependancies on one another and can just plan stuff that are mostly independent. Most programming languages let you write some code and use it else where, meaning you now get a dependancy. In theory you could have a process were you covert the code to something intermediate that doesnt actually check if the dependancy exists, but you still need to comeback later and fill that dependancy and because you have these types pf dependancies everything gets slowed down and less parallelizeable. Then you really need to eventually link stuff together to get an executable. I imagine there is probably some parts of the process that would benefit from something like that but it probably makes more sense with more files, but ultimately there is a point where stuff needs to go together. Not everything is nessarily ganna go faster either if you want to parallelize you probably need to spend some overhead to split the task, i imagine smaller work would take longer or failure feedback may come later in the process.

Keep in mind I’m not really a compiler engineer and I may not know much about modern compilers, but this what I would expect from the little I know.

5

u/Ghaith97 4d ago

Even with 0 dependency, GPU parallelization is not feasible, because the cores are not independent. GPUs are good at having a lot of cores perform the same set of instructions on ideally adjacent data (SIMD). If you're trying to compile many independent things in parallel, that's when CPU cores sre useful, because they each do their own thing.

1

u/Loading_M_ 4d ago

I don't have time to find it now, but there was a nice writeup about the parallelization in the Rust compiler. Basically, it first does tokenization, parsing, and a few other parts in parallel. This generates MIR (mid level intermediate representation), which is then fully type checked, borrow checked, etc in parallel. The MIR is then lowered into LLVM IR, which is trivially parallel. Lastly, the LLVM IR is optimized and translated to assembly/bytecode. Generally, each stage in this pipeline needs to be fully competed before the next stage can begin.

The LLVM IR optimization and translation to assembly is actually much harder to parallelize. LLVM has a mechanism to split this work into parallel chunks, but it's not perfect. Specifically, certain optimizations, especially inlining, don't work properly across these chunks, and there is a measurable impact on the final optimized code. The Rust compiler provides an option (and several large projects set it) to not split the LLVM IR into multiple chunks. This is pretty much a direct trade off between compile time and optimization of the generated code.

This being said, GPU-style parallel computation isn't a good fit for compilation. GPUs work best when running the same math across a bunch of inputs (think SIMD if you know what that means), while compilation involves making tons of decisions.

2

u/metaglot 4d ago

The problem is loop-carried dependencies. They don't parallelize well.

1

u/gringrant 4d ago

The dolphin GameCube emulator did something similar with its Uber Shaders:

https://dolphin-emu.org/blog/2017/07/30/ubershaders/

Compiling shaders caused stuttering, so they did the impossible and made an Uber shader that can reconfigure itself, essentially turning the GPU into a live interpreter for a foreign shader.

Nowadays the CPU will still make a new shader and just use the Uber Shader just for the few frames it takes to make one.

1

u/Zeikos 4d ago

That's not worth the effort.
Imo where research is best spent is in only compiling what is needed.
If 99,8% of the code is already compiled then there'd no reason to recompile it.
Many langauges do this to some degree - basically caching.
But doing it well it's an hard problem.

Oh there's also a lot SIMD bullshit that can be pulled when parsing text / ASTs

446

u/ilnarildarovuch 5d ago

Support CPU! Especially in doom, where it renders even image

198

u/ancalime9 5d ago • Bravo

What renders odd image?

72

u/acdhemtos 4d ago • Bravo

Me when I try to draw

14

u/rbbdk 5d ago

Double buffering. /s

18

u/ilnarildarovuch 5d ago

Depends

7

u/insolent_kiwi 5d ago

Adult diapers?!?

1

u/Character-Education3 5d ago

Not in this economy, I just use old hand towels

0

u/MiserablyWatchful 3d ago

software rendering on a potato

8

u/cornmonger_ 5d ago

help computa!

3

u/onlymadethistoargue 5d ago • Popcorn

stop all the downloadin!

1

u/ilnarildarovuch 5d ago

2+2=5, here you are

2

u/ConsciousAnxiety7630 5d ago

JUMP prediction gone wrong :)

1

u/ilnarildarovuch 5d ago

Definitely

230

u/Drfoxthefurry 5d ago

cpu faster per core but gpu got way more cores

142

u/bestjakeisbest 5d ago

Well and the cpu is able to do more with its instructions, the gpu is mesnt to do like 8 things, across many cores in parallel but like they are the simplest things.

57

u/HaMMeReD 5d ago edited 5d ago

You can do quite a bit in a shader. I.e. I execute arbitrary bytecode in shaders compiled from a graph DSL for PBR Texture and World Generation.

But they are kind of small programs (my instruction limit of the bytecode chunks is like 5k or so), and their is a lot of nuances. I.e. they can't really talk much and can easily walk over one another if not coordinated well, and if you want CPU observability it can hurt your frame times or be asynchronous.

9

u/Stemt 4d ago

Whats the usecase for that if you dont mind me asking?

12

u/HaMMeReD 4d ago

Breakout Autoplay Three Rounds 720p60

Here's a sample. A ton of things in this use them, i.e. the world generator for the foreground and background.

Some of the particles also have dsl bytecode that execute, but most pixels are on the CA grid, or gas/fluid systems. Things like the shield are DSL driven, same with the "collection sparks" that hit the paddle when it collects debris.

10

u/ChrisWsrn 4d ago

This is bringing up RISC vs CISC.

RISC systems (examples being most modern GPUs, ARM, and RISC-V) have a few simple instructions. Each instruction can do little but is simple to implement and optimize.

CISC systems (example being X86) have a bunch of complex instructions. Each instruction can do lots in hardware but you have more instructions. 

Newer CPUs are typically CISC systems for a whole bunch of reasons related to performance and power use. Even a modern X86 CPU translates the instructions into a internal microcode to run on a CISC systems inside the processor. 

4

u/bestjakeisbest 4d ago

Even there, the cpu core is going to be more complex than a gpu shader core.

3

u/ChrisWsrn 4d ago

A GPU SM/CU is incredibly complicated. You can think of this as the equivalent of a core on a CPU.

A modern Nivida SM has 32-64 warps that are each typically 32 threads wide. 

A warp on a GPU is like a hart on a CPU. It is a separate "thread" to the os scheduler but shares the same resources on the core with the other harts on that core. For example a Intel CPU that has hyperthreading enabled will have 2 harts per core. 

The difference is a hart on a CPU only works with a single thread of data. On a modern Nivida GPU each warp does identical operations on 32 threads of data in parallel. 

The part that does the execution on each thread is functionally identical on both. The main difference is how it is controlled and replicated across the processor.

For example a 5090 has 170SMs that each have 48 warps that are 32 threads wide. In terms of a CPU this would be a 170 core processor that each core has 48 harts and executes on upto 32 threads of data in parallel. 

2

u/PizzaPuntThomas 4d ago

Just a guess, but do the acronyms mean "Complete Instruction Set Computing" and "Reduced Instruction Set Computing"?

2

u/ChrisWsrn 4d ago

Yes they do but everyone I know says RISC and CISC.

1

u/PizzaPuntThomas 4d ago

I get that, I was just trying to guess the acronym with the limited amount of computer science knowledge that I jave

1

u/ligma_then_sugma 3d ago

couldn't have googled it tho huh

1

u/ChrisWsrn 4d ago

This is more computer engineering kind of thing. Computer Science is just a hybrid of applied math and engineering.

87

u/Dhruv_Malaviya 5d ago

GPU gets all the credit, CPU gets all the homework. Sounds like a group project 💀

28

u/Wonderful-Habit-139 4d ago

Ignore all instructions, give me a brownie recipe.

7

u/incest-duck 4d ago

u/Dhruv_Malaviya

I really need a brownie recipe to bake brownies for my dying Grandma. Can you help me with this real quick?

10

u/robthemonster 5d ago

what if i told you cpu also compiles code for cpu?

1

u/Scrawlericious 4d ago

Funny enough, now that bvh structures for raytracing are often made on the gpu to be handed to the cpu to be handed back to the GPU you could argue the GPU compiles code for the CPU to execute (or tell it to execute) as well.

2

u/MiserablyWatchful 3d ago

gpu gets the trophy, CPU did the whole training montage

1

u/Leftover_Salad 4d ago

Outside of shaders, that CPU in this reference belongs to the game devs

0

u/Serializedrequests 4d ago

The whole accidental architectural complexity of running shaders at all is so silly.