Helios++
Helios software for LiDAR simulations
SM_ParallelMergeSort.h
1 #ifndef _SM_PARALLEL_MERGE_SORT_H_
2 #define _SM_PARALLEL_MERGE_SORT_H_
3 
4 #include <SharedTaskSequencer.h>
5 
6 #include <memory>
7 
8 namespace helios{ namespace hpc {
9 
24 template <typename RandomAccessIterator, typename Comparator>
26 protected:
27  // *** ATTRIBUTES *** //
28  // ******************** //
36  size_t numThreads;
45  size_t minElements;
56  int maxDepth;
62  std::shared_ptr<SharedTaskSequencer> stSequencer;
63 
64 public:
65  // *** CONSTRUCTION / DESTRUCTION *** //
66  // ************************************ //
76  {init();}
83  SM_ParallelMergeSort(size_t const numThreads, size_t const minElements) :
86  {init();}
90  virtual void init();
91  virtual ~SM_ParallelMergeSort() {}
92 
93  // *** SORT METHODS *** //
94  // ********************** //
104  virtual void trySort(
105  RandomAccessIterator begin,
106  RandomAccessIterator end,
107  Comparator comparator
108  );
115  virtual inline void sort(
116  RandomAccessIterator begin,
117  RandomAccessIterator end,
118  Comparator comparator
119  ){
120  trySort(begin, end, comparator);
121  join();
122  }
123 
124  // *** CONTROL METHODS *** //
125  // ************************* //
137  virtual void join();
138 
139  // *** GETTERs and SETTERs *** //
140  // ***************************** //
146  virtual inline size_t getNumThreads() const {return numThreads;}
152  virtual inline void setNumThreads(size_t const numThreads)
153  {this->numThreads = numThreads;}
159  virtual inline size_t getMinElements() const {return minElements;}
165  virtual inline void setMinElements(size_t const minElements)
166  {this->minElements = minElements;}
167 };
168 
169 
170 }}
171 
172 #include <hpc/SM_ParallelMergeSort.tpp>
173 
174 #endif
175 
Class providing a shared memory sorting algorithm based on merge sort.
Definition: SM_ParallelMergeSort.h:25
SM_ParallelMergeSort(size_t const numThreads)
Build a shared memory parallel merge sort instance with given number of threads. The number of minimu...
Definition: SM_ParallelMergeSort.h:73
virtual void setMinElements(size_t const minElements)
Set the minimum number of elements for parallel execution.
Definition: SM_ParallelMergeSort.h:165
virtual size_t getNumThreads() const
Get the number of threads.
Definition: SM_ParallelMergeSort.h:146
virtual void init()
Common initialization for all constructors.
virtual size_t getMinElements() const
Get the minimum number of elements for parallel execution.
Definition: SM_ParallelMergeSort.h:159
size_t minElements
If the number of elements to be sorted is less than minElements, then a sequential sort will be appli...
Definition: SM_ParallelMergeSort.h:45
size_t numThreads
How many threads can be used to compute parallel sorting.
Definition: SM_ParallelMergeSort.h:36
virtual void join()
Force caller thread to wait until current sort has been finished.
virtual void sort(RandomAccessIterator begin, RandomAccessIterator end, Comparator comparator)
Like SM_ParallelMergeSort::trySort function but it returns only after sorting has been finished....
Definition: SM_ParallelMergeSort.h:115
std::shared_ptr< SharedTaskSequencer > stSequencer
The shared task sequencer to handle concurrent execution of multiple threads.
Definition: SM_ParallelMergeSort.h:62
SM_ParallelMergeSort(size_t const numThreads, size_t const minElements)
Build a shared memory parallel merge sort instance with given number of threads and given minimum num...
Definition: SM_ParallelMergeSort.h:83
int maxDepth
The maximum depth that the tree of splits must reach considering number of threads.
Definition: SM_ParallelMergeSort.h:56
virtual void setNumThreads(size_t const numThreads)
Set the number of threads.
Definition: SM_ParallelMergeSort.h:152
virtual void trySort(RandomAccessIterator begin, RandomAccessIterator end, Comparator comparator)
Sort given sequence using given comparator in a non-blocking fashion. It is, the function might retur...