Is std :: make_unique <T []> to return aligned memory?

  • Is memory a unique pointer to array_ptr :

     auto array_ptr = std::make_unique<double[]>(size); 

    coincides with the border sizeof(double) alignof(double) (i.e. it is required that std be correctly aligned)?

  • Is the first element of an array the first element of a cache line?

  • Otherwise: what is the correct way to achieve this in C ++ 14?

Motivation (update): I plan to use SIMD instructions for the array, and since the cache lines are the base block of memory for each individual architecture that I know, I would rather allocate the memory correctly so that the first array element is at the beginning of the cache line. Note that SIMD instructions work as long as the elements are correctly aligned (regardless of the position of the elements between cache lines). However, I do not know if this has any influence at all, but I can guess that yes, yes. In addition, I want to use these SIMD instructions in my raw memory inside the kernel. This is a detail of kernel optimization, so I do not want to highlight, for example. __int128 instead of int.

+6
source share
1 answer
  • All the objects you get are “normally” aligned appropriately, that is, aligned in alignof(T) (which do not have to be the same as sizeof(T) , including dynamic arrays. (As a rule, the distributor ::operator new will just return the most aligned address so you don’t have to worry about how memory is used.)

  • In C ++ there are no cache lines. This is a platform-specific issue you need to deal with (but alignas may help).

  • Try alignas plus static checking if it works (since support for custom types depends on the platform), otherwise just add manual padding. You don’t care if your data is at the beginning of the cache line, only if there are no two data elements in the same cache line.

It should be emphasized that alignment is not really a concept that you can test directly in C ++, since pointers are not numbers. They are converted to numbers, but conversion usually does not make sense other than being reversible. You need something like std::align to actually say “I have aligned the memory” or just use alignas for your types directly.

+7
source

Source: https://habr.com/ru/post/969505/


All Articles