compact.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@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_ALGORITHM_DETAIL_COMPACT_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_COMPACT_HPP
  12. #include <iterator>
  13. #include <boost/compute/container/vector.hpp>
  14. #include <boost/compute/detail/iterator_range_size.hpp>
  15. #include <boost/compute/detail/meta_kernel.hpp>
  16. #include <boost/compute/system.hpp>
  17. namespace boost {
  18. namespace compute {
  19. namespace detail {
  20. ///
  21. /// \brief Compact kernel class
  22. ///
  23. /// Subclass of meta_kernel to compact the result of set kernels to
  24. /// get actual sets
  25. ///
  26. class compact_kernel : public meta_kernel
  27. {
  28. public:
  29. unsigned int tile_size;
  30. compact_kernel() : meta_kernel("compact")
  31. {
  32. tile_size = 4;
  33. }
  34. template<class InputIterator1, class InputIterator2, class OutputIterator>
  35. void set_range(InputIterator1 start,
  36. InputIterator2 counts_begin,
  37. InputIterator2 counts_end,
  38. OutputIterator result)
  39. {
  40. m_count = iterator_range_size(counts_begin, counts_end) - 1;
  41. *this <<
  42. "uint i = get_global_id(0);\n" <<
  43. "uint count = i*" << tile_size << ";\n" <<
  44. "for(uint j = " << counts_begin[expr<uint_>("i")] << "; j<" <<
  45. counts_begin[expr<uint_>("i+1")] << "; j++, count++)\n" <<
  46. "{\n" <<
  47. result[expr<uint_>("j")] << " = " << start[expr<uint_>("count")]
  48. << ";\n" <<
  49. "}\n";
  50. }
  51. event exec(command_queue &queue)
  52. {
  53. if(m_count == 0) {
  54. return event();
  55. }
  56. return exec_1d(queue, 0, m_count);
  57. }
  58. private:
  59. size_t m_count;
  60. };
  61. } //end detail namespace
  62. } //end compute namespace
  63. } //end boost namespace
  64. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_COMPACT_HPP