-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHyperQuickSort.cpp
More file actions
230 lines (196 loc) · 5.82 KB
/
Copy pathHyperQuickSort.cpp
File metadata and controls
230 lines (196 loc) · 5.82 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
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <iostream>
#include <limits>
#include <math.h>
#include "quicksort_utils.cpp"
#include "hypercube_utils.cpp"
#include "mpi.h"
using namespace std;
void printBuffer (int list[], int length) {
std::cout << "[";
for (int i = 0; i < length; i++) {
std::cout << list[i];
if (i < length - 1) {
std::cout << ", ";
}
}
std::cout << "]";
}
int main (int argc, const char *argv[]) {
/**
* Initialize
*/
int length = atoi(argv[1]);
int rank, size, MASTER = 0;
int* sortMe;
bool IS_MASTER;
QuicksortUtils q_utils;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
IS_MASTER = rank == 0;
/**
* Check that the user initialized a hypercube
*/
int hypercubeDimensions = 1;
int sizee;
sizee = pow(2, hypercubeDimensions);
while (sizee < size) {
hypercubeDimensions += 1;
sizee = pow(2, hypercubeDimensions);
}
if (sizee != size) {
if (IS_MASTER) {
std::cout << "Invalid number of processes..." << endl;
std::cout << "We're running a hypercube here..." << endl;
}
MPI_Finalize();
exit(0);
}
/**
* Initialize hypercube utils
*/
HypercubeUtils h_utils(hypercubeDimensions);
/**
* Generate a large random array
*/
if (IS_MASTER) {
srand(time(NULL));
int intMax = std::numeric_limits<int>::max();
sortMe = (int *) malloc(length * sizeof(int));
for (int i = 0; i < length + 1; i++) {
sortMe[i] = rand();
}
std::cout << rank << ": Generated an array of " << length << " random integers between 0 and " << intMax << endl;
}
/**
* Initialize the timer
*/
struct timeval startTime, endTime;
suseconds_t timeElapsed;
if (IS_MASTER) {
std::cout << "Starting timer \n";
gettimeofday(&startTime, NULL);
}
/**
* Calculate how the list will be scattered
*/
int list_size = length / size;
int scatter_sizes[size];
int scatter_offsets[size];
int remainz = length % size;
for (int i = 0; i < size; i++) {
scatter_sizes[i] = list_size;
if (remainz > 0) {
scatter_sizes[i] += 1;
remainz -= 1;
}
if (i == 0) {
scatter_offsets[i] = 0;
} else {
scatter_offsets[i] = scatter_offsets[i-1] + scatter_sizes[i-1];
}
}
if (IS_MASTER) {
for (int i = 0; i < size; i++) {
cout << "Sending " << scatter_sizes[i] << " elements to process " << i << endl;
}
}
/**
* Scatter array
*/
int *currBuffer;
int currBufferSize = scatter_sizes[rank];
currBuffer = new int [currBufferSize];
MPI_Scatterv(sortMe, scatter_sizes, scatter_offsets, MPI_INT, currBuffer, currBufferSize, MPI_INT, MASTER, MPI_COMM_WORLD);
if (IS_MASTER) {
delete[] sortMe;
}
/**
* Iterate compare & exchange
*/
MPI_Comm currComm = MPI_COMM_WORLD;
for (int iteration = 1; iteration <= hypercubeDimensions; iteration++) {
// choose & broadcast pivot
int currentRank;
int pivot;
MPI_Comm_rank(currComm, ¤tRank);
if (currentRank == 0) {
pivot = q_utils.choosePivot(currBuffer, currBufferSize);
}
MPI_Bcast(&pivot, 1, MPI_INT, 0, currComm);
// split array
int midIndex, lowLen, highLen;
q_utils.split(currBuffer, currBufferSize, pivot, midIndex, lowLen, highLen);
// determine details for this communication
bool shouldPassLargerList = h_utils.shouldPassLargerList(iteration, rank);
int commLink = h_utils.getCommLink(iteration, rank);
int recvLen;
// send size
if (shouldPassLargerList) {
MPI_Sendrecv(&highLen, 1, MPI_INT, commLink, 0, &recvLen, 1, MPI_INT, commLink, 0, MPI_COMM_WORLD, NULL);
} else {
MPI_Sendrecv(&lowLen, 1, MPI_INT, commLink, 0, &recvLen, 1, MPI_INT, commLink, 0, MPI_COMM_WORLD, NULL);
}
// initialize new array
int keepLen = shouldPassLargerList ? lowLen : highLen;
int *recvBuffer;
recvBuffer = new int [recvLen + keepLen];
// send array
if (shouldPassLargerList) {
MPI_Sendrecv(&currBuffer[midIndex], highLen, MPI_INT, commLink, 1, recvBuffer, recvLen, MPI_INT, commLink, 1, MPI_COMM_WORLD, NULL);
} else {
MPI_Sendrecv(currBuffer, lowLen, MPI_INT, commLink, 1, recvBuffer, recvLen, MPI_INT, commLink, 1, MPI_COMM_WORLD, NULL);
}
// copy array
if (shouldPassLargerList) {
memcpy(&recvBuffer[recvLen], currBuffer, lowLen * sizeof(int));
} else {
memcpy(&recvBuffer[recvLen], &currBuffer[midIndex], highLen * sizeof(int));
}
delete[] currBuffer;
currBuffer = recvBuffer;
currBufferSize = recvLen + keepLen;
// split communicator
MPI_Comm nextComm;
int nextGroup = h_utils.getNextGroup(iteration, rank);
MPI_Comm_split(currComm, nextGroup, rank, &nextComm);
currComm = nextComm;
}
/**
* Perform sequential quicksort
*/
q_utils.sort(currBuffer, currBufferSize);
/**
* Gather all values
*/
int *gatherSizes, *displacements;
if (IS_MASTER) {
gatherSizes = (int*) malloc(size * sizeof(int));
displacements = (int*) malloc(size * sizeof(int));
}
MPI_Gather(&currBufferSize, 1, MPI_INT, gatherSizes, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
if (IS_MASTER) {
displacements[0] = 0;
for (int i = 1; i < size; i++) {
displacements[i] = displacements[i-1] + gatherSizes[i-1];
}
sortMe = (int*) malloc(length * sizeof(int));
}
MPI_Gatherv(currBuffer, currBufferSize, MPI_INT, sortMe, gatherSizes, displacements, MPI_INT, MASTER, MPI_COMM_WORLD);
/**
* Finish timer
*/
MPI_Barrier(MPI_COMM_WORLD);
if (IS_MASTER) {
gettimeofday(&endTime, NULL);
timeElapsed = ((endTime.tv_sec - startTime.tv_sec) * 1000) + ((endTime.tv_usec - startTime.tv_usec) / 1000);
std::cout << "Finished parallel quicksort in " << timeElapsed << " milliseconds\n";
}
/**
* Close up MPI
*/
MPI_Finalize();
}