Intel® ISA Extensions
Use hardware-based isolation and memory encryption to provide more code protection in your solutions.

how aligned class data member,

Timocafe
Beginner
967 Views
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
967 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

0 Kudos
9 Replies
Nicolae_P_Intel
Employee
967 Views

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

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

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

0 Kudos
Matthias_Kretz
New Contributor I
968 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); }

0 Kudos
Timocafe
Beginner
968 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
0 Kudos
Gaiger_Chen
New Contributor I
968 Views
By the way, if your platform is windows , use :

_aligned_malloc / _aligned_free



to allocate aligned memory.

0 Kudos
Matthias_Kretz
New Contributor I
968 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...
0 Kudos
Gaiger_Chen
New Contributor I
968 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.)
0 Kudos
Reply