[Insight-developers] Empty FixedArray destructor: Performance hit using gcc (times 2) : __attribute__ ((aligned (8))) & derivation
Luis Ibanez
luis.ibanez at kitware.com
Fri Jun 6 10:19:20 EDT 2008
Hi Tom,
Derivation combined with __attribute__ ((aligned(8))) seems to work.
With the following code:
class MyArray
{
public:
MyArray() {};
~MyArray() {};
double operator[](unsigned int k)
{
return foo[k];
}
double foo[2];
};
class MyAlignedArray : public MyArray
{
} __attribute__ ((aligned(8)));
An array of MyArray"s is aligned to 4 bytes,
while an array of "MyAlignedArray"s is aligned to 8 bytes.
Therefore, one option for your application is to do
class MyPixelType : public itk::FixedArray<double,2>
{
} __attribute__ ((aligned(8)));
typedef itk::Image< MyPixelType, N > MyImageType;
and...
move from there.
-----
We still have not found an explanation of why the
presence of an empty non-virtual the destructor
should affect the alignment... :-/
Luis
----------------------
Luis Ibanez wrote:
> Hi Tom,
>
> More on this, Bradley Lowekamp kindly pointed us to the following
> GCC mechanism for specifying the alignment of structures:
>
> http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Type-Attributes.html
>
> Thank Brad !
>
>
>
> ---
>
> The Attribute:
>
>
> __attribute__ ((aligned (8)))
>
>
> also does the trick.
>
>
> When we added it to the end of the minimalistic array:
>
> class MyArray
> {
> public:
> MyArray() {};
> ~MyArray() {};
> double operator[](unsigned int k)
> {
> return foo[k];
> }
> double foo[2];
> } __attribute__ ((aligned (8))) ;
>
>
>
> we can compile without -malign-double and the structure
> is still aligned to 8 bytes, despites the fact that the
> destructor is sill present.
>
>
> We could create ITK macros for this attribute options,
> and define the macros at configuration time by using
> TRY_COMPILES.
>
>
> The remaining question is:
>
>
> Are there any drawbacks to this approach ?
>
>
> At first sight, it is much better than the global
> -malign-double option, and we can apply it only
> to structures that we know must be aligned.
>
>
> One challenge here is that although we want FixedArray<double,N>
> to be 8-bytes aligned, we don't always want the FixedArray<T,N>
> to be aligned this way. For example: FixedArray<char,3> ??
>
>
> One option could be to create your pixel type as a class derivied
> from FixedArray<double,2>, and see if we can apply the attribute
> just to the derived class....
>
> In this way, this will be an application specific issue, as opposed
> to something that has to be done pervasively in ITK.
>
>
> Any suggestions ?
>
>
> Luis
>
>
>
More information about the Insight-developers
mailing list