-
Notifications
You must be signed in to change notification settings - Fork 0
/
ots.cpp
284 lines (243 loc) · 5.55 KB
/
ots.cpp
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
/**
* VUT FIT PRL 2020 Project - Odd-Even Transposition Sort.
*
* @author Dominik Harmim <harmim6@gmail.com>
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include <mpi.h>
#ifdef DEBUG
#include <chrono>
#endif
#define COMM MPI_COMM_WORLD /// Default MPI communicator.
#define TAG 0 /// An MPI tag used for the transmission of messages with numbers.
#define MASTER 0 /// A rank of the master process.
#define NUMBERS_FILE "numbers" /// A name of the input file with numbers.
using namespace std;
/**
* Prints an error message due to an MPI error and terminates the program with
* an erroneous exit code.
*/
auto MPI_error() -> void
{
cerr << "Error: an MPI library call has failed." << endl;
MPI_Abort(COMM, EXIT_FAILURE);
}
/**
* The master process reads numbers from the input file, prints them to the
* standard output, and sends them to the others processes.
*
* @param rank A rank of a process.
* @return Returns the number of numbers that have been read.
*/
auto read_numbers(const int rank) -> size_t
{
if (rank != MASTER)
{
return 0;
}
ifstream numbers(NUMBERS_FILE);
if (!numbers.is_open() || numbers.bad())
{
cerr << "Error: could not read the input file." << endl;
MPI_Abort(COMM, EXIT_FAILURE);
}
size_t numbers_count = 0;
for (int r = MASTER; !numbers.eof(); numbers_count = ++r)
{
const int number = numbers.get();
if (numbers.eof())
{
break;
}
else if (r != MASTER)
{
cout << " ";
}
cout << number;
if (MPI_Send(&number, 1, MPI_INT, r, TAG, COMM))
{
MPI_error();
}
}
cout << endl;
return numbers_count;
}
/**
* Receives a number from the master process.
*
* @return Returns the received number.
*/
auto receive_number() -> int
{
int number;
if (MPI_Recv(&number, 1, MPI_INT, MASTER, TAG, COMM, nullptr))
{
MPI_error();
}
return number;
}
/**
* Compares either odd or even processes with their neighbours and do
* a swap if necessary.
*
* @param number A number of a process.
* @param rank A rank of a process.
* @param limit The right limit for processes ranks to compare.
* @param odd_even Determines whether to compare odd or even processes with
* their neighbours.
*/
auto ots_cmp(
int &number, const int rank, const int limit, const bool odd_even
) -> void
{
if (rank > limit || (rank == MASTER && odd_even))
{
return;
}
// even/odd processes send their value to odd/even processes
if (!odd_even && rank < limit)
{
if (MPI_Send(&number, 1, MPI_INT, rank + 1, TAG, COMM))
{
MPI_error();
}
if (MPI_Recv(&number, 1, MPI_INT, rank + 1, TAG, COMM, nullptr))
{
MPI_error();
}
}
else // odd/even processes compare their value with even/odd processes
{
int neigh;
if (MPI_Recv(&neigh, 1, MPI_INT, rank - 1, TAG, COMM, nullptr))
{
MPI_error();
}
if (neigh > number) // swap
{
MPI_Send(&number, 1, MPI_INT, rank - 1, TAG, COMM);
number = neigh;
}
else
{
MPI_Send(&neigh, 1, MPI_INT, rank - 1, TAG, COMM);
}
}
}
/**
* The odd-even transposition sort algorithm.
*
* @param number A number of a process.
* @param rank A rank of a process.
* @param procs_count The number of all processes.
*/
auto ots(int &number, const int rank, const int procs_count) -> void
{
const int odd_limit = 2 * (procs_count / 2) - 1;
const int even_limit = 2 * ((procs_count - 1) / 2);
for (int i = 0; i < ceil(procs_count / 2.0); i++)
{
// odd processes compare
ots_cmp(number, rank, odd_limit, rank % 2);
// even processes compare
ots_cmp(number, rank, even_limit, !(rank % 2));
}
}
/**
* Receives numbers from all processes.
*
* @param numbers Received numbers of all processes.
* @param number A number of a process.
* @param rank A rank of a process.
* @param procs_count The number of all processes.
*/
auto receive_all_numbers(
int *const numbers, const int number, const int rank, const int procs_count
) -> void
{
if (rank == MASTER)
{
numbers[rank] = number;
for (int r = MASTER + 1; r < procs_count; r++)
{
if (MPI_Recv(&numbers[r], 1, MPI_INT, r, TAG, COMM, nullptr))
{
MPI_error();
}
}
}
else
{
if (MPI_Send(&number, 1, MPI_INT, MASTER, TAG, COMM))
{
MPI_error();
}
}
}
/**
* Prints given numbers to the standard output.
*
* @param numbers Numbers to be printed.
* @param rank A rank of a process.
* @param procs_count The number of all processes.
*/
auto print_numbers(
const int *const numbers, const int rank, const int procs_count
) -> void
{
if (rank != MASTER)
{
return;
}
for (int r = MASTER; r < procs_count; r++)
{
cout << numbers[r] << endl;
}
}
auto main(int argc, char *argv[]) -> int
{
if (MPI_Init(&argc, &argv))
{
MPI_error();
}
int rank;
if (MPI_Comm_rank(COMM, &rank))
{
MPI_error();
}
const auto numbers_count = read_numbers(rank);
if (!numbers_count && rank == MASTER)
{
MPI_Abort(COMM, EXIT_SUCCESS);
}
auto number = receive_number();
int procs_count;
if (MPI_Comm_size(COMM, &procs_count))
{
MPI_error();
}
#ifdef DEBUG
chrono::time_point<chrono::high_resolution_clock> start, end;
if (rank == MASTER) start = chrono::high_resolution_clock::now();
#endif
ots(number, rank, procs_count);
#ifdef DEBUG
if (rank == MASTER) end = chrono::high_resolution_clock::now();
#endif
auto *const numbers = new int[procs_count];
receive_all_numbers(numbers, number, rank, procs_count);
print_numbers(numbers, rank, procs_count);
#ifdef DEBUG
if (rank == MASTER)
{
chrono::duration<double> diff = end - start;
cout << endl << "Time: " << diff.count() << endl;
}
#endif
delete[] numbers;
MPI_Finalize();
return EXIT_SUCCESS;
}