Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Using MKL arrays within objects

bstrouse
Beginner
629 Views
I understand that the MKL operates better when arrays of values are 16 byte aligned. So for allocating simple arrays of values, it is best to use the mkl_malloc to ensure alignment. My question concerns array alignment when the array is a member of an encapsulating object in C++.

So let's say I have the following object with a few private members

class LAvector
{
/* Excluding constructors, member functions, etc. */
MKL_INT m_length;
double * m_elements;
};

If this LAvector object is dynamically allocated itself, isn't it true that the byte alignment of the m_elements array could be off (even if mkl_malloc was used to dynamically allocate the internal member m_elements)? Would I have to write an aligned new operator for this LAvector object?

Also if the LAvector object is used as an element for an STL vector, etc, wouldn't I also need a special LAvector object allocator?

Thanks for your time.
0 Kudos
4 Replies
Chao_Y_Intel
Moderator
629 Views

Hi,

Possibly you can have some clarification on the problem? My understanding that the class memory 'm_elements' is used by MKL function call, and it the memory is allocated by mkl_malloc (), the question is if the class still need to be allocated by mkl_malloc(), and make sure the address for the class is also aligned with 16 bytes?

If MKL function only use 'm_elements', it is not required that the class itself is aligned with 16 bytes, the alignment for 'm_elements' address will be fine.

Thanks,
Chao

0 Kudos
bstrouse
Beginner
629 Views
Hi Chao,

Thanks for the response.

Yes, m_elements is the C style array that will be passed to the mkl functions. LAVector is a just a class wrapper around the array of data.

I wasn't sure what happens to the alignment of the m_elements array if the LAVector class is defined dynamically. For example:

vector1 = new LAVector(4);

Since m_elements is a member of LAVector, I wanted to be sure that the allocation of LAVector didn't cause m_elements member to be off on its byte alignment. I also wanted to be sure that m_elements isn't off of alignment if the following happens:

std::vector vectorOfVectors;
LAVector vector2(1);
vectorOfVectors.push_back(vector2);

The reason being here that the LAVector is being allocated by std::vector's default allocator when it's placed into the vector.

Please confirm that the byte alignment of m_elements would be ok in these two situations.

Thanks.
0 Kudos
Sergey_M_Intel2
Employee
629 Views
Hi,

Even if LAVector is dynamically allocated it is still programmer's responsibility to allocate memory for the m_elements (in the constructor). Am I correct?

As Chao suggests you can allocate m_elements using mkl_alloc() and m_elements will be properly aligned. The class object itself can be allocated in any way you prefer.

I hope that helps,
Regards,
Sergey
0 Kudos
bstrouse
Beginner
629 Views
Sergey,

You are correct. It doesn't matter if the LAVector object is statically or dynamically allocated, regardless the m_elements member must have its array elements dynamically allocated during construction.

I think this answers my question.

Thanks guys.
0 Kudos
Reply