Newer
Older
/*
* Parallel Quicksort.
*
* Demo application that shows how one might use threadpools/futures
* in an application.
*
* Requires threadpool.c/threadpool.h
*
* Written by Godmar Back gback@cs.vt.edu for CS3214 Fall 2014.
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
*/
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#include <getopt.h>
#include "threadpool_lib.h"
#include "threadpool.h"
typedef void (*sort_func)(int *, int);
#define DEFAULT_THREADS 4
static int nthreads = DEFAULT_THREADS;
/* Return true if array 'a' is sorted. */
static bool
check_sorted(int a[], int n)
{
int i;
for (i = 0; i < n-1; i++)
if (a[i] > a[i+1])
return false;
return true;
}
/* -------------------------------------------------------------
* Built-in qsort.
*/
static int cmp_int(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
static void builtin_qsort(int *a, int N)
{
qsort(a, N, sizeof(int), cmp_int);
}
/* -------------------------------------------------------------
* Quicksort utility routines.
*/
/* Swap two elements */
static inline void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
/* Partitioning using middle element as pivot */
static int
qsort_partition(int * array, int left, int right)
{
int middle = left + (right-left)/2;
// left <=> middle
swap(array + left, array + middle);
int current, last = left;
for (current = left + 1; current <= right; ++current) {
if (array[current] < array[left]) {
++last;
// last <=> current
swap(array + last, array + current);
}
}
// left <=> last
swap(array + left, array + last);
return last;
}
/* -------------------------------------------------------------
* Serial implementation.
*/
static void
qsort_internal_serial(int *array, int left, int right)
{
if (left >= right)
return;
int split = qsort_partition(array, left, right);
qsort_internal_serial(array, left, split - 1);
qsort_internal_serial(array, split + 1, right);
}
static void
qsort_serial(int *array, int N)
{
qsort_internal_serial(array, 0, N - 1);
}
/* -------------------------------------------------------------
* Parallel implementation.
*/
/* qsort_task describes a unit of parallel work */
struct qsort_task {
int *array;
int left, right, depth;
};
/* Parallel qsort - returns size of segment sorted */
static int
qsort_internal_parallel(struct thread_pool * threadpool, struct qsort_task * s)
{
int * array = s->array;
int left = s->left;
int right = s->right;
int depth = s->depth;
if (left >= right)
return 0;
int split = qsort_partition(array, left, right);
if (depth < 1) {
qsort_internal_serial(array, left, split - 1);
qsort_internal_serial(array, split + 1, right);
} else {
struct qsort_task qleft = {
.left = s->left,
.right = split-1,
.depth = s->depth-1,
.array = s->array
};
struct future * lhalf = thread_pool_submit(threadpool,
(fork_join_task_t) qsort_internal_parallel,
&qleft);
struct qsort_task qright = {
.left = split+1,
.right = s->right,
.depth = s->depth-1,
.array = s->array
};
qsort_internal_parallel(threadpool, &qright);
future_get(lhalf);
future_free(lhalf);
}
return right - left;
}
// maximum depth to which each recursive call is executed in parallel
static int depth = 3;
static void
qsort_parallel(int *array, int N)
{
struct qsort_task root = {
.left = 0, .right = N-1, .depth = depth, .array = array
};
struct thread_pool * threadpool = thread_pool_new(nthreads);
struct future * top = thread_pool_submit(threadpool,
(fork_join_task_t) qsort_internal_parallel,
&root);
future_get(top);
future_free(top);
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
thread_pool_shutdown_and_destroy(threadpool);
}
/*
* Benchmark one run of sort_func sorter
*/
static void
benchmark(const char *benchmark_name, sort_func sorter, int *a0, int N, bool report)
{
int *a = malloc(N * sizeof(int));
memcpy(a, a0, N * sizeof(int));
struct benchmark_data * bdata = start_benchmark();
// parallel section here, including thread pool startup and shutdown
sorter(a, N);
stop_benchmark(bdata);
// consistency check
if (!check_sorted(a, N)) {
fprintf(stderr, "Sort failed\n");
abort();
}
// report only if successful
if (report)
report_benchmark_results(bdata);
printf("%s result ok. Timings follow\n", benchmark_name);
report_benchmark_results_to_human(stdout, bdata);
free(bdata);
free(a);
}
static void
usage(char *av0, int depth)
{
fprintf(stderr, "Usage: %s [-d <n>] [-n <n>] [-b] [-q] [-s <n>] <N>\n"
" -d parallel recursion depth, default %d\n"
" -n number of threads in pool, default %d\n"
" -b run built-in qsort\n"
" -s specify srand() seed\n"
" -q run serial qsort\n"
, av0, depth, DEFAULT_THREADS);
exit(0);
}
int
main(int ac, char *av[])
{
int c;
bool run_builtin_qsort = false;
bool run_serial_qsort = false;
while ((c = getopt(ac, av, "d:n:bhs:q")) != EOF) {
switch (c) {
case 'd':
depth = atoi(optarg);
break;
case 'n':
nthreads = atoi(optarg);
break;
case 's':
srand(atoi(optarg));
break;
case 'b':
run_builtin_qsort = true;
break;
case 'q':
run_serial_qsort = true;
break;
case 'h':
usage(av[0], depth);
}
}
if (optind == ac)
usage(av[0], depth);
int N = atoi(av[optind]);
int i, * a0 = malloc(N * sizeof(int));
for (i = 0; i < N; i++)
a0[i] = random();
if (run_builtin_qsort)
benchmark("Built-in qsort", builtin_qsort, a0, N, false);
if (run_serial_qsort)
benchmark("qsort serial", qsort_serial, a0, N, false);
printf("Using %d threads, recursive parallel depth=%d\n", nthreads, depth);
benchmark("qsort parallel", qsort_parallel, a0, N, true);
return 0;
}