-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelBitonicSort.c
More file actions
315 lines (172 loc) · 6.29 KB
/
ParallelBitonicSort.c
File metadata and controls
315 lines (172 loc) · 6.29 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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>
#include <time.h>
struct boundary {
int start;
int end;
};
void singlemergesort(int* a, int p, int r);
int* merge(int* vet, int start, int center, int end);
void coEx(int* a, struct boundary b1, struct boundary b2);
void printVector(int* a, int start, int end);
int getMaxThreads();
#define max 65535 //massimo valore raggiungibile da un elmento di un vettore
#define dimension 100000 //massima dimensione del vettore
int main()
{
printf("Benvenuto. Questo Software effettua il BitonicSort su N numeri utilizzando piu core.\n");
printf("Attenzione: il numero di core utilizzati e una potenza di 2. Verra calcolato il massimo numero di core utilizzabili tra quelli messi a disposizione per ottenere un risultato corretto\n");
srand(time(NULL));
int dim, t;
int nproc = getMaxThreads();
printf("Numero di thread realmente usati: %d\n", nproc);
//Struttura che memorizzerá l'inizio e la fine di ogni sezione del vettore
struct boundary* positions = (struct boundary*)malloc(nproc * sizeof(struct boundary));
printf("Inserire dimensione array: ");
scanf_s("%d", &dim);
int* vet = (int*)malloc(dim * sizeof(int));
for (int i = 0; i < dim; i++) {
vet[i] = rand() % max + 1;
}
printf("Vettore disordinato:\n");
printVector(vet, 0, dim - 1);
printf("\n");
double t0, t1, t2;
int start, center, end;
//ordinamento dei p vettori di dimensione n/p con il merge sort
t0 = omp_get_wtime();
#pragma omp parallel num_threads(nproc) private(start,center,end)
{
t = omp_get_num_threads();
int nloc = dim / t;
int r = dim % t;
int id = omp_get_thread_num();
int step;
//gestisco il caso della non esatta divisibilitá del vettore
if (id < r) {
nloc++;
step = 0;
}
else { step = r; }
start = nloc * id + step;
end = start + nloc - 1;
positions[id].start = start;
positions[id].end = end;
//Ogni processore effettua il mergeSort sulla sua sezione di vettore
singlemergesort(vet, start, end);
}
t1 = omp_get_wtime();
//t1-t0 : tempo per effettuare l'ordinamento dei p vettori
//qui inizia implementazione del bitonic sort
int ibit, jbit, proc, menum, r;
int m = log2(t);
#pragma omp parallel num_threads(nproc) shared(m) private(ibit,jbit,proc,menum,r)
{
menum = omp_get_thread_num();
int i,j;
for ( i = 1; i <= m; i++) {
ibit = (menum >> i) & 1;
for ( j = i - 1; j >= 0; j--) {
jbit = (menum >> j) & 1;
int potenza = pow(2, j + 1);
r = menum % potenza;
if (r < pow(2, j)) {
proc = menum + pow(2, j);
}
else { proc = menum - pow(2, j); }
if (ibit == jbit) {
//sono un processore basso, effettuando tutti gli scambi con coEx non c'é bisogno di un else
coEx(vet, positions[menum], positions[proc]);
}
#pragma omp barrier
//dopo aver effettuato tutti gli scambi ogni sezione del vettore viene ri-ordinata
singlemergesort(vet, positions[menum].start, positions[menum].end);
#pragma omp barrier
}
}
}
t2 = omp_get_wtime();
//t2-t1 : tempo per ordinare completamente il vettore con il BitonicSort
printf("\nTotal time: %f (s)\nTime for mergeSort: %f (s)\nTime for bitonicSort: %f (s)\n", t2 - t0, t1 - t0, t2 - t1);
printf("\nVettore: ");
printVector(vet, 0, dim - 1);
return 0;
}
//funzione che unisce 2 vettori ordinati
int* merge(int a[], int p, int q, int r) {
int i, j, k = 0;
int b[dimension];
i = p;
j = q + 1;
while (i <= q && j <= r) {
if (a[i] < a[j]) {
b[k] = a[i];
i++;
}
else {
b[k] = a[j];
j++;
}
k++;
}
while (i <= q) {
b[k] = a[i];
i++;
k++;
}
while (j <= r) {
b[k] = a[j];
j++;
k++;
}
for (k = p; k <= r; k++) {
a[k] = b[k - p];
}
return a;
}
//funzione che richiama il mergeSort (sequenziale)
void singlemergesort(int* a, int p, int r) {
int q;
if (p < r) {
q = (p + r) / 2;
singlemergesort(a, p, q);
singlemergesort(a, q + 1, r);
merge(a, p, q, r);
}
return;
}
//metodo per effettuare la CoExLo e la CoExHi
void coEx(int* a, struct boundary b1, struct boundary b2) {
//scambio tutti gli elementi finché il processore "basso" non ha tutti gli elementi piú piccoli e quello "alto quelli piú grandi"
int indexmax = b1.end;
int indexmin = b2.start;
while (a[indexmax] > a[indexmin] && indexmax >= b1.start && indexmin <= b2.end) {
a[indexmax] = a[indexmax] ^ a[indexmin];
a[indexmin] = a[indexmin] ^ a[indexmax];
a[indexmax] = a[indexmax] ^ a[indexmin];
if (a[indexmax] < a[indexmax - 1]) {
indexmax--;
}
if (a[indexmin] > a[indexmin + 1]) {
indexmin++;
}
}
}
//Funzione di utility per stampare il vettore
void printVector(int* a, int start, int end) {
for (int i = start; i <= end; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
//funzione per calcolare il massimo numero di core utilizzabili
int getMaxThreads() {
int t = omp_get_max_threads()+10;
int i = 1;
while (pow(2, i) <= t) {
i++;
}
return pow(2, i - 1);
}