CLOGS
C++ library for sorting and searching in OpenCL applications
reduce.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, 2015 Bruce Merry
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21 
28 #ifndef CLOGS_REDUCE_H
29 #define CLOGS_REDUCE_H
30 
31 #include <clogs/visibility_push.h>
32 #include <CL/cl.hpp>
33 #include <cstddef>
34 #include <clogs/visibility_pop.h>
35 
36 #include <clogs/core.h>
37 #include <clogs/platform.h>
38 #include <clogs/tune.h>
39 
40 namespace clogs
41 {
42 
43 class ReduceProblem;
44 
45 namespace detail
46 {
47  class Reduce;
48  class ReduceProblem;
49 
50  const ReduceProblem &getDetail(const clogs::ReduceProblem &);
51 } // namespace detail
52 
53 class Reduce;
54 
59 class CLOGS_API ReduceProblem
60 {
61 private:
62  detail::ReduceProblem *detail_;
63  friend const detail::ReduceProblem &detail::getDetail(const clogs::ReduceProblem &);
64 
65 public:
66  ReduceProblem();
67  ~ReduceProblem();
69  ReduceProblem &operator=(const ReduceProblem &);
70 
76  void setType(const Type &type);
77 
81  void setTunePolicy(const TunePolicy &tunePolicy);
82 };
83 
98 class CLOGS_API Reduce : public Algorithm
99 {
100 private:
101  detail::Reduce *getDetail() const;
102  detail::Reduce *getDetailNonNull() const;
103  void construct(
104  cl_context context, cl_device_id device, const ReduceProblem &problem,
105  cl_int &err, const char *&errStr);
106  void moveAssign(Reduce &other);
107  friend void swap(Reduce &, Reduce &);
108 
109 protected:
110  void enqueue(cl_command_queue commandQueue,
111  cl_mem inBuffer,
112  cl_mem outBuffer,
113  ::size_t first,
114  ::size_t elements,
115  ::size_t outPosition,
116  cl_uint numEvents,
117  const cl_event *events,
118  cl_event *event,
119  cl_int &err,
120  const char *&errStr);
121 
122  void enqueue(cl_command_queue commandQueue,
123  bool blocking,
124  cl_mem inBuffer,
125  void *out,
126  ::size_t first,
127  ::size_t elements,
128  cl_uint numEvents,
129  const cl_event *events,
130  cl_event *event,
131  cl_int &err,
132  const char *&errStr);
133 
134 public:
138  Reduce();
139 
140 #ifdef CLOGS_HAVE_RVALUE_REFERENCES
141  Reduce(Reduce &&other) CLOGS_NOEXCEPT
142  {
143  moveConstruct(other);
144  }
145 
146  Reduce &operator=(Reduce &&other) CLOGS_NOEXCEPT
147  {
148  moveAssign(other);
149  return *this;
150  }
151 #endif
152 
163  Reduce(const cl::Context &context, const cl::Device &device, const ReduceProblem &problem)
164  {
165  int err;
166  const char *errStr;
167  construct(context(), device(), problem, err, errStr);
168  detail::handleError(err, errStr);
169  }
170 
181  Reduce(cl_context context, cl_device_id device, const ReduceProblem &problem)
182  {
183  int err;
184  const char *errStr;
185  construct(context, device, problem, err, errStr);
186  detail::handleError(err, errStr);
187  }
188 
189  ~Reduce();
190 
212  void enqueue(const cl::CommandQueue &commandQueue,
213  const cl::Buffer &inBuffer,
214  const cl::Buffer &outBuffer,
215  ::size_t first,
216  ::size_t elements,
217  ::size_t outPosition,
218  const VECTOR_CLASS<cl::Event> *events = NULL,
219  cl::Event *event = NULL)
220  {
221  cl_event outEvent;
222  int err;
223  const char *errStr;
224  detail::UnwrapArray<cl::Event> rawEvents(events);
225  enqueue(commandQueue(), inBuffer(), outBuffer(), first, elements, outPosition,
226  rawEvents.size(), rawEvents.data(),
227  event != NULL ? &outEvent : NULL,
228  err, errStr);
229  detail::handleError(err, errStr);
230  if (event != NULL)
231  *event = outEvent; // steals the reference
232  }
233 
235  void enqueue(cl_command_queue commandQueue,
236  cl_mem inBuffer,
237  cl_mem outBuffer,
238  ::size_t first,
239  ::size_t elements,
240  ::size_t outPosition,
241  cl_uint numEvents = 0,
242  const cl_event *events = NULL,
243  cl_event *event = NULL)
244  {
245  cl_int err;
246  const char *errStr;
247  enqueue(commandQueue, inBuffer, outBuffer, first, elements, outPosition,
248  numEvents, events, event, err, errStr);
249  detail::handleError(err, errStr);
250  }
251 
257  void enqueue(const cl::CommandQueue &commandQueue,
258  bool blocking,
259  const cl::Buffer &inBuffer,
260  void *out,
261  ::size_t first,
262  ::size_t elements,
263  const VECTOR_CLASS<cl::Event> *events = NULL,
264  cl::Event *event = NULL)
265  {
266  cl_event outEvent;
267  cl_int err;
268  const char *errStr;
269  detail::UnwrapArray<cl::Event> rawEvents(events);
270  enqueue(commandQueue(), blocking, inBuffer(), out, first, elements,
271  rawEvents.size(), rawEvents.data(),
272  event != NULL ? &outEvent : NULL,
273  err, errStr);
274  detail::handleError(err, errStr);
275  if (event != NULL)
276  *event = outEvent; // steals the reference
277  }
278 
280  void enqueue(cl_command_queue commandQueue,
281  bool blocking,
282  cl_mem inBuffer,
283  void *out,
284  ::size_t first,
285  ::size_t elements,
286  cl_uint numEvents = 0,
287  const cl_event *events = NULL,
288  cl_event *event = NULL)
289  {
290  cl_int err;
291  const char *errStr;
292  enqueue(commandQueue, blocking, inBuffer, out, first, elements,
293  numEvents, events, event, err, errStr);
294  detail::handleError(err, errStr);
295  }
296 };
297 
298 void swap(Reduce &a, Reduce &b);
299 
300 } // namespace clogs
301 
302 #endif /* !CLOGS_REDUCE_H */
Pops default visibility if appropriate.
void enqueue(cl_command_queue commandQueue, cl_mem inBuffer, cl_mem outBuffer,::size_t first,::size_t elements,::size_t outPosition, cl_uint numEvents=0, const cl_event *events=NULL, cl_event *event=NULL)
Definition: reduce.h:235
Pushes default visibility if appropriate.
Encapsulation of an OpenCL built-in type that can be stored in a buffer.
Definition: core.h:134
Basic types shared by several primitives.
Creates an array of handles from a vector of wrappers.
Definition: core.h:198
Encapsulates the specifics of a reduction problem.
Definition: reduce.h:59
Reduce(const cl::Context &context, const cl::Device &device, const ReduceProblem &problem)
Constructor.
Definition: reduce.h:163
void enqueue(const cl::CommandQueue &commandQueue, const cl::Buffer &inBuffer, const cl::Buffer &outBuffer,::size_t first,::size_t elements,::size_t outPosition, const VECTOR_CLASS< cl::Event > *events=NULL, cl::Event *event=NULL)
Enqueue a reduction operation on a command queue.
Definition: reduce.h:212
Detects compiler properties.
Reduce(cl_context context, cl_device_id device, const ReduceProblem &problem)
Constructor.
Definition: reduce.h:181
void enqueue(const cl::CommandQueue &commandQueue, bool blocking, const cl::Buffer &inBuffer, void *out,::size_t first,::size_t elements, const VECTOR_CLASS< cl::Event > *events=NULL, cl::Event *event=NULL)
Enqueue a reduction operation and read the result back to the host.
Definition: reduce.h:257
Reduction primitive.
Definition: reduce.h:98
void enqueue(cl_command_queue commandQueue, bool blocking, cl_mem inBuffer, void *out,::size_t first,::size_t elements, cl_uint numEvents=0, const cl_event *events=NULL, cl_event *event=NULL)
Definition: reduce.h:280
Definition: tune.h:55
Base class for all algorithm classes.
Definition: core.h:305
OpenCL primitives.
Definition: clogs.h:56
Control over tuning policy.