-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviterbi_parallel.cc
448 lines (354 loc) · 11.2 KB
/
viterbi_parallel.cc
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// viterbi.cc
// Viterbi
//
// Created by Shubham Chaudhary on 19/03/17.
// Copyright © 2017 Shubham Chaudhary. All rights resized.
//
#include <iostream>
#include <limits>
#include <vector>
#include <random>
#include <fstream>
#include <utility>
#include <exception>
#include <mpi.h>
#include <sys/mman.h>
#include "viterbi.hh"
int num_procs, tid, lp, rp;
#pragma omp declare reduction( \
max_argmax \
: std::pair<double, int> \
: omp_out = (omp_in.first > omp_out.first ? omp_out : omp_in)) \
initializer (omp_priv = \
std::make_pair(-std::numeric_limits<double>::max(), \
std::numeric_limits<int>::max()))
template <typename T>
inline bool is_parallel(std::vector<T>& a, std::vector<T>& b) {
bool parallel = a.size() == b.size();
int length = std::min(a.size(), b.size());
for (int i = 0; i < length - 1; i++) {
if (!parallel)
break;
if (a[i] - b[i] != a[i + 1] - b[i + 1])
parallel = false;
}
return parallel;
}
viterbi::viterbi(std::string &arg1, std::string& arg2) {
// Initialise MPI runtime information
MPI_Init (nullptr, nullptr);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
MPI_Comm_rank(MPI_COMM_WORLD, &tid);
// Load info of the decoder
std::FILE *file_p = std::fopen(("data/viterbi_" + arg1 + "_" + arg2).c_str(), "r");
// Calculate file size
std::fseek(file_p, 0, SEEK_END);
unsigned length = std::ftell(file_p);
// Memory map the file
char *file_map = (char*) mmap(
nullptr,
length,
PROT_READ,
MAP_PRIVATE,
fileno(file_p),
0);
// Get sequence info
seq_len = *((unsigned int*)(file_map) + 0);
num_hidden = *((unsigned int*)(file_map) + 1);
// Determine the range of processing
lp = (seq_len / num_procs) * tid - 1;
rp = (seq_len / num_procs) * (tid + 1) - 1;
if (tid == num_procs - 1)
rp = seq_len - 1;
unsigned int offset = sizeof(unsigned int) * 2
+ sizeof(double) * (lp + 1) * num_hidden * num_hidden;
// Allocate memory for data
ltdp_matrix.resize(rp - lp);
for (auto& a : ltdp_matrix) {
a.resize(num_hidden);
for (auto& b: a) {
b.resize(num_hidden);
}
}
double *matrix = (double*) (file_map + offset);
// Load LTDP matrix
for (int i = 0; i < rp - lp; i++) {
#ifdef WAVEFRONT
#pragma omp parallel for default(shared)
#endif
for (int j = 0; j < num_hidden; j++) {
for (int k = 0; k < num_hidden; k++) {
ltdp_matrix[i][j][k] =
matrix[k + num_hidden * j + num_hidden * num_hidden * i];
}
}
}
std::fclose(file_p);
// Allocate memory for sequence
predicted_sequence.resize(rp - lp);
#ifdef DEBUG
std::cout << "Finished loading data from disk." << std::endl;
#endif
}
void viterbi::decode() {
std::default_random_engine generator;
std::uniform_real_distribution<double> dist_rand(-100.0, 0);
// Setup dynamic programming tables
std::vector<std::vector<double>> dp1;
std::vector<std::vector<unsigned int>> dp2;
// Test variables
uint8_t conv, global_conv;
// Allocate memory for the dp tables
dp1.resize(rp - lp);
dp2.resize(rp - lp);
for (auto& a : dp1)
a.resize(num_hidden);
for (auto& a : dp2)
a.resize(num_hidden);
#ifdef DEBUG
std::cout << "Running algorithm for " << num_hidden
<< " hidden states, and a sequence of length "
<< seq_len << std::endl;
#endif
/* BEGIN LTDP PARALLEL VITERBI ALGORITHM
*/
// Time the execution
double start, end;
if (tid == 0)
start = MPI_Wtime();
#ifdef DEBUG
std::cout << "Forward pass: Thread "
+ std::to_string(tid)
+ "/"
+ std::to_string(num_procs)
+ " running from "
+ std::to_string(lp + 1)
+ " to "
+ std::to_string(rp)
+ "\n";
#endif
// Initialize local vector
std::vector<double> s(num_hidden, 0.0);
if (tid != 0) {
for (auto& a : s) {
a = dist_rand(generator);
}
}
for (int i = lp + 1; i <= rp; i++) {
#ifdef WAVEFRONT
#pragma omp parallel for default(shared)
#endif
for (int j = 0; j < num_hidden; j++) {
std::pair<double, int> max_p = std::make_pair(
-std::numeric_limits<double>::max(),
std::numeric_limits<int>::max());
double entry;
for (size_t k = 0; k < num_hidden; k++) {
entry = s[k] + ltdp_matrix[i - lp - 1][j][k];
if (entry > max_p.first) {
max_p.first = entry;
max_p.second = k;
}
}
dp1[i - lp - 1][j] = max_p.first;
dp2[i - lp - 1][j] = max_p.second;
}
s = dp1[i - lp - 1];
}
MPI_Barrier(MPI_COMM_WORLD);
// Fix up loop
do {
conv = 0b1;
// Initialize local vector
std::vector<double> s(num_hidden, 0.0), next_s(num_hidden, 0.0);
#ifdef DEBUG
std::cout << "Forward fix up loop: Thread "
+ std::to_string(tid)
+ " running from "
+ std::to_string(lp + 1)
+ " to "
+ std::to_string(rp)
+ "\n";
#endif
if (tid != num_procs - 1) {
// Send the solution vector to the next section
MPI_Send(
&dp1[rp - lp - 1].front(),
num_hidden,
MPI_DOUBLE,
tid + 1,
0,
MPI_COMM_WORLD);
}
if (tid != 0) {
conv = 0b0;
// Get the solution vector of the previous section
MPI_Recv(
&s.front(),
num_hidden,
MPI_DOUBLE,
tid - 1,
0,
MPI_COMM_WORLD,
nullptr);
}
#ifdef DEBUG
std::cout << "Forward fix up loop: Thread "
+ std::to_string(tid)
+ "/"
+ std::to_string(num_procs)+
+ " finished communication.\n";
#endif
if (tid != 0) {
for (int i = lp + 1; i <= rp; i++) {
#ifdef WAVEFRONT
#pragma omp parallel for default(shared)
#endif
for (int j = 0; j < num_hidden; j++) {
std::pair<double, int> max_p = std::make_pair(
-std::numeric_limits<double>::max(),
std::numeric_limits<int>::max());
double entry;
for (size_t k = 0; k < num_hidden; k++) {
entry = s[k] + ltdp_matrix[i - lp - 1][j][k];
if (entry > max_p.first) {
max_p.first = entry;
max_p.second = k;
}
}
next_s[j] = max_p.first;
dp2[i - lp - 1][j] = max_p.second;
}
s = next_s;
if (is_parallel(s, dp1[i - lp - 1])) {
conv = 0b1;
break;
}
dp1[i - lp - 1] = s;
}
}
MPI_Barrier(MPI_COMM_WORLD);
#ifdef DEBUG
std::cout << "Forward fix up loop: Thread "
+ std::to_string(tid)
+ " finished forward pass.\n";
#endif
// Get the global value of conv
MPI_Allreduce(&conv, &global_conv, 1, MPI_BYTE, MPI_LAND, MPI_COMM_WORLD);
#ifdef DEBUG
std::cout << "Forward fix up loop: Thread "
+ std::to_string(tid)
+ " sent value of conv as "
+ (conv ? "true" : "false")
+ " and recieved global conv as "
+ (global_conv ? "true" : "false")
+ "\n";
#endif
} while (!global_conv);
// Backward pass
#ifdef DEBUG
std::cout << "Backward pass: Thread "
+ std::to_string(tid)
+ " running from "
+ std::to_string(rp)
+ " to "
+ std::to_string(lp + 1)
+ "\n";
#endif
// Initialize local variable
unsigned int x = 0;
for (int i = rp; i >= lp + 1; i--) {
predicted_sequence[i - lp - 1] = dp2[i - lp - 1][x];
x = predicted_sequence[i - lp - 1];
}
MPI_Barrier(MPI_COMM_WORLD);
// Fix up loop for backward pass
do {
conv = 0b1;
// Initialize local variable
unsigned int x;
#ifdef DEBUG
std::cout << "Backward fix up loop: Thread "
+ std::to_string(tid)
+ " running from "
+ std::to_string(rp)
+ " to "
+ std::to_string(lp + 1)
+ "\n";
#endif
if (tid != 0) {
// Send the result value to the previous section
MPI_Send(
&predicted_sequence[0],
1,
MPI_UNSIGNED,
tid - 1,
0,
MPI_COMM_WORLD);
}
if (tid != num_procs - 1) {
conv = 0b0;
// Get the result value of the next section
MPI_Recv(
&x,
1,
MPI_UNSIGNED,
tid + 1,
0,
MPI_COMM_WORLD,
nullptr);
}
#ifdef DEBUG
std::cout << "Backward fix up loop: Thread "
+ std::to_string(tid)
+ "/"
+ std::to_string(num_procs)+
+ " finished communication.\n";
#endif
if (tid != num_procs - 1) {
for (int i = rp; i >= lp + 1; i--) {
x = dp2[i - lp - 1][x];
if (predicted_sequence[i - lp - 1] == x) {
conv = 0b1;
break;
}
predicted_sequence[i - lp - 1] = x;
}
}
MPI_Barrier(MPI_COMM_WORLD);
#ifdef DEBUG
std::cout << "Backward fix up loop: Thread "
+ std::to_string(tid)
+ " finished backward pass.\n";
#endif
// Get the global value of conv
MPI_Allreduce(&conv, &global_conv, 1, MPI_BYTE, MPI_LAND, MPI_COMM_WORLD);
#ifdef DEBUG
std::cout << "Backward fix up loop: Thread "
+ std::to_string(tid)
+ " sent value of conv as "
+ (conv ? "true" : "false")
+ " and recieved global conv as "
+ (global_conv ? "true" : "false")
+ "\n";
#endif
} while (!global_conv);
MPI_Barrier(MPI_COMM_WORLD);
if (tid == 0) {
end = MPI_Wtime();
std::cout << "Total time elapsed: "
<< end - start
<< std::endl;
}
MPI_Finalize();
}
void viterbi::show_predicted() {
std::ofstream file(
"output_parallel_" + std::to_string(tid) + "_" + std::to_string(num_procs),
std::ios::out | std::ios::trunc);
for (auto& a : predicted_sequence)
file << a << " ";
file << std::endl;
file.close();
}