Ten helpful tips when building SPE applications in C
Added 31 Jul 2008
1. Choose the right SPU intrinsic for data conversion.
Vectors can be cast between vectors of other types, as well as back-and-forth between the vector types and the special quad type, but none of these casts perform any data conversion. If you need to convert between types, use an appropriate SPU intrinsic.
2. Align your pointers correctly.
Vector and non-vector pointers can be cast between each other, but when converting from a scalar pointer to a vector pointer it is the programmer's responsibility to be sure that the pointer is quadword-aligned.
3. Align your declared vectors correctly.
Declared vectors are always quadword-aligned when allocated.
Remember that DMA transfers of 16 bytes or more must be in 16-byte multiples and aligned to 16-byte boundaries on both the SPE and the PPE. Transfers smaller than that must be a power of two and be naturally aligned. Optimal transfers are multiples of 128 bytes that are on 128-byte boundaries.
5. Allocate aligned pointers correctly.
If you are not sure about the alignment of data on the PPE, use memalign or posix_memalign to allocate an aligned pointer from the heap; use memcpy or an equivalent to move the data to the aligned area.
6. Watch for missing prototype messages.
Always compile with -Wall; pay special attention to missing prototype messages. Incorrectly implied prototypes (especially between 32- and 64-bit types) can lead to bizarre error conditions.
7. Store effective addresses correctly.
Always store effective addresses as unsigned long longs
on both the PPE and the SPE. This way they can be treated in a unified
fashion on the SPE and on the PPE whether the PPE code is compiled for
32-bit or 64-bit execution.
Avoid
integer multiplies (especially 32-bit multiplies) on the SPE. It takes
five instructions to perform the multiply. If you must multiply, cast
to an unsigned short before multiplying.
9. Declare scalar values as vectors and pointers.
In scalar code on the SPE, declaring scalar values as vectors and vector pointers (even if you aren't using them as vectors) can speed up code because it doesn't have to do unaligned loads and stores.
10. Watch for floats and doubles.
Be aware that on the SPE, floats and doubles are implemented differently; they round differently as well. floats in particular deviate from the C99 standard. (More on this in the article "Smart buffer management with DMA transfers.")