Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the SSE instructions outperform normal instructions

Tags:

c

x86-64

sse

Where does the x86-64's SSE instructions (vector instructions) outperform the normal instructions. Because what I'm seeing is that the frequent loads and stores that are required for executing SSE instructions is nullifying any gain we have due to vector calculation. So could someone give me an example SSE code where it performs better than the normal code.

Its maybe because I am passing each parameter separately, like this...

__m128i a = _mm_set_epi32(pa[0], pa[1], pa[2], pa[3]);
__m128i b = _mm_set_epi32(pb[0], pb[1], pb[2], pb[3]);
__m128i res = _mm_add_epi32(a, b);

for( i = 0; i < 4; i++ )
 po[i] = res.m128i_i32[i];

Isn't there a way I can pass all the 4 integers at one go, I mean pass the whole 128 bytes of pa at one go? And assign res.m128i_i32 to po at one go?

like image 622
pythonic Avatar asked Apr 25 '12 10:04

pythonic


1 Answers

Summarizing comments into an answer:

You have basically fallen into the same trap that catches most first-timers. Basically there are two problems in your example:

  1. You are misusing _mm_set_epi32().
  2. You have a very low computation/load-store ratio. (1 to 3 in your example)

_mm_set_epi32() is a very expensive intrinsic. Although it's convenient to use, it doesn't compile to a single instruction. Some compilers (such as VS2010) can generate very poor performing code when using _mm_set_epi32().

Instead, since you are loading contiguous blocks of memory, you should use _mm_load_si128(). That requires that the pointer is aligned to 16 bytes. If you can't guarantee this alignment, you can use _mm_loadu_si128() - but with a performance penalty. Ideally, you should properly align your data so that don't need to resort to using _mm_loadu_si128().


The be truly efficient with SSE, you'll also want to maximize your computation/load-store ratio. A target that I shoot for is 3 - 4 arithmetic instructions per memory-access. This is a fairly high ratio. Typically you have to refactor the code or redesign the algorithm to increase it. Combining passes over the data is a common approach.

Loop unrolling is often necessary to maximize performance when you have large loop bodies with long dependency chains.


Some examples of SO questions that successfully use SSE to achieve speedup.

  • C code loop performance (non-vectorized)
  • C code loop performance [continued] (vectorized)
  • How do I achieve the theoretical maximum of 4 FLOPs per cycle? (contrived example for achieving peak processor performance)
like image 165
Mysticial Avatar answered Nov 03 '22 01:11

Mysticial