【问题标题】:How to use hbw_malloc library within C++ program?如何在 C++ 程序中使用 hbw_malloc 库?
【发布时间】:2017-03-10 10:23:43
【问题描述】:

我希望能够使用 hbwmalloc 库直接在 MCDRAM 上分配像 Vectors 这样的 C++ 对象。问题是只实现了 C malloc。因此,我考虑编写一个 Vector 的子类,使用 hbw_malloc 实现 resize、reserve 和动态分配。

这将允许程序员选择分配数据的 NUMA 节点。

这是做我想做的最好的主意吗?

【问题讨论】:

标签: c++ memory vector memory-management resize


【解决方案1】:

memkind library(可让您轻松访问 MCDRAM)已经为您提供了 C++ 分配器。请参阅此manual entry 了解更多信息。

用法很简单,如本例所示

#include <hbw_allocator.h>
#include <vector>
#include <assert.h>

int main(int argc, char*argv[])
{
        std::vector<unsigned, hbw::allocator<unsigned> > v;
        v.push_back (123);
        assert(v.size() == 1);
        assert(v[0] == 123);
        return 0;
}

【讨论】:

    【解决方案2】:

    你可以这样做:

    #include <hbwmalloc.h> // hbw_check_available, hbw_malloc, hbw_free
    #include <cstddef>     // std::size_t
    #include <cstdint>     // std::intptr_t
    #include <stdexcept>   // std::logic_error
    #include <new>         // std::bad_alloc
    #include <memory>      // std::addressof
    
    #define do_hbw_check_available
    
    template <class T>
    struct hbw_allocator
    {
        using value_type = T;
        using pointer = T*;
        using const_pointer = const T*;
        using reference = T&;
        using const_reference = const T&;
        using size_type = std::size_t;
        using difference_type = std::intptr_t;
    
        static pointer allocate(size_type n)
        {
    #if defined(do_hbw_check_available)
            if (0 != hbw_check_available())
            {
                throw std::logic_error("HBW not available");
            }
    #endif
    
            // Calculate size of storage with alignment and padding
            const size_type size = sizeof(value_type) * n;
    
            // Allocate storage
            pointer p = hbw_malloc(size);
    
            // Ensure that memory was in fact allocated
            if (nullptr == p)
            {
                throw std::bad_alloc();
            }
    
            return p;
        }
    
        static void deallocate(const_pointer p, size_type /* n */)
        {
            hbw_free(p);
        }
    
        void construct(pointer p, const_reference t)
        {
            // Call copy-constructor of the element pointed to by p
            new ((void*) p) value_type(t);
        }
    
        void destroy(pointer p)
        {
            // Call destructor of the element pointed to by p
            p->~value_type();
        }
    
        size_type max_size () const
        {
            return size_type(-1) / sizeof(value_type);
        }
    
        bool operator!=(const hbw_allocator<value_type>& arg) const
        {
            return !(*this == arg);
        }
    
        // Returns true if and only if storage allocated from *this can be deallocated from other, and vice versa.
        // Always returns true for stateless allocators.
        bool operator==(const hbw_allocator<value_type>& /* arg */) const
        {
            return true;
        }
    
        pointer adress(reference arg)
        {
            return std::addressof(arg);
        }
    
        const_pointer adress(const_reference arg) const
        {
            return std::addressof(arg);
        }
    
        template <class U>
        struct rebind
        {
            using other = hbw_allocator<U>;
        };
    };
    

    示例:

    然后你可以像这样使用它:

    template <class T>
    using hbw_vector = std::vector<T, hbw_allocator<T>>;
    
    hbw_vector<int> vec(5);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多