-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpth_msort.c
300 lines (256 loc) · 7.47 KB
/
pth_msort.c
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
// Include your C header files here
#include "pth_msort.h"
#include <stdlib.h>
#include <stdio.h>
unsigned int size;
// define a struct to package sort module arguments
struct SortArgs
{
int* data;
unsigned int lower;
unsigned int upper;
};
// define a struct to package merge module arguments
struct MergeArgs
{
int* data;
unsigned int lower;
unsigned int upper;
};
// define a struct to package parallel merge module arguments
struct PMergeArgs
{
int* data;
int* sorted;
unsigned int* partitions;
unsigned char part;
};
// swap function used in quickSort algorithm
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
// partition function used in quickSort algorithm
int partition(int* data, int low, int high)
{
int pivot = data[high];
int i = (low- 1);
int j;
for (j = low; j <= high - 1; j++) {
if (data[j] < pivot) {
i++;
swap(&data[i], &data[j]);
}
}
swap(&data[i + 1], &data[high]);
return (i + 1);
}
// quickSort algorithm
void quickSort(int* data, int low, int high)
{
if (low < high)
{
int pi = partition(data, low, high);
quickSort(data, low, pi - 1);
quickSort(data, pi + 1, high);
}
}
// sort module
void* SortModule(void* arguments)
{
struct SortArgs* args = (struct SortArgs*) arguments;
quickSort(args->data, args->lower, args->upper);
}
// merge algorithm
void Merge(int* data, unsigned int lower, unsigned int middle, unsigned int upper)
{
// define loop counters
unsigned int i, j, k;
// determine the size of data parts
int n1 = middle - lower + 1;
int n2 = upper - middle;
// define a variable to keep the lower part of data
int* l_data = (int*) malloc(n1*sizeof(int));
// define a variable to keep the upper part of data
int* u_data = (int*) malloc(n2*sizeof(int));
// move the lower part of the data to l_data
for(i = 0; i < n1; i++)
l_data[i] = data[lower + i];
// move the upper part of the data to u_data
for(j = 0; j < n2; j++)
u_data[j] = data[middle + 1 + j];
// main merge algorithm
i = 0;
j = 0;
k = lower;
while (i < n1 && j < n2)
{
if (l_data[i] <= u_data[j])
data[k++] = l_data[i++];
else
data[k++] = u_data[j++];
}
while (i < n1)
data[k++] = l_data[i++];
while (j < n2)
data[k++] = u_data[j++];
// set allocated space free
free(l_data);
free(u_data);
}
// merge module
void* MergeModule(void* arguments)
{
struct MergeArgs* args = (struct MergeArgs*) arguments;
unsigned int middle = args->lower + (args->upper - args->lower)/2;
Merge(args->data, args->lower, middle, args->upper);
}
// binary search algorithms
unsigned int binarySearchCount(int* data, unsigned int upper, int x)
{
unsigned int count = 0, mid = 0, lower = 0;
while (lower <= upper)
{
mid = (upper + lower) / 2;
if (data[mid] <= x)
{
count = mid + 1;
lower = mid + 1;
}
else
upper = mid - 1;
}
return count;
}
// find partitions of data for parallel merge
void FindPartitions(int* data, unsigned int* partitions)
{
// find the array length
unsigned int M = size/2;
// define a loop counter
unsigned int i;
// split the data
int* u_data = (int*) malloc(M*sizeof(int));
// move the lower part of the data to l_data
for(i = 0; i < M; i++)
u_data[i] = data[M + i];
// find partitions
partitions[0] = 0;
partitions[1] = binarySearchCount(u_data, M-1, data[(M/4) - 1]);
partitions[2] = binarySearchCount(u_data, M-1, data[(M/2) - 1]);
partitions[3] = binarySearchCount(u_data, M-1, data[(3*M/4) - 1]);
partitions[4] = M;
// set allocated space free
free(u_data);
}
// parallel merge algorithm
void PMerge(int* data, int* sorted, unsigned int* partitions, unsigned char part)
{
// define loop counters
unsigned int i, j, k;
// determine array sizes
int n1 = size/8;
int n2 = partitions[part+1] - partitions[part];
// move the lower part of the data to l_data
int sp1 = part*n1;
// main merge algorithm
if ( n2 == 0 )
{
for(i = 0; i < n1; i++)
sorted[sp1 + partitions[part] + i] = data[sp1 + i];
}
else
{
i = 0;
j = 0;
k = 0;
while (i < n1 && j < n2)
{
if (data[sp1+i] <= data[size/2+partitions[part]+j])
{
sorted[sp1 + partitions[part]+k] = data[sp1+i];
i++;
}
else
{
sorted[sp1 + partitions[part]+k] = data[size/2+partitions[part]+j];
j++;
}
k++;
}
while (i < n1)
{
sorted[sp1 + partitions[part]+k] = data[sp1+i];
i++;
k++;
}
while (j < n2)
{
sorted[sp1 + partitions[part]+k] = data[size/2+partitions[part]+j];
j++;
k++;
}
}
}
// parallel merge module
void* PMergeModule(void* arguments)
{
struct PMergeArgs* args = (struct PMergeArgs*) arguments;
PMerge(args->data, args->sorted, args->partitions, args->part);
}
// main code
void mergeSortParallel(const int* values, unsigned int N, int* sorted)
{
// define variables to keep data parts
int* data = (int*) values;
// define four thread handles
pthread_t* handles = (pthread_t*) malloc (4*sizeof(pthread_t));
// define a loop counter
unsigned char i;
// define a variable to keep partitions
unsigned int* partitions = (unsigned int*) malloc (5*sizeof(unsigned int));;
// fill global variables
size = N;
unsigned int hsize = N/2;
unsigned int qsize = N/4;
// call 4 threads where each thread calls a sort module
struct SortArgs arguments1 = {data, 0, qsize - 1};
struct SortArgs arguments2 = {data, qsize, hsize - 1};
struct SortArgs arguments3 = {data, hsize, 3*qsize - 1};
struct SortArgs arguments4 = {data, 3*qsize, N - 1};
pthread_create(&handles[0], NULL, SortModule, (void*)(&arguments1));
pthread_create(&handles[1], NULL, SortModule, (void*)(&arguments2));
pthread_create(&handles[2], NULL, SortModule, (void*)(&arguments3));
pthread_create(&handles[3], NULL, SortModule, (void*)(&arguments4));
// wait for called threads to finish
for (i=0; i<4; i++)
pthread_join(handles[i], NULL);
// call 2 threads where each thread calls a merge module
struct MergeArgs arguments5 = {data, 0, hsize - 1};
struct MergeArgs arguments6 = {data, hsize, N - 1};
pthread_create(&handles[0], NULL, MergeModule, (void*)(&arguments5));
pthread_create(&handles[1], NULL, MergeModule, (void*)(&arguments6));
// wait for called threads to finish
for (i=0; i<2; i++)
pthread_join(handles[i], NULL);
// find partitions of parallel merge algorithm
FindPartitions(data, partitions);
// call 4 threads to do the final merge
struct PMergeArgs pArg0 = {data, sorted, partitions, 0};
struct PMergeArgs pArg1 = {data, sorted, partitions, 1};
struct PMergeArgs pArg2 = {data, sorted, partitions, 2};
struct PMergeArgs pArg3 = {data, sorted, partitions, 3};
pthread_create(&handles[0], NULL, PMergeModule, (void*)(&pArg0));
pthread_create(&handles[1], NULL, PMergeModule, (void*)(&pArg1));
pthread_create(&handles[2], NULL, PMergeModule, (void*)(&pArg2));
pthread_create(&handles[3], NULL, PMergeModule, (void*)(&pArg3));
// wait for called threads to finish
for (i=0; i<4; i++)
pthread_join(handles[i], NULL);
// set allocated space free
free(partitions);
// set thread handles free
free(handles);
}