| 28 | |
| 29 | template<class InputIterator, class OutputIterator, class UnaryFunction, class Predicate> |
| 30 | inline OutputIterator transform_if_impl(InputIterator first, |
| 31 | InputIterator last, |
| 32 | OutputIterator result, |
| 33 | UnaryFunction function, |
| 34 | Predicate predicate, |
| 35 | bool copyIndex, |
| 36 | command_queue &queue) |
| 37 | { |
| 38 | typedef typename std::iterator_traits<OutputIterator>::difference_type difference_type; |
| 39 | |
| 40 | size_t count = detail::iterator_range_size(first, last); |
| 41 | if(count == 0){ |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | const context &context = queue.get_context(); |
| 46 | |
| 47 | // storage for destination indices |
| 48 | ::boost::compute::vector<cl_uint> indices(count, context); |
| 49 | |
| 50 | // write counts |
| 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); |
| 55 | |
| 56 | // count number of elements to be copied |
| 57 | size_t copied_element_count = |
| 58 | ::boost::compute::count(indices.begin(), indices.end(), 1, queue); |
| 59 | |
| 60 | // scan indices |
| 61 | ::boost::compute::exclusive_scan( |
| 62 | indices.begin(), indices.end(), indices.begin(), queue |
| 63 | ); |
| 64 | |
| 65 | // copy values |
| 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)]] << "="; |
| 69 | |
| 70 | if(copyIndex){ |
| 71 | k2 << k2.get_global_id(0) << ";\n"; |
| 72 | } |
| 73 | else { |
| 74 | k2 << function(first[k2.get_global_id(0)]) << ";\n"; |
| 75 | } |
| 76 | |
| 77 | k2.exec_1d(queue, 0, count); |
| 78 | |
| 79 | return result + static_cast<difference_type>(copied_element_count); |
| 80 | } |
| 81 | |
| 82 | template<class InputIterator, class UnaryFunction, class Predicate> |
| 83 | inline discard_iterator transform_if_impl(InputIterator first, |
no test coverage detected