generate.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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_ALGORITHM_GENERATE_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_GENERATE_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/algorithm/copy.hpp>
  16. #include <boost/compute/iterator/function_input_iterator.hpp>
  17. #include <boost/compute/detail/iterator_range_size.hpp>
  18. #include <boost/compute/type_traits/is_device_iterator.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// Stores the result of \p generator for each element in the range
  22. /// [\p first, \p last).
  23. ///
  24. /// Space complexity: \Omega(1)
  25. template<class OutputIterator, class Generator>
  26. inline void generate(OutputIterator first,
  27. OutputIterator last,
  28. Generator generator,
  29. command_queue &queue = system::default_queue())
  30. {
  31. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  32. size_t count = detail::iterator_range_size(first, last);
  33. if(count == 0){
  34. return;
  35. }
  36. ::boost::compute::copy(
  37. ::boost::compute::make_function_input_iterator(generator,
  38. first.get_index()),
  39. ::boost::compute::make_function_input_iterator(generator,
  40. last.get_index()),
  41. first,
  42. queue
  43. );
  44. }
  45. } // end compute namespace
  46. } // end boost namespace
  47. #endif // BOOST_COMPUTE_ALGORITHM_GENERATE_HPP