]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
11 | #ifndef BOOST_COMPUTE_ALGORITHM_REPLACE_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_REPLACE_HPP | |
13 | ||
14 | #include <boost/compute/system.hpp> | |
15 | #include <boost/compute/command_queue.hpp> | |
16 | #include <boost/compute/detail/meta_kernel.hpp> | |
17 | #include <boost/compute/detail/iterator_range_size.hpp> | |
18 | ||
19 | namespace boost { | |
20 | namespace compute { | |
21 | namespace detail { | |
22 | ||
23 | template<class Iterator, class T> | |
24 | class replace_kernel : public meta_kernel | |
25 | { | |
26 | public: | |
27 | replace_kernel() | |
28 | : meta_kernel("replace") | |
29 | { | |
30 | m_count = 0; | |
31 | } | |
32 | ||
33 | void set_range(Iterator first, Iterator last) | |
34 | { | |
35 | m_count = detail::iterator_range_size(first, last); | |
36 | ||
37 | *this << | |
38 | "const uint i = get_global_id(0);\n" << | |
39 | "if(" << first[var<cl_uint>("i")] << " == " << var<T>("old_value") << ")\n" << | |
40 | " " << first[var<cl_uint>("i")] << '=' << var<T>("new_value") << ";\n"; | |
41 | } | |
42 | ||
43 | void set_old_value(const T &old_value) | |
44 | { | |
45 | add_set_arg<T>("old_value", old_value); | |
46 | } | |
47 | ||
48 | void set_new_value(const T &new_value) | |
49 | { | |
50 | add_set_arg<T>("new_value", new_value); | |
51 | } | |
52 | ||
53 | void exec(command_queue &queue) | |
54 | { | |
55 | if(m_count == 0){ | |
56 | // nothing to do | |
57 | return; | |
58 | } | |
59 | ||
60 | exec_1d(queue, 0, m_count); | |
61 | } | |
62 | ||
63 | private: | |
64 | size_t m_count; | |
65 | }; | |
66 | ||
67 | } // end detail namespace | |
68 | ||
69 | /// Replaces each instance of \p old_value in the range [\p first, | |
70 | /// \p last) with \p new_value. | |
71 | template<class Iterator, class T> | |
72 | inline void replace(Iterator first, | |
73 | Iterator last, | |
74 | const T &old_value, | |
75 | const T &new_value, | |
76 | command_queue &queue = system::default_queue()) | |
77 | { | |
78 | detail::replace_kernel<Iterator, T> kernel; | |
79 | ||
80 | kernel.set_range(first, last); | |
81 | kernel.set_old_value(old_value); | |
82 | kernel.set_new_value(new_value); | |
83 | ||
84 | kernel.exec(queue); | |
85 | } | |
86 | ||
87 | } // end compute namespace | |
88 | } // end boost namespace | |
89 | ||
90 | #endif // BOOST_COMPUTE_ALGORITHM_REPLACE_HPP |