-
Notifications
You must be signed in to change notification settings - Fork 6
/
driver.cpp
489 lines (413 loc) · 25.6 KB
/
driver.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "driver.hpp"
#include "utils.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/scan.h>
size_t gpu_bsw_driver::get_tot_gpu_mem(int id) {
cudaDeviceProp prop;
cudaErrchk(cudaGetDeviceProperties(&prop, id));
return prop.totalGlobalMem;
}
void
gpu_bsw_driver::kernel_driver_dna(std::vector<std::string> reads, std::vector<std::string> contigs, gpu_bsw_driver::alignment_results *alignments, short scores[4], float factor)
{
short matchScore = scores[0], misMatchScore = scores[1], startGap = scores[2], extendGap = scores[3];
unsigned maxContigSize = getMaxLength(contigs);
unsigned maxReadSize = getMaxLength(reads);
unsigned totalAlignments = contigs.size(); // assuming that read and contig vectors are same length
int deviceCount;
cudaGetDeviceCount(&deviceCount);
omp_set_num_threads(deviceCount);
std::cout<<"Number of available GPUs:"<<deviceCount<<"\n";
unsigned NBLOCKS = totalAlignments;
unsigned alignmentsPerDevice = NBLOCKS / deviceCount;
unsigned leftOver_device = NBLOCKS % deviceCount;
unsigned max_per_device = alignmentsPerDevice + leftOver_device;
initialize_alignments(alignments, totalAlignments); // pinned memory allocation
auto start = NOW;
size_t tot_mem_req_per_aln = maxReadSize + maxContigSize + 2 * sizeof(int) + 5 * sizeof(short);
#pragma omp parallel
{
int my_cpu_id = omp_get_thread_num();
cudaSetDevice(my_cpu_id);
int myGPUid;
cudaGetDevice(&myGPUid);
float total_time_cpu = 0;
cudaStream_t streams_cuda[NSTREAMS];
for(int stm = 0; stm < NSTREAMS; stm++){
cudaStreamCreate(&streams_cuda[stm]);
}
if(my_cpu_id == 0)std::cout<<"Number of GPUs being used:"<<omp_get_num_threads()<<"\n";
size_t gpu_mem_avail = get_tot_gpu_mem(myGPUid);
unsigned max_alns_gpu = floor(((double)gpu_mem_avail*factor)/tot_mem_req_per_aln);
unsigned max_alns_sugg = 20000;
max_alns_gpu = max_alns_gpu > max_alns_sugg ? max_alns_sugg : max_alns_gpu;
int its = (max_per_device>max_alns_gpu)?(ceil((double)max_per_device/max_alns_gpu)):1;
std::cout<<"Mem (bytes) avail on device "<<myGPUid<<":"<<(long unsigned)gpu_mem_avail<<"\n";
std::cout<<"Mem (bytes) using on device "<<myGPUid<<":"<<(long unsigned)gpu_mem_avail*factor<<"\n";
int BLOCKS_l = alignmentsPerDevice;
if(my_cpu_id == deviceCount - 1)
BLOCKS_l += leftOver_device;
unsigned leftOvers = BLOCKS_l % its;
unsigned stringsPerIt = BLOCKS_l / its;
gpu_alignments gpu_data(stringsPerIt + leftOvers); // gpu mallocs
short* alAbeg = alignments->ref_begin + my_cpu_id * alignmentsPerDevice;
short* alBbeg = alignments->query_begin + my_cpu_id * alignmentsPerDevice;
short* alAend = alignments->ref_end + my_cpu_id * alignmentsPerDevice;
short* alBend = alignments->query_end + my_cpu_id * alignmentsPerDevice; // memory on CPU for copying the results
short* top_scores_cpu = alignments->top_scores + my_cpu_id * alignmentsPerDevice;
unsigned* offsetA_h;// = new unsigned[stringsPerIt + leftOvers];
cudaMallocHost(&offsetA_h, sizeof(int)*(stringsPerIt + leftOvers));
unsigned* offsetB_h;// = new unsigned[stringsPerIt + leftOvers];
cudaMallocHost(&offsetB_h, sizeof(int)*(stringsPerIt + leftOvers));
char *strA_d, *strB_d;
cudaErrchk(cudaMalloc(&strA_d, maxContigSize * (stringsPerIt + leftOvers) * sizeof(char)));
cudaErrchk(cudaMalloc(&strB_d, maxReadSize *(stringsPerIt + leftOvers)* sizeof(char)));
char* strA;
cudaMallocHost(&strA, sizeof(char)*maxContigSize * (stringsPerIt + leftOvers));
char* strB;
cudaMallocHost(&strB, sizeof(char)* maxReadSize *(stringsPerIt + leftOvers));
float total_packing = 0;
auto start2 = NOW;
for(int perGPUIts = 0; perGPUIts < its; perGPUIts++)
{
auto packing_start = NOW;
int blocksLaunched = 0;
std::vector<std::string>::const_iterator beginAVec;
std::vector<std::string>::const_iterator endAVec;
std::vector<std::string>::const_iterator beginBVec;
std::vector<std::string>::const_iterator endBVec;
if(perGPUIts == its - 1)
{
beginAVec = contigs.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endAVec = contigs.begin() + ((alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt) + leftOvers; // so that each openmp thread has a copy of strings it needs to align
beginBVec = reads.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endBVec = reads.begin() + ((alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt) + leftOvers; // so that each openmp thread has a copy of strings it needs to align
blocksLaunched = stringsPerIt + leftOvers;
}
else
{
beginAVec = contigs.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endAVec = contigs.begin() + (alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt; // so that each openmp thread has a copy of strings it needs to align
beginBVec = reads.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endBVec = reads.begin() + (alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt; // so that each openmp thread has a copy of strings it needs to align
blocksLaunched = stringsPerIt;
}
std::vector<std::string> sequencesA(beginAVec, endAVec);
std::vector<std::string> sequencesB(beginBVec, endBVec);
long unsigned running_sum = 0;
int sequences_per_stream = (blocksLaunched) / NSTREAMS;
int sequences_stream_leftover = (blocksLaunched) % NSTREAMS;
long unsigned half_length_A = 0;
long unsigned half_length_B = 0;
auto start_cpu = NOW;
for(int i = 0; i < sequencesA.size(); i++)
{
running_sum +=sequencesA[i].size();
offsetA_h[i] = running_sum;//sequencesA[i].size();
if(i == sequences_per_stream - 1){
half_length_A = running_sum;
running_sum = 0;
}
}
long unsigned totalLengthA = half_length_A + offsetA_h[sequencesA.size() - 1];
running_sum = 0;
for(int i = 0; i < sequencesB.size(); i++)
{
running_sum +=sequencesB[i].size();
offsetB_h[i] = running_sum; //sequencesB[i].size();
if(i == sequences_per_stream - 1){
half_length_B = running_sum;
running_sum = 0;
}
}
long unsigned totalLengthB = half_length_B + offsetB_h[sequencesB.size() - 1];
auto end_cpu = NOW;
std::chrono::duration<double> cpu_dur = end_cpu - start_cpu;
total_time_cpu += cpu_dur.count();
long unsigned offsetSumA = 0;
long unsigned offsetSumB = 0;
for(int i = 0; i < sequencesA.size(); i++)
{
char* seqptrA = strA + offsetSumA;
memcpy(seqptrA, sequencesA[i].c_str(), sequencesA[i].size());
char* seqptrB = strB + offsetSumB;
memcpy(seqptrB, sequencesB[i].c_str(), sequencesB[i].size());
offsetSumA += sequencesA[i].size();
offsetSumB += sequencesB[i].size();
}
auto packing_end = NOW;
std::chrono::duration<double> packing_dur = packing_end - packing_start;
total_packing += packing_dur.count();
asynch_mem_copies_htd(&gpu_data, offsetA_h, offsetB_h, strA, strA_d, strB, strB_d, half_length_A, half_length_B, totalLengthA, totalLengthB, sequences_per_stream, sequences_stream_leftover, streams_cuda);
unsigned minSize = (maxReadSize < maxContigSize) ? maxReadSize : maxContigSize;
unsigned totShmem = 3 * (minSize + 1) * sizeof(short);
unsigned alignmentPad = 4 + (4 - totShmem % 4);
size_t ShmemBytes = totShmem + alignmentPad;
if(ShmemBytes > 48000)
cudaFuncSetAttribute(gpu_bsw::sequence_dna_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, ShmemBytes);
gpu_bsw::sequence_dna_kernel<<<sequences_per_stream, minSize, ShmemBytes, streams_cuda[0]>>>(
strA_d, strB_d, gpu_data.offset_ref_gpu, gpu_data.offset_query_gpu, gpu_data.ref_start_gpu,
gpu_data.ref_end_gpu, gpu_data.query_start_gpu, gpu_data.query_end_gpu, gpu_data.scores_gpu, matchScore, misMatchScore, startGap, extendGap);
cudaErrchk(cudaGetLastError());
gpu_bsw::sequence_dna_kernel<<<sequences_per_stream + sequences_stream_leftover, minSize, ShmemBytes, streams_cuda[1]>>>(
strA_d + half_length_A, strB_d + half_length_B, gpu_data.offset_ref_gpu + sequences_per_stream, gpu_data.offset_query_gpu + sequences_per_stream,
gpu_data.ref_start_gpu + sequences_per_stream, gpu_data.ref_end_gpu + sequences_per_stream, gpu_data.query_start_gpu + sequences_per_stream, gpu_data.query_end_gpu + sequences_per_stream,
gpu_data.scores_gpu + sequences_per_stream, matchScore, misMatchScore, startGap, extendGap);
cudaErrchk(cudaGetLastError());
// copyin back end index so that we can find new min
asynch_mem_copies_dth_mid(&gpu_data, alAend, alBend, sequences_per_stream, sequences_stream_leftover, streams_cuda);
cudaStreamSynchronize (streams_cuda[0]);
cudaStreamSynchronize (streams_cuda[1]);
auto sec_cpu_start = NOW;
int newMin = get_new_min_length(alAend, alBend, blocksLaunched); // find the new largest of smaller lengths
auto sec_cpu_end = NOW;
std::chrono::duration<double> dur_sec_cpu = sec_cpu_end - sec_cpu_start;
total_time_cpu += dur_sec_cpu.count();
gpu_bsw::sequence_dna_reverse<<<sequences_per_stream, newMin, ShmemBytes, streams_cuda[0]>>>(
strA_d, strB_d, gpu_data.offset_ref_gpu, gpu_data.offset_query_gpu, gpu_data.ref_start_gpu,
gpu_data.ref_end_gpu, gpu_data.query_start_gpu, gpu_data.query_end_gpu, gpu_data.scores_gpu, matchScore, misMatchScore, startGap, extendGap);
cudaErrchk(cudaGetLastError());
gpu_bsw::sequence_dna_reverse<<<sequences_per_stream + sequences_stream_leftover, newMin, ShmemBytes, streams_cuda[1]>>>(
strA_d + half_length_A, strB_d + half_length_B, gpu_data.offset_ref_gpu + sequences_per_stream, gpu_data.offset_query_gpu + sequences_per_stream ,
gpu_data.ref_start_gpu + sequences_per_stream, gpu_data.ref_end_gpu + sequences_per_stream, gpu_data.query_start_gpu + sequences_per_stream, gpu_data.query_end_gpu + sequences_per_stream,
gpu_data.scores_gpu + sequences_per_stream, matchScore, misMatchScore, startGap, extendGap);
cudaErrchk(cudaGetLastError());
asynch_mem_copies_dth(&gpu_data, alAbeg, alBbeg, top_scores_cpu, sequences_per_stream, sequences_stream_leftover, streams_cuda);
alAbeg += stringsPerIt;
alBbeg += stringsPerIt;
alAend += stringsPerIt;
alBend += stringsPerIt;
top_scores_cpu += stringsPerIt;
cudaStreamSynchronize (streams_cuda[0]);
cudaStreamSynchronize (streams_cuda[1]);
} // for iterations end here
auto end1 = NOW;
std::chrono::duration<double> diff2 = end1 - start2;
cudaErrchk(cudaFree(strA_d));
cudaErrchk(cudaFree(strB_d));
cudaFreeHost(offsetA_h);
cudaFreeHost(offsetB_h);
cudaFreeHost(strA);
cudaFreeHost(strB);
for(int i = 0; i < NSTREAMS; i++)
cudaStreamDestroy(streams_cuda[i]);
std::cout <<"cpu time:"<<total_time_cpu<<std::endl;
std::cout <<"packing time:"<<total_packing<<std::endl;
#pragma omp barrier
} // paralle pragma ends
auto end = NOW;
std::chrono::duration<double> diff = end - start;
std::cout << "Total Alignments:"<<totalAlignments<<"\n"<<"Max Reference Size:"<<maxContigSize<<"\n"<<"Max Query Size:"<<maxReadSize<<"\n" <<"Total Execution Time (seconds):"<< diff.count() <<std::endl;
}// end of DNA kernel
void
gpu_bsw_driver::kernel_driver_aa(std::vector<std::string> reads, std::vector<std::string> contigs, gpu_bsw_driver::alignment_results *alignments, short scoring_matrix[], short openGap, short extendGap, float factor)
{
unsigned maxContigSize = getMaxLength(contigs);
unsigned maxReadSize = getMaxLength(reads);
unsigned totalAlignments = contigs.size(); // assuming that read and contig vectors are same length
short encoding_matrix[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,20,4,3,6,
13,7,8,9,0,11,10,12,2,0,14,5,
1,15,16,0,19,17,22,18,21};
int deviceCount;
cudaGetDeviceCount(&deviceCount);
omp_set_num_threads(deviceCount);// one OMP thread per GPU
std::cout<<"Number of available GPUs:"<<deviceCount<<"\n";
cudaDeviceProp prop[deviceCount];
for(int i = 0; i < deviceCount; i++)
cudaGetDeviceProperties(&prop[i], 0);
unsigned NBLOCKS = totalAlignments;
unsigned alignmentsPerDevice = NBLOCKS / deviceCount;
unsigned leftOver_device = NBLOCKS % deviceCount;
unsigned max_per_device = alignmentsPerDevice + leftOver_device;
initialize_alignments(alignments, totalAlignments); // pinned memory allocation
auto start = NOW;
size_t tot_mem_req_per_aln = maxReadSize + maxContigSize + 2 * sizeof(int) + 5 * sizeof(short);
#pragma omp parallel
{
int my_cpu_id = omp_get_thread_num();
cudaSetDevice(my_cpu_id);
int myGPUid;
cudaGetDevice(&myGPUid);
float total_time_cpu = 0;
cudaStream_t streams_cuda[NSTREAMS];
for(int stm = 0; stm < NSTREAMS; stm++){
cudaStreamCreate(&streams_cuda[stm]);
}
if(my_cpu_id == 0)std::cout<<"Number of GPUs being used:"<<omp_get_num_threads()<<"\n";
size_t gpu_mem_avail = get_tot_gpu_mem(myGPUid);
unsigned max_alns_gpu = floor(((double)gpu_mem_avail*factor)/tot_mem_req_per_aln);
unsigned max_alns_sugg = 20000;
max_alns_gpu = max_alns_gpu > max_alns_sugg ? max_alns_sugg : max_alns_gpu;
int its = (max_per_device>max_alns_gpu)?(ceil((double)max_per_device/max_alns_gpu)):1;
std::cout<<"Mem (bytes) avail on device "<<myGPUid<<":"<<(long unsigned)gpu_mem_avail<<"\n";
std::cout<<"Mem (bytes) using on device "<<myGPUid<<":"<<(long unsigned)gpu_mem_avail*factor<<"\n";
int BLOCKS_l = alignmentsPerDevice;
if(my_cpu_id == deviceCount - 1)
BLOCKS_l += leftOver_device;
unsigned leftOvers = BLOCKS_l % its;
unsigned stringsPerIt = BLOCKS_l / its;
gpu_alignments gpu_data(stringsPerIt + leftOvers); // gpu mallocs
short *d_encoding_matrix, *d_scoring_matrix;
cudaErrchk(cudaMalloc(&d_encoding_matrix, ENCOD_MAT_SIZE * sizeof(short)));
cudaErrchk(cudaMalloc(&d_scoring_matrix, SCORE_MAT_SIZE * sizeof(short)));
cudaErrchk(cudaMemcpy(d_encoding_matrix, encoding_matrix, ENCOD_MAT_SIZE * sizeof(short), cudaMemcpyHostToDevice));
cudaErrchk(cudaMemcpy(d_scoring_matrix, scoring_matrix, SCORE_MAT_SIZE * sizeof(short), cudaMemcpyHostToDevice));
short* alAbeg = alignments->ref_begin + my_cpu_id * alignmentsPerDevice;
short* alBbeg = alignments->query_begin + my_cpu_id * alignmentsPerDevice;
short* alAend = alignments->ref_end + my_cpu_id * alignmentsPerDevice;
short* alBend = alignments->query_end + my_cpu_id * alignmentsPerDevice; // memory on CPU for copying the results
short* top_scores_cpu = alignments->top_scores + my_cpu_id * alignmentsPerDevice;
unsigned* offsetA_h;// = new unsigned[stringsPerIt + leftOvers];
cudaMallocHost(&offsetA_h, sizeof(int)*(stringsPerIt + leftOvers));
unsigned* offsetB_h;// = new unsigned[stringsPerIt + leftOvers];
cudaMallocHost(&offsetB_h, sizeof(int)*(stringsPerIt + leftOvers));
char *strA_d, *strB_d;
cudaErrchk(cudaMalloc(&strA_d, maxContigSize * (stringsPerIt + leftOvers) * sizeof(char)));
cudaErrchk(cudaMalloc(&strB_d, maxReadSize *(stringsPerIt + leftOvers)* sizeof(char)));
char* strA;
cudaMallocHost(&strA, sizeof(char)*maxContigSize * (stringsPerIt + leftOvers));
char* strB;
cudaMallocHost(&strB, sizeof(char)* maxReadSize *(stringsPerIt + leftOvers));
float total_packing = 0;
auto start2 = NOW;
std::cout<<"loop begin\n";
for(int perGPUIts = 0; perGPUIts < its; perGPUIts++)
{
auto packing_start = NOW;
int blocksLaunched = 0;
std::vector<std::string>::const_iterator beginAVec;
std::vector<std::string>::const_iterator endAVec;
std::vector<std::string>::const_iterator beginBVec;
std::vector<std::string>::const_iterator endBVec;
if(perGPUIts == its - 1)
{
beginAVec = contigs.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endAVec = contigs.begin() + ((alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt) + leftOvers; // so that each openmp thread has a copy of strings it needs to align
beginBVec = reads.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endBVec = reads.begin() + ((alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt) + leftOvers; // so that each openmp thread has a copy of strings it needs to align
blocksLaunched = stringsPerIt + leftOvers;
}
else
{
beginAVec = contigs.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endAVec = contigs.begin() + (alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt; // so that each openmp thread has a copy of strings it needs to align
beginBVec = reads.begin() + ((alignmentsPerDevice * my_cpu_id) + perGPUIts * stringsPerIt);
endBVec = reads.begin() + (alignmentsPerDevice * my_cpu_id) + (perGPUIts + 1) * stringsPerIt; // so that each openmp thread has a copy of strings it needs to align
blocksLaunched = stringsPerIt;
}
std::vector<std::string> sequencesA(beginAVec, endAVec);
std::vector<std::string> sequencesB(beginBVec, endBVec);
unsigned running_sum = 0;
int sequences_per_stream = (blocksLaunched) / NSTREAMS;
int sequences_stream_leftover = (blocksLaunched) % NSTREAMS;
unsigned half_length_A = 0;
unsigned half_length_B = 0;
auto start_cpu = NOW;
for(int i = 0; i < sequencesA.size(); i++)
{
running_sum +=sequencesA[i].size();
offsetA_h[i] = running_sum;//sequencesA[i].size();
if(i == sequences_per_stream - 1){
half_length_A = running_sum;
running_sum = 0;
}
}
unsigned totalLengthA = half_length_A + offsetA_h[sequencesA.size() - 1];
running_sum = 0;
for(int i = 0; i < sequencesB.size(); i++)
{
running_sum +=sequencesB[i].size();
offsetB_h[i] = running_sum; //sequencesB[i].size();
if(i == sequences_per_stream - 1){
half_length_B = running_sum;
running_sum = 0;
}
}
unsigned totalLengthB = half_length_B + offsetB_h[sequencesB.size() - 1];
auto end_cpu = NOW;
std::chrono::duration<double> cpu_dur = end_cpu - start_cpu;
total_time_cpu += cpu_dur.count();
unsigned offsetSumA = 0;
unsigned offsetSumB = 0;
for(int i = 0; i < sequencesA.size(); i++)
{
char* seqptrA = strA + offsetSumA;
memcpy(seqptrA, sequencesA[i].c_str(), sequencesA[i].size());
char* seqptrB = strB + offsetSumB;
memcpy(seqptrB, sequencesB[i].c_str(), sequencesB[i].size());
offsetSumA += sequencesA[i].size();
offsetSumB += sequencesB[i].size();
}
auto packing_end = NOW;
std::chrono::duration<double> packing_dur = packing_end - packing_start;
total_packing += packing_dur.count();
asynch_mem_copies_htd(&gpu_data, offsetA_h, offsetB_h, strA, strA_d, strB, strB_d, half_length_A, half_length_B, totalLengthA, totalLengthB, sequences_per_stream, sequences_stream_leftover, streams_cuda);
unsigned minSize = (maxReadSize < maxContigSize) ? maxReadSize : maxContigSize;
unsigned totShmem = 3 * (minSize + 1) * sizeof(short);
unsigned alignmentPad = 4 + (4 - totShmem % 4);
size_t ShmemBytes = totShmem + alignmentPad;
if(ShmemBytes > 48000)
cudaFuncSetAttribute(gpu_bsw::sequence_dna_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, ShmemBytes);
gpu_bsw::sequence_aa_kernel<<<sequences_per_stream, minSize, ShmemBytes, streams_cuda[0]>>>(
strA_d, strB_d, gpu_data.offset_ref_gpu, gpu_data.offset_query_gpu, gpu_data.ref_start_gpu,
gpu_data.ref_end_gpu, gpu_data.query_start_gpu, gpu_data.query_end_gpu, gpu_data.scores_gpu,
openGap, extendGap, d_scoring_matrix, d_encoding_matrix);
cudaErrchk(cudaGetLastError());
gpu_bsw::sequence_aa_kernel<<<sequences_per_stream + sequences_stream_leftover, minSize, ShmemBytes, streams_cuda[1]>>>(
strA_d + half_length_A, strB_d + half_length_B, gpu_data.offset_ref_gpu + sequences_per_stream, gpu_data.offset_query_gpu + sequences_per_stream,
gpu_data.ref_start_gpu + sequences_per_stream, gpu_data.ref_end_gpu + sequences_per_stream, gpu_data.query_start_gpu + sequences_per_stream, gpu_data.query_end_gpu + sequences_per_stream,
gpu_data.scores_gpu + sequences_per_stream, openGap, extendGap, d_scoring_matrix, d_encoding_matrix);
cudaErrchk(cudaGetLastError());
// copyin back end index so that we can find new min
asynch_mem_copies_dth_mid(&gpu_data, alAend, alBend, sequences_per_stream, sequences_stream_leftover, streams_cuda);
cudaStreamSynchronize (streams_cuda[0]);
cudaStreamSynchronize (streams_cuda[1]);
auto sec_cpu_start = NOW;
int newMin = get_new_min_length(alAend, alBend, blocksLaunched); // find the new largest of smaller lengths
auto sec_cpu_end = NOW;
std::chrono::duration<double> dur_sec_cpu = sec_cpu_end - sec_cpu_start;
total_time_cpu += dur_sec_cpu.count();
gpu_bsw::sequence_aa_reverse<<<sequences_per_stream, newMin, ShmemBytes, streams_cuda[0]>>>(
strA_d, strB_d, gpu_data.offset_ref_gpu, gpu_data.offset_query_gpu, gpu_data.ref_start_gpu,
gpu_data.ref_end_gpu, gpu_data.query_start_gpu, gpu_data.query_end_gpu, gpu_data.scores_gpu, openGap, extendGap, d_scoring_matrix, d_encoding_matrix);
cudaErrchk(cudaGetLastError());
gpu_bsw::sequence_aa_reverse<<<sequences_per_stream + sequences_stream_leftover, newMin, ShmemBytes, streams_cuda[1]>>>(
strA_d + half_length_A, strB_d + half_length_B, gpu_data.offset_ref_gpu + sequences_per_stream, gpu_data.offset_query_gpu + sequences_per_stream ,
gpu_data.ref_start_gpu + sequences_per_stream, gpu_data.ref_end_gpu + sequences_per_stream, gpu_data.query_start_gpu + sequences_per_stream, gpu_data.query_end_gpu + sequences_per_stream,
gpu_data.scores_gpu + sequences_per_stream, openGap, extendGap, d_scoring_matrix, d_encoding_matrix);
cudaErrchk(cudaGetLastError());
asynch_mem_copies_dth(&gpu_data, alAbeg, alBbeg, top_scores_cpu, sequences_per_stream, sequences_stream_leftover, streams_cuda);
alAbeg += stringsPerIt;
alBbeg += stringsPerIt;
alAend += stringsPerIt;
alBend += stringsPerIt;
top_scores_cpu += stringsPerIt;
cudaStreamSynchronize (streams_cuda[0]);
cudaStreamSynchronize (streams_cuda[1]);
} // for iterations end here
auto end1 = NOW;
std::chrono::duration<double> diff2 = end1 - start2;
cudaErrchk(cudaFree(strA_d));
cudaErrchk(cudaFree(strB_d));
cudaFreeHost(offsetA_h);
cudaFreeHost(offsetB_h);
cudaFreeHost(strA);
cudaFreeHost(strB);
for(int i = 0; i < NSTREAMS; i++)
cudaStreamDestroy(streams_cuda[i]);
std::cout <<"cpu time:"<<total_time_cpu<<std::endl;
std::cout <<"packing time:"<<total_packing<<std::endl;
#pragma omp barrier
} // paralle pragma ends
auto end = NOW;
std::chrono::duration<double> diff = end - start;
std::cout << "Total Alignments:"<<totalAlignments<<"\n"<<"Max Reference Size:"<<maxContigSize<<"\n"<<"Max Query Size:"<<maxReadSize<<"\n" <<"Total Execution Time (seconds):"<< diff.count() <<std::endl;
}// end of amino acids kernel