2017-03-10 4 views
2

私は、hbwmallocライブラリを使って、MCDRAM上のVectorsのようなC++オブジェクトを直接割り当てることができます。問題は、C mallocだけが実装されていることです。したがって、私はVectorのサブクラスを、hbw_mallocを使って動的割り当てを使ってresize、reserveを実装してコーディングすることを考えました。C++プログラム内でhbw_mallocライブラリを使用するには?

これにより、プログラマはデータを割り当てるNUMAノードを選択できます。

これは私がしたいことをするための最良のアイデアですか?

+2

が見る、ベクトルのための 'のstd :: allocator'を実装し、この例のように使用量は、非常に簡単ですです:http://en.cppreference.com/w/cpp/memory /アロケータ –

答えて

2

あなたMCDRAMへの容易なアクセスを提供していmemkind library( )はすでに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; 
} 
1

あなたはこのような何かを行うことができます:

#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); 
関連する問題