pipe.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_PIPE_HPP
  11. #define BOOST_COMPUTE_PIPE_HPP
  12. #include <boost/compute/cl.hpp>
  13. #include <boost/compute/context.hpp>
  14. #include <boost/compute/memory_object.hpp>
  15. #include <boost/compute/exception/opencl_error.hpp>
  16. #include <boost/compute/detail/get_object_info.hpp>
  17. // pipe objects require opencl 2.0
  18. #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  19. namespace boost {
  20. namespace compute {
  21. /// \class pipe
  22. /// \brief A FIFO data pipe
  23. ///
  24. /// \opencl_version_warning{2,0}
  25. ///
  26. /// \see memory_object
  27. class pipe : public memory_object
  28. {
  29. public:
  30. /// Creates a null pipe object.
  31. pipe()
  32. : memory_object()
  33. {
  34. }
  35. /// Creates a pipe object for \p mem. If \p retain is \c true, the
  36. /// reference count for \p mem will be incremented.
  37. explicit pipe(cl_mem mem, bool retain = true)
  38. : memory_object(mem, retain)
  39. {
  40. }
  41. /// Creates a new pipe in \p context.
  42. pipe(const context &context,
  43. uint_ pipe_packet_size,
  44. uint_ pipe_max_packets,
  45. cl_mem_flags flags = read_write,
  46. const cl_pipe_properties *properties = 0)
  47. {
  48. cl_int error = 0;
  49. m_mem = clCreatePipe(context,
  50. flags,
  51. pipe_packet_size,
  52. pipe_max_packets,
  53. properties,
  54. &error);
  55. if(!m_mem){
  56. BOOST_THROW_EXCEPTION(opencl_error(error));
  57. }
  58. }
  59. /// Creates a new pipe object as a copy of \p other.
  60. pipe(const pipe &other)
  61. : memory_object(other)
  62. {
  63. }
  64. /// Copies the pipe object from \p other to \c *this.
  65. pipe& operator=(const pipe &other)
  66. {
  67. if(this != &other){
  68. memory_object::operator=(other);
  69. }
  70. return *this;
  71. }
  72. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  73. /// Move-constructs a new pipe object from \p other.
  74. pipe(pipe&& other) BOOST_NOEXCEPT
  75. : memory_object(std::move(other))
  76. {
  77. }
  78. /// Move-assigns the pipe from \p other to \c *this.
  79. pipe& operator=(pipe&& other) BOOST_NOEXCEPT
  80. {
  81. memory_object::operator=(std::move(other));
  82. return *this;
  83. }
  84. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  85. /// Destroys the pipe object.
  86. ~pipe()
  87. {
  88. }
  89. /// Returns the packet size.
  90. uint_ packet_size() const
  91. {
  92. return get_info<uint_>(CL_PIPE_PACKET_SIZE);
  93. }
  94. /// Returns the max number of packets.
  95. uint_ max_packets() const
  96. {
  97. return get_info<uint_>(CL_PIPE_MAX_PACKETS);
  98. }
  99. /// Returns information about the pipe.
  100. ///
  101. /// \see_opencl2_ref{clGetPipeInfo}
  102. template<class T>
  103. T get_info(cl_pipe_info info) const
  104. {
  105. return detail::get_object_info<T>(clGetPipeInfo, m_mem, info);
  106. }
  107. /// \overload
  108. template<int Enum>
  109. typename detail::get_object_info_type<pipe, Enum>::type get_info() const;
  110. };
  111. /// \internal_ define get_info() specializations for pipe
  112. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(pipe,
  113. ((cl_uint, CL_PIPE_PACKET_SIZE))
  114. ((cl_uint, CL_PIPE_MAX_PACKETS))
  115. )
  116. namespace detail {
  117. // set_kernel_arg specialization for pipe
  118. template<>
  119. struct set_kernel_arg<pipe>
  120. {
  121. void operator()(kernel &kernel_, size_t index, const pipe &pipe_)
  122. {
  123. kernel_.set_arg(index, pipe_.get());
  124. }
  125. };
  126. } // end detail namespace
  127. } // end compute namespace
  128. } // end boost namespace
  129. #endif // BOOST_COMPUTE_CL_VERSION_2_0
  130. #endif // BOOST_COMPUTE_PIPE_HPP