Live data from Hacker News

Why does this code execute more slowly after strength-reducing multiplications?

stackoverflow.com

151–155 of 155 posts

Re: Why does this code execute more slowly after strength-reducing multiplications?

#151
post #76

Vectorisation is not free, There is one other dimension to optimise for: power. The suggested "slower" optimisation does fundamentally use less instructions. Chucking more hardware at parallelisable problems makes it run faster but does not necessarily reduce the power requirements much because there are fundamentally the same number of instructions, it's just gobbling up the same power over a shorter period of time…

Unless we're talking extremes (AVX512...), optimizing for power in modern CPUs is almost always optimizing the race to idle.

I.e. the CPU doesn't use much less power if most ALUs are idle, so you might as well use all of them to compute redundant results -> and then turn ~everything off sooner.

Re: Why does this code execute more slowly after strength-reducing multiplications?

#152
post #22

In the post, multiplications and 2 additions are not faster than 2 additions. The post compares (1) loop code that can be vectorized, as loop rounds are independent and do not depend on the result from the previous round, and (2) an "optimization" that makes calculations shorter, but also makes each loop round depend on the result of the previous round, so this cannot be vectorized.

To be precise, they're both "vectorized" in the sense that both versions are using SSE vector instructions (and in fact, Clang will even generate AVX instructions for the first version if you use -mavx2). The difference is really the data dependency which has a massive effect on the ability of the CPU to pipeline the operation. For the first version w/ AVX I get: $ perf stat ./a.out [-] Took: 225634 ns. Performance c…

> To be precise, they're both "vectorized" in the sense that both versions are using SSE vector instructions

No, just because it's using SSE doesn't mean it's vectorized. SSE has both scalar and vector instructions.

Re: Why does this code execute more slowly after strength-reducing multiplications?

#153
post #110

Earlier quoted context omitted.

Agreed. But can we stop using "premature optimization is the root of all evil". It has jumped the shark. I have more grief in my life because people adhere to this tenet. This is why we end up with the software equivalent of concrete airplanes. Ok it's your turn now. Make it fly!

Did you measure ? Because if you didn't measure you aren't optimising, you are just wanking. And of course one reason we say premature is that most likely until the project is mostly finished you can't really measure because you don't have anything to measure.

Obviously measuring is the only way to optimize. Goodbye.

Re: Why does this code execute more slowly after strength-reducing multiplications?

#155

Earlier quoted context omitted.

That is the 'why' autovectorization fails. But it seems solvable in this case pretty easily actually. EDIT: Solvable by a human. I dunno much about compilers though, so I dunno if there's some kind of language issue that would prevent the unrolling of this dependency.

I think the point GP is making is that even without vectorization, the data dependency causes stalls even in normal, single data instructions. That is, a data dependency between iterations of loops will hurt performance even for non-vectorizable calculations (or on CPUs with high ILP but no really good vector instructions, which granted is probably a small pool since both those things came about at about the same tim…

I wonder if it becomes a math problem to optimize then, like Euler solving the sum of 1-100 by adding 1 to 100 and multiplying by the 50 pairs of numbers that operation created to get 5050?
Post reply on HN