]>
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_DETAIL_SERIAL_FIND_EXTREMA_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP | |
13 | ||
14 | #include <boost/compute/command_queue.hpp> | |
15 | #include <boost/compute/types/fundamental.hpp> | |
16 | #include <boost/compute/detail/meta_kernel.hpp> | |
17 | #include <boost/compute/detail/iterator_range_size.hpp> | |
18 | #include <boost/compute/container/detail/scalar.hpp> | |
19 | ||
20 | namespace boost { | |
21 | namespace compute { | |
22 | namespace detail { | |
23 | ||
24 | template<class InputIterator, class Compare> | |
25 | inline InputIterator serial_find_extrema(InputIterator first, | |
26 | InputIterator last, | |
27 | Compare compare, | |
28 | const bool find_minimum, | |
29 | command_queue &queue) | |
30 | { | |
31 | typedef typename std::iterator_traits<InputIterator>::value_type value_type; | |
32 | typedef typename std::iterator_traits<InputIterator>::difference_type difference_type; | |
33 | ||
34 | const context &context = queue.get_context(); | |
35 | ||
36 | meta_kernel k("serial_find_extrema"); | |
37 | ||
38 | k << | |
39 | k.decl<value_type>("value") << " = " << first[k.expr<uint_>("0")] << ";\n" << | |
40 | k.decl<uint_>("value_index") << " = 0;\n" << | |
41 | "for(uint i = 1; i < size; i++){\n" << | |
42 | " " << k.decl<value_type>("candidate") << "=" | |
43 | << first[k.expr<uint_>("i")] << ";\n" << | |
44 | ||
45 | "#ifndef BOOST_COMPUTE_FIND_MAXIMUM\n" << | |
46 | " if(" << compare(k.var<value_type>("candidate"), | |
47 | k.var<value_type>("value")) << "){\n" << | |
48 | "#else\n" << | |
49 | " if(" << compare(k.var<value_type>("value"), | |
50 | k.var<value_type>("candidate")) << "){\n" << | |
51 | "#endif\n" << | |
52 | ||
53 | " value = candidate;\n" << | |
54 | " value_index = i;\n" << | |
55 | " }\n" << | |
56 | "}\n" << | |
57 | "*index = value_index;\n"; | |
58 | ||
59 | size_t index_arg_index = k.add_arg<uint_ *>(memory_object::global_memory, "index"); | |
60 | size_t size_arg_index = k.add_arg<uint_>("size"); | |
61 | ||
62 | std::string options; | |
63 | if(!find_minimum){ | |
64 | options = "-DBOOST_COMPUTE_FIND_MAXIMUM"; | |
65 | } | |
66 | kernel kernel = k.compile(context, options); | |
67 | ||
68 | // setup index buffer | |
69 | scalar<uint_> index(context); | |
70 | kernel.set_arg(index_arg_index, index.get_buffer()); | |
71 | ||
72 | // setup count | |
73 | size_t count = iterator_range_size(first, last); | |
74 | kernel.set_arg(size_arg_index, static_cast<uint_>(count)); | |
75 | ||
76 | // run kernel | |
77 | queue.enqueue_task(kernel); | |
78 | ||
79 | // read index and return iterator | |
80 | return first + static_cast<difference_type>(index.read(queue)); | |
81 | } | |
82 | ||
83 | } // end detail namespace | |
84 | } // end compute namespace | |
85 | } // end boost namespace | |
86 | ||
87 | #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP |