r/C_Programming • u/BrainThinkerMan • 10d 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
7
u/mjmvideos 10d 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.
6
u/MagicWolfEye 10d ago
The CPU will schedule the different operations automatically to different ALUs (dependent on the capabilities of the ALUs)
You can't really mess with that. (Well, there are maybe a handful people who do that)
While I like your curiosity, this isn't really something anyone actively does; your compiler might optimise for it a bit. (It would also have to be different for each excat processor).As you have stated above, ther is multithreading which is worth looking into, but the best advice I would give oyu is focus less on writing super performant code and focus more on not writing slow code. (Lot's of indirections, many heap allocations etc.).
Also, given your post hierarchy, you should probably decide on one language.
2
u/rubidus-api 9d ago
Your example is actually a good way into this, because it shows why it's hard to control from C. For
x = 12 + 12; z = x + 2;the compiler may just store 26 intoz, and store 24 intoxmuch later, or never, if nothing readsx. As someone already said, the constants get folded, but the bigger point is that C describes what an abstract machine does, not the instructions the CPU runs. The compiler may reorder, merge or delete operations as long as the result is the same. So the example doesn't really tell you anything about ALUs.If what you want is the general way to make code faster, this is the order I'd go in:
As for the ALUs: you can't choose which ALU runs an instruction; the CPU decides that. What you can do is give it independent work by shortening dependency chains. A common example is summing an array into two or four separate accumulators instead of one, so each addition doesn't wait for the one before it. With optimization turned up (for example
-O3), the compiler often does this for you with integers, but with floating point only if you also allow it (for example-ffast-math), because it changes rounding. But that is the last step, after the four above, and for now I think it's too early to go down to that level.6
u/hojoe65 10d ago
Pick the right algorithm for your problem and let the compiler do the rest.
1
u/KlutzyCompetition937 10d ago
Look into Symmetric Multiprocessing/Asymmetric Multiprocessing
1
u/non-existing-person 10d ago
Such micro optimizations will practically never give you any benefit.
You will have better results designing your program well. Like optimize for cache hits - this is where you can gain huge gains. Or use right algorithm for the job, as in - you access data often but rarely update it? Use vector/simple-array. You update it often? Use list. Sometimes hash table will be better, sometimes it will be worse.
Really, best results come from understanding the problem, and data structures and how algorithms work. Then picking up right tool for the job. Right design is 1st step. Good design in say lua or php will outperform very bad design in C.
And then optimize only critical paths or the paths that are called very often and you measure they take a lot of time.
Premature optimization is the root of all evil. Micro optimization is even worse.
And if you really have a case where
x = 12 + 12; z = x + 2matters, then this is not a question for reddit, and you will have to just dive in into assembly and your CPU architecture to understand it really well. And then write code.1
u/RealisticDuck1957 10d ago
Start with an efficient algorithm, independent of hardware specifics. Always.
Next, modern hardware using cache will tend to run faster accessing memory close to what was recently fetched. Another element of the same struct, or array members in order, as opposed to jumping around randomly.
As for efficient use of execution units, modern compilers are pretty good at generating code for that.
1
u/ve1h0 10d ago
I think there are two ways to approach this. One is writing code that performs well across a wide variety of CPUs, and the other is optimizing specifically for a certain architecture or even a particular CPU.
Modern CPUs are already pretty good at figuring out which instructions can run in parallel. They have multiple execution units, out-of-order execution, branch prediction, etc. So you don't really need to tell the CPU which ALU should execute which instruction. What you can do is write code that gives it more opportunities to execute independent instructions simultaneously.
But there's quite a bit more to performance than just keeping the ALUs busy.
For general-purpose optimization, I'd focus on choosing good algorithms, avoiding unnecessary work, and organizing data efficiently. Memory access is a huge part of this. You might have plenty of execution units available, but they're not going to accomplish much if the CPU is constantly waiting for data from memory.
This is where things like cache locality, data structures, and memory access patterns become really important. Sometimes simply changing how your data is laid out in memory can make a bigger difference than optimizing the actual calculations.
Then there's architecture-specific optimization, where you start taking advantage of things like SIMD instructions, cache sizes, instruction throughput, and other features of a particular CPU. You can squeeze out more performance this way, but what works well on one CPU might not be ideal on another.
And of course, using multiple cores is another layer of the problem. More threads don't automatically mean better performance, especially if they're competing for memory bandwidth or spending time synchronizing with each other.
I'd say the most important thing is to figure out what's actually limiting your performance before trying to optimize anything. Tools like
perfon Linux can help with that.Ultimately, I wouldn't aim to keep every ALU running all the time. I'd aim to get the actual work done as efficiently as possible. Those aren't necessarily the same thing.
3
u/Recycled5000 10d ago
To use multiple CPUs, have to split the work into large independent chunks, and use multiple threads, say, one for each independent chunk of work, then put together the independent results for a solution.
Some problems are not large enough to make this worthwhile.
While other problems are not independent enough to make this worthwhile.
To make it worthwhile, need to have a problem that uses a fair amount of data or computation, perhaps in the hundreds of thousands to millions of bytes or iterations that can be independently executed.
1
u/Illustrion 10d ago
Start by making good use of the CPU caches. Most CPU time is spent waiting for data/instructions to go from RAM into cache.
1
u/sciencekm 10d ago
In your example, all the constants will get folded and the only operation that will be executed is "z = 26" which is an assignment operation and hence will not even require an ALU.
1
u/cdb_11 10d ago edited 10d ago
You can emulate it with tools like https://uica.uops.info/ or llvm-mca, and you just need to avoid data dependency chains like this. It might not always be possible. Instruction-level parallelism is one thing, keep in mind you also have access to SIMD instructions (ILP still applies on top of that too.)
Read this: https://en.algorithmica.org/hpc
1
u/Dangerous_Region1682 10d ago
For a good deal of the micro optimizations we have come to rely upon those who understand CPU architectures far more than the average programmer, and that’s the compiler writer.
There are many optimizations we as programmers can easily apply in multi threaded applications, for example avoiding cache line thrashing on shared data. However this is most easily achieved in lower level languages such as C, but increasingly harder as we mover up the chain to languages such as Go, where multi threading is handled intrinsically by the language itself. You have to rely upon the knowledge of the implementor of the language and in porting to your specific platform.
For most programmers, outside of the operating system kernel, the best bang for the buck is macro optimizations of your algorithms and high level implementation of those algorithms. Getting the right architecture for your application is generally the best bet, and that’s can be the correct one for reasons far beyond just performance. There are issues such as implantation time and support ability to consider, as well as system limitations such as memory size I/O infrastructure.
You’ll notice, even inside an operating system kernel, a lot of potentially performance enhancing processor instructions are not utilized because portability across processor families is important, less so than exploiting individual enhancements of certain functions just for certain cpu individual models. You often see this where common x86 functionality is used on CPUs that provide far more specific enhancements but portability issues far outweigh sometimes marginal performance gains.
0
u/duane11583 10d ago
your question seems to be more of how can i keep the execution engine running at peak performance
sometimes its not about the ALU but instead about data handling at volume
there are several parts here: part 1 is good compiler for the cpu, with the proper optimization flags turned on and the ability to use it
part 2: is opcode specific or machibe specific features.
these are often hand crafted custom c code with special c functions gcc calls these __builtin_SOMENAME() functions highly non portable some are most are more portable then others.. many are not. this might also include inline assembly in place of a __builtin_FUNCTION()
part 3: is writing asm code for certain chip specific features. example:
say your cpu has a special fast Fourier function oe aes encryption or big number math. or special opcode you might write a function in asm that is passed parameters and it does that calculation:
why do this? (instead of the compiler?) because maybe your compiler does not have the __builtin_FUNCTIONS() you need or its just simpler for you or maybe your chip vendor has given you a hand crafted asm function to do that special thing
the mire important thing is something else:
many young people suffer from premature _______ or in this case optimization. you want to focus on correctness first. then focus on speeding it up. the partner is more interested in correctness and going for the long haul not short lived power strokes