CLOGS
C++ library for sorting and searching in OpenCL applications
radixsort.h
Go to the documentation of this file.
1 /* Copyright (c) 2012 University of Cape Town
2  * Copyright (c) 2014, 2015 Bruce Merry
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
29 #ifndef CLOGS_RADIXSORT_H
30 #define CLOGS_RADIXSORT_H
31 
32 #include <clogs/visibility_push.h>
33 #include <CL/cl.hpp>
34 #include <cstddef>
35 #include <clogs/visibility_pop.h>
36 
37 #include <clogs/core.h>
38 #include <clogs/platform.h>
39 #include <clogs/tune.h>
40 
41 namespace clogs
42 {
43 
44 class RadixsortProblem;
45 
46 namespace detail
47 {
48  class Radixsort;
49  class RadixsortProblem;
50 
51  const RadixsortProblem &getDetail(const clogs::RadixsortProblem &);
52 } // namespace detail
53 
54 class Radixsort;
55 
61 class CLOGS_API RadixsortProblem
62 {
63 private:
64  detail::RadixsortProblem *detail_;
65  friend const detail::RadixsortProblem &detail::getDetail(const clogs::RadixsortProblem &);
66 
67 public:
71  RadixsortProblem &operator=(const RadixsortProblem &);
72 
79  void setKeyType(const Type &keyType);
80 
85  void setValueType(const Type &valueType);
86 
90  void setTunePolicy(const TunePolicy &tunePolicy);
91 };
92 
109 class CLOGS_API Radixsort : public Algorithm
110 {
111 private:
112  detail::Radixsort *getDetail() const;
113  detail::Radixsort *getDetailNonNull() const;
114  void construct(cl_context context, cl_device_id device, const RadixsortProblem &problem,
115  cl_int &err, const char *&errStr);
116  void moveAssign(Radixsort &other);
117  friend void swap(Radixsort &, Radixsort &);
118 
119 protected:
120  void enqueue(cl_command_queue command_queue,
121  cl_mem keys, cl_mem values,
122  ::size_t elements, unsigned int maxBits,
123  cl_uint numEvents,
124  const cl_event *events,
125  cl_event *event,
126  cl_int &err,
127  const char *&errStr);
128 
129  void setTemporaryBuffers(cl_mem keys, cl_mem values,
130  cl_int &err, const char *&errStr);
131 
132 public:
136  Radixsort();
137 
138 #ifdef CLOGS_HAVE_RVALUE_REFERENCES
139  Radixsort(Radixsort &&other) CLOGS_NOEXCEPT
140  {
141  moveConstruct(other);
142  }
143 
144  Radixsort &operator=(Radixsort &&other) CLOGS_NOEXCEPT
145  {
146  moveAssign(other);
147  return *this;
148  }
149 #endif
150 
166  Radixsort(const cl::Context &context, const cl::Device &device,
167  const Type &keyType, const Type &valueType = Type())
168  {
169  cl_int err;
170  const char *errStr;
171  RadixsortProblem problem;
172  problem.setKeyType(keyType);
173  problem.setValueType(valueType);
174  construct(context(), device(), problem, err, errStr);
175  detail::handleError(err, errStr);
176  }
177 
189  Radixsort(const cl::Context &context, const cl::Device &device,
190  const RadixsortProblem &problem)
191  {
192  cl_int err;
193  const char *errStr;
194  construct(context(), device(), problem, err, errStr);
195  detail::handleError(err, errStr);
196  }
197 
198  ~Radixsort();
199 
229  void enqueue(const cl::CommandQueue &commandQueue,
230  const cl::Buffer &keys, const cl::Buffer &values,
231  ::size_t elements, unsigned int maxBits = 0,
232  const VECTOR_CLASS<cl::Event> *events = NULL,
233  cl::Event *event = NULL)
234  {
235  cl_int err;
236  const char *errStr;
237  detail::UnwrapArray<cl::Event> events_(events);
238  cl_event outEvent;
239  enqueue(commandQueue(), keys(), values(), elements, maxBits,
240  events_.size(), events_.data(),
241  event != NULL ? &outEvent : NULL,
242  err, errStr);
243  detail::handleError(err, errStr);
244  if (event != NULL)
245  *event = outEvent; // steals reference
246  }
247 
249  void enqueue(cl_command_queue commandQueue,
250  cl_mem keys, cl_mem values,
251  ::size_t elements, unsigned int maxBits = 0,
252  cl_uint numEvents = 0,
253  const cl_event *events = NULL,
254  cl_event *event = NULL)
255  {
256  cl_int err;
257  const char *errStr;
258  enqueue(commandQueue, keys, values, elements, maxBits, numEvents, events, event,
259  err, errStr);
260  detail::handleError(err, errStr);
261  }
262 
278  void setTemporaryBuffers(const cl::Buffer &keys, const cl::Buffer &values)
279  {
280  cl_int err;
281  const char *errStr;
282  setTemporaryBuffers(keys(), values(), err, errStr);
283  detail::handleError(err, errStr);
284  }
285 
287  void setTemporaryBuffers(cl_mem keys, cl_mem values)
288  {
289  cl_int err;
290  const char *errStr;
291  setTemporaryBuffers(keys, values, err, errStr);
292  detail::handleError(err, errStr);
293  }
294 };
295 
296 void swap(Radixsort &a, Radixsort &b);
297 
298 } // namespace clogs
299 
300 #endif /* !CLOGS_RADIXSORT_H */
Pops default visibility if appropriate.
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
Detects compiler properties.
void setKeyType(const Type &keyType)
Set the key type for sorting.
void enqueue(const cl::CommandQueue &commandQueue, const cl::Buffer &keys, const cl::Buffer &values,::size_t elements, unsigned int maxBits=0, const VECTOR_CLASS< cl::Event > *events=NULL, cl::Event *event=NULL)
Enqueue a sort operation on a command queue.
Definition: radixsort.h:229
void enqueue(cl_command_queue commandQueue, cl_mem keys, cl_mem values,::size_t elements, unsigned int maxBits=0, cl_uint numEvents=0, const cl_event *events=NULL, cl_event *event=NULL)
Definition: radixsort.h:249
Encapsulates the specifics of a radixsort problem.
Definition: radixsort.h:61
Radix-sort interface.
Definition: radixsort.h:109
Radixsort(const cl::Context &context, const cl::Device &device, const Type &keyType, const Type &valueType=Type())
Constructor.
Definition: radixsort.h:166
void setTemporaryBuffers(cl_mem keys, cl_mem values)
Definition: radixsort.h:287
void setValueType(const Type &valueType)
Set the value type for sorting.
Definition: tune.h:55
void setTemporaryBuffers(const cl::Buffer &keys, const cl::Buffer &values)
Set temporary buffers used during sorting.
Definition: radixsort.h:278
Radixsort(const cl::Context &context, const cl::Device &device, const RadixsortProblem &problem)
Constructor.
Definition: radixsort.h:189
Base class for all algorithm classes.
Definition: core.h:305
OpenCL primitives.
Definition: clogs.h:56
Control over tuning policy.