local_buffer.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  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_MEMORY_LOCAL_BUFFER_HPP
  11. #define BOOST_COMPUTE_MEMORY_LOCAL_BUFFER_HPP
  12. #include <boost/compute/cl.hpp>
  13. #include <boost/compute/kernel.hpp>
  14. namespace boost {
  15. namespace compute {
  16. /// \class local_buffer
  17. /// \brief Represents a local memory buffer on the device.
  18. ///
  19. /// The local_buffer class represents a block of local memory on a compute
  20. /// device.
  21. ///
  22. /// This class is most commonly used to set local memory arguments for compute
  23. /// kernels:
  24. /// \code
  25. /// // set argument to a local buffer with storage for 32 float's
  26. /// kernel.set_arg(0, local_buffer<float>(32));
  27. /// \endcode
  28. ///
  29. /// \see buffer, kernel
  30. template<class T>
  31. class local_buffer
  32. {
  33. public:
  34. /// Creates a local buffer object for \p size elements.
  35. local_buffer(const size_t size)
  36. : m_size(size)
  37. {
  38. }
  39. /// Creates a local buffer object as a copy of \p other.
  40. local_buffer(const local_buffer &other)
  41. : m_size(other.m_size)
  42. {
  43. }
  44. /// Copies \p other to \c *this.
  45. local_buffer& operator=(const local_buffer &other)
  46. {
  47. if(this != &other){
  48. m_size = other.m_size;
  49. }
  50. return *this;
  51. }
  52. /// Destroys the local memory object.
  53. ~local_buffer()
  54. {
  55. }
  56. /// Returns the number of elements in the local buffer.
  57. size_t size() const
  58. {
  59. return m_size;
  60. }
  61. private:
  62. size_t m_size;
  63. };
  64. namespace detail {
  65. // set_kernel_arg specialization for local_buffer<T>
  66. template<class T>
  67. struct set_kernel_arg<local_buffer<T> >
  68. {
  69. void operator()(kernel &kernel_, size_t index, const local_buffer<T> &buffer)
  70. {
  71. kernel_.set_arg(index, buffer.size() * sizeof(T), 0);
  72. }
  73. };
  74. } // end detail namespace
  75. } // end compute namespace
  76. } // end boost namespace
  77. #endif // BOOST_COMPUTE_MEMORY_SVM_PTR_HPP