4 ms·
> So I think that means that it spends 32% of its time accessing RAM, and the other 68% of its time doing calculations Not sure if you can do such conclusion a
by zokier 13y ago
> So I think that means that it spends 32% of its time accessing RAM, and the other 68% of its time doing calculations
Not sure if you can do such conclusion actually, because of pipelining etc. I'd assume that the CPU is doing memory transfers simultaneously while doing the calculations.
I also think that only the first movdqa instruction is accessing RAM, the others are shuffling data from one register to another inside the CPU. I'd venture a guess that the last movdqa is shown taking so much time because of a pipeline stall. That would probably be the first place I'd look for further optimization.
On the other hand, I don't have a clue about assembly programming or low-level optimization, so take my comments with a chunk of salt.
- userbinator 13y agoIt spends the other 68% of its time doing calculations and waiting for the results of the memory accesses. I personally don't like using per-instruction timings like the one presented in the article to measure performance; on a pipelined, superscalar/out-of-order CPU, the fact that one instruction takes a long time to execute doesn't matter so much since others can execute "around it" if they don't depend on it. On the other hand, macrobenchmarks like the timing of the execution of a whole program, are very useful. I timed it, and it took 0.5 seconds!!! [...] So our program now runs twice as fast 0.5s down from 0.6s is not "twice as fast", it is a 16.67% improvement. I'm not sure where the 0.25s was pulled from.
- zokier 13y ago> It spends the other 68% of its time doing calculations and waiting for the results of the memory accesses. It is bit odd though, the code is processing data at about 4GB/s, and modern system should have lot more RAM bandwidth (eg about 10GB/s for DDR3-1333). It feels like there should be still significant room for optimization. > 0.5s down from 0.6s is not "twice as fast", it is a 16.67% improvement. I'm not sure where the 0.25s was pulled from. I think the 0.5 was typo (missing '2') and should be instead 0.25
- sanxiyn 13y agoYes, there is a room for optimization. It seems to me bytesum_intrinsics.c doesn't saturate RAM bandwidth because loops are not unrolled. You should unroll the loop in addition to SIMD so that SIMD loads complete while SIMD adds are done, hiding memory latency. Otherwise you wait on loads.
- zokier 13y agoI tested the program with -funroll-loops (which did unroll the sum_array loop) with no measurable difference in performance. I guess there are no easy shortcuts available anymore, you'd need to delve into the assembly to push for more performance.
- userbinator 13y agoso that SIMD loads complete while SIMD adds are done That's almost certainly already being done by the hardware. Loop unrolling has been counterproductive ever since ~Sandy Bridge or so, and probably hasn't been that great of an idea since the post-P4 days: http://www.agner.org/optimize/blog/read.php?i=142#142 http://www.agner.org/optimize/blog/read.php?i=142#142 It is so important to economize the use of the micro-op cache that I would give the advice never to unroll loops.
- sanxiyn 13y agoThis is a great advice, but I think it doesn't apply here. If code normally fits in uop cache but unrolled code doesn't, you are punished. But here both normal code and unrolled code fit uop cache. Loop unrolling may be counterproductive, but loop unrolling for vectorization almost certainly isn't, even in the current CPUs. It really can't be done by the hardware, and all compilers unroll loops for vectorization, even if you disable loop unrolling in general.
- nkurz 13y agoYou are correct that both fit in uop cache, but I think 'userbinator' is correct here that loop unrolling is of very limited benefit here (and almost everywhere else). Because of speculative execution, processors are basically blind to loops. They will keep executing ahead along the most likely path even if this means they are simultaneously executing different iterations of a loop. The loads will pre-execute just fine across the loop boundary. While there are cases where reducing the amount of loop logic will speed things up, it really can be done in hardware!
- kingosticks 13y agoUnless 0.5 is a typo and it should have actually read 0.25. Then the last line makes more sense too. Except of course 20/0.25 = 80x and not 20x as claimed in step 4... But never mind, it's a fun post.