buffer_allocator.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <[email protected]>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP
  11. #define BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP
  12. #include <boost/compute/buffer.hpp>
  13. #include <boost/compute/config.hpp>
  14. #include <boost/compute/context.hpp>
  15. #include <boost/compute/detail/device_ptr.hpp>
  16. namespace boost {
  17. namespace compute {
  18. /// \class buffer_allocator
  19. /// \brief The buffer_allocator class allocates memory with \ref buffer objects
  20. ///
  21. /// \see buffer
  22. template<class T>
  23. class buffer_allocator
  24. {
  25. public:
  26. typedef T value_type;
  27. typedef detail::device_ptr<T> pointer;
  28. typedef const detail::device_ptr<T> const_pointer;
  29. typedef std::size_t size_type;
  30. typedef std::ptrdiff_t difference_type;
  31. explicit buffer_allocator(const context &context)
  32. : m_context(context),
  33. m_mem_flags(buffer::read_write)
  34. {
  35. }
  36. buffer_allocator(const buffer_allocator<T> &other)
  37. : m_context(other.m_context),
  38. m_mem_flags(other.m_mem_flags)
  39. {
  40. }
  41. buffer_allocator<T>& operator=(const buffer_allocator<T> &other)
  42. {
  43. if(this != &other){
  44. m_context = other.m_context;
  45. m_mem_flags = other.m_mem_flags;
  46. }
  47. return *this;
  48. }
  49. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  50. buffer_allocator(buffer_allocator<T>&& other) BOOST_NOEXCEPT
  51. : m_context(std::move(other.m_context)),
  52. m_mem_flags(other.m_mem_flags)
  53. {
  54. }
  55. buffer_allocator<T>& operator=(buffer_allocator<T>&& other) BOOST_NOEXCEPT
  56. {
  57. m_context = std::move(other.m_context);
  58. m_mem_flags = other.m_mem_flags;
  59. return *this;
  60. }
  61. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  62. ~buffer_allocator()
  63. {
  64. }
  65. pointer allocate(size_type n)
  66. {
  67. buffer buf(m_context, n * sizeof(T), m_mem_flags);
  68. clRetainMemObject(buf.get());
  69. return detail::device_ptr<T>(buf);
  70. }
  71. void deallocate(pointer p, size_type n)
  72. {
  73. BOOST_ASSERT(p.get_buffer().get_context() == m_context);
  74. (void) n;
  75. clReleaseMemObject(p.get_buffer().get());
  76. }
  77. size_type max_size() const
  78. {
  79. return m_context.get_device().max_memory_alloc_size() / sizeof(T);
  80. }
  81. context get_context() const
  82. {
  83. return m_context;
  84. }
  85. protected:
  86. void set_mem_flags(cl_mem_flags flags)
  87. {
  88. m_mem_flags = flags;
  89. }
  90. private:
  91. context m_context;
  92. cl_mem_flags m_mem_flags;
  93. };
  94. } // end compute namespace
  95. } // end boost namespace
  96. #endif // BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP