r/C_Programming • • 27d ago

Question Performant code. How

how to make perfomant code, by that i mean, some cpu's have multiple ALU's/ excecution units, and some computers have multiple cores, how to have my code running on all those ALU's at all times? and those multiple cores,

i know about threading for the second part [running code on multiple cores]

now how do i make it run on multiple ALU' an example being

in code to not wait for a variable ie x = 12 + 12 then z = x + 2, i can imagine that code doesnt use all the ALU's because z is waiting for x's operation to be done. so what are ways to make my code most optimal and use all ALU's at the same time

0 Upvotes

39 comments sorted by

View all comments

7

u/mjmvideos 27d ago

It’s not clear that you understand what ALUs are. Read this: https://en.wikipedia.org/wiki/Arithmetic_logic_unit?wprov=sfti1

And then decide whether you really want (or if it’s even possible) to keep an ALU busy all the time.

1

u/FlippingGerman 27d ago

Amusingly I once tested a CPU to see how hot it got, using some standard CPU benchmark (might have been Cinebench). I then also ran prime95 (Mersenne Prime searcher), and that got quite a bit higher temps, despite both being ostensibly 100% and without thermally throttling. I assume the difference is in just which bits of the CPU are doing how much; Cinebench might be more memory bound than prime95.

1

u/BrainThinkerMan 27d ago

so then why are some programs slow and other programs fast?

4

u/Old_Tax4792 27d ago edited 27d ago

Some programs are faster than others for many different reasons... Nowadays the major reasons are related to memory management : cpu cache thrashing, garbage collectors, etc

1

u/BrainThinkerMan 26d ago

wow i diddnt know memory management could play such a big role in it, more memory = slower? or leaky memory

1

u/RealisticDuck1957 26d ago

If a memory fetch isn't in cache it can be hundreds of clock cycles for the memory to be loaded. Vs. about 3 clock cycles for a fetch from L1 cache. When that happens an entire cache line, far more than a single register's worth of storage, is loaded into cache. Which makes the next fetch from that cache line fast. An algorithm that focuses on a small amount of memory, rather than random access all over memory, is more likely to hit an already loaded cache line.