-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysort.cpp
More file actions
434 lines (383 loc) · 12 KB
/
Copy pathmysort.cpp
File metadata and controls
434 lines (383 loc) · 12 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* mysort.cpp
* sorting integers based on bucket sort, using bar and lock arguments
* * that choose which barrier and lock to use
* Upon sorting, all integers will be saved to argument specified file
* Final check of sorted array will compare against sort() algo
*/
#define _POSIX_C_SOURCE 200112L
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <atomic>
#include <mutex>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include "pthread_add.h" // for when running on macOS
#include "arg_parser.h"
#define SEQ_CST memory_order_seq_cst
#define RELAXED memory_order_relaxed
using namespace std;
pthread_t* threads;
// size_t* args;
size_t NUM_THREADS = 0;
pthread_barrier_t bar;
pthread_mutex_t mutexLock;
pthread_mutexattr_t mutexAttr;
atomic<int> sense (0), cnt (0), count (0), b_count (0);
atomic<int> next_num (0), now_serving (0);
atomic<bool> tas_flag (false);
vector<int> arr, arrCheck;
int arraysize = 0;
/* execution time struct */
typedef chrono::high_resolution_clock Clock;
/* bar functions */
void sense_wait();
void *bucketSort_sense(void *);
void *bucketSort_bar_pthread(void *);
/* lock functions */
static bool tas();
static void tas_lock();
static void tas_unlock();
void *bucketSort_TAS(void *arg);
void ttas_lock();
void *bucketSort_TTAS(void *arg);
void ticket_lock();
void ticket_unlock();
void *bucketSort_ticket_lock(void *arg);
void *bucketSort_lock_pthread(void *arg);
/* bucket sort functions */
void bucketSort_sense_fn(int low, int high);
void bucketSort_bar_pthread_fn(int low, int high);
void bucketSort_TAS_fn(int low, int high);
void bucketSort_TTAS_fn(int low, int high);
void bucketSort_ticket_lock_fn(int low, int high);
void bucketSort_lock_pthread_fn(int low, int high);
/* array checker */
void arrayCheck(int asize, vector<int> a1, vector<int> aCheck2);
/* ===================== MAIN ==================== */
int main(int argc, const char* argv[]){
struct arg_params args_parsed = arg_parser(argc, argv);
string inputFile = args_parsed.inputFile;
string outputFile = args_parsed.outputFile;
NUM_THREADS = args_parsed.NUM_THREADS;
// string algorithm = args_parsed.algorithm;
int argument = args_parsed.argument;
// init memory allocations
threads = static_cast<pthread_t*>(malloc(NUM_THREADS*sizeof(pthread_t)));
// args = static_cast<size_t*>(malloc(NUM_THREADS*sizeof(size_t)));
// read in array of integers from input file
fstream file(inputFile.c_str(), ios_base::in);
int a, b = 0;
string line;
while (getline(file, line)) arraysize++;
arr.resize(arraysize);
arrCheck.resize(arraysize);
fstream infile(inputFile, ios_base::in);
while (infile >> a) {
arr[b++] = a;
}
arrCheck = arr;
// execution start time
auto start_time = Clock::now();
/* argument statement (from parser) is as follows:
* barrier: 1-sense, 2-pthread
* lock: 3-tas, 4-ttas, 5-ticket, 6-pthread */
switch (argument) {
// barrier sense
case 1:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, bucketSort_sense, (void*)i);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// barrier pthread
case 2:
pthread_barrier_init(&bar, NULL, NUM_THREADS);
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, bucketSort_bar_pthread, (void*)i);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
pthread_barrier_destroy(&bar);
break;
// lock tas
case 3:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, bucketSort_TAS, (void*)i);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// lock ttas
case 4:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, bucketSort_TTAS, (void*)i);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// lock ticket
case 5:
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, bucketSort_ticket_lock, (void*)i);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
break;
// lock pthread
case 6:
pthread_mutex_init(&mutexLock, NULL);
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&threads[i], NULL, bucketSort_lock_pthread, (void*)i);
for (int i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL);
pthread_mutex_destroy(&mutexLock);
break;
// something didn't match up
default:
printf("An error occured in main argument switch.");
exit(-1);
}
// execute a final sort
sort(arr.begin(), arr.end());
// execution end time
auto end_time = Clock::now();
// unsigned int 4,294,967,295, which is only 4.3 seconds
// unsigned long, plan on never running out (over 5 centuries)
unsigned long time_spent = chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
// printf("Time elapsed is %lu nanoseconds\n", time_spent);
// printf(" %f seconds\n", time_spent/1e9);
/* check to make sure the array is sorted as expected */
sort(arrCheck.begin(), arrCheck.end()); // sort
arrayCheck(arraysize, arr, arrCheck); // check against bucketsort
/* WRITE SORTED ARRAY TO FILE */
ofstream outfile;
outfile.open(outputFile);
for (int i = 0; i < arraysize; i++) outfile << arr[i] << endl;
outfile.close();
// free dynamic memory
free(threads);
// free(args);
}
/* ************************************************************** */
/* ************************* FUNCTIONS ************************** */
/* ************************************************************** */
/* *************** BAR FUNCTIONS *************** */
/* ****** sense ****** */
void sense_wait() {
thread_local bool cur_sense=0;
if (cur_sense==0) cur_sense=1;
else cur_sense=0;
int cnt_copy = atomic_fetch_add(&cnt, 1);
if (cnt_copy == NUM_THREADS - 1) {
cnt.store(0, RELAXED);
sense.store(cur_sense, SEQ_CST);
} else while(sense.load(SEQ_CST) != cur_sense);
}
void *bucketSort_sense(void *arg) {
int tid = (int)(size_t)arg;
int low = (tid * arraysize) / NUM_THREADS;
int high = (((tid + 1) * arraysize) / NUM_THREADS) - 1;
bucketSort_sense_fn(low, high);
return 0;
}
void bucketSort_sense_fn(int low, int high){
int max_value = 0;
// find max key value in arr
for (int i = low; i <= high; i++)
if (arr[i] > max_value) max_value = arr[i];
// create n empty local buckets
auto buckets = vector<unsigned >(static_cast<unsigned int>(max_value + 1));
// make sure all threads are here
sense_wait();
// put array elements in different buckets
for (int i = low; i <= high; i++)
buckets[arr[i]]++;
// and then make sure threads are here
sense_wait();
// concatenate all buckets into arr[]
for (int i = 0; i < buckets.capacity(); i++) {
for (int j = 0; j < buckets[i]; j++) {
arr[b_count++] = i;
// b_count++;
}
}
}
/* ****** pthread ****** */
void *bucketSort_bar_pthread(void *arg) {
int tid = (int)(size_t)arg;
int low = (tid * arraysize) / NUM_THREADS;
int high = (((tid + 1) * arraysize) / NUM_THREADS) - 1;
bucketSort_bar_pthread_fn(low, high);
return 0;
}
void bucketSort_bar_pthread_fn(int low, int high){
int max_value = 0;
// find max key value in arr
for (int i = low; i <= high; i++)
if (arr[i] > max_value) max_value = arr[i];
// create n empty local buckets
auto buckets = vector<unsigned >(static_cast<unsigned int>(max_value + 1));
pthread_barrier_wait(&bar);
// put array elements in different buckets
for (int i = low; i <= high; i++)
buckets[arr[i]]++;
pthread_barrier_wait(&bar);
// concatenate all buckets into arr[]
for (int i = 0; i < buckets.capacity(); i++) {
for (int j = 0; j < buckets[i]; j++) {
arr[b_count++] = i;
}
}
}
/* *************** LOCK FUNCTIONS *************** */
/* ****** tas ****** */
static bool tas() {
if (tas_flag == false){
tas_flag.store(true, SEQ_CST);
return tas_flag;
} else return false;
}
static void tas_lock() {
while(tas() == false) {}
}
static void tas_unlock() {
tas_flag.store(false, SEQ_CST);
}
void *bucketSort_TAS(void *arg) {
int tid = (int)(size_t)arg;
int low = (tid * arraysize) / NUM_THREADS;
int high = (((tid + 1) * arraysize) / NUM_THREADS) - 1;
bucketSort_TAS_fn(low, high);
return 0;
}
void bucketSort_TAS_fn(int low, int high){
int max_value = 0;
// find max key value in arr
for (int i = low; i <= high; i++)
if (arr[i] > max_value) max_value = arr[i];
// create n empty local buckets
auto buckets = vector<unsigned >(static_cast<unsigned int>(max_value + 1));
// put array elements in different buckets
for (int i = low; i <= high; i++)
buckets[arr[i]]++;
tas_lock();
// concatenate all buckets into arr[]
for (int i = 0; i < buckets.capacity(); i++) {
for (int j = 0; j < buckets[i]; j++) {
arr[b_count++] = i;
tas_unlock();
tas_lock();
}
}
tas_unlock();
}
/* ****** ttas ****** */
void ttas_lock() {
while(tas_flag.load(SEQ_CST))
if(tas() == false) return;
}
void *bucketSort_TTAS(void *arg) {
int tid = (int)(size_t)arg;
int low = (tid * arraysize) / NUM_THREADS;
int high = (((tid + 1) * arraysize) / NUM_THREADS) - 1;
bucketSort_TTAS_fn(low, high);
return 0;
}
void bucketSort_TTAS_fn(int low, int high){
int max_value = 0;
// find max key value in arr
for (int i = low; i <= high; i++)
if (arr[i] > max_value) max_value = arr[i];
// create n empty local buckets
auto buckets = vector<unsigned >(static_cast<unsigned int>(max_value + 1));
// put array elements in different buckets
for (int i = low; i <= high; i++)
buckets[arr[i]]++;
ttas_lock();
// concatenate all buckets into arr[]
for (int i = 0; i < buckets.capacity(); i++) {
for (int j = 0; j < buckets[i]; j++) {
arr[b_count++] = i;
tas_unlock();
ttas_lock();
}
}
tas_unlock();
}
/* ****** ticket ****** */
void ticket_lock() {
// fai is fetch and increment, but not in C++?
int num = atomic_fetch_add(&next_num, 1);
while(now_serving.load(SEQ_CST) != num);
}
void ticket_unlock() {
atomic_fetch_add(&now_serving, 1);
}
void *bucketSort_ticket_lock(void *arg) {
int tid = (int)(size_t)arg;
int low = (tid * arraysize) / NUM_THREADS;
int high = (((tid + 1) * arraysize) / NUM_THREADS) - 1;
bucketSort_ticket_lock_fn(low, high);
return 0;
}
void bucketSort_ticket_lock_fn(int low, int high){
int max_value = 0;
// find max key value in arr
for (int i = low; i <= high; i++)
if (arr[i] > max_value) max_value = arr[i];
// create n empty local buckets
auto buckets = vector<unsigned >(static_cast<unsigned int>(max_value + 1));
// put array elements in different buckets
for (int i = low; i <= high; i++)
buckets[arr[i]]++;
ticket_lock();
// concatenate all buckets into arr[]
for (int i = 0; i < buckets.capacity(); i++) {
for (int j = 0; j < buckets[i]; j++) {
arr[b_count++] = i;
ticket_unlock();
ticket_lock();
}
}
ticket_unlock();
}
/* ****** pthread ****** */
void *bucketSort_lock_pthread(void* arg) {
int tid = (int)(size_t)arg;
int low = (tid * arraysize) / NUM_THREADS;
int high = (((tid + 1) * arraysize) / NUM_THREADS) - 1;
bucketSort_lock_pthread_fn(low, high);
return 0;
}
void bucketSort_lock_pthread_fn(int low, int high) {
int max_value = 0;
// find max key value in arr
for (int i = low; i <= high; i++)
if (arr[i] > max_value) max_value = arr[i];
// create n empty local buckets
auto buckets = vector<unsigned >(static_cast<unsigned int>(max_value + 1));
// put array elements in different buckets
for (int i = low; i <= high; i++)
buckets[arr[i]]++;
// concatenate all buckets into arr[]
for (int i = 0; i < buckets.capacity(); i++) {
for (int j = 0; j < buckets[i]; j++) {
pthread_mutex_lock(&mutexLock);
arr[b_count++] = i;
pthread_mutex_unlock(&mutexLock);
}
}
}
// check if array sorted correctly - by comparing two arrays (one sorted with sort())
void arrayCheck(int asize, vector<int> a1, vector<int> aCheck2) {
for (int i = 0; i < asize; i++) {
if (a1[i] != aCheck2[i]) {
printf("Array is incorrectly sorted!\nbucket_sort(%i) != sort(%i) at position [%i]\n", a1[i], aCheck2[i], i);
return;
}
}
}