print_range.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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_DETAIL_PRINT_RANGE_HPP
  11. #define BOOST_COMPUTE_DETAIL_PRINT_RANGE_HPP
  12. #include <vector>
  13. #include <iostream>
  14. #include <iterator>
  15. #include <boost/compute/algorithm/copy.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/detail/is_buffer_iterator.hpp>
  18. #include <boost/compute/detail/iterator_range_size.hpp>
  19. namespace boost {
  20. namespace compute {
  21. namespace detail {
  22. template<class InputIterator>
  23. inline void print_range(InputIterator first,
  24. InputIterator last,
  25. command_queue &queue,
  26. typename boost::enable_if<
  27. is_buffer_iterator<InputIterator>
  28. >::type* = 0)
  29. {
  30. typedef typename
  31. std::iterator_traits<InputIterator>::value_type
  32. value_type;
  33. const size_t size = iterator_range_size(first, last);
  34. // copy values to temporary vector on the host
  35. std::vector<value_type> tmp(size);
  36. ::boost::compute::copy(first, last, tmp.begin(), queue);
  37. // print values
  38. std::cout << "[ ";
  39. for(size_t i = 0; i < size; i++){
  40. std::cout << tmp[i];
  41. if(i != size - 1){
  42. std::cout << ", ";
  43. }
  44. }
  45. std::cout << " ]" << std::endl;
  46. }
  47. template<class InputIterator>
  48. inline void print_range(InputIterator first,
  49. InputIterator last,
  50. command_queue &queue,
  51. typename boost::enable_if_c<
  52. !is_buffer_iterator<InputIterator>::value
  53. >::type* = 0)
  54. {
  55. typedef typename
  56. std::iterator_traits<InputIterator>::value_type
  57. value_type;
  58. const context &context = queue.get_context();
  59. const size_t size = iterator_range_size(first, last);
  60. // copy values to temporary vector on the device
  61. ::boost::compute::vector<value_type> tmp(size, context);
  62. ::boost::compute::copy(first, last, tmp.begin(), queue);
  63. print_range(tmp.begin(), tmp.end(), queue);
  64. }
  65. } // end detail namespace
  66. } // end compute namespace
  67. } // end boost namespace
  68. #endif // BOOST_COMPUTE_DETAIL_PRINT_RANGE_HPP