简体   繁体   English

是std :: make_unique <T[]> 需要返回对齐的内存?

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

  • Is the memory owned by the unique pointer array_ptr : 内存是否由唯一指针array_ptr拥有:

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

    aligned to a sizeof(double) alignof(double) boundary (ie is it required by the std to be correctly aligned)? 对齐 sizeof(double) alignof(double)边界(即std要求正确对齐)?

  • Is the first element of the array the first element of a cache line? 数组的第一个元素是缓存行的第一个元素吗?

  • Otherwise: what is the correct way of achieving this in C++14? 否则:在C ++ 14中实现这一目的的正确方法是什么?

Motivation (update): I plan to use SIMD instructions on the array and since cache lines are the basic unit of memory on every single architecture that I know of I'd rather just allocate memory correctly such that the first element of the array is at the beginning of a cache line. 动机(更新):我打算在阵列上使用SIMD指令,因为高速缓存行是每个架构上的基本内存单元,我知道我只是正确地分配内存,这样阵列的第一个元素就在缓存行的开头。 Note that SIMD instructions work as long as the elements are correctly aligned (independently of the position of the elements between cache lines). 请注意,只要元素正确对齐(与高速缓存行之间元素的位置无关),SIMD指令就可以工作。 However, I don't know if that has an influence at all but I can guess that yes, it does. 但是,我不知道这是否会产生影响,但我可以猜测是的,确实如此。 Furthermore, I want to use these SIMD instructions on my raw memory inside a kernel. 此外,我想在内核中的原始内存上使用这些SIMD指令。 It is an optimization detail of a kernel so I don't want to allocate eg __int128 instead of int. 它是内核的优化细节,所以我不想分配例如__int128而不是int。

  • All objects that you obtain "normally" are suitably aligned, ie aligned at alignof(T) (which need not be the same as sizeof(T) . That includes dynamic arrays. (Typically, the allocator ::operator new will just return a maximally aligned address so as not to have to worry about how the memory is used.) 您“正常”获得的所有对象都是适当对齐的,即在alignof(T)alignof(T)不需要与sizeof(T)相同。 sizeof(T)包括动态数组。(通常,allocator ::operator new将返回一个最大对齐地址,以便不必担心如何使用内存。)

  • There are no cache lines in C++. C ++中没有缓存行。 This is a platform specific issue that you need to deal with yourself (but alignas may help). 这是一个特定于平台的问题,您需要自己处理(但是alignas可能有所帮助)。

  • Try alignas plus a static check if it works (since support for over-aligned types is platform dependent), otherwise just add manual padding. 尝试使用alignas加上静态检查是否有效(因为支持过度对齐类型取决于平台),否则只需添加手动填充。 You don't really care whether your data is at the beginning of a cache line, only that no two data elements are on the same cache line. 您并不关心数据是否位于缓存行的开头 ,只是没有两个数据元素位于同一缓存行中。

It is worth stressing that alignment isn't actually a concept you can check directly in C++, since pointers are not numbers. 值得强调的是,对齐实际上并不是一个可以直接在C ++中检查的概念,因为指针不是数字。 They are convertible to numbers, but the conversion is not generally meaningful other than being reversible. 它们可以转换为数字,但转换除了可逆之外通常没有意义。 You need something like std::align to actually say "I have aligned memory", or just use alignas on your types directly. 你需要像std::align这样的东西来实际说“我已经对齐了内存”,或者直接在你的类型上使用alignas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM