-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslurp.c
327 lines (303 loc) · 10.4 KB
/
slurp.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
#include "bison.h"
/******************************************************************************
*
* Add an element to the end of a linked-list
*
* struct packed_struct *last: last sentinel struct
* void *packed: a packed read
*
*******************************************************************************/
void add_element(struct packed_struct *last, void *packed) {
struct packed_struct *new = malloc(sizeof(struct packed_struct));
struct packed_struct *next_to_last = last->previous;
assert(new);
//Setup the new element
new->packed = packed;
new->next = last;
new->previous = next_to_last;
new->state = 0;
//Update the sentinel struct
last->previous = new;
//Update the next_to_last struct
next_to_last->next = new;
next_to_last->state = 1;
}
/******************************************************************************
*
* Destroy a (typically already removed) element from a linked-list
*
* struct packed_struct *remove: element to destroy
*
*******************************************************************************/
void destroy_element(struct packed_struct *remove) {
bam1_t *pbam1_t = remove->packed;
if(pbam1_t != NULL) {
if(pbam1_t->data != NULL) free(pbam1_t->data);
free(pbam1_t);
}
free(remove);
}
/******************************************************************************
*
* Remove an element from the start of a linked-list
* is_ready(first, 0) must return 1!
*
* struct packed_struct *first: first sentinel struct
*
*******************************************************************************/
void remove_element(struct packed_struct *first) {
struct packed_struct *remove = first->next;
struct packed_struct *new_next = remove->next;
first->next = new_next;
destroy_element(remove);
}
/******************************************************************************
*
* Is the first or second element ready?
*
* struct packed_struct *first: first sentinel struct
* int offset: 0 (first element) or 1 (second element)
*
* returns 1 for element ready, or 0 otherwise
*
*******************************************************************************/
inline int is_ready(struct packed_struct *first, int offset) {
if(offset == 0) {
if(first->next->state == 1) return 1;
} else {
if(first->next->next->state == 1) return 1;
}
return 0;
}
/******************************************************************************
*
* Is the linked list finished?
*
* struct packed_struct *first: first sentinel struct
*
* returns 1 for finished, 0 otherwise
*
*******************************************************************************/
inline int is_finished(struct packed_struct *first) {
if(first->next->packed == NULL) return 1;
return 0;
}
/******************************************************************************
*
* Add a finished element to a linked list
*
* struct packed_struct *last: last sentenel struct of targeted list
*
*******************************************************************************/
void add_finished(struct packed_struct *last) {
struct packed_struct *new = malloc(sizeof(struct packed_struct));
struct packed_struct *next_to_last = last->previous;
assert(new);
new->packed = NULL;
new->next = last;
new->previous = NULL;
new->state = 1;
//Update the sentinel struct
last->previous = new;
//Update the next_to_last struct
next_to_last->next = new;
next_to_last->state = 1;
if(config.paired) next_to_last->previous->state = 1;
}
/******************************************************************************
*
* Initialize a linked list, returning the last sentinel struct
*
* struct packed_struct *first: first sentinel struct
*
* returns first sentinel struct
*
*******************************************************************************/
struct packed_struct *initialize_list(struct packed_struct *first) {
first = malloc(sizeof(struct packed_struct));
assert(first);
struct packed_struct *last= malloc(sizeof(struct packed_struct));
assert(last);
first->next = last;
first->previous = first;
first->packed = NULL;
last->next = last;
last->previous = first;
last->packed = NULL;
last->state = 0; //is_ready(last) should always be 0;
first->state = 0; //is_ready(last) should always be 0;
return first;
}
/******************************************************************************
*
* Destroy a linked list of packed_structs
*
* struct packed_struct *first: linked list to destroy
*
*******************************************************************************/
void destroy_list(struct packed_struct *first) {
while(first->next->next != first->next) remove_element(first);
free(first->next);
free(first);
}
/******************************************************************************
*
* The MPI receiver thread on the main node
*
* void *a: NULL input
*
* returns NULL
*
*******************************************************************************/
void *slurp(void *a) {
time_t t0, t1;
#ifndef DEBUG
void *p = NULL;
int nnodes = (config.directional) ? 2 : 4;
int nfinished = 0;
int source = 0;
int size = 0;
struct packed_struct *target_node = NULL;
MPI_Status status;
int start = 1;
int ntasks = (config.directional) ? 3: 5;
int i;
for(i=1; i<ntasks; i++) {
if(!config.quiet) fprintf(stderr, "Sending start to node %i\n", i); fflush(stderr);
MPI_Ssend(&start, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
//Get the header
if(MPI_Recv((void *) &size, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, &status) != MPI_SUCCESS) {
fprintf(stderr, "Received an error when trying to receive header size.\n");
fflush(stderr);
quit(3, -2);
}
p = malloc((size_t) size);
assert(p);
if(MPI_Recv(p, size, MPI_BYTE, 1, 2, MPI_COMM_WORLD, &status) != MPI_SUCCESS) {
fprintf(stderr, "Received an error when trying to receive header.\n");
fflush(stderr);
quit(3, -2);
}
global_header = bam_hdr_init();
unpack_header(global_header, p);
free(p);
#else
char *iname = malloc(sizeof(char) * (1+strlen(config.odir)+strlen(config.basename)+strlen("_CTOT.bam")));
assert(iname);
sprintf(iname, "%s%s_OT.bam", config.odir, config.basename);
fp1 = sam_open(iname, "rb");
sprintf(iname, "%s%s_OB.bam", config.odir, config.basename);
fp2 = sam_open(iname, "rb");
if(!config.directional) {
sprintf(iname, "%s%s_CTOT.bam", config.odir, config.basename);
fp3 = sam_open(iname, "rb");
sprintf(iname, "%s%s_CTOB.bam", config.odir, config.basename);
fp4 = sam_open(iname, "rb");
}
free(iname);
global_header = sam_hdr_read(fp1);
if(!config.quiet) fprintf(stderr, "header written\n"); fflush(stderr);
bam1_t *read = bam_init1();
MPI_read *packed = calloc(1, sizeof(MPI_read));
assert(packed);
packed->packed = NULL;
packed->size = 0;
bam_hdr_t *tmp;
tmp = sam_hdr_read(fp2);
bam_hdr_destroy(tmp);
if(!config.directional) {
tmp = sam_hdr_read(fp3);
bam_hdr_destroy(tmp);
tmp = sam_hdr_read(fp4);
bam_hdr_destroy(tmp);
}
#endif
//Write a header
sam_hdr_write(OUTPUT_BAM, global_header);
t0 = time(NULL);
if(!config.quiet) fprintf(stderr, "Started slurping @%s", ctime(&t0)); fflush(stderr);
#ifndef DEBUG
while(nfinished < nnodes) {
MPI_Probe(MPI_ANY_SOURCE, 5, MPI_COMM_WORLD, &status);
source = status.MPI_SOURCE;
MPI_Get_count(&status, MPI_BYTE, &size);
if(source == 1) target_node = node1_last_sentinel;
else if(source == 2) target_node = node2_last_sentinel;
else if(source == 3) target_node = node3_last_sentinel;
else if(source == 4) target_node = node4_last_sentinel;
if(size > 1) {
p = malloc((size_t) size);
assert(p);
MPI_Recv(p, size, MPI_BYTE, source, 5, MPI_COMM_WORLD, &status);
add_element(target_node, p);
} else {
p = malloc((size_t) size);
assert(p);
MPI_Recv(p, size, MPI_BYTE, source, 5, MPI_COMM_WORLD, &status);
free(p);
add_finished(target_node);
nfinished++;
}
}
#else
//OT
while(sam_read1(fp1, global_header, read) > 1) {
packed->size = 0;
packed = pack_read(read, packed);
add_element(node1_last_sentinel, packed->packed);
if(config.paired) {
sam_read1(fp1, global_header, read);
packed->size = 0;
packed = pack_read(read, packed);
add_element(node1_last_sentinel, packed->packed);
}
//OB
sam_read1(fp2, global_header, read);
packed->size = 0;
packed = pack_read(read, packed);
add_element(node2_last_sentinel, packed->packed);
if(config.paired) {
sam_read1(fp2, global_header, read);
packed->size = 0;
packed = pack_read(read, packed);
add_element(node2_last_sentinel, packed->packed);
}
if(!config.directional) {
//CTOT
sam_read1(fp3, global_header, read);
packed->size = 0;
packed = pack_read(read, packed);
add_element(node3_last_sentinel, packed->packed);
if(config.paired) {
sam_read1(fp3, global_header, read);
packed->size = 0;
packed = pack_read(read, packed);
add_element(node3_last_sentinel, packed->packed);
}
//CTOB
sam_read1(fp4, global_header, read);
packed->size = 0;
packed = pack_read(read, packed);
add_element(node4_last_sentinel, packed->packed);
if(config.paired) {
sam_read1(fp4, global_header, read);
packed->size = 0;
packed = pack_read(read, packed);
add_element(node4_last_sentinel, packed->packed);
}
}
}
bam_destroy1(read);
free(packed);
add_finished(node1_last_sentinel);
add_finished(node2_last_sentinel);
if(!config.directional) {
add_finished(node3_last_sentinel);
add_finished(node4_last_sentinel);
}
#endif
t1 = time(NULL);
if(!config.quiet) fprintf(stderr, "Finished slurping @%s\t(%f seconds elapsed)\n", ctime(&t1), difftime(t1, t0)); fflush(stderr);
return NULL;
}