Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Timocafe
Beginner
123 Views

how aligned class data member,

Jump to solution
Hello All,
I am implemented a unlimited integer class, and I used SIMD to calculate basic operation + *.
At the beginning I developed a single kernel to test my code, and I declared and aligned my data
in the main by the __attribute__((aligned(16))) command, it works. Now I am writing
a basic class, nevertheless the command__attribute__((aligned(16))) does not align (why) my array
and so the code crash when I run it.

Any idea to align a basic array on 16 bytes boundary as data member of a class.

Thanks

Tim


0 Kudos
1 Solution
Matthias_Kretz
New Contributor I
123 Views

Hi,

note that __attribute__((aligned(16))) only works for allocation on the stack. When you allocate a type defined with that attribute on the heap (i.e. new/malloc) then the alignment restriction of the attribute does not apply. Instead you should rather use _mm_malloc with the correct alignment parameter.

It is possible to overload the new and delete operators of the class to achieve the desired effect automatically:

void *operator new(size_t size) { return _mm_malloc(size, alignment); }

void *operator new[](size_t size) { return _mm_malloc(size, alignment); }

void operator delete(void *ptr, size_t) { _mm_free(ptr); }

void operator delete[](void *ptr, size_t) { _mm_free(ptr); }

View solution in original post

9 Replies
Nicolae_P_Intel
Employee
123 Views

could you please re-check your post?your question is somehow missing (or not displayed properly).

Timocafe
Beginner
123 Views
Done, I do not why it does not work the first time !
Timocafe
Beginner
123 Views
I find I make a typo on my kernel, sorry.
Nicolae_P_Intel
Employee
123 Views

can you share what typo have you find?
what compiler are you using and which platform? 64 or 32 bit?
what compiler options?

Matthias_Kretz
New Contributor I
124 Views

Hi,

note that __attribute__((aligned(16))) only works for allocation on the stack. When you allocate a type defined with that attribute on the heap (i.e. new/malloc) then the alignment restriction of the attribute does not apply. Instead you should rather use _mm_malloc with the correct alignment parameter.

It is possible to overload the new and delete operators of the class to achieve the desired effect automatically:

void *operator new(size_t size) { return _mm_malloc(size, alignment); }

void *operator new[](size_t size) { return _mm_malloc(size, alignment); }

void operator delete(void *ptr, size_t) { _mm_free(ptr); }

void operator delete[](void *ptr, size_t) { _mm_free(ptr); }

View solution in original post

Timocafe
Beginner
123 Views
Thank you very much for these informations, I did not know about __attribute__((aligned(16))) runs only in the stack. No problem to overload the operator, great solution !
Thank you
Tim
Gaiger_Chen
New Contributor I
123 Views
By the way, if your platform is windows , use :

_aligned_malloc / _aligned_free



to allocate aligned memory.

Matthias_Kretz
New Contributor I
123 Views
If you can use _mm_malloc, then please don't use windows specific API for the sake of portability. The only real standard way of allocating aligned memory on the heap probably is posix_memalign (sadly Windows doesn't offer POSIX compat per default, though). So using the _mm_ intrinsics probably comes closest to a portable standard...
Gaiger_Chen
New Contributor I
123 Views
Hi

quick solution : use

_mm_loadu_si128 / _mm_storeu_si128

to replase

_mm_load_si128 and _mm_store_si128

it should be pass.

(the __attribute__((aligned(16))) could be remove also.)
Reply