-
Notifications
You must be signed in to change notification settings - Fork 0
/
psort.cpp
286 lines (258 loc) · 6.79 KB
/
psort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "psort.h"
#include <ctime>
/**
* psort - public wrapper for the parallel sort implementation
* @param array - the structure to sort
*/
template <class T>
void ParallelSort::psort(vector<T>* array)
{
int size = array->size();
int threads;
//If size is one, already sorted.
if(size < 2)
return;
//If size is less than 5000, use serial quicksort
//to avoid overhead.
if(size <= 5000)
{
quickSortSerial(array, 0, size - 1);
}
else
{
#pragma omp parallel default(shared) num_threads(16)
{
#pragma omp single nowait
quickSort(array, 0, size - 1);
}
}
}
/**
* quickSort - implementation of parallel sort using quicksort
* @param array - the structure to sort
* @param start - index of start of array section to sort
* @param end - index of end of array section to sort
*/
template <class T>
void ParallelSort::quickSort(vector<T> * array, int start, int end)
{
int i = start, j = end, middle = (start + end)/2;
T temp;
//Select median of three for pivot to avoid
//worst case pivot.
if((*array)[start] > (*array)[middle])
{
temp = (*array)[start];
(*array)[start] = (*array)[middle];
(*array)[middle] = temp;
}
if((*array)[start] > (*array)[end])
{
temp = (*array)[start];
(*array)[start] = (*array)[end];
(*array)[end] = temp;
}
if((*array)[middle] > (*array)[end])
{
temp = (*array)[middle];
(*array)[middle] = (*array)[end];
(*array)[end] = temp;
}
T pivot = (*array)[middle];
//Organize sections
while(i <= j)
{
while((*array)[i] < pivot)
i++;
while((*array)[j] > pivot)
j--;
if(i <= j)
{
temp = (*array)[i];
(*array)[i] = (*array)[j];
(*array)[j] = temp;
i++;
j--;
}
}
//Parallelization of first section using omp task
#pragma omp task
if(start < j)
{
//If remaining sections are less than 10000, use serial
//quicksort to avoid overhead.
if(j - start < 10000)
quickSortSerial(array, start, j);
else
quickSort(array, start, j);
}
//Parallelization of second section using omp task
#pragma omp task
if(i < end)
{
//If remaining sections are less than 10000, use serial
//quicksort to avoid overhead.
if(end - i < 10000)
quickSortSerial(array, i, end);
else
quickSort(array, i, end);
}
}
/**
* quickSortSections - unused implementation of quicksort
* parallelization using omp sections instead of tasks
* @param array - the structure to sort
* @param start - index of start of array section to sort
* @param end - index of end of array section to sort
*/
template <class T>
void ParallelSort::quickSortSection(vector<T> * array, int start, int end)
{
int i = start, j = end, middle = (start + end)/2;
T temp;
//Select median of three for pivot to avoid
//worst case pivot.
if((*array)[start] > (*array)[middle])
{
temp = (*array)[start];
(*array)[start] = (*array)[middle];
(*array)[middle] = temp;
}
if((*array)[start] > (*array)[end])
{
temp = (*array)[start];
(*array)[start] = (*array)[end];
(*array)[end] = temp;
}
if((*array)[middle] > (*array)[end])
{
temp = (*array)[middle];
(*array)[middle] = (*array)[end];
(*array)[end] = temp;
}
T pivot = (*array)[middle];
//Organize sections
while(i <= j)
{
while((*array)[i] < pivot)
i++;
while((*array)[j] > pivot)
j--;
if(i <= j)
{
temp = (*array)[i];
(*array)[i] = (*array)[j];
(*array)[j] = temp;
i++;
j--;
}
}
//Parallelization of two section using omp sections
int size;
#pragma omp parallel sections if(end - start > sortSize) num_threads(2)
{
#pragma omp section
if(start < j)
{
size = j - start;
if(size < 10000)
quickSortSerial(array, start, j);
else
quickSortSection(array, start, j);
}
#pragma omp section
if(i < end)
{
size = end - i;
if(size < 10000)
quickSortSerial(array, i, end);
else
quickSortSection(array, i, end);
}
}
}
/**
* quickSortSerial - serial quicksort
* @param array - the structure to sort
* @param start - index of start of array section to sort
* @param end - index of end of array section to sort
*/
template <class T>
void ParallelSort::quickSortSerial(vector<T> * array, int start, int end)
{
int i = start, j = end, middle = (start + end)/2;
T temp;
//Select median of three for pivot to avoid
//worst case pivot.
if((*array)[start] > (*array)[middle])
{
temp = (*array)[start];
(*array)[start] = (*array)[middle];
(*array)[middle] = temp;
}
if((*array)[start] > (*array)[end])
{
temp = (*array)[start];
(*array)[start] = (*array)[end];
(*array)[end] = temp;
}
if((*array)[middle] > (*array)[end])
{
temp = (*array)[middle];
(*array)[middle] = (*array)[end];
(*array)[end] = temp;
}
T pivot = (*array)[middle];
//Organize sections
while(i <= j)
{
while((*array)[i] < pivot)
i++;
while((*array)[j] > pivot)
j--;
if(i <= j)
{
temp = (*array)[i];
(*array)[i] = (*array)[j];
(*array)[j] = temp;
i++;
j--;
}
}
//Sort first section.
if(start < j)
{
quickSortSerial(array, start, j);
}
//Sort second section.
if(i < end)
{
quickSortSerial(array, i, end);
}
}
/**
* insertionSort - Unused insertion sort that could be used to
* optimize sorts of small arrays of items, as insertion sort is
* faster on smaller sized arrays, however testing indicated that
* sort did not come out faster, so it was not used.
* @param array - the structure to sort
* @param start - index of start of array section to sort
* @param end - index of end of array section to sort
*/
template <class T>
void ParallelSort::insertionSort(vector<T> * array, int start, int end)
{
T item;
int iPlace;
for(int i = start; i <= end; i++)
{
item = (*array)[i];
iPlace = i;
while(iPlace > 0 && (*array)[iPlace - 1] > item)
{
(*array)[iPlace] = (*array)[--iPlace];
}
(*array)[iPlace] = item;
}
return;
}