1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_TRANSFORM_IF_HPP
12 #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_IF_HPP
14 #include <boost/compute/cl.hpp>
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/command_queue.hpp>
17 #include <boost/compute/algorithm/count.hpp>
18 #include <boost/compute/algorithm/count_if.hpp>
19 #include <boost/compute/algorithm/exclusive_scan.hpp>
20 #include <boost/compute/container/vector.hpp>
21 #include <boost/compute/detail/meta_kernel.hpp>
22 #include <boost/compute/detail/iterator_range_size.hpp>
23 #include <boost/compute/iterator/discard_iterator.hpp>
29 template<class InputIterator, class OutputIterator, class UnaryFunction, class Predicate>
30 inline OutputIterator transform_if_impl(InputIterator first,
32 OutputIterator result,
33 UnaryFunction function,
38 typedef typename std::iterator_traits<OutputIterator>::difference_type difference_type;
40 size_t count = detail::iterator_range_size(first, last);
45 const context &context = queue.get_context();
47 // storage for destination indices
48 ::boost::compute::vector<cl_uint> indices(count, context);
51 ::boost::compute::detail::meta_kernel k1("transform_if_write_counts");
52 k1 << indices.begin()[k1.get_global_id(0)] << " = "
53 << predicate(first[k1.get_global_id(0)]) << " ? 1 : 0;\n";
54 k1.exec_1d(queue, 0, count);
56 // count number of elements to be copied
57 size_t copied_element_count =
58 ::boost::compute::count(indices.begin(), indices.end(), 1, queue);
61 ::boost::compute::exclusive_scan(
62 indices.begin(), indices.end(), indices.begin(), queue
66 ::boost::compute::detail::meta_kernel k2("transform_if_do_copy");
67 k2 << "if(" << predicate(first[k2.get_global_id(0)]) << ")" <<
68 " " << result[indices.begin()[k2.get_global_id(0)]] << "=";
71 k2 << k2.get_global_id(0) << ";\n";
74 k2 << function(first[k2.get_global_id(0)]) << ";\n";
77 k2.exec_1d(queue, 0, count);
79 return result + static_cast<difference_type>(copied_element_count);
82 template<class InputIterator, class UnaryFunction, class Predicate>
83 inline discard_iterator transform_if_impl(InputIterator first,
85 discard_iterator result,
86 UnaryFunction function,
94 return result + count_if(first, last, predicate, queue);
97 } // end detail namespace
99 /// Copies each element in the range [\p first, \p last) for which
100 /// \p predicate returns \c true to the range beginning at \p result.
101 template<class InputIterator, class OutputIterator, class UnaryFunction, class Predicate>
102 inline OutputIterator transform_if(InputIterator first,
104 OutputIterator result,
105 UnaryFunction function,
107 command_queue &queue = system::default_queue())
109 return detail::transform_if_impl(
110 first, last, result, function, predicate, false, queue
114 } // end compute namespace
115 } // end boost namespace
117 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_IF_HPP