-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw_hybrid_v7.c
355 lines (297 loc) · 10.1 KB
/
sw_hybrid_v7.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
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <omp.h>
#include "mpi.h"
#define MAXLINELEN 100002
#define ALIGNMENTS 32
// define scoring scheme
#define MATCH 1
#define MISMATCH -1
#define GAP -1
double gettime(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec + 1e-6*tv.tv_usec;
}
void show_args(int argc, const char * argv[]) {
int count;
printf( "\nCommand-line arguments:\n" );
for( count = 0; count < argc; count++ ) {
printf( " argv[%d] %s\n", count, argv[count] );
}
}
void fill_matrix(int * matrix, int x, int y, int value) {
int i, j;
for (i = 0; i <= x; i++) {
for (j = 0; j <= y; j++) {
//printf("before %d*%d + %d = matrix[%d]\t%d\n", x, i, j, x*i+j, matrix[x*i+j]);
matrix[x*i + j] = value;
//printf(" after %d*%d + %d = matrix[%d]\t%d\n", x, i, j, x*i+j, matrix[x*i+j]);
}
}
}
void print_matrix(int * matrix, int x, int y) {
int i, j;
for (i = 0; i <= x; i++) {
for (j = 0; j <= y; j++) {
int value = matrix[x*i + j];
printf("\t(%d,%d)=%d", i, j, value);
}
printf("\n");
}
printf("\n");
}
void walk_matrix(int * score_matrix, int * ptr_matrix, char * seq1, char * seq2, int * max_i, int * max_j, int * max_score) {
int i, j;
int seq1len = strlen(seq1);
int seq2len = strlen(seq2);
// fill every cell in the matrix
for (i = 1; i <= seq1len; i++) {
for (j = 1; j <= seq2len; j++) {
int diagonal_score, left_score, up_score;
// calculate match score
char a = seq1[i-1];
char b = seq2[j-1];
if (a == b) {
diagonal_score = score_matrix[seq1len*(i-1) + (j-1)] + MATCH;
}
else {
diagonal_score = score_matrix[seq1len*(i-1) + (j-1)] + MISMATCH;
}
// calculate gap scores
up_score = score_matrix[seq1len*(i-1) + j ] + GAP;
left_score = score_matrix[seq1len*i + (j-1)] + GAP;
if (diagonal_score <= 0 && up_score <= 0 && left_score <= 0) {
score_matrix[seq1len*i + j] = 0;
ptr_matrix[seq1len*i + j] = 0;
//printf("(%c %d, %c %d) = %f **CONT\n", a, i, b, j, score_matrix[i*seq1len +j]);
continue;
}
// choose best score
// ptr_matrix values:
// 0 : no pointer
// 1 : diagonal
// 2 : left
// 3 : up
// (these could be replaced with a 2 bit value instead)
if (diagonal_score >= up_score) {
if (diagonal_score >= left_score) {
score_matrix[seq1len*i + j] = diagonal_score;
ptr_matrix[seq1len*i + j] = 1;
}
else {
score_matrix[seq1len*i + j] = left_score;
ptr_matrix[seq1len*i + j] = 2;
}
}
else {
if (up_score >= left_score) {
score_matrix[seq1len*i + j] = up_score;
ptr_matrix[seq1len*i + j] = 3;
}
else {
score_matrix[seq1len*i + j] = left_score;
ptr_matrix[seq1len*i + j] = 2;
}
}
// set maximum score
if (score_matrix[seq1len*i + j] > *max_score) {
*max_i = i;
*max_j = j;
*max_score = score_matrix[seq1len*i + j];
// printf("** now max_score is %d\n", max_score);
}
//printf("(%c %d, %c %d) = %f\n", a, i, b, j, score_matrix[i*seq1len +j]);
}
}
}
/* append a char to a string */
void append(char * string, char c)
{
int len = strlen(string);
string[len] = c;
string[len+1] = '\0';
}
/* remove newline from string */
void chomp (char * string) {
char * ptr;
if( (ptr = strchr(string, '\n')) != NULL)
*ptr = '\0';
}
/* reverse: reverse string s in place */
void reverse(char s[]) {
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void traceback(int * score_matrix, int * ptr_matrix, char * seq1, char * seq2, int max_i, int max_j) {
int seq1len = strlen(seq1);
int seq2len = strlen(seq2);
int max_align_len = seq1len + seq2len;
int flag = 1; // used to exit the while loop
int ii = max_i; // ii and jj are our position in the matrix as we traceback
int jj = max_j; // starting from the i,j where the greatest score was obtained
// initialize output strings
char align1[max_align_len+1];
strcpy(align1, "");
char align2[max_align_len+1];
strcpy(align2, "");
while (flag) {
int tb = ptr_matrix[ii*seq1len + jj];
char a, b;
switch (tb) {
// if we reach a 0 traceback ptr, we're done
case 0:
flag = 0;
break;
// diagonal
case 1:
a = seq1[ii-1];
b = seq2[jj-1];
append(align1, a);
append(align2, b);
ii--;
jj--;
break;
// left
case 2:
a = seq1[ii-1];
b = '-';
append(align1, a);
append(align2, b);
jj--;
break;
// up
case 3:
a = '-';
b = seq2[jj-1];
append(align1, a);
append(align2, b);
ii--;
break;
default:
perror("invalid value in ptr_matrix!\n");
break;
break;
}
}
// reverse the strings (traceback starts at the end)
reverse(align1);
reverse(align2);
printf("%s\n%s\n", align1, align2);
}
void do_alignment(char * line1, char * line2) {
// get start time
double align_t = gettime();
// prep seqs
int seq1len = strlen(line1);
int seq2len = strlen(line2);
char seq1[seq1len];
strcpy(seq1, line1);
char seq2[seq2len];
strcpy(seq2, line2);
// initialize score and pointer matrices
int * score_matrix = (int *) malloc((10000+1) * (10000+1) * sizeof(int));
int * ptr_matrix = (int *) malloc((10000+1) * (10000+1) * sizeof(int));
double fill1_t = gettime();
fill_matrix(score_matrix, seq1len, seq2len, 0);
fill1_t = gettime() - fill1_t;
double fill2_t = gettime();
fill_matrix( ptr_matrix, seq1len, seq2len, 0);
fill2_t = gettime() - fill2_t;
//print_matrix(ptr_matrix, seq1len, seq2len);
//print_matrix(score_matrix, seq1len, seq2len);
// do the score calculation for each cell in the matrix
int max_i = 0, max_j = 0, max_score = 0;
walk_matrix(score_matrix, ptr_matrix, seq1, seq2, &max_i, &max_j, &max_score);
// find the alignment by following the pointers back through the matrix
traceback(score_matrix, ptr_matrix, seq1, seq2, max_i, max_j);
// release matrix memory
free(score_matrix);
free(ptr_matrix);
// print score
printf("max score: %d\n", max_score);
// print num of cells, time in secs, and flops
align_t = gettime() - align_t;
fprintf(stderr, "%d\t%f\t%E f(%f, %f)\n", seq1len*seq2len,
align_t, (seq1len*seq2len)/align_t), fill1_t, fill2_t;
}
int main (int argc, char * argv[]) {
// mpi variables
int rank, omp_rank, mpisupport, num_procs;
MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &mpisupport);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
// get start time
double t = gettime();
// Display each command-line argument
// show_args(argc, argv);
if (argc <= 1) {
fprintf(stderr, "sw <seqfile> [threads]\n");
exit(1);
}
// get number of threads per processor (default is 1)
// and set that number for OpenMP
int num_threads = 1;
if (argc == 3) {
num_threads = atoi(argv[2]);
}
omp_set_num_threads(num_threads);
int tot_threads = num_procs * num_threads;
printf("running with %d procs and %d threads, %d total threads\n", num_procs, omp_get_max_threads(), tot_threads );
// open file and initialize vars
FILE *file;
char lineAs[ALIGNMENTS][MAXLINELEN];
char lineBs[ALIGNMENTS][MAXLINELEN];
file = fopen(argv[1], "r");
if (file == NULL) {
perror ("Error reading file");
exit(1);
}
int c;
int num_lines;
num_lines = ALIGNMENTS * 2;
// read file into arrays
for (c = 0; c < ALIGNMENTS; c++) {
// grab two lines and store them in our line arrays A & B
char lineA [MAXLINELEN];
char lineB [MAXLINELEN];
fgets(lineA, MAXLINELEN, file);
fgets(lineB, MAXLINELEN, file);
chomp(lineA);
chomp(lineB);
strcpy(lineAs[c], lineA);
strcpy(lineBs[c], lineB);
}
fclose(file);
// set up chunking
int chunk_count, chunk;
chunk_count = ALIGNMENTS/tot_threads;
// each thread does its own alignments
#pragma omp parallel private(omp_rank, chunk)
{
for (chunk = 0; chunk < chunk_count; chunk++) {
int chunk_start = chunk * tot_threads;
omp_rank = omp_get_thread_num();
char line1 [MAXLINELEN];
char line2 [MAXLINELEN];
int line_num = (rank * num_threads) + omp_rank + chunk_start;
strcpy(line1, lineAs[line_num]);
strcpy(line2, lineBs[line_num]);
//fprintf(stderr, "process %d, thread %d - chunk %d/%d - seq %s\n", rank, omp_rank, chunk, chunk_count-1, line1);
do_alignment(line1, line2);
}
}
// get and print elapsed time
t = gettime() - t;
printf("process %d elapsed time: %f secs\n", rank, t);
MPI_Finalize();
return 0;
}