Making std::vector allocate aligned memory(使 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.
这篇关于使 std::vector 分配对齐的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使 std::vector 分配对齐的内存
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
