-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcs-omp.cpp
190 lines (149 loc) · 3.77 KB
/
lcs-omp.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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <omp.h>
#include <math.h>
using namespace std;
/***********************
** GLOBAL VARIABLES
***********************/
//Direccions
#define MATCH 0
#define UP 1
#define LEFT 2
//Input strings
char* N;
char* M;
//String size
int N_LENGTH;
int M_LENGTH;
//Auxiliary tables
short** TABLE;
short** TRACKER;
/***********************
** AUXILIARY PROCEDURES
***********************/
//The cost routine
short cost(int x) {
int i, n_iter = 20;
double dcost = 0;
for (i = 0; i < n_iter; i++)
dcost += pow(sin((double) x),2) + pow(cos((double) x),2);
return (short) (dcost / n_iter + 0.1);
}
//Prints the auxiliary table
void printTable() {
for (int i = 0; i <= N_LENGTH; i++) {
for (int j = 0; j <= M_LENGTH; j++)
printf(" %d |", TABLE[i][j]);
printf("\n");
}
printf("\n\n");
}
//Reads an input file with a given name and initializes global variables
void initProblem() {
int i = 0;
int correctedSizeN = N_LENGTH + 1;
int correctedSizeM = M_LENGTH + 1;
//Create auxiliary tables
TABLE = new short*[correctedSizeN];
TRACKER = new short*[correctedSizeN];
#pragma omp parallel for
for (i = 0; i < N_LENGTH + 1; ++i) {
TABLE[i] = new short[correctedSizeM]();
TRACKER[i] = new short[correctedSizeM]();
}
}
//Computes the value of a single position of the table;
void computePosition(int i, int j) {
if (N[i-1] == M[j-1]) {
TABLE[i][j] = TABLE[i-1][j-1] + cost(j);
//DIAGONAL - IT'S A MATCH
TRACKER[i][j] = MATCH;
} else if (TABLE[i-1][j] > TABLE[i][j-1]) {
TABLE[i][j] = TABLE[i-1][j];
//UP - FETCH VALUE FROM PREVIOUS SUBPROBLEM
TRACKER[i][j] = UP;
} else {
TABLE[i][j] = TABLE[i][j-1];
//LEFT - FETCH VALUE FROM CURRENT SUBPROBLEM
TRACKER[i][j] = LEFT;
}
}
//Computes the solution using an auxiliary table
void computeSolution() {
int imin = 1, imax = 1, jmin = 1, jmax = 1;
int diagonal, k;
int minDim = (N_LENGTH<M_LENGTH)?N_LENGTH:M_LENGTH;
int dimDiff = abs(N_LENGTH - M_LENGTH);
//Phase 1: increasing workload
for(diagonal = 1; imax < minDim; imax++, jmax++, diagonal++){
#pragma omp parallel for schedule(dynamic,64)
for(k = 0; k < diagonal; k++){
computePosition(imax-k, jmin+k);
}
}
//Phase 2: constant workload
for(int iter = 0; iter < dimDiff; iter++, (imax < N_LENGTH)?imax++:jmin++, (jmax < M_LENGTH)?jmax++:imin++){
#pragma omp parallel for
for(k = 0; k < diagonal; k++){
computePosition(imax-k, jmin+k);
}
}
//Phase 3: decreasing workload
for(; imin <= imax; imin++, jmin++, diagonal--){
#pragma omp parallel for schedule(guided,64)
for(k = 0; k < diagonal; k++){
computePosition(imax-k, jmin+k);
}
}
}
//Prints the result of our previous computation
void printResult() {
int length = TABLE[N_LENGTH][M_LENGTH];
char result[length + 1];
//Prints the size of the biggest subsequence
cout << TABLE[N_LENGTH][M_LENGTH] << endl;;
result[length--] = '\0';
//Tracks the biggest subsequence
int i = N_LENGTH;
int j = M_LENGTH;
while (length >= 0) {
if (TRACKER[i][j] == MATCH) {
result[length--] = N[i-1];
--i;
--j;
}
else if(TRACKER[i][j] == UP)
--i;
else
--j;
}
cout << result << endl;
}
/***********************
** MAIN
***********************/
int main(int argc, const char* argv[]) {
double start = omp_get_wtime();
FILE *myfile;
string filename = argv[1];
myfile = fopen(filename.c_str(), "r");
if (myfile != NULL){
fscanf(myfile, "%d %d", &N_LENGTH, &M_LENGTH);
//Create the arrays to store the strings;
N = new char[N_LENGTH + 1];
M = new char[M_LENGTH + 1];
fscanf(myfile, "%s", N);
fscanf(myfile, "%s", M);
initProblem();
//Compute
computeSolution();
//Print
printResult();
}
double end = omp_get_wtime(), time = end - start;
cout << time << endl;
return 0;
}