使 std::vector 分配对齐的内存

是否可以使自定义结构的 std::vector 分配对齐的内存以使用 SIMD 指令进行进一步处理?如果可以使用 Allocator,有没有人碰巧有这样一个他可以共享的分配器?

Is it possible to make std::vector of custom structs allocate aligned memory for further processing with SIMD instructions? If it is possible to do with Allocator, does anyone happen to have such an allocator he could share?

推荐答案

从 C++17 开始,只需使用 std::vector<__m256i> 或任何其他对齐类型.operator new 有对齐版本,它被 std::allocator 用于对齐类型(以及普通的 new 表达式,所以new __m256i[N] 从 C++17 开始也是安全的.

Starting in C++17, just use std::vector<__m256i> or with any other aligned type. There's aligned version of operator new, it is used by std::allocator for aligned types (as well as by plain new-expression, so new __m256i[N] is also safe starting in C++17).

@MarcGlisse 发表了这样的评论,这是一个使其更显眼的答案.

There's a comment by @MarcGlisse saying this, making this an answer to make it more visible.

相关文章