-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitonic.c
More file actions
374 lines (337 loc) · 11.9 KB
/
bitonic.c
File metadata and controls
374 lines (337 loc) · 11.9 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* File: bitonic.c
* Author: Brandon Fowler, cs220 - 01
*
* Purpose: Implements a butterfly-structured bitonic sort
*
* Input: thread_count: number of threads
* n: number of elements
* g: optional flag to generate numbers randomly
* o: optional flag to print sorted and unsorted list
* Output: Sorted list of elements
*
* Compile: gcc -g -Wall -o bitonic bitonic.c -lpthreads
* Usage: ./bitonic <thread_count> <n> [g] [o]
*
* Notes:
* 1. Assumes number of threads is a power of 2 and number of elements in
* the list is evenly divisble by the number of threads, does not check
* this
* 2. Debug flag can be used to print updated list after each stage of the
* sorting algorithm. Includes information on which butterfly is being
* executed and which stage has been completed.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "timer.h"
/* Global variables */
int thread_count;
int n; // number of elements
int* x; // array to store list
int* temp; // temporary array to store list throughout stages of butterfly
int isRand = 0; // determines if [g] was provided in command line
int toPrint = 0; // determines if [o] was provided in command line
int barrier_thread_count = 0; // Useful for synchronization
int stage; // for debugging only
pthread_mutex_t barrier_mutex;
pthread_cond_t ok_to_proceed;
/* Serial Functions */
void Get_info(int argc, char* argv[]);
void Usage(char prog[]);
void Gen_vector(int x[]);
void Print_vector(int x[]);
void Merge_inc(int A[], int B[], int rank, int partner);
void Merge_dec(int A[], int B[], int rank, int partner);
void Swap_list(int **curr_arr, int **temp_arr);
int cmpfunc(const void * a, const void * b);
/* Parallel function */
void *Thread_work(void* rank);
int main(int argc, char* argv[]) {
long thread;
pthread_t* thread_handles;
double start, finish;
Get_info(argc, argv);
thread_handles = malloc(thread_count*sizeof(pthread_t));
pthread_mutex_init(&barrier_mutex, NULL);
pthread_cond_init(&ok_to_proceed, NULL);
GET_TIME(start);
for (thread = 0; thread < thread_count; thread++)
pthread_create(&thread_handles[thread], (pthread_attr_t*) NULL,
Thread_work, (void*) thread);
for (thread = 0; thread < thread_count; thread++)
pthread_join(thread_handles[thread], NULL);
GET_TIME(finish);
if (toPrint) {
printf("Sorted list is: ");
Print_vector(x);
}
printf("Elapsed time = %e seconds\n", finish - start);
pthread_mutex_destroy(&barrier_mutex);
pthread_cond_destroy(&ok_to_proceed);
free(thread_handles);
free(x);
free(temp);
return 0;
} /* main */
/*-------------------------------------------------------------------
* Function: Get_info
* Purpose: Get the input values from command line
* Input args: argc: number of command line args
* argv: array of command line args
* Output args: thread_count: number of threads
* n: number of elements
* x: list of elements
* isRand: determines if numbers are generated randomly
* toPrint: determines if sorted and unsorted lists are printed
*
*/
void Get_info(int argc, char* argv[]) {
if (argc < 3) Usage(argv[0]);
if (argc > 5) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);
n = strtol(argv[2], NULL, 10);
x = malloc(n*sizeof(int));
temp = malloc(n*sizeof(int));
if (argc == 4) {
if (strcmp(argv[3], "g") == 0)
isRand = 1;
if (strcmp(argv[3], "o") == 0)
toPrint = 1;
}
if (argc == 5) {
if (strcmp(argv[3], "g") == 0)
isRand = 1;
if (strcmp(argv[3], "o") == 0)
toPrint = 1;
if (strcmp(argv[4], "g") == 0)
isRand = 1;
if (strcmp(argv[4], "o") == 0)
toPrint = 1;
}
Gen_vector(x);
} /* Get_info */
/*-------------------------------------------------------------------
* Function: Usage
* Purpose: Print a brief message explaining how the program is run.
* The quit.
* In arg: prog: name of the executable
*/
void Usage(char prog[]) {
fprintf(stderr, "usage: %s <thread_count> <n> [g] [o]\n", prog);
fprintf(stderr, " thread_count = number of threads\n");
fprintf(stderr, " n = number of elements in list\n");
fprintf(stderr, " g = optional flag to generate numbers randomly\n");
fprintf(stderr, " o = optional flag to print unsorted and sorted list\n");
exit(0);
} /* Usage */
/*------------------------------------------------------------------
* Function: Gen_vector
* Purpose: Generates vector randomly if [g] was provided on the command line
* or reads in the vector from stdin
* Out arg: x
*/
void Gen_vector(int x[]) {
int i;
if (isRand) {
srandom(1);
for (i = 0; i < n; i++)
x[i] = random() % 999999;
}
else {
for (i = 0; i < n; i ++)
scanf("%d", &x[i]);
}
if (toPrint) {
printf("Unsorted list is: ");
Print_vector(x);
}
} /* Gen_vector */
/*------------------------------------------------------------------
* Function: Print_vector
* Purpose: Prints vector
* In args:
*/
void Print_vector(int x[]) {
int i;
for (i = 0; i < n; i++)
printf("%d ", x[i]);
printf("\n");
} /* Print_vector */
/*-------------------------------------------------------------------
* Function: Merge_inc
* Purpose: Merges the thread's assigned section of the array with its
* partner's section into B in increasing order (left to right)
* Input args: rank: thread rank
* partner: partner's rank
* A: the array
* Output arg: B: result array
*/
void Merge_inc(int A[], int B[], int rank, int partner) {
int my_first = rank * (n / thread_count); // thread's start point
int my_last = (((rank + 1) * n) / (thread_count)) - 1; // thread's end
int p_first = partner * (n / thread_count); // partner's start point
int p_last = (((partner + 1) * n) / (thread_count)) - 1; // partner's end
int i = my_first;
int j = p_first;
// Increasing order so function moves from left to right and compares using
// less than
while (i <= my_last) {
if (A[my_first] <= A[p_first]) {
B[i] = A[my_first];
i ++; my_first ++;
} else {
B[i] = A[p_first];
i ++; p_first ++;
}
}
while (j <= p_last && my_first <= my_last && p_first <= p_last) {
if (A[my_first] <= A[p_first]) {
B[j] = A[my_first];
j ++; my_first ++;
} else {
B[j] = A[p_first];
j ++; p_first ++;
}
}
if (my_first > my_last)
for (; j <= p_last; j ++, p_first ++)
B[j] = A[p_first];
else
for (; j <= p_last; j ++, my_first ++)
B[j] = A[my_first];
} /* Merge_inc */
/*-------------------------------------------------------------------
* Function: Merge_dec
* Purpose: Merges the thread's assigned section of the array with its
* partner's section into B in decreasing order (right to left)
* Input args: rank: thread rank
* partner: partner's rank
* A: the array
* Output arg: B: result array
*/
void Merge_dec(int A[], int B[], int rank, int partner) {
int my_first = (((rank + 1) * n) / (thread_count)) - 1; //thread's start
int my_last = rank * (n / thread_count); // thread's end
int p_first = (((partner + 1) * n) / (thread_count)) - 1; // partner start
int p_last = partner * (n / thread_count); // partner end
int i = my_first;
int j = p_first;
// Decreasing order so function moves from right to left and uses greater
// than
while (i >= my_last) {
if (A[my_first] >= A[p_first]) {
B[i] = A[my_first];
i --; my_first --;
} else {
B[i] = A[p_first];
i --; p_first --;
}
}
while (j >= p_last && my_first >= my_last && p_first >= p_last) {
if (A[my_first] >= A[p_first]) {
B[j] = A[my_first];
j --; my_first --;
} else {
B[j] = A[p_first];
j --; p_first --;
}
}
if (my_first < my_last)
for (; j >= p_last; j --, p_first --)
B[j] = A[p_first];
else
for (; j >= p_last; j --, my_first --)
B[j] = A[my_first];
} /* Merge_dec */
/*-------------------------------------------------------------------
* Function: Swap_list
* Purpose: Swaps pointers for array
* In/Out args: On input arrays are original arrays. On output, array pointers
* are swapped (curr becomes temp, temp becomes curr)
*
*/
void Swap_list(int **curr_arr, int **temp_arr) {
int *c = *curr_arr; // create pointer to curr_arr;
int *t = *temp_arr; // create pointer to temp_arr;
*curr_arr = t;
*temp_arr = c;
} /* Swap_list */
/*-------------------------------------------------------------------
* Function: Thread_work
* Purpose: Implement butterfly structured bitonic sort
* In arg: rank
* Global var: thread_count, barrier_thread_count, barrier_mutex,
* ok_to_proceed, n, x, temp, stage (for debug only)
* Return val: Ignored
*/
void *Thread_work(void* rank) {
long my_rank = (long) rank;
unsigned bitmask = 1;
unsigned bitmask2 = 0;
unsigned and_bit = 2;
int partner;
// Original sort of each thread's section of the array
qsort(&x[(my_rank * (n / thread_count))], (n / thread_count), sizeof(int),
cmpfunc);
// Lock to make sure every thread is done sorting before proceeding
pthread_mutex_lock(&barrier_mutex);
barrier_thread_count++;
if (barrier_thread_count == thread_count) {
# ifdef DEBUG
printf("List after local sort is: ");
Print_vector(x);
# endif
barrier_thread_count = 0;
pthread_cond_broadcast(&ok_to_proceed);
} else {
while (pthread_cond_wait(&ok_to_proceed,
&barrier_mutex) != 0);
}
pthread_mutex_unlock(&barrier_mutex);
while (bitmask < thread_count) {
bitmask2 = bitmask;
# ifdef DEBUG
stage = 1;
# endif
while (bitmask2 > 0) {
partner = my_rank ^ bitmask2;
if (my_rank < partner) { // Determines if thread merges or idle
if ((my_rank & and_bit) == 0) // Determine inc or dec merge
Merge_inc(x, temp, my_rank, partner);
else
Merge_dec(x, temp, my_rank, partner);
}
// Lock to make sure all threads have finished merging
pthread_mutex_lock(&barrier_mutex);
barrier_thread_count++;
if (barrier_thread_count == thread_count) {
barrier_thread_count = 0;
// Swap pointers with x and temp array
Swap_list(&x, &temp);
# ifdef DEBUG
printf("List after stage %d of %d-element butterfly is: ",
stage, and_bit);
Print_vector(x);
stage ++;
# endif
pthread_cond_broadcast(&ok_to_proceed);
} else {
while (pthread_cond_wait(&ok_to_proceed,
&barrier_mutex) != 0);
}
pthread_mutex_unlock(&barrier_mutex);
bitmask2 >>= 1;
}
bitmask <<= 1;
and_bit <<= 1;
}
return NULL;
} /* Thread_work */
/*-------------------------------------------------------------------
* Function: cmpfunc
* Purpose: c library qsort helper function
*/
int cmpfunc(const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
} /* cmpfunc */