1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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_DETAIL_SCAN_ON_GPU_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SCAN_ON_GPU_HPP
14 #include <boost/compute/kernel.hpp>
15 #include <boost/compute/detail/meta_kernel.hpp>
16 #include <boost/compute/command_queue.hpp>
17 #include <boost/compute/container/vector.hpp>
18 #include <boost/compute/detail/iterator_range_size.hpp>
19 #include <boost/compute/memory/local_buffer.hpp>
20 #include <boost/compute/iterator/buffer_iterator.hpp>
26 template<class InputIterator, class OutputIterator, class BinaryOperator>
27 class local_scan_kernel : public meta_kernel
30 local_scan_kernel(InputIterator first,
32 OutputIterator result,
35 : meta_kernel("local_scan")
37 typedef typename std::iterator_traits<InputIterator>::value_type T;
43 m_block_sums_arg = add_arg<T *>(memory_object::global_memory, "block_sums");
44 m_scratch_arg = add_arg<T *>(memory_object::local_memory, "scratch");
45 m_block_size_arg = add_arg<const cl_uint>("block_size");
46 m_count_arg = add_arg<const cl_uint>("count");
47 m_init_value_arg = add_arg<const T>("init");
49 // work-item parameters
51 "const uint gid = get_global_id(0);\n" <<
52 "const uint lid = get_local_id(0);\n";
54 // check against data size
60 // copy values from input to local memory
63 decl<const T>("local_init") << "= (gid == 0) ? init : 0;\n" <<
64 "if(lid == 0){ scratch[lid] = local_init; }\n" <<
65 "else { scratch[lid] = " << first[expr<cl_uint>("gid-1")] << "; }\n";
69 "scratch[lid] = " << first[expr<cl_uint>("gid")] << ";\n";
76 " scratch[lid] = 0;\n" <<
80 // wait for all threads to read from input
82 "barrier(CLK_LOCAL_MEM_FENCE);\n";
86 "for(uint i = 1; i < block_size; i <<= 1){\n" <<
87 " " << decl<const T>("x") << " = lid >= i ? scratch[lid-i] : 0;\n" <<
88 " barrier(CLK_LOCAL_MEM_FENCE);\n" <<
90 " scratch[lid] = " << op(var<T>("scratch[lid]"), var<T>("x")) << ";\n" <<
92 " barrier(CLK_LOCAL_MEM_FENCE);\n" <<
95 // copy results to output
102 result[expr<cl_uint>("gid")] << " = scratch[lid];\n";
108 // store sum for the block
111 "if(lid == block_size - 1 && gid < count) {\n" <<
112 " block_sums[get_group_id(0)] = " <<
113 op(first[expr<cl_uint>("gid")], var<T>("scratch[lid]")) <<
119 "if(lid == block_size - 1){\n" <<
120 " block_sums[get_group_id(0)] = scratch[lid];\n" <<
125 size_t m_block_sums_arg;
126 size_t m_scratch_arg;
127 size_t m_block_size_arg;
129 size_t m_init_value_arg;
132 template<class T, class BinaryOperator>
133 class write_scanned_output_kernel : public meta_kernel
136 write_scanned_output_kernel(BinaryOperator op)
137 : meta_kernel("write_scanned_output")
141 m_output_arg = add_arg<T *>(memory_object::global_memory, "output");
142 m_block_sums_arg = add_arg<const T *>(memory_object::global_memory, "block_sums");
143 m_count_arg = add_arg<const cl_uint>("count");
145 // work-item parameters
147 "const uint gid = get_global_id(0);\n" <<
148 "const uint block_id = get_group_id(0);\n";
150 // check against data size
152 *this << "if(gid < count){\n";
158 op(var<T>("block_sums[block_id]"), var<T>("output[gid] ")) << ";\n";
166 size_t m_block_sums_arg;
170 template<class InputIterator>
171 inline size_t pick_scan_block_size(InputIterator first, InputIterator last)
173 size_t count = iterator_range_size(first, last);
175 if(count == 0) { return 0; }
176 else if(count <= 1) { return 1; }
177 else if(count <= 2) { return 2; }
178 else if(count <= 4) { return 4; }
179 else if(count <= 8) { return 8; }
180 else if(count <= 16) { return 16; }
181 else if(count <= 32) { return 32; }
182 else if(count <= 64) { return 64; }
183 else if(count <= 128) { return 128; }
187 template<class InputIterator, class OutputIterator, class T, class BinaryOperator>
188 inline OutputIterator scan_impl(InputIterator first,
190 OutputIterator result,
194 command_queue &queue)
197 std::iterator_traits<InputIterator>::value_type
200 std::iterator_traits<InputIterator>::difference_type
203 std::iterator_traits<OutputIterator>::value_type
206 const context &context = queue.get_context();
207 const size_t count = detail::iterator_range_size(first, last);
209 size_t block_size = pick_scan_block_size(first, last);
210 size_t block_count = count / block_size;
212 if(block_count * block_size < count){
216 ::boost::compute::vector<input_type> block_sums(block_count, context);
220 std::memset(&zero, 0, sizeof(input_type));
221 ::boost::compute::fill(block_sums.begin(), block_sums.end(), zero, queue);
224 local_scan_kernel<InputIterator, OutputIterator, BinaryOperator>
225 local_scan_kernel(first, last, result, exclusive, op);
227 ::boost::compute::kernel kernel = local_scan_kernel.compile(context);
228 kernel.set_arg(local_scan_kernel.m_scratch_arg, local_buffer<input_type>(block_size));
229 kernel.set_arg(local_scan_kernel.m_block_sums_arg, block_sums);
230 kernel.set_arg(local_scan_kernel.m_block_size_arg, static_cast<cl_uint>(block_size));
231 kernel.set_arg(local_scan_kernel.m_count_arg, static_cast<cl_uint>(count));
232 kernel.set_arg(local_scan_kernel.m_init_value_arg, static_cast<output_type>(init));
234 queue.enqueue_1d_range_kernel(kernel,
236 block_count * block_size,
239 // inclusive scan block sums
241 scan_impl(block_sums.begin(),
251 // add block sums to each block
253 write_scanned_output_kernel<input_type, BinaryOperator>
254 write_output_kernel(op);
255 kernel = write_output_kernel.compile(context);
256 kernel.set_arg(write_output_kernel.m_output_arg, result.get_buffer());
257 kernel.set_arg(write_output_kernel.m_block_sums_arg, block_sums);
258 kernel.set_arg(write_output_kernel.m_count_arg, static_cast<cl_uint>(count));
260 queue.enqueue_1d_range_kernel(kernel,
262 block_count * block_size,
266 return result + static_cast<difference_type>(count);
269 template<class InputIterator, class OutputIterator, class T, class BinaryOperator>
270 inline OutputIterator dispatch_scan(InputIterator first,
272 OutputIterator result,
276 command_queue &queue)
278 return scan_impl(first, last, result, exclusive, init, op, queue);
281 template<class InputIterator, class T, class BinaryOperator>
282 inline InputIterator dispatch_scan(InputIterator first,
284 InputIterator result,
288 command_queue &queue)
290 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
293 // scan input in-place
294 const context &context = queue.get_context();
296 // make a temporary copy the input
297 size_t count = iterator_range_size(first, last);
298 vector<value_type> tmp(count, context);
299 copy(first, last, tmp.begin(), queue);
301 // scan from temporary values
302 return scan_impl(tmp.begin(), tmp.end(), first, exclusive, init, op, queue);
305 // scan input to output
306 return scan_impl(first, last, result, exclusive, init, op, queue);
310 template<class InputIterator, class OutputIterator, class T, class BinaryOperator>
311 inline OutputIterator scan_on_gpu(InputIterator first,
313 OutputIterator result,
317 command_queue &queue)
323 return dispatch_scan(first, last, result, exclusive, init, op, queue);
326 } // end detail namespace
327 } // end compute namespace
328 } // end boost namespace
330 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SCAN_ON_GPU_HPP