内存 32 位对齐约束对 AVX 意味着什么?

问题描述

_mm256_load_ps 的文档指出内存必须是 32 位对齐的,以便将值加载到寄存器中。

所以我发现 post 解释了地址是如何 32 位对齐的。

#include <immintrin.h>
#include <vector>

int main() {
    std::vector<float> A(height * width,0);
    std::cout << "&A = " << A.data() << std::endl; // 0x55e960270eb0
    __m256 a_row = _mm256_load_ps(A.data());
    return 0; // Exit Code 139 SIGSEGV 
}

所以尝试了该代码。 我预计它会起作用。 我查了地址
0x55e960270eb0 % 4 = 0 并且浮点数为 4 个字节。
我完全被这个原因弄糊涂了。 如果我使用带有 malloc 的原始数组,突然一切正常

#include <immintrin.h>
#include <vector>

int main() {
    std::vector<float> A(height * width,0);
    std::cout << "&A = " << A.data() << std::endl; // &A = 0x55e960270eb0


    float* m = static_cast<float*>(_mm_malloc(A.size() * sizeof(float),32));
    std::cout << "m* = " << m << std::endl; // m* = 0x562bbe989700

    __m256 a_row = _mm256_load_ps(m);

    delete m;

    return 0; // Returns 0
}

我错过了什么/误解了什么?

解决方法

你看错了 this - 它说 32 BYTE 对齐,而不是 BIT。

所以你必须做 32 字节对齐而不是 4 字节对齐。

要对齐任何堆栈变量,您可以使用 alignas(32) T var;,其中 T 可以是任何类型,例如 std::array<float,8>

要对齐 std::vector 的内存或任何其他基于堆的结构 alignas(...) 是不够的,您必须编写特殊的对齐分配器(使用示例参见 Test() 函数):

Try it online!

#include <cstdlib>
#include <memory>

// Following includes for tests only
#include <vector>
#include <iostream>
#include <cmath>

template <typename T,std::size_t N>
class AlignmentAllocator {
  public:
    typedef T value_type;
    typedef std::size_t size_type;
    typedef std::ptrdiff_t difference_type;
    typedef T * pointer;
    typedef const T * const_pointer;
    typedef T & reference;
    typedef const T & const_reference;

  public:
    inline AlignmentAllocator() throw() {}
    template <typename T2> inline AlignmentAllocator(const AlignmentAllocator<T2,N> &) throw() {}
    inline ~AlignmentAllocator() throw() {}
    inline pointer adress(reference r) { return &r; }
    inline const_pointer adress(const_reference r) const { return &r; }
    inline pointer allocate(size_type n);
    inline void deallocate(pointer p,size_type);
    inline void construct(pointer p,const value_type & wert);
    inline void destroy(pointer p) { p->~value_type(); }
    inline size_type max_size() const throw() { return size_type(-1) / sizeof(value_type); }
    template <typename T2> struct rebind { typedef AlignmentAllocator<T2,N> other; };
    bool operator!=(const AlignmentAllocator<T,N> & other) const { return !(*this == other); }
    bool operator==(const AlignmentAllocator<T,N> & other) const { return true; }
};

template <typename T,std::size_t N>
inline typename AlignmentAllocator<T,N>::pointer AlignmentAllocator<T,N>::allocate(size_type n) {
    #if _MSC_VER
        return (pointer)_aligned_malloc(n * sizeof(value_type),N);
    #else
        void * p0 = nullptr;
        int r = posix_memalign(&p0,N,n * sizeof(value_type));
        if (r != 0) return 0;
        return (pointer)p0;
    #endif
}
template <typename T,std::size_t N>
inline void AlignmentAllocator<T,N>::deallocate(pointer p,size_type) {
    #if _MSC_VER
        _aligned_free(p);
    #else
        std::free(p);
    #endif
}
template <typename T,N>::construct(pointer p,const value_type & wert) {
    new (p) value_type(wert);
}

template <typename T,size_t N = 64>
using AlignedVector = std::vector<T,AlignmentAllocator<T,N>>;

template <size_t Align>
void Test() {
    AlignedVector<float,Align> v(1);
    size_t uptr = size_t(v.data()),alignment = 0;
    while (!(uptr & 1)) {
        ++alignment;
        uptr >>= 1;
    }
    std::cout << "Requested: " << Align << ",Actual: " << (1 << alignment) << std::endl;
}

int main() {
    Test<8>();
    Test<16>();
    Test<32>();
    Test<64>();
    Test<128>();
    Test<256>();
}

输出:

Requested: 8,Actual: 16
Requested: 16,Actual: 16
Requested: 32,Actual: 32
Requested: 64,Actual: 128
Requested: 128,Actual: 8192
Requested: 256,Actual: 256

您可能在上面的代码中看到,我将 posix_memalign() 用于 CLang/GCC,将 _aligned_malloc() 用于 MSVC。从 C++17 开始也存在 std::aligned_alloc() 但似乎并非所有编译器都实现了它,至少 MSVC 没有。看起来在 CLang/GCC 上,您可以使用此 std::aligned_alloc() 而不是 posix_memalign() 作为 @Mgetz 的 commented

此外,作为英特尔指南 says here,您可以使用 _mm_malloc()_mm_free() 代替 posix_memalign()/_aligned_malloc()/_aligned_free()/{{1} }/std::aligned_alloc()