2012-10-17 78 views
38

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

+1

的文档是否检查过标准分配器是否已经为您做了这些工作? – TemplateRex

+2

@rhalbersma:我不这么认为,它不需要对齐参数。 –

+0

没有我的意思是:你的STL实现是否已经为你调整了内存?你是否计算过'v.begin()'的内存地址并检查它是否以X字节的倍数开始?即使你不能明确配置对齐方式,std :: allocator可能已经帮你完成了。 – TemplateRex

回答

26

编辑:我按照GManNickG的建议去除了std::allocator的继承,并将alignment参数设置为编译时间的东西。

我最近写了这段代码。它没有像我想要的那样测试,所以继续并报告错误。 :-)

enum class Alignment : size_t 
{ 
    Normal = sizeof(void*), 
    SSE = 16, 
    AVX = 32, 
}; 


namespace detail { 
    void* allocate_aligned_memory(size_t align, size_t size); 
    void deallocate_aligned_memory(void* ptr) noexcept; 
} 


template <typename T, Alignment Align = Alignment::AVX> 
class AlignedAllocator; 


template <Alignment Align> 
class AlignedAllocator<void, Align> 
{ 
public: 
    typedef void*    pointer; 
    typedef const void*  const_pointer; 
    typedef void    value_type; 

    template <class U> struct rebind { typedef AlignedAllocator<U, Align> other; }; 
}; 


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

    typedef std::true_type propagate_on_container_move_assignment; 

    template <class U> 
    struct rebind { typedef AlignedAllocator<U, Align> other; }; 

public: 
    AlignedAllocator() noexcept 
    {} 

    template <class U> 
    AlignedAllocator(const AlignedAllocator<U, Align>&) noexcept 
    {} 

    size_type 
    max_size() const noexcept 
    { return (size_type(~0) - size_type(Align))/sizeof(T); } 

    pointer 
    address(reference x) const noexcept 
    { return std::addressof(x); } 

    const_pointer 
    address(const_reference x) const noexcept 
    { return std::addressof(x); } 

    pointer 
    allocate(size_type n, typename AlignedAllocator<void, Align>::const_pointer = 0) 
    { 
     const size_type alignment = static_cast<size_type>(Align); 
     void* ptr = detail::allocate_aligned_memory(alignment , n * sizeof(T)); 
     if (ptr == nullptr) { 
      throw std::bad_alloc(); 
     } 

     return reinterpret_cast<pointer>(ptr); 
    } 

    void 
    deallocate(pointer p, size_type) noexcept 
    { return detail::deallocate_aligned_memory(p); } 

    template <class U, class ...Args> 
    void 
    construct(U* p, Args&&... args) 
    { ::new(reinterpret_cast<void*>(p)) U(std::forward<Args>(args)...); } 

    void 
    destroy(pointer p) 
    { p->~T(); } 
}; 


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

    typedef std::true_type propagate_on_container_move_assignment; 

    template <class U> 
    struct rebind { typedef AlignedAllocator<U, Align> other; }; 

public: 
    AlignedAllocator() noexcept 
    {} 

    template <class U> 
    AlignedAllocator(const AlignedAllocator<U, Align>&) noexcept 
    {} 

    size_type 
    max_size() const noexcept 
    { return (size_type(~0) - size_type(Align))/sizeof(T); } 

    const_pointer 
    address(const_reference x) const noexcept 
    { return std::addressof(x); } 

    pointer 
    allocate(size_type n, typename AlignedAllocator<void, Align>::const_pointer = 0) 
    { 
     const size_type alignment = static_cast<size_type>(Align); 
     void* ptr = detail::allocate_aligned_memory(alignment , n * sizeof(T)); 
     if (ptr == nullptr) { 
      throw std::bad_alloc(); 
     } 

     return reinterpret_cast<pointer>(ptr); 
    } 

    void 
    deallocate(pointer p, size_type) noexcept 
    { return detail::deallocate_aligned_memory(p); } 

    template <class U, class ...Args> 
    void 
    construct(U* p, Args&&... args) 
    { ::new(reinterpret_cast<void*>(p)) U(std::forward<Args>(args)...); } 

    void 
    destroy(pointer p) 
    { p->~T(); } 
}; 

template <typename T, Alignment TAlign, typename U, Alignment UAlign> 
inline 
bool 
operator== (const AlignedAllocator<T,TAlign>&, const AlignedAllocator<U, UAlign>&) noexcept 
{ return TAlign == UAlign; } 

template <typename T, Alignment TAlign, typename U, Alignment UAlign> 
inline 
bool 
operator!= (const AlignedAllocator<T,TAlign>&, const AlignedAllocator<U, UAlign>&) noexcept 
{ return TAlign != UAlign; } 

实际分配呼叫的实施只有posix,但您可以轻松扩展。

void* 
detail::allocate_aligned_memory(size_t align, size_t size) 
{ 
    assert(align >= sizeof(void*)); 
    assert(nail::is_power_of_two(align)); 

    if (size == 0) { 
     return nullptr; 
    } 

    void* ptr = nullptr; 
    int rc = posix_memalign(&ptr, align, size); 

    if (rc != 0) { 
     return nullptr; 
    } 

    return ptr; 
} 


void 
detail::deallocate_aligned_memory(void *ptr) noexcept 
{ 
    return free(ptr); 
} 

需要C++ 11,顺便说一句。

+0

我不认为你需要或应该从'std :: exception <>''std :: allocator <>'继承。 – GManNickG

+0

@GManNickG,也许你的意思是'allocator'? :) – avakar

+0

@avakar:哇,甚至没有注意到我写的! – GManNickG

3

是的,应该是可以的。如果你把谷歌这个问题,那么你将获得大量的示例代码,下面是一些令人鼓舞的结果:

https://bitbucket.org/marten/alignedallocator/wiki/Home

http://code.google.com/p/mastermind-strategy/source/browse/trunk/src/util/aligned_allocator.hpp?r=167

https://gist.github.com/1471329

+1

尽管此链接可能回答问题,但最好在此处包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [发表评论](/ review/low-quality-posts/18787539) –

15

在即将到来的版本1.56,Boost库将包括Boost.Align。在其他内存对齐帮助程序中,它提供boost::alignment::aligned_allocator,它可以用于std::allocator的直接替换,并允许您指定对齐方式。请参阅https://boostorg.github.io/align/

+0

很高兴知道,但是我个人觉得'boost'很难融入我的项目(那些不是标题的库)。 –

+3

我同意,整合提升可能有点痛苦。然而,'Boost.Align' _is_只有头,也只依赖于其他仅头的库AFAICS。 – tklauser

+2

现已推出:http://www.boost.org/doc/libs/1_56_0/libs/core/doc/html/index.html – fireboot