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

What does memory 32bit Alignement constraint mean for 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
}

我是什么missing/misinterpreting?

你看错了 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, std::size_t N>
inline void AlignmentAllocator<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

您可能会在上面的代码中看到我使用了@Mgetz 的posix_memalign() for CLang/GCC and _aligned_malloc() for MSVC. Starting from C++17 there also exists std::aligned_alloc() but seems that not all compilers implemented it, at least MSVC didn't. Looks like on CLang/GCC you can use this std::aligned_alloc() instead of posix_memalign() as

此外,作为 Intel 指南 says here,您可以使用 _mm_malloc()_mm_free() 而不是 posix_memalign()/_aligned_malloc()/_aligned_free()/std::aligned_alloc()/std::free().