forked from gary-funck/parallel-merge-sort
/
hybrid_mergesort.c
314 lines (299 loc) · 8.49 KB
/
hybrid_mergesort.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* Hybrid MPI+OpenMP parallel recursive mergesort
Copyright (C) 2011 Atanas Radenski
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include <omp.h>
// Arrays size <= SMALL switches to insertion sort
#define SMALL 32
extern double get_time (void);
void merge (int a[], int size, int temp[]);
void insertion_sort (int a[], int size);
void mergesort_serial (int a[], int size, int temp[]);
void mergesort_parallel_mpi (int a[], int size, int temp[],
int level, int my_rank, int max_rank,
int tag, MPI_Comm comm, int threads);
int topmost_level_mpi (int my_rank);
void run_root_mpi (int a[], int size, int temp[], int max_rank, int tag,
MPI_Comm comm, int threads);
void run_node_mpi (int my_rank, int max_rank, int tag, MPI_Comm comm,
int threads);
void mergesort_parallel_omp (int a[], int size, int temp[], int threads);
int main (int argc, char *argv[]);
int
main (int argc, char *argv[])
{
// All processes
MPI_Init (&argc, &argv);
// Enable nested parallelism, if available
omp_set_nested (1);
// Check processes and their ranks
// number of processes == communicator size
int comm_size;
MPI_Comm_size (MPI_COMM_WORLD, &comm_size);
int my_rank;
MPI_Comm_rank (MPI_COMM_WORLD, &my_rank);
int max_rank = comm_size - 1;
int tag = 123;
// Check arguments
if (argc != 3) /* argc must be 3 for proper execution! */
{
if (my_rank == 0)
{
printf ("Usage: %s array-size OMP-threads-per-MPI-process>0\n",
argv[0]);
}
MPI_Abort (MPI_COMM_WORLD, 1);
}
// Get arguments
int size = atoi (argv[1]); // Array size
int threads = atoi (argv[2]); // Requested number of threads per node
if (threads < 1)
{
if (my_rank == 0)
{
printf
("Error: requested %d threads per MPI process, must be at least 1\n",
threads);
}
MPI_Abort (MPI_COMM_WORLD, 1);
}
// Set test data
if (my_rank == 0)
{ // Only root process sets test data
puts
("-Multilevel parallel Recursive Mergesort with MPI and OpenMP-\t");
printf ("Array size = %d\nProcesses = %d\nThreads per process = %d\n",
size, comm_size, threads);
// Check nested parallelism availability
if (omp_get_nested () != 1)
{
puts ("Warning: Nested parallelism desired but unavailable");
}
// Array allocation
int *a = malloc (sizeof (int) * size);
int *temp = malloc (sizeof (int) * size);
if (a == NULL || temp == NULL)
{
printf ("Error: Could not allocate array of size %d\n", size);
MPI_Abort (MPI_COMM_WORLD, 1);
}
// Random array initialization
srand (314159);
int i;
for (i = 0; i < size; i++)
{
a[i] = rand () % size;
}
// Sort with root process
double start = get_time ();
run_root_mpi (a, size, temp, max_rank, tag, MPI_COMM_WORLD, threads);
double end = get_time ();
printf ("Start = %.2f\nEnd = %.2f\nElapsed = %.2f\n",
start, end, end - start);
// Result check
for (i = 1; i < size; i++)
{
if (!(a[i - 1] <= a[i]))
{
printf ("Implementation error: a[%d]=%d > a[%d]=%d\n", i - 1,
a[i - 1], i, a[i]);
MPI_Abort (MPI_COMM_WORLD, 1);
}
}
} // Root process end
else
{ // Node processes
run_node_mpi (my_rank, max_rank, tag, MPI_COMM_WORLD, threads);
}
fflush (stdout);
MPI_Finalize ();
return 0;
}
// Root process code
void
run_root_mpi (int a[], int size, int temp[], int max_rank, int tag,
MPI_Comm comm, int threads)
{
int my_rank;
MPI_Comm_rank (comm, &my_rank);
if (my_rank != 0)
{
printf
("Error: run_root_mpi called from process %d; must be called from process 0 only\n",
my_rank);
MPI_Abort (MPI_COMM_WORLD, 1);
}
mergesort_parallel_mpi (a, size, temp, 0, my_rank, max_rank, tag, comm, threads); // level=0; my_rank=root_rank=0
return;
}
// Node process code
void
run_node_mpi (int my_rank, int max_rank, int tag, MPI_Comm comm, int threads)
{
// Probe for a message and determine its size and sender
MPI_Status status;
int size;
MPI_Probe (MPI_ANY_SOURCE, tag, comm, &status);
MPI_Get_count (&status, MPI_INT, &size);
int parent_rank = status.MPI_SOURCE;
// Allocate int a[size], temp[size]
int *a = malloc (sizeof (int) * size);
MPI_Recv (a, size, MPI_INT, parent_rank, tag, comm, &status);
// Send sorted array to parent process
MPI_Send (a, size, MPI_INT, parent_rank, tag, comm);
return;
}
// Given a process rank, calculate the top level of the process tree in which the process participates
// Root assumed to always have rank 0 and to participate at level 0 of the process tree
int
topmost_level_mpi (int my_rank)
{
int level = 0;
while (pow (2, level) <= my_rank)
level++;
return level;
}
// MPI merge sort
void
mergesort_parallel_mpi (int a[], int size, int temp[],
int level, int my_rank, int max_rank,
int tag, MPI_Comm comm, int threads)
{
int helper_rank = my_rank + pow (2, level);
if (helper_rank > max_rank)
{ // no more MPI processes available, then use OpenMP
mergesort_parallel_omp (a, size, temp, threads);
// Was: mergesort_serial(a, size, temp);
}
else
{
MPI_Request request;
MPI_Status status;
// Send second half, asynchronous
MPI_Isend (a + size / 2, size - size / 2, MPI_INT, helper_rank, tag,
comm, &request);
// Sort first half with OpenMP
// mergesort_parallel_omp(a, size/2, temp, threads);
mergesort_parallel_mpi (a, size / 2, temp, level + 1, my_rank, max_rank,
tag, comm, threads);
// Free the async request (matching receive will complete the transfer).
MPI_Request_free (&request);
// Receive second half sorted
MPI_Recv (a + size / 2, size - size / 2, MPI_INT, helper_rank, tag,
comm, &status);
// Merge the two sorted sub-arrays through temp
merge (a, size, temp);
}
return;
}
// OpenMP merge sort with given number of threads
void
mergesort_parallel_omp (int a[], int size, int temp[], int threads)
{
if (threads == 1)
{
//printf("Thread %d begins serial mergesort\n", omp_get_thread_num());
mergesort_serial (a, size, temp);
}
else if (threads > 1)
{
#pragma omp parallel sections
{
#pragma omp section
mergesort_parallel_omp (a, size / 2, temp, threads / 2);
#pragma omp section
mergesort_parallel_omp (a + size / 2, size - size / 2,
temp + size / 2, threads - threads / 2);
}
// Thread allocation is implementation dependent
// Some threads can execute multiple sections while others are idle
// Merge the two sorted sub-arrays through temp
merge (a, size, temp);
}
else
{
printf ("Error: %d threads\n", threads);
return;
}
}
void
mergesort_serial (int a[], int size, int temp[])
{
// Switch to insertion sort for small arrays
if (size <= SMALL)
{
insertion_sort (a, size);
return;
}
mergesort_serial (a, size / 2, temp);
mergesort_serial (a + size / 2, size - size / 2, temp);
// Merge the two sorted subarrays into a temp array
merge (a, size, temp);
}
void
merge (int a[], int size, int temp[])
{
int i1 = 0;
int i2 = size / 2;
int tempi = 0;
while (i1 < size / 2 && i2 < size)
{
if (a[i1] < a[i2])
{
temp[tempi] = a[i1];
i1++;
}
else
{
temp[tempi] = a[i2];
i2++;
}
tempi++;
}
while (i1 < size / 2)
{
temp[tempi] = a[i1];
i1++;
tempi++;
}
while (i2 < size)
{
temp[tempi] = a[i2];
i2++;
tempi++;
}
// Copy sorted temp array into main array, a
memcpy (a, temp, size * sizeof (int));
}
void
insertion_sort (int a[], int size)
{
int i;
for (i = 0; i < size; i++)
{
int j, v = a[i];
for (j = i - 1; j >= 0; j--)
{
if (a[j] <= v)
break;
a[j + 1] = a[j];
}
a[j + 1] = v;
}
}