向量的数据如何对齐?
如果我想使用 SSE 处理 std::vector
中的数据,我需要 16 字节对齐。我怎样才能做到这一点?我需要编写自己的分配器吗?或者默认分配器是否已与 16 字节边界对齐?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果我想使用 SSE 处理 std::vector
中的数据,我需要 16 字节对齐。我怎样才能做到这一点?我需要编写自己的分配器吗?或者默认分配器是否已与 16 字节边界对齐?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
C++ 标准需要分配函数(
malloc()
和operator new()
)来分配与任何标准类型适当对齐的内存。由于这些函数不接收对齐要求作为参数,因此实际上这意味着所有分配的对齐方式都是相同的,并且是具有最大对齐要求的标准类型,通常是 long double< /code> 和/或long long
(参见 提升 max_align 联合)。向量指令(例如 SSE 和 AVX)比标准 C++ 分配函数具有更强的对齐要求(128 位访问需要 16 字节对齐,256 位访问需要 32 字节对齐)。可以使用 posix_memalign() 或 memalign() 来满足此类具有更强对齐要求的分配。
在 C++17 中,分配函数接受类型为 < 的附加参数a href="https://en.cppreference.com/w/cpp/memory/new/align_val_t" rel="noreferrer">
std::align_val_t
。您可以像这样使用它:
此外,在 C++17 中,标准分配器已更新以尊重类型的对齐,因此您可以简单地执行以下操作:
或者(C++11 中不涉及和支持堆分配):
C++ standard requires allocation functions (
malloc()
andoperator new()
) to allocate memory suitably aligned for any standard type. As these functions don't receive the alignment requirement as an argument, in practice it means that the alignment for all allocations is the same, and is that of a standard type with the largest alignment requirement, which often islong double
and/orlong long
(see boost max_align union).Vector instructions, such as SSE and AVX, have stronger alignment requirements (16-byte aligned for 128-bit access and 32-byte aligned for 256-bit access) than that provided by the standard C++ allocation functions.
posix_memalign()
ormemalign()
can be used to satisfy such allocations with stronger alignment requirements.In C++17 the allocation functions accept an additional argument of type
std::align_val_t
.You can make use of it like:
Moreover, in C++17 the standard allocators have been updated to respect type's alignment, so you can simply do:
Or (no heap allocation involved and supported in C++11):
您应该将自定义分配器与
std::
容器一起使用,例如vector
。不记得谁写了下面的代码,但我使用了一段时间并且它似乎有效(您可能必须将_aligned_malloc
更改为_mm_malloc
,具体取决于编译器/ platform):像这样使用它(如果需要,将 16 更改为另一种对齐方式):
但是,这只能确保
std::vector
使用的内存块是 16 字节对齐的。如果sizeof(T)
不是 16 的倍数,则某些元素将不会对齐。根据您的数据类型,这可能不是问题。如果T
为int
(4字节),则仅加载索引为4倍数的元素。如果为double
(8字节),则仅加载索引为4的倍数的元素。 2 的倍数等。真正的问题是,如果您将类用作
T
,在这种情况下,您必须在类本身中指定对齐要求(同样,根据编译器的不同,这可能会有所不同) ; 该示例适用于 GCC):我们快完成了!如果您使用 Visual C++(至少版本 2010),您将无法将
std::vector
与您指定对齐的类一起使用,因为 <代码>std::vector::调整大小。编译时,如果出现以下错误:
You will have to hack your
stl::vector header
file:vector
header file [C:\Program Files\Microsoft Visual Studio 10.0\VC\include\vector]void resize( _Ty _Val )
方法 [VC2010 上的第 870 行]void resize( const _Ty&_Val)
。You should use a custom allocator with
std::
containers, such asvector
. Can't remember who wrote the following one, but I used it for some time and it seems to work (you might have to change_aligned_malloc
to_mm_malloc
, depending on compiler/platform):Use it like this (change the 16 to another alignment, if needed):
This, however, only makes sure the memory block
std::vector
uses is 16-bytes aligned. Ifsizeof(T)
is not a multiple of 16, some of your elements will not be aligned. Depending on your data-type, this might be a non-issue. IfT
isint
(4 bytes), only load elements whose index is a multiple of 4. If it'sdouble
(8 bytes), only multiples of 2, etc.The real issue is if you use classes as
T
, in which case you will have to specify your alignment requirements in the class itself (again, depending on compiler, this might be different; the example is for GCC):We're almost done! If you use Visual C++ (at least, version 2010), you won't be able to use an
std::vector
with classes whose alignment you specified, because ofstd::vector::resize
.When compiling, if you get the following error:
You will have to hack your
stl::vector header
file:vector
header file [C:\Program Files\Microsoft Visual Studio 10.0\VC\include\vector]void resize( _Ty _Val )
method [line 870 on VC2010]void resize( const _Ty& _Val )
.您可以使用 之前的建议编写自己的分配器.org/doc/libs/1_65_0/boost/align/aligned_allocator.hpp" rel="noreferrer">
boost::alignment::aligned_allocator
对于std::vector
如下:Instead of writing your own allocator, as suggested before, you can use
boost::alignment::aligned_allocator
forstd::vector
like this:编写您自己的分配器。
allocate
和deallocate
是重要的。这是一个例子:Write your own allocator.
allocate
anddeallocate
are the important ones. Here is one example:简短回答:
如果
sizeof(T)*vector.size() > 16
然后是。假设您的向量使用普通分配器
注意:只要
alignof(std::max_align_t) >= 16
,因为这是最大对齐。长答案:
2017 年 8 月 25 日更新新标准 n4659
如果它针对任何大于 16 的值进行对齐,那么它也会针对 16 正确对齐。
6.11 对齐(第 4/5 段)
new 和 new[] 返回对齐的值,以便对象正确对齐其大小:
8.3.4 新(第 17 段)
注意大多数系统都有最大对齐。动态分配的内存不需要对齐到大于此的值。
6.11 对齐(第 2 段)
因此,只要分配的向量内存大于 16 字节,它就会在 16 字节边界上正确对齐。
Short Answer:
If
sizeof(T)*vector.size() > 16
then Yes.Assuming you vector uses normal allocators
Caveat: As long as
alignof(std::max_align_t) >= 16
as this is the max alignment.Long Answer:
Updated 25/Aug/2017 new standard n4659
If it is aligned for anything that is greater than 16 it is also aligned correctly for 16.
6.11 Alignment (Paragraph 4/5)
new and new[] return values that are aligned so that objects are correctly aligned for their size:
8.3.4 New (paragraph 17)
Note most systems have a maximum alignment. Dynamically allocated memory does not need to be aligned to a value greater than this.
6.11 Alignment (paragraph 2)
Thus as long as your vector memory allocated is greater than 16 bytes it will be correctly aligned on 16 byte boundaries.
对一个过时(但重要)问题的当代回答。
正如其他人所说,立即想到编写自己的
Allocator
类[模板]。从 C++11 到 C++17,实现主要限于(按标准)使用alignas
和放置new
。 C++17 提升了 C11 的aligned_alloc,这很方便。此外,C++17 的std::pmr
命名空间(标头
)引入了polymorphic_allocator
类模板和memory_resource
多态分配的抽象接口,很大程度上受到 Boost 的启发。除了允许真正通用的动态代码之外,这些代码已被证明在某些情况下可以提高速度;在这种情况下,您的 SIMD 代码的性能会更好。Contemporary answer to a dated (but important) question.
Writing your own
Allocator
class [template] immediately comes to mind, as said by others. Since C++11 and until C++17, an implementation would be mostly limited (by standard) to usingalignas
and placementnew
. C++17 lifts C11'saligned_alloc
which is convenient. Furthermore, C++17'sstd::pmr
namespace (header<memory_resource>
) introduces thepolymorphic_allocator
class template and thememory_resource
abstract interface for polymorphic allocations, heavily inspired by Boost. Aside from allowing for truly generic, dynamic code, these have been shown to offer speed improvements in some cases; in which case, your SIMD code will perform even better.按照 Intel 向量化教程 “nofollow">http://d3f8ykwhia686p.cloudfront.net/1live/intel/CompilerAutovectorizationGuide.pdf
Use
declspec(align(x,y))
as explained in vectorization tutorial for Intel, http://d3f8ykwhia686p.cloudfront.net/1live/intel/CompilerAutovectorizationGuide.pdf该标准要求
new
和new[]
返回与任何数据类型对齐的数据,其中应包括 SSE。 MSVC 是否真正遵循该规则是另一个问题。The Standard mandates that
new
andnew[]
return data aligned for any data type, which should include SSE. Whether or not MSVC actually follows that rule is another question.